Skip to content

Commit

Permalink
chore: refactor
Browse files Browse the repository at this point in the history
Signed-off-by: ShutingZhao <[email protected]>
  • Loading branch information
realshuting committed Aug 14, 2024
1 parent 9dd646b commit 02c08f0
Showing 1 changed file with 11 additions and 11 deletions.
22 changes: 11 additions & 11 deletions pkg/background/generate/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,13 @@ func (c *GenerateController) ProcessUR(ur *kyvernov2.UpdateRequest) error {
logger.Info("start processing UR", "ur", ur.Name, "resourceVersion", ur.GetResourceVersion())

var failures []error

policy, err := c.getPolicyObject(*ur)
if err != nil && !apierrors.IsNotFound(err) {
fmt.Errorf("error in fetching policy: %v", err)

Check failure on line 103 in pkg/background/generate/generate.go

View workflow job for this annotation

GitHub Actions / prepare-cli

result of fmt.Errorf call not used

Check failure on line 103 in pkg/background/generate/generate.go

View workflow job for this annotation

GitHub Actions / tests

unusedresult: result of fmt.Errorf call not used (govet)
return err
}

for i := 0; i < len(ur.Spec.RuleContext); i++ {
rule := ur.Spec.RuleContext[i]
trigger, err := c.getTrigger(ur.Spec, i)
Expand All @@ -107,7 +114,7 @@ func (c *GenerateController) ProcessUR(ur *kyvernov2.UpdateRequest) error {
}

namespaceLabels := engineutils.GetNamespaceSelectorsFromNamespaceLister(trigger.GetKind(), trigger.GetNamespace(), c.nsLister, logger)
genResources, err = c.applyGenerate(*trigger, *ur, i, namespaceLabels)
genResources, err = c.applyGenerate(*trigger, *ur, policy, i, namespaceLabels)
if err != nil {
if strings.Contains(err.Error(), doesNotApply) {
logger.V(4).Info(fmt.Sprintf("skipping rule %s: %v", rule.Rule, err.Error()))
Expand Down Expand Up @@ -194,20 +201,13 @@ func (c *GenerateController) getTriggerForCreateOperation(spec kyvernov2.UpdateR
return trigger, err
}

func (c *GenerateController) applyGenerate(trigger unstructured.Unstructured, ur kyvernov2.UpdateRequest, i int, namespaceLabels map[string]string) ([]kyvernov1.ResourceSpec, error) {
func (c *GenerateController) applyGenerate(trigger unstructured.Unstructured, ur kyvernov2.UpdateRequest, policy kyvernov1.PolicyInterface, i int, namespaceLabels map[string]string) ([]kyvernov1.ResourceSpec, error) {
logger := c.log.WithValues("name", ur.GetName(), "policy", ur.Spec.GetPolicyKey())
logger.V(3).Info("applying generate policy")

policy, err := c.getPolicyObject(ur)
if err != nil && !apierrors.IsNotFound(err) {
logger.Error(err, "error in fetching policy")
return nil, err
}

ruleContext := ur.Spec.RuleContext[i]
if ruleContext.DeleteDownstream || apierrors.IsNotFound(err) {
err = c.deleteDownstream(policy, ruleContext, &ur)
return nil, err
if ruleContext.DeleteDownstream || policy == nil {
return nil, c.deleteDownstream(policy, ruleContext, &ur)
}

var rule *kyvernov1.Rule
Expand Down

0 comments on commit 02c08f0

Please sign in to comment.