mirror of
https://github.com/kyverno/kyverno.git
synced 2025-03-28 02:18:15 +00:00
refactor: add update status helper (#4985)
Signed-off-by: Charles-Edouard Brétéché <charles.edouard@nirmata.com> Signed-off-by: Charles-Edouard Brétéché <charles.edouard@nirmata.com> Co-authored-by: shuting <shuting@nirmata.com>
This commit is contained in:
parent
f3e40efcd7
commit
3ebb6284cc
2 changed files with 51 additions and 25 deletions
|
@ -412,7 +412,7 @@ func (c *controller) updatePolicyStatuses(ctx context.Context) error {
|
|||
if err != nil {
|
||||
return err
|
||||
}
|
||||
for _, policy := range policies {
|
||||
updateStatusFunc := func(policy kyvernov1.PolicyInterface) error {
|
||||
policyKey, err := cache.MetaNamespaceKeyFunc(policy)
|
||||
if err != nil {
|
||||
return err
|
||||
|
@ -421,35 +421,43 @@ func (c *controller) updatePolicyStatuses(ctx context.Context) error {
|
|||
for _, set := range c.policyState {
|
||||
if !set.Has(policyKey) {
|
||||
ready = false
|
||||
break
|
||||
}
|
||||
}
|
||||
if policy.IsReady() != ready {
|
||||
policy = policy.CreateDeepCopy()
|
||||
status := policy.GetStatus()
|
||||
status.SetReady(ready)
|
||||
if toggle.AutogenInternals.Enabled() {
|
||||
var rules []kyvernov1.Rule
|
||||
for _, rule := range autogen.ComputeRules(policy) {
|
||||
if strings.HasPrefix(rule.Name, "autogen-") {
|
||||
rules = append(rules, rule)
|
||||
}
|
||||
}
|
||||
status.Autogen.Rules = rules
|
||||
} else {
|
||||
status.Autogen.Rules = nil
|
||||
}
|
||||
if policy.GetNamespace() == "" {
|
||||
_, err := c.kyvernoClient.KyvernoV1().ClusterPolicies().UpdateStatus(ctx, policy.(*kyvernov1.ClusterPolicy), metav1.UpdateOptions{})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
} else {
|
||||
_, err := c.kyvernoClient.KyvernoV1().Policies(policy.GetNamespace()).UpdateStatus(ctx, policy.(*kyvernov1.Policy), metav1.UpdateOptions{})
|
||||
if err != nil {
|
||||
return err
|
||||
status := policy.GetStatus()
|
||||
status.SetReady(ready)
|
||||
status.Autogen.Rules = nil
|
||||
if toggle.AutogenInternals.Enabled() {
|
||||
for _, rule := range autogen.ComputeRules(policy) {
|
||||
if strings.HasPrefix(rule.Name, "autogen-") {
|
||||
status.Autogen.Rules = append(status.Autogen.Rules, rule)
|
||||
}
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
for _, policy := range policies {
|
||||
if policy.GetNamespace() == "" {
|
||||
_, err := controllerutils.UpdateStatus(
|
||||
ctx,
|
||||
policy.(*kyvernov1.ClusterPolicy),
|
||||
c.kyvernoClient.KyvernoV1().ClusterPolicies(),
|
||||
func(policy *kyvernov1.ClusterPolicy) error {
|
||||
return updateStatusFunc(policy)
|
||||
},
|
||||
)
|
||||
return err
|
||||
} else {
|
||||
_, err := controllerutils.UpdateStatus(
|
||||
ctx,
|
||||
policy.(*kyvernov1.Policy),
|
||||
c.kyvernoClient.KyvernoV1().Policies(policy.GetNamespace()),
|
||||
func(policy *kyvernov1.Policy) error {
|
||||
return updateStatusFunc(policy)
|
||||
},
|
||||
)
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
|
|
@ -148,6 +148,24 @@ 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,
|
||||
) (T, error) {
|
||||
mutated := obj.DeepCopy()
|
||||
if err := build(mutated); err != nil {
|
||||
var d T
|
||||
return d, err
|
||||
} else {
|
||||
if reflect.DeepEqual(obj, mutated) {
|
||||
return mutated, nil
|
||||
} else {
|
||||
return setter.UpdateStatus(ctx, mutated, metav1.UpdateOptions{})
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func Cleanup[T any, R Object[T]](ctx context.Context, actual []R, expected []R, deleter Deleter) error {
|
||||
keep := sets.NewString()
|
||||
for _, obj := range expected {
|
||||
|
|
Loading…
Add table
Reference in a new issue