From 431a237e4c1cce9ecfae67b598c2a57f5e20247f Mon Sep 17 00:00:00 2001 From: Jahvon Dockery Date: Wed, 13 Oct 2021 13:58:14 -0400 Subject: [PATCH] Always render some sort of validation schema (#305) * Always render some sort of validation schema * changelog and fix test * hash --- .../v0.21.2/always-include-schema-field.yaml | 4 +++ codegen/kuberesource/crd.go | 25 +++++++++++++++---- codegen/kuberesource/crd_test.go | 4 +-- codegen/render/manifests_renderer_test.go | 2 +- 4 files changed, 27 insertions(+), 8 deletions(-) create mode 100644 changelog/v0.21.2/always-include-schema-field.yaml diff --git a/changelog/v0.21.2/always-include-schema-field.yaml b/changelog/v0.21.2/always-include-schema-field.yaml new file mode 100644 index 000000000..4ea7d1913 --- /dev/null +++ b/changelog/v0.21.2/always-include-schema-field.yaml @@ -0,0 +1,4 @@ +changelog: + - type: FIX + description: Add the Schema field with preserve unknown fields even if we should not render schemas + issueLink: https://github.com/solo-io/skv2/issues/306 diff --git a/codegen/kuberesource/crd.go b/codegen/kuberesource/crd.go index 8e9aed373..fd694fa49 100644 --- a/codegen/kuberesource/crd.go +++ b/codegen/kuberesource/crd.go @@ -23,11 +23,13 @@ func CustomResourceDefinitions( for _, resource := range group.Resources { var validationSchema *apiextv1.CustomResourceValidation - if group.RenderValidationSchemas { - validationSchema, err = constructValidationSchema(resource, group.OpenApiSchemas) - if err != nil { - return nil, err - } + validationSchema, err = constructValidationSchema( + group.RenderValidationSchemas, + resource, + group.OpenApiSchemas, + ) + if err != nil { + return nil, err } objects = append(objects, CustomResourceDefinition(resource, validationSchema, group.SkipSpecHash)) @@ -36,9 +38,22 @@ func CustomResourceDefinitions( } func constructValidationSchema( + renderValidationSchema bool, resource model.Resource, oapiSchemas model.OpenApiSchemas, ) (*apiextv1.CustomResourceValidation, error) { + // Even if we do not want to render validation schemas, we should include + // the top level schema definition and preserve unknown fields since Helm + // requires that some sort of schema is defined + if !renderValidationSchema { + preserveUnknownFields := true + return &apiextv1.CustomResourceValidation{ + OpenAPIV3Schema: &apiextv1.JSONSchemaProps{ + Type: "object", + XPreserveUnknownFields: &preserveUnknownFields, + }, + }, nil + } validationSchema := &apiextv1.CustomResourceValidation{ OpenAPIV3Schema: &apiextv1.JSONSchemaProps{ Type: "object", diff --git a/codegen/kuberesource/crd_test.go b/codegen/kuberesource/crd_test.go index 3d4186385..cd4469dc2 100644 --- a/codegen/kuberesource/crd_test.go +++ b/codegen/kuberesource/crd_test.go @@ -40,9 +40,9 @@ var _ = Describe("Crd", func() { o, err := CustomResourceDefinitions(grp) Expect(err).NotTo(HaveOccurred()) Expect(o).To(HaveLen(1)) - // note: we intentionally provide the "c7fe90c34aa430b" hash in the test, as it shouldn't change + // note: we intentionally provide the "5770277ee8c9bc92" hash in the test, as it shouldn't change // between runs. - Expect(o[0].GetAnnotations()).To(HaveKeyWithValue(crdutils.CRDSpecHashKey, "c7fe90c34aa430b")) + Expect(o[0].GetAnnotations()).To(HaveKeyWithValue(crdutils.CRDSpecHashKey, "5770277ee8c9bc92")) }) It("should not generate spec hash", func() { diff --git a/codegen/render/manifests_renderer_test.go b/codegen/render/manifests_renderer_test.go index 13a81254f..a5d208dd5 100644 --- a/codegen/render/manifests_renderer_test.go +++ b/codegen/render/manifests_renderer_test.go @@ -64,7 +64,7 @@ var _ = Describe("ManifestsRenderer", func() { Expect(err).NotTo(HaveOccurred()) Expect(outFiles).To(HaveLen(1)) Expect(outFiles[0].Content).To(ContainSubstring(crdutils.CRDVersionKey + ": 1.0.0")) - Expect(outFiles[0].Content).To(ContainSubstring(crdutils.CRDSpecHashKey + ": c7fe90c34aa430b")) + Expect(outFiles[0].Content).To(ContainSubstring(crdutils.CRDSpecHashKey + ": 5770277ee8c9bc92")) }) })