Skip to content

Commit

Permalink
chore: handled error gracefully, update slice search command
Browse files Browse the repository at this point in the history
Signed-off-by: VedRatan <[email protected]>
  • Loading branch information
VedRatan committed Nov 8, 2024
1 parent 276fa0b commit 9b3eebe
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 14 deletions.
27 changes: 19 additions & 8 deletions pkg/adapter/nimbus-kyverno/processor/kpbuilder.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
Expand All @@ -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)
Expand Down Expand Up @@ -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{
Expand Down Expand Up @@ -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{
Expand Down Expand Up @@ -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{
Expand Down
8 changes: 2 additions & 6 deletions pkg/adapter/nimbus-kyverno/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"fmt"
"os"
"reflect"
"slices"
"strings"
"sync"

Expand Down Expand Up @@ -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) {
Expand Down

0 comments on commit 9b3eebe

Please sign in to comment.