1
0
Fork 0
mirror of https://github.com/kyverno/kyverno.git synced 2025-03-30 19:35:06 +00:00

handle CRDs with no props (#2975)

* handle CRDs with no props

Signed-off-by: Jim Bugwadia <jim@nirmata.com>

* fix tests

Signed-off-by: Jim Bugwadia <jim@nirmata.com>
This commit is contained in:
Jim Bugwadia 2022-01-14 12:08:04 -08:00 committed by GitHub
parent 1f3e625b99
commit 1fec430249
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 9 additions and 8 deletions

View file

@ -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 {

View file

@ -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) {