From c6c2c5d1b11d743a9fbc909859a789de3d5262da Mon Sep 17 00:00:00 2001 From: sangkenlee Date: Tue, 23 Apr 2024 10:22:07 +0900 Subject: [PATCH] =?UTF-8?q?Gatekeeper=20=ED=85=9C=ED=94=8C=EB=A6=BF=20?= =?UTF-8?q?=EC=8A=A4=ED=82=A4=EB=A7=88=EC=97=90=20addtionalProperties?= =?UTF-8?q?=EA=B0=80=20=EC=B6=94=EA=B0=80=EB=90=98=EC=96=B4=20=EC=A0=84?= =?UTF-8?q?=EB=8B=AC=EB=90=98=EB=8A=94=20=EB=B2=84=EA=B7=B8=20=EC=88=98?= =?UTF-8?q?=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- internal/policy-template/paramdef-util.go | 19 ++++++++++++------- internal/policy-template/policy-operator.go | 2 +- internal/policy-template/validation.go | 2 +- 3 files changed, 14 insertions(+), 9 deletions(-) diff --git a/internal/policy-template/paramdef-util.go b/internal/policy-template/paramdef-util.go index e87ffbb2..a0e4c64d 100644 --- a/internal/policy-template/paramdef-util.go +++ b/internal/policy-template/paramdef-util.go @@ -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 { @@ -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: @@ -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 diff --git a/internal/policy-template/policy-operator.go b/internal/policy-template/policy-operator.go index ad192c00..1d1fd9a6 100644 --- a/internal/policy-template/policy-operator.go +++ b/internal/policy-template/policy-operator.go @@ -104,7 +104,7 @@ func PolicyTemplateToTksPolicyTemplateCR(policyTemplate *model.PolicyTemplate) * Kind: policyTemplate.Kind, }, Validation: &Validation{ - OpenAPIV3Schema: ParamDefsToJSONSchemaProeprties(policyTemplate.ParametersSchema), + OpenAPIV3Schema: ParamDefsToJSONSchemaProeprties(policyTemplate.ParametersSchema, false), }, }, }, diff --git a/internal/policy-template/validation.go b/internal/policy-template/validation.go index 5cb01946..ccb83b00 100644 --- a/internal/policy-template/validation.go +++ b/internal/policy-template/validation.go @@ -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 { // 파라미터가 없는데 "{}" 이나 ""이면 에러가 아님