From 1fec430249bfd19fce20062b4919d01be426458c Mon Sep 17 00:00:00 2001 From: Jim Bugwadia Date: Fri, 14 Jan 2022 12:08:04 -0800 Subject: [PATCH] handle CRDs with no props (#2975) * handle CRDs with no props Signed-off-by: Jim Bugwadia * fix tests Signed-off-by: Jim Bugwadia --- pkg/openapi/crdSync.go | 13 +++++++------ pkg/openapi/validation_test.go | 4 ++-- 2 files changed, 9 insertions(+), 8 deletions(-) diff --git a/pkg/openapi/crdSync.go b/pkg/openapi/crdSync.go index 1335c2fa02..6aa5474663 100644 --- a/pkg/openapi/crdSync.go +++ b/pkg/openapi/crdSync.go @@ -171,13 +171,13 @@ func (o *Controller) ParseCRD(crd unstructured.Unstructured) { schemaRaw, _ := json.Marshal(openV3schema) if len(schemaRaw) < 1 { - log.Log.V(4).Info("could not parse crd schema", "name", crdName) + log.Log.V(4).Info("failed to parse crd schema", "name", crdName) return } - schemaRaw, err = addingDefaultFieldsToSchema(schemaRaw) + schemaRaw, err = addingDefaultFieldsToSchema(crdName, schemaRaw) if err != nil { - log.Log.Error(err, "could not parse crd schema", "name", crdName) + log.Log.Error(err, "failed to parse crd schema", "name", crdName) return } @@ -188,7 +188,7 @@ func (o *Controller) ParseCRD(crd unstructured.Unstructured) { if err != nil { v3valueFound := isOpenV3Error(err) if v3valueFound == false { - log.Log.Error(err, "could not parse crd schema", "name", crdName) + log.Log.Error(err, "failed to parse crd schema", "name", crdName) } return } @@ -211,14 +211,15 @@ func isOpenV3Error(err error) bool { } // addingDefaultFieldsToSchema will add any default missing fields like apiVersion, metadata -func addingDefaultFieldsToSchema(schemaRaw []byte) ([]byte, error) { +func addingDefaultFieldsToSchema(crdName string, schemaRaw []byte) ([]byte, error) { var schema struct { Properties map[string]interface{} `json:"properties"` } _ = json.Unmarshal(schemaRaw, &schema) if len(schema.Properties) < 1 { - return nil, errors.New("crd schema has no properties") + log.Log.V(4).Info("crd schema has no properties", "name", crdName) + return schemaRaw, nil } if schema.Properties["apiVersion"] == nil { diff --git a/pkg/openapi/validation_test.go b/pkg/openapi/validation_test.go index c9746a3514..0ac87d7e74 100644 --- a/pkg/openapi/validation_test.go +++ b/pkg/openapi/validation_test.go @@ -61,8 +61,8 @@ func Test_ValidateMutationPolicy(t *testing.T) { } func Test_addDefaultFieldsToSchema(t *testing.T) { - addingDefaultFieldsToSchema([]byte(`null`)) - addingDefaultFieldsToSchema(nil) + addingDefaultFieldsToSchema("", []byte(`null`)) + addingDefaultFieldsToSchema("", nil) } func Test_matchGVK(t *testing.T) {