Skip to content

Commit

Permalink
check for label
Browse files Browse the repository at this point in the history
  • Loading branch information
rp-thomas committed Oct 19, 2024
1 parent 56d4faa commit 9e06f46
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 0 deletions.
43 changes: 43 additions & 0 deletions check.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
#!/bin/bash

# Usage: ./wait_for_label.sh <namespace> <deployment_name> <label_key> <label_value> <timeout>
# Example: ./wait_for_label.sh default my-deployment my-label ready 300

NAMESPACE=$1
DEPLOYMENT_NAME=$2
LABEL_KEY=$3
LABEL_VALUE=$4
TIMEOUT=${5:-300} # Default timeout is 300 seconds

if [[ -z "$NAMESPACE" || -z "$DEPLOYMENT_NAME" || -z "$LABEL_KEY" || -z "$LABEL_VALUE" ]]; then
echo "Usage: $0 <namespace> <deployment_name> <label_key> <label_value> <timeout>"
exit 1
fi

echo "Waiting for deployment '$DEPLOYMENT_NAME' in namespace '$NAMESPACE' to have label '$LABEL_KEY=$LABEL_VALUE' and be ready..."

end_time=$(( $(date +%s) + TIMEOUT ))

while true; do
# Check if the label is set on the deployment
current_label=$(kubectl get deployment "$DEPLOYMENT_NAME" -n "$NAMESPACE" -o=jsonpath="{.metadata.labels['$LABEL_KEY']}")

# Check if the deployment is ready
ready_replicas=$(kubectl get deployment "$DEPLOYMENT_NAME" -n "$NAMESPACE" -o=jsonpath="{.status.readyReplicas}")
desired_replicas=$(kubectl get deployment "$DEPLOYMENT_NAME" -n "$NAMESPACE" -o=jsonpath="{.spec.replicas}")

# Check if the label matches and the deployment is ready
if [[ "$current_label" == "$LABEL_VALUE" && "$ready_replicas" -eq "$desired_replicas" ]]; then
echo "Deployment '$DEPLOYMENT_NAME' has the label '$LABEL_KEY=$LABEL_VALUE' and is ready."
exit 0
fi

# Check if we've exceeded the timeout
if [[ $(date +%s) -ge $end_time ]]; then
echo "Timed out waiting for deployment '$DEPLOYMENT_NAME' to have label '$LABEL_KEY=$LABEL_VALUE' and be ready."
exit 1
fi

# Wait a few seconds before checking again
sleep 5
done
10 changes: 10 additions & 0 deletions infrastructure/kustomization.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
resources:
- deployment.yaml

labels:
- includeSelectors: false
includeTemplates: false
pairs:
app.kubernetes.io/version: "abc123"

0 comments on commit 9e06f46

Please sign in to comment.