Skip to content

Commit 4c1b43b

Browse files
committed
Make sure no pods of old replicas of the pod are running anymore in WaitForDeploymentsAvailable functions
1 parent 7c23a24 commit 4c1b43b

File tree

1 file changed

+39
-3
lines changed

1 file changed

+39
-3
lines changed

pkg/k8s/wait.go

Lines changed: 39 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,31 @@ func checkIfDeploymentIsAvailable(ctx context.Context, clientset *kubernetes.Cli
6666
deployment.Status.AvailableReplicas == desiredReplicas &&
6767
deployment.Status.UnavailableReplicas == 0 {
6868

69-
// Verify all pods are actually running
69+
// Get the current ReplicaSet for this deployment
70+
replicaSets, err := clientset.AppsV1().ReplicaSets(deployment.Namespace).List(ctx, metav1.ListOptions{
71+
LabelSelector: metav1.FormatLabelSelector(deployment.Spec.Selector),
72+
})
73+
if err != nil {
74+
return false, err
75+
}
76+
77+
// Find the current active ReplicaSet (the one with desired replicas > 0)
78+
var currentPodTemplateHash string
79+
for _, rs := range replicaSets.Items {
80+
if rs.Spec.Replicas != nil && *rs.Spec.Replicas > 0 {
81+
// The pod-template-hash label identifies pods from this ReplicaSet
82+
if hash, ok := rs.Labels["pod-template-hash"]; ok {
83+
currentPodTemplateHash = hash
84+
break
85+
}
86+
}
87+
}
88+
89+
if currentPodTemplateHash == "" {
90+
return false, fmt.Errorf("could not find current pod-template-hash for deployment %s", deployment.Name)
91+
}
92+
93+
// Verify all pods are from the current ReplicaSet and are running
7094
labelSelector := metav1.FormatLabelSelector(deployment.Spec.Selector)
7195
pods, err := clientset.CoreV1().Pods(deployment.Namespace).List(ctx, metav1.ListOptions{
7296
LabelSelector: labelSelector,
@@ -75,9 +99,21 @@ func checkIfDeploymentIsAvailable(ctx context.Context, clientset *kubernetes.Cli
7599
return false, err
76100
}
77101

78-
// Count ready pods
102+
// Count ready pods from current ReplicaSet only
79103
readyPods := 0
80104
for _, pod := range pods.Items {
105+
// Check if pod belongs to current ReplicaSet
106+
podHash, hasPodHash := pod.Labels["pod-template-hash"]
107+
if !hasPodHash || podHash != currentPodTemplateHash {
108+
// Pod is from an old ReplicaSet - deployment not fully rolled out
109+
if pod.DeletionTimestamp == nil {
110+
// Old pod still exists and not being deleted
111+
return false, nil
112+
}
113+
continue
114+
}
115+
116+
// Check if pod is ready
81117
for _, podCondition := range pod.Status.Conditions {
82118
if podCondition.Type == corev1.PodReady && podCondition.Status == corev1.ConditionTrue {
83119
readyPods++
@@ -86,7 +122,7 @@ func checkIfDeploymentIsAvailable(ctx context.Context, clientset *kubernetes.Cli
86122
}
87123
}
88124

89-
// Ensure we have the desired number of running pods
125+
// Ensure we have the desired number of running pods from current ReplicaSet
90126
if int32(readyPods) == desiredReplicas {
91127
return true, nil
92128
}

0 commit comments

Comments
 (0)