Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add nil checks for schema when evaluating XRD schema field #161

Merged
merged 1 commit into from
Mar 28, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 14 additions & 4 deletions internal/vendor/github.com/crossplane/crossplane/xcrd/crd.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

61 changes: 34 additions & 27 deletions internal/xpkg/snapshot/validators.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,20 +144,24 @@ func validatorsFromV1CRD(c *extv1.CustomResourceDefinition, acc map[schema.Group

func validatorsFromV1Beta1XRD(x *xpextv1beta1.CompositeResourceDefinition, acc map[schema.GroupVersionKind]*validator.ObjectValidator) error {
for _, v := range x.Spec.Versions {
schema, err := buildSchema(v.Schema.OpenAPIV3Schema)
if err != nil {
return err
// NOTE(tnthornton) if the version does not have a schema, do not
// attempt to create a validator.
if v.Schema != nil {
schema, err := buildSchema(v.Schema.OpenAPIV3Schema)
if err != nil {
return err
}

sv, _, err := newV1SchemaValidator(*schema)
if err != nil {
return err
}

if x.Spec.ClaimNames != nil {
appendToValidators(gvk(x.Spec.Group, v.Name, x.Spec.ClaimNames.Kind), acc, sv)
}
appendToValidators(gvk(x.Spec.Group, v.Name, x.Spec.Names.Kind), acc, sv)
}

sv, _, err := newV1SchemaValidator(*schema)
if err != nil {
return err
}

if x.Spec.ClaimNames != nil {
appendToValidators(gvk(x.Spec.Group, v.Name, x.Spec.ClaimNames.Kind), acc, sv)
}
appendToValidators(gvk(x.Spec.Group, v.Name, x.Spec.Names.Kind), acc, sv)
}
return nil
}
Expand All @@ -173,21 +177,24 @@ func validatorsFromV1XRD(x *xpextv1.CompositeResourceDefinition, acc map[schema.
}

for _, v := range x.Spec.Versions {

schema, err := buildSchema(v.Schema.OpenAPIV3Schema)
if err != nil {
return err
}

sv, _, err := newV1SchemaValidator(*schema)
if err != nil {
return err
}

if x.Spec.ClaimNames != nil {
appendToValidators(gvk(x.Spec.Group, v.Name, x.Spec.ClaimNames.Kind), acc, sv)
// NOTE(tnthornton) if the version does not have a schema, do not
// attempt to create a validator.
if v.Schema != nil {
schema, err := buildSchema(v.Schema.OpenAPIV3Schema)
if err != nil {
return err
}

sv, _, err := newV1SchemaValidator(*schema)
if err != nil {
return err
}

if x.Spec.ClaimNames != nil {
appendToValidators(gvk(x.Spec.Group, v.Name, x.Spec.ClaimNames.Kind), acc, sv)
}
appendToValidators(gvk(x.Spec.Group, v.Name, x.Spec.Names.Kind), acc, sv)
}
appendToValidators(gvk(x.Spec.Group, v.Name, x.Spec.Names.Kind), acc, sv)
}
return nil
}
Expand Down
35 changes: 35 additions & 0 deletions internal/xpkg/snapshot/xrd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,41 @@ func TestValidateOpenAPIV3Schema(t *testing.T) {
},
},
},
"NoSchemaDefined": {
reason: "Not defining a schema is valid.",
args: args{
xrd: &xpextv1.CompositeResourceDefinition{
TypeMeta: metav1.TypeMeta{
APIVersion: "apiextensions.crossplane.io/v1",
Kind: "CompositeResourceDefinition",
},
ObjectMeta: metav1.ObjectMeta{
Name: "xpostgresqlinstances.database.example.org",
},
Spec: xpextv1.CompositeResourceDefinitionSpec{
Group: "database.example.org",
Names: v1.CustomResourceDefinitionNames{
Kind: "XPostgreSQLInstance",
Plural: "xpostgresqlinstances",
},
ClaimNames: &v1.CustomResourceDefinitionNames{
Kind: "PostgreSQLInstance",
Plural: "postgresqlinstances",
},
Versions: []xpextv1.CompositeResourceDefinitionVersion{
{
Name: "v1alpha1",
Served: true,
Referenceable: true,
},
},
},
},
},
want: want{
errs: nil,
},
},
}

for name, tc := range cases {
Expand Down