mirror of
https://github.com/kyverno/kyverno.git
synced 2025-03-31 03:45:17 +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 {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
for _, policy := range policies {
|
updateStatusFunc := func(policy kyvernov1.PolicyInterface) error {
|
||||||
policyKey, err := cache.MetaNamespaceKeyFunc(policy)
|
policyKey, err := cache.MetaNamespaceKeyFunc(policy)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
|
@ -421,36 +421,44 @@ func (c *controller) updatePolicyStatuses(ctx context.Context) error {
|
||||||
for _, set := range c.policyState {
|
for _, set := range c.policyState {
|
||||||
if !set.Has(policyKey) {
|
if !set.Has(policyKey) {
|
||||||
ready = false
|
ready = false
|
||||||
|
break
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if policy.IsReady() != ready {
|
|
||||||
policy = policy.CreateDeepCopy()
|
|
||||||
status := policy.GetStatus()
|
status := policy.GetStatus()
|
||||||
status.SetReady(ready)
|
status.SetReady(ready)
|
||||||
|
status.Autogen.Rules = nil
|
||||||
if toggle.AutogenInternals.Enabled() {
|
if toggle.AutogenInternals.Enabled() {
|
||||||
var rules []kyvernov1.Rule
|
|
||||||
for _, rule := range autogen.ComputeRules(policy) {
|
for _, rule := range autogen.ComputeRules(policy) {
|
||||||
if strings.HasPrefix(rule.Name, "autogen-") {
|
if strings.HasPrefix(rule.Name, "autogen-") {
|
||||||
rules = append(rules, rule)
|
status.Autogen.Rules = append(status.Autogen.Rules, rule)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
status.Autogen.Rules = rules
|
|
||||||
} else {
|
|
||||||
status.Autogen.Rules = nil
|
|
||||||
}
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
for _, policy := range policies {
|
||||||
if policy.GetNamespace() == "" {
|
if policy.GetNamespace() == "" {
|
||||||
_, err := c.kyvernoClient.KyvernoV1().ClusterPolicies().UpdateStatus(ctx, policy.(*kyvernov1.ClusterPolicy), metav1.UpdateOptions{})
|
_, err := controllerutils.UpdateStatus(
|
||||||
if err != nil {
|
ctx,
|
||||||
|
policy.(*kyvernov1.ClusterPolicy),
|
||||||
|
c.kyvernoClient.KyvernoV1().ClusterPolicies(),
|
||||||
|
func(policy *kyvernov1.ClusterPolicy) error {
|
||||||
|
return updateStatusFunc(policy)
|
||||||
|
},
|
||||||
|
)
|
||||||
return err
|
return err
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
_, err := c.kyvernoClient.KyvernoV1().Policies(policy.GetNamespace()).UpdateStatus(ctx, policy.(*kyvernov1.Policy), metav1.UpdateOptions{})
|
_, err := controllerutils.UpdateStatus(
|
||||||
if err != nil {
|
ctx,
|
||||||
|
policy.(*kyvernov1.Policy),
|
||||||
|
c.kyvernoClient.KyvernoV1().Policies(policy.GetNamespace()),
|
||||||
|
func(policy *kyvernov1.Policy) error {
|
||||||
|
return updateStatusFunc(policy)
|
||||||
|
},
|
||||||
|
)
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
}
|
|
||||||
return nil
|
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 {
|
func Cleanup[T any, R Object[T]](ctx context.Context, actual []R, expected []R, deleter Deleter) error {
|
||||||
keep := sets.NewString()
|
keep := sets.NewString()
|
||||||
for _, obj := range expected {
|
for _, obj := range expected {
|
||||||
|
|
Loading…
Add table
Reference in a new issue