#!/bin/bash
NAMESPACE=$1
TARGET_VERSION=$2
INTERVAL=${3:-10} # Default check interval is 10 seconds
if [ -z "$NAMESPACE" ] || [ -z "$TARGET_VERSION" ]; then
echo "Usage: $0 <namespace> <target-version> [check-interval-seconds]"
exit 1
fi
echo "Monitoring pod versions in namespace '$NAMESPACE' to match version '$TARGET_VERSION' every $INTERVAL seconds"
while true; do
# Get all pods in the namespace
PODS=$(kubectl get pods -n $NAMESPACE -o jsonpath='{.items[*].metadata.name}')
ALL_MATCH=true
for POD in $PODS; do
# Get the image version of each container in the pod
CONTAINERS=$(kubectl get pod $POD -n $NAMESPACE -o jsonpath='{.spec.containers[*].image}' | grep -e fsts-data -e fsts-system -e fsts-inventory -e fsts-shdr -e fsts-gateway)
for CONTAINER in $CONTAINERS; do
IMAGE_VERSION=$(echo $CONTAINER | awk -F: '{print $2}')
if [ "$IMAGE_VERSION" != "$TARGET_VERSION" ]; then
echo "Pod $POD has container with image version $IMAGE_VERSION which does not match target version $TARGET_VERSION"
ALL_MATCH=false
else
echo "Pod $POD has container with image version $IMAGE_VERSION which matches target version $TARGET_VERSION"
fi
done
done
if [ "$ALL_MATCH" = true ]; then
echo "All pods in namespace '$NAMESPACE' have containers with the target version '$TARGET_VERSION'."
break
else
echo "Not all pods have the target version. Waiting for $INTERVAL seconds before next check..."
sleep $INTERVAL
fi
done
echo "Version check complete."
echo "发布结束,所有应用服务版本发布至: "$TARGET_VERSION
NAMESPACE=$1
TARGET_VERSION=$2 是脚本在执行是传入参数。拉取发布版本,10秒查询一次。