Skip to content

Commit

Permalink
fix: pod-readiness-controller use Patch to modify Pod
Browse files Browse the repository at this point in the history
Signed-off-by: BruceAko <[email protected]>
  • Loading branch information
BruceAko committed Apr 2, 2024
1 parent ac3fa11 commit 64e4385
Showing 1 changed file with 20 additions and 6 deletions.
26 changes: 20 additions & 6 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,6 +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/apimachinery/pkg/types"
"k8s.io/client-go/util/retry"
"k8s.io/klog/v2"
"sigs.k8s.io/controller-runtime/pkg/client"
Expand Down Expand Up @@ -129,12 +132,23 @@ func (r *ReconcilePodReadiness) Reconcile(_ context.Context, request reconcile.R
return 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)
// 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 fmt.Errorf("failed to patch pod status: %v", err)
}

Check warning on line 150 in pkg/controller/podreadiness/pod_readiness_controller.go

View check run for this annotation

Codecov / codecov/patch

pkg/controller/podreadiness/pod_readiness_controller.go#L149-L150

Added lines #L149 - L150 were not covered by tests
return nil
})
return reconcile.Result{}, err
}

0 comments on commit 64e4385

Please sign in to comment.