1
0
Fork 0
mirror of https://github.com/kyverno/kyverno.git synced 2025-03-31 03:45:17 +00:00

GVK format

This commit is contained in:
Vyankatesh Kudtarkar 2021-08-12 23:26:35 +05:30
parent 6ba341ff9b
commit 71803c46d8

View file

@ -60,6 +60,17 @@ func GenerateJSONPatchesForDefaults(policy *kyverno.ClusterPolicy, log logr.Logg
}
patches = append(patches, convertPatch...)
formatedGVK, errs := checkForGVKFormatPatch(policy, log)
if len(errs) > 0 {
var errMsgs []string
for _, err := range errs {
errMsgs = append(errMsgs, err.Error())
log.Error(err, "failed to format the kind")
}
updateMsgs = append(updateMsgs, strings.Join(errMsgs, ";"))
}
patches = append(patches, formatedGVK...)
overlaySMPPatches, errs := convertOverlayToStrategicMerge(policy, log)
if len(errs) > 0 {
var errMsgs []string
@ -74,6 +85,42 @@ func GenerateJSONPatchesForDefaults(policy *kyverno.ClusterPolicy, log logr.Logg
return utils.JoinPatches(patches), updateMsgs
}
func checkForGVKFormatPatch(policy *kyverno.ClusterPolicy, log logr.Logger) (patches [][]byte, errs []error) {
patches = make([][]byte, 0)
for i, rule := range policy.Spec.Rules {
kindList := []string{}
for _, k := range rule.MatchResources.Kinds {
kindList = append(kindList, getFormatedKind(k))
}
jsonPatch := struct {
Path string `json:"path"`
Op string `json:"op"`
Value []string `json:"value"`
}{
fmt.Sprintf("/spec/rules/%s/match/resources/kinds", strconv.Itoa(i)),
"replace",
kindList,
}
patchByte, err := json.Marshal(jsonPatch)
if err != nil {
errs = append(errs, fmt.Errorf("failed to convert policy '%s': %v", policy.Name, err))
}
patches = append(patches, patchByte)
}
return patches, errs
}
func getFormatedKind(str string) (kind string) {
if strings.Count(str, "/") == 0 {
return strings.Title(strings.ToLower(str))
}
splitString := strings.Split(str, "/")
if strings.Count(str, "/") == 1 {
return splitString[0] + "/" + strings.Title(strings.ToLower(splitString[1]))
}
return splitString[0] + "/" + splitString[1] + "/" + strings.Title(strings.ToLower(splitString[2]))
}
func convertPatchToJSON6902(policy *kyverno.ClusterPolicy, log logr.Logger) (patches [][]byte, errs []error) {
patches = make([][]byte, 0)