diff --git a/pkg/adapter/nimbus-kyverno/processor/kpbuilder.go b/pkg/adapter/nimbus-kyverno/processor/kpbuilder.go index 74d3a2e3..9393dad5 100644 --- a/pkg/adapter/nimbus-kyverno/processor/kpbuilder.go +++ b/pkg/adapter/nimbus-kyverno/processor/kpbuilder.go @@ -375,18 +375,30 @@ func virtualPatch(np *v1alpha1.NimbusPolicy, logger logr.Logger) ([]kyvernov1.Po pol := policy.(map[string]any) policyData, ok := pol["karmor"].(map[string]any) if ok { - kps = append(kps, generatePol("karmor", cve, image, np, policyData, karmorPolCount, logger)) + karmorPol, err := generatePol("karmor", cve, image, np, policyData, karmorPolCount, logger) + if err != nil { + logger.V(2).Error(err, "Error while generating karmor policy") + } + kps = append(kps, karmorPol) karmorPolCount += 1 } policyData, ok = pol["kyverno"].(map[string]any) if ok { - kps = append(kps, generatePol("kyverno", cve, image, np, policyData, kyvPolCount, logger)) + kyvernoPol, err := generatePol("kyverno", cve, image, np, policyData, kyvPolCount, logger) + if err != nil { + logger.V(2).Error(err, "Error while generating kyverno policy") + } + kps = append(kps, kyvernoPol) kyvPolCount += 1 } policyData, ok = pol["netpol"].(map[string]any) if ok { - kps = append(kps, generatePol("netpol", cve, image, np, policyData, netPolCount, logger)) + netPol, err := generatePol("netpol", cve, image, np, policyData, netPolCount, logger) + if err != nil { + logger.V(2).Error(err, "Error while generating network policy") + } + kps = append(kps, netPol) netPolCount += 1 } } @@ -400,7 +412,7 @@ func addManagedByAnnotation(kp *kyvernov1.Policy) { kp.Annotations["app.kubernetes.io/managed-by"] = "nimbus-kyverno" } -func generatePol(polengine string, cve string, image string, np *v1alpha1.NimbusPolicy, policyData map[string]any, count int, logger logr.Logger) kyvernov1.Policy { +func generatePol(polengine string, cve string, image string, np *v1alpha1.NimbusPolicy, policyData map[string]any, count int, logger logr.Logger) (kyvernov1.Policy, error) { var pol kyvernov1.Policy labels := np.Spec.Selector.MatchLabels cve = strings.ToLower(cve) @@ -466,9 +478,8 @@ func generatePol(polengine string, cve string, image string, np *v1alpha1.Nimbus selector["matchLabels"] = selectorLabels policyBytes, err := json.Marshal(policyData) - if err != nil { - panic(err.Error()) + return pol, err } pol = kyvernov1.Policy{ ObjectMeta: metav1.ObjectMeta{ @@ -561,7 +572,7 @@ func generatePol(polengine string, cve string, image string, np *v1alpha1.Nimbus policyBytes, err := json.Marshal(policyData) if err != nil { - logger.V(2).Error(err, "error while marshalling the policies") + return pol, err } pol = kyvernov1.Policy{ @@ -613,7 +624,7 @@ func generatePol(polengine string, cve string, image string, np *v1alpha1.Nimbus policyBytes, err := json.Marshal(policyData) if err != nil { - panic(err.Error()) + return pol, err } pol = kyvernov1.Policy{ ObjectMeta: metav1.ObjectMeta{ diff --git a/pkg/adapter/nimbus-kyverno/utils/utils.go b/pkg/adapter/nimbus-kyverno/utils/utils.go index 7f779605..6d1b48b6 100644 --- a/pkg/adapter/nimbus-kyverno/utils/utils.go +++ b/pkg/adapter/nimbus-kyverno/utils/utils.go @@ -8,6 +8,7 @@ import ( "fmt" "os" "reflect" + "slices" "strings" "sync" @@ -153,12 +154,7 @@ func FetchVirtualPatchData[T any]()(T, error) { } func Contains(slice []string, value string) bool { - for _, item := range slice { - if item == value { - return true - } - } - return false + return slices.Contains(slice, value) } func ParseImageString(imageString string) (string, string) {