Skip to content

Commit

Permalink
Always render some sort of validation schema (#305)
Browse files Browse the repository at this point in the history
* Always render some sort of validation schema
* changelog and fix test
* hash
  • Loading branch information
jahvon authored Oct 13, 2021
1 parent 4f05a71 commit 431a237
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 8 deletions.
4 changes: 4 additions & 0 deletions changelog/v0.21.2/always-include-schema-field.yaml
Original file line number Diff line number Diff line change
@@ -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
25 changes: 20 additions & 5 deletions codegen/kuberesource/crd.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand All @@ -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",
Expand Down
4 changes: 2 additions & 2 deletions codegen/kuberesource/crd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
2 changes: 1 addition & 1 deletion codegen/render/manifests_renderer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"))

})
})
Expand Down

0 comments on commit 431a237

Please sign in to comment.