Skip to content

Commit

Permalink
Merge pull request #417 from sangkenlee/addtionalprops-fix
Browse files Browse the repository at this point in the history
Gatekeeper 템플릿 스키마에 addtionalProperties가 추가되어 전달되는 버그 수정
  • Loading branch information
intelliguy authored Apr 23, 2024
2 parents 1cb2878 + c6c2c5d commit 3bb9367
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 9 deletions.
19 changes: 12 additions & 7 deletions internal/policy-template/paramdef-util.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,21 +141,26 @@ func CompareParamDefAndExtractedParamDef(paramdef *domain.ParameterDef, extracte
return true
}

func ParamDefsToJSONSchemaProeprties(paramdefs []*domain.ParameterDef) *apiextensionsv1.JSONSchemaProps {
func ParamDefsToJSONSchemaProeprties(paramdefs []*domain.ParameterDef, forValidation bool) *apiextensionsv1.JSONSchemaProps {
if len(paramdefs) == 0 {
return nil
}

result := apiextensionsv1.JSONSchemaProps{
Type: "object",
Properties: convert(paramdefs),
AdditionalProperties: &apiextensionsv1.JSONSchemaPropsOrBool{Allows: false},
Type: "object",
Properties: convert(paramdefs, forValidation),
}

// 파라미터 validation인 경우에는 AddtionalProperties로 스키마에 없는 필드가 처리되지 않아야 함
// Operator에 보낼때는 해당 속성이 없어야 함
if forValidation {
result.AdditionalProperties = &apiextensionsv1.JSONSchemaPropsOrBool{Allows: false}
}

return &result
}

func convert(paramdefs []*domain.ParameterDef) map[string]apiextensionsv1.JSONSchemaProps {
func convert(paramdefs []*domain.ParameterDef, forValidation bool) map[string]apiextensionsv1.JSONSchemaProps {
result := map[string]apiextensionsv1.JSONSchemaProps{}

for _, paramdef := range paramdefs {
Expand All @@ -167,7 +172,7 @@ func convert(paramdefs []*domain.ParameterDef) map[string]apiextensionsv1.JSONSc
result[paramdef.Key] = apiextensionsv1.JSONSchemaProps{
Type: "array",
Items: &apiextensionsv1.JSONSchemaPropsOrArray{
Schema: ParamDefsToJSONSchemaProeprties(paramdef.Children),
Schema: ParamDefsToJSONSchemaProeprties(paramdef.Children, forValidation),
},
}
case isArary:
Expand All @@ -178,7 +183,7 @@ func convert(paramdefs []*domain.ParameterDef) map[string]apiextensionsv1.JSONSc
},
}
case isObject:
props := ParamDefsToJSONSchemaProeprties(paramdef.Children)
props := ParamDefsToJSONSchemaProeprties(paramdef.Children, forValidation)

if props != nil {
result[paramdef.Key] = *props
Expand Down
2 changes: 1 addition & 1 deletion internal/policy-template/policy-operator.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ func PolicyTemplateToTksPolicyTemplateCR(policyTemplate *model.PolicyTemplate) *
Kind: policyTemplate.Kind,
},
Validation: &Validation{
OpenAPIV3Schema: ParamDefsToJSONSchemaProeprties(policyTemplate.ParametersSchema),
OpenAPIV3Schema: ParamDefsToJSONSchemaProeprties(policyTemplate.ParametersSchema, false),
},
},
},
Expand Down
2 changes: 1 addition & 1 deletion internal/policy-template/validation.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ func ValidateParamDefs(paramdefs []*domain.ParameterDef) error {
}

func ValidateJSONusingParamdefs(paramdefs []*domain.ParameterDef, jsonStr string) error {
jsonSchema := ParamDefsToJSONSchemaProeprties(paramdefs)
jsonSchema := ParamDefsToJSONSchemaProeprties(paramdefs, true)

if jsonSchema == nil {
// 파라미터가 없는데 "{}" 이나 ""이면 에러가 아님
Expand Down

0 comments on commit 3bb9367

Please sign in to comment.