Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: pod-readiness-controller use Patch to modify Pod instead of Update #1560

Merged
merged 1 commit into from
Apr 23, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 40 additions & 29 deletions pkg/controller/podreadiness/pod_readiness_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ package podreadiness

import (
"context"
"encoding/json"
"fmt"
"time"

appspub "github.com/openkruise/kruise/apis/apps/pub"
Expand All @@ -27,7 +29,7 @@ import (
v1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/util/retry"
"k8s.io/apimachinery/pkg/types"
"k8s.io/klog/v2"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/controller"
Expand Down Expand Up @@ -107,34 +109,43 @@ func (r *ReconcilePodReadiness) Reconcile(_ context.Context, request reconcile.R
}
}()

err = retry.RetryOnConflict(retry.DefaultBackoff, func() error {
pod := &v1.Pod{}
err = r.Get(context.TODO(), request.NamespacedName, pod)
if err != nil {
if errors.IsNotFound(err) {
// Object not found, return. Created objects are automatically garbage collected.
// For additional cleanup logic use finalizers.
return nil
}
// Error reading the object - requeue the request.
return err
}
if pod.DeletionTimestamp != nil {
return nil
}
if !utilpodreadiness.ContainsReadinessGate(pod) {
return nil
}
if utilpodreadiness.GetReadinessCondition(pod) != nil {
return nil
pod := &v1.Pod{}
err = r.Get(context.TODO(), request.NamespacedName, pod)
if err != nil {
if errors.IsNotFound(err) {
// Object not found, return. Created objects are automatically garbage collected.
// For additional cleanup logic use finalizers.
return reconcile.Result{}, nil
}
// Error reading the object - requeue the request.
return reconcile.Result{}, err
}
if pod.DeletionTimestamp != nil {
return reconcile.Result{}, nil
}
if !utilpodreadiness.ContainsReadinessGate(pod) {
return reconcile.Result{}, nil
}
if utilpodreadiness.GetReadinessCondition(pod) != nil {
return reconcile.Result{}, nil
}

pod.Status.Conditions = append(pod.Status.Conditions, v1.PodCondition{
Type: appspub.KruisePodReadyConditionType,
Status: v1.ConditionTrue,
LastTransitionTime: metav1.Now(),
})
return r.Status().Update(context.TODO(), pod)
})
return reconcile.Result{}, err
// patch pod condition
status := v1.PodStatus{
Conditions: []v1.PodCondition{
{
Type: appspub.KruisePodReadyConditionType,
Status: v1.ConditionTrue,
LastTransitionTime: metav1.Now(),
},
},
}
by, _ := json.Marshal(status)
patchBody := fmt.Sprintf(`{"status":%s}`, string(by))
rcvObject := &v1.Pod{ObjectMeta: metav1.ObjectMeta{Namespace: pod.Namespace, Name: pod.Name}}

if err := r.Status().Patch(context.TODO(), rcvObject, client.RawPatch(types.StrategicMergePatchType, []byte(patchBody))); err != nil {
return reconcile.Result{}, fmt.Errorf("failed to patch pod status: %v", err)
}
return reconcile.Result{}, nil
}
Loading