Skip to content

Commit

Permalink
Merge pull request #357 from sangkenlee/param_fix
Browse files Browse the repository at this point in the history
파라미터가 빈 경우 CR 변환버그 픽스
  • Loading branch information
seungkyua authored Apr 9, 2024
2 parents 89b485e + 8b1dcb0 commit aa5d0ed
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 8 deletions.
8 changes: 6 additions & 2 deletions internal/policy-template/paramdef-util.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down Expand Up @@ -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}
}
Expand Down
37 changes: 33 additions & 4 deletions internal/policy-template/policy-operator.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package policytemplate

import (
"encoding/json"
"strings"

"github.com/openinfradev/tks-api/internal/model"
Expand All @@ -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
Expand Down Expand Up @@ -56,7 +66,7 @@ func PolicyToTksPolicyCR(policy *model.Policy) *TKSPolicy {
Clusters: policy.TargetClusterIds,
Template: policy.PolicyTemplate.Kind,
Match: policy.Match,
Params: &jsonParams,
Params: params,
},
}
}
Expand Down Expand Up @@ -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") != ""
Expand Down
4 changes: 2 additions & 2 deletions internal/policy-template/tkspolicytemplate.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
}

Expand Down

0 comments on commit aa5d0ed

Please sign in to comment.