Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

파라미터가 빈 경우 CR 변환버그 픽스 #357

Merged
merged 1 commit into from
Apr 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading