diff --git a/pkg/controllers/webhook/controller.go b/pkg/controllers/webhook/controller.go index aee0d7d5fb..cd6d78930f 100644 --- a/pkg/controllers/webhook/controller.go +++ b/pkg/controllers/webhook/controller.go @@ -487,9 +487,14 @@ func (c *controller) updatePolicyStatuses(ctx context.Context) error { } for _, policy := range policies { if policy.GetNamespace() == "" { - _, err := controllerutils.UpdateStatus( + p, err := c.kyvernoClient.KyvernoV1().ClusterPolicies().Get(ctx, policy.GetName(), metav1.GetOptions{}) + if err != nil { + logger.Error(err, "failed to get latest clusterpolicy for status reconciliation", "policy", policy.GetName()) + continue + } + _, err = controllerutils.UpdateStatus( ctx, - policy.(*kyvernov1.ClusterPolicy), + p, c.kyvernoClient.KyvernoV1().ClusterPolicies(), func(policy *kyvernov1.ClusterPolicy) error { return updateStatusFunc(policy) @@ -499,9 +504,14 @@ func (c *controller) updatePolicyStatuses(ctx context.Context) error { return err } } else { - _, err := controllerutils.UpdateStatus( + p, err := c.kyvernoClient.KyvernoV1().Policies(policy.GetNamespace()).Get(ctx, policy.GetName(), metav1.GetOptions{}) + if err != nil { + logger.Error(err, "failed to get latest policy for status reconciliation", "namespace", policy.GetNamespace, "policy", policy.GetName()) + continue + } + _, err = controllerutils.UpdateStatus( ctx, - policy.(*kyvernov1.Policy), + p, c.kyvernoClient.KyvernoV1().Policies(policy.GetNamespace()), func(policy *kyvernov1.Policy) error { return updateStatusFunc(policy) diff --git a/pkg/engine/jmespath/functions_test.go b/pkg/engine/jmespath/functions_test.go index cf3962d359..8cb05735ea 100644 --- a/pkg/engine/jmespath/functions_test.go +++ b/pkg/engine/jmespath/functions_test.go @@ -1243,7 +1243,7 @@ ZDGRs55xuoeLDJ/ZRFf9bI+IaCUd1YrfYcHIl3G87Av+r49YVwqRDT0VDV7uLgqn // expectedResult: map[string]interface{}{}, // } } - for i, tc := range testCases { + for _, tc := range testCases { t.Run(tc.jmesPath, func(t *testing.T) { jp, err := jmespathInterface.Query(tc.jmesPath) assert.NilError(t, err) @@ -1255,7 +1255,6 @@ ZDGRs55xuoeLDJ/ZRFf9bI+IaCUd1YrfYcHIl3G87Av+r49YVwqRDT0VDV7uLgqn res, ok := result.(map[string]interface{}) assert.Assert(t, ok) - fmt.Println("======i", i) assert.DeepEqual(t, res, tc.expectedResult) }) } diff --git a/pkg/utils/controller/utils.go b/pkg/utils/controller/utils.go index 12f7d5f3f4..389b7126cb 100644 --- a/pkg/utils/controller/utils.go +++ b/pkg/utils/controller/utils.go @@ -149,9 +149,14 @@ func Update[T interface { func UpdateStatus[T interface { metav1.Object DeepCopy[T] -}, S StatusClient[T]](ctx context.Context, obj T, setter S, build func(T) error, +}, S ObjectStatusClient[T]](ctx context.Context, obj T, setter S, build func(T) error, ) (T, error) { - mutated := obj.DeepCopy() + var objNew T + objNew, err := setter.Get(ctx, obj.GetName(), metav1.GetOptions{}) + if err != nil { + return objNew, err + } + mutated := objNew.DeepCopy() if err := build(mutated); err != nil { var d T return d, err