From 8b1dcb09049fd3c610754f72cf66355c1f80071c Mon Sep 17 00:00:00 2001 From: sangkenlee Date: Tue, 9 Apr 2024 00:39:21 +0900 Subject: [PATCH] =?UTF-8?q?=ED=8C=8C=EB=9D=BC=EB=AF=B8=ED=84=B0=EA=B0=80?= =?UTF-8?q?=20=EB=B9=88=20=EA=B2=BD=EC=9A=B0=20CR=20=EB=B3=80=ED=99=98?= =?UTF-8?q?=EB=B2=84=EA=B7=B8=20=ED=94=BD=EC=8A=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- internal/policy-template/paramdef-util.go | 8 +++- internal/policy-template/policy-operator.go | 37 +++++++++++++++++-- internal/policy-template/tkspolicytemplate.go | 4 +- 3 files changed, 41 insertions(+), 8 deletions(-) diff --git a/internal/policy-template/paramdef-util.go b/internal/policy-template/paramdef-util.go index 55603847..b136b42c 100644 --- a/internal/policy-template/paramdef-util.go +++ b/internal/policy-template/paramdef-util.go @@ -142,7 +142,7 @@ func CompareParamDefAndExtractedParamDef(paramdef *domain.ParameterDef, extracte } func ParamDefsToJSONSchemaProeprties(paramdefs []*domain.ParameterDef) *apiextensionsv1.JSONSchemaProps { - if paramdefs == nil { + if len(paramdefs) == 0 { return nil } @@ -174,7 +174,11 @@ func convert(paramdefs []*domain.ParameterDef) map[string]apiextensionsv1.JSONSc }, } case isObject: - result[paramdef.Key] = *ParamDefsToJSONSchemaProeprties(paramdef.Children) + props := ParamDefsToJSONSchemaProeprties(paramdef.Children) + + if props != nil { + result[paramdef.Key] = *props + } default: result[paramdef.Key] = apiextensionsv1.JSONSchemaProps{Type: paramdef.Type} } diff --git a/internal/policy-template/policy-operator.go b/internal/policy-template/policy-operator.go index cb10995f..84831e1f 100644 --- a/internal/policy-template/policy-operator.go +++ b/internal/policy-template/policy-operator.go @@ -1,6 +1,7 @@ package policytemplate import ( + "encoding/json" "strings" "github.com/openinfradev/tks-api/internal/model" @@ -23,7 +24,16 @@ func PolicyToTksPolicyCR(policy *model.Policy) *TKSPolicy { return nil } - jsonParams := apiextensionsv1.JSON{Raw: []byte(policy.Parameters)} + var params *apiextensionsv1.JSON = nil + + var jsonResult map[string]interface{} + + err := json.Unmarshal([]byte(policy.Parameters), &jsonResult) + + if err == nil && len(jsonResult) > 0 { + jsonParams := apiextensionsv1.JSON{Raw: []byte(policy.Parameters)} + params = &jsonParams + } labels := map[string]string{} labels[PartOfKey] = PartOfVal @@ -56,7 +66,7 @@ func PolicyToTksPolicyCR(policy *model.Policy) *TKSPolicy { Clusters: policy.TargetClusterIds, Template: policy.PolicyTemplate.Kind, Match: policy.Match, - Params: &jsonParams, + Params: params, }, } } @@ -94,14 +104,33 @@ func PolicyTemplateToTksPolicyTemplateCR(policyTemplate *model.PolicyTemplate) * }, Targets: []Target{{ Target: "admission.k8s.gatekeeper.sh", - Rego: policyTemplate.Rego, - Libs: policyTemplate.Libs, + Rego: stripCarriageReturn(policyTemplate.Rego), + Libs: stripCarriageReturns(policyTemplate.Libs), }}, Version: policyTemplate.Version, }, } } +func stripCarriageReturn(str string) string { + return strings.ReplaceAll(str, "\r", "") +} + +func stripCarriageReturns(strs []string) []string { + if strs == nil { + return nil + } + + result := make([]string, len(strs)) + + for i, str := range strs { + result[i] = stripCarriageReturn(str) + } + + return result + +} + func syncToKubernetes() bool { return true // return os.Getenv("SYNC_POLICY_TO_K8S") != "" diff --git a/internal/policy-template/tkspolicytemplate.go b/internal/policy-template/tkspolicytemplate.go index 412d9007..653952dd 100644 --- a/internal/policy-template/tkspolicytemplate.go +++ b/internal/policy-template/tkspolicytemplate.go @@ -46,8 +46,8 @@ type Validation struct { type Target struct { Target string `json:"target,omitempty"` - Rego string `json:"rego,omitempty"` - Libs []string `json:"libs,omitempty"` + Rego string `json:"rego,omitempty" yaml:"rego,omitempty,flow"` + Libs []string `json:"libs,omitempty" yaml:"libs,omitempty,flow"` Code []Code `json:"code,omitempty"` }