Skip to content

Commit

Permalink
fix, add service-available when pod is ready (#57)
Browse files Browse the repository at this point in the history
* fix, add service-available when pod is ready
  • Loading branch information
shaofan-hs committed Aug 24, 2023
1 parent b4cceb9 commit 9e0d581
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 8 deletions.
14 changes: 6 additions & 8 deletions pkg/webhook/server/generic/pod/opslifecycle/mutating.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@ func (lc *OpsLifecycle) Mutating(ctx context.Context, c client.Client, oldPod, n
}
numOfIDs := len(newIDToLabelsMap)

if numOfIDs == 0 {
return lc.podServiceAvailableLabel(newPod)
}

var operatingCount, operateCount, operatedCount, completeCount int
var undoTypeToNumsMap = map[string]int{}
for id, labels := range newIDToLabelsMap {
Expand Down Expand Up @@ -122,14 +126,10 @@ func (lc *OpsLifecycle) Mutating(ctx context.Context, c client.Client, oldPod, n
}

if completeCount == numOfIDs { // all operations are completed
satisfied, expectedFinalizer, err := lc.satisfyExpectedFinalizers(newPod) // whether all expected finalizers are satisfied
if err != nil || !satisfied {
klog.Infof("pod: %s/%s, expected finalizers: %v, err: %v", newPod.Namespace, newPod.Name, expectedFinalizer, err)
err := lc.podServiceAvailableLabel(newPod)
if err != nil {
return err
}
if !lc.isPodReady(newPod) {
return nil
}

// all operations are done and all expected finalizers are satisfied, then remove all unuseful labels, and add service available label
for id := range newIDToLabelsMap {
Expand All @@ -143,8 +143,6 @@ func (lc *OpsLifecycle) Mutating(ctx context.Context, c client.Client, oldPod, n
delete(newPod.Labels, fmt.Sprintf("%s/%s", v, id))
}
}
lc.addLabelWithTime(newPod, v1alpha1.PodServiceAvailableLabel)

return nil
}

Expand Down
23 changes: 23 additions & 0 deletions pkg/webhook/server/generic/pod/opslifecycle/webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (

corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/util/sets"
"k8s.io/klog/v2"

"kusionstack.io/kafed/apis/apps/v1alpha1"
controllerutils "kusionstack.io/kafed/pkg/controllers/utils"
Expand Down Expand Up @@ -84,6 +85,28 @@ func (lc *OpsLifecycle) addLabelWithTime(pod *corev1.Pod, key string) {
pod.Labels[key] = lc.timeLabelValue()
}

func (lc *OpsLifecycle) podServiceAvailableLabel(pod *corev1.Pod) error {
if pod.Labels == nil {
return nil
}
if _, ok := pod.Labels[v1alpha1.PodServiceAvailableLabel]; ok {
return nil
}

satisfied, expectedFinalizer, err := lc.satisfyExpectedFinalizers(pod) // whether all expected finalizers are satisfied
if err != nil || !satisfied {
klog.Infof("pod: %s/%s, expected finalizers: %v, err: %v", pod.Namespace, pod.Name, expectedFinalizer, err)
return err
}

if !lc.isPodReady(pod) {
return nil
}

lc.addLabelWithTime(pod, v1alpha1.PodServiceAvailableLabel)
return nil
}

func addReadinessGates(pod *corev1.Pod, conditionType corev1.PodConditionType) {
for _, v := range pod.Spec.ReadinessGates {
if v.ConditionType == conditionType {
Expand Down

0 comments on commit 9e0d581

Please sign in to comment.