Skip to content

Commit

Permalink
Merge pull request #230 from b4nst/fix/fix-crd-import
Browse files Browse the repository at this point in the history
fix: vendor CRDs with no spec
  • Loading branch information
stefanprodan authored Oct 27, 2023
2 parents 1f97d49 + b199f82 commit 2b7d159
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 5 deletions.
13 changes: 8 additions & 5 deletions internal/engine/importer.go
Original file line number Diff line number Diff line change
Expand Up @@ -325,12 +325,15 @@ func convertCRD(crd cue.Value) (*IntermediateCRD, error) {
}
return true
}, nil)
specd := &ast.Field{
Label: ast.NewIdent("#" + kname + "Spec"),
Value: specf.Value,

if specf != nil {
specd := &ast.Field{
Label: ast.NewIdent("#" + kname + "Spec"),
Value: specf.Value,
}
astutil.CopyComments(specd, specf)
schast.Decls = append(schast.Decls, specd)
}
astutil.CopyComments(specd, specf)
schast.Decls = append(schast.Decls, specd)

if statusf != nil {
statusd := &ast.Field{
Expand Down
59 changes: 59 additions & 0 deletions internal/engine/importer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,65 @@ var multiline = cmpopts.AcyclicTransformer("multiline", func(s string) []string
return strings.Split(s, "\n")
})

func TestConvertCRDWithNoSpec(t *testing.T) {
ctx := cuecontext.New()
g := NewWithT(t)

crds := `{
apiVersion: "apiextensions.k8s.io/v1"
kind: "CustomResourceDefinition"
metadata: {
name: "nospeccases.testing.timoni.sh"
}
spec: {
group: "testing.timoni.sh"
names: {
kind: "NoSpecCase"
listKind: "NoSpecCaseList"
plural: "nospeccases"
singular: "nospeccase"
}
scope: "Namespaced"
versions: [{
name: "v1"
storage: true
schema: {
openAPIV3Schema: {
type: "object"
properties: {
apiVersion: {
description: "APIVersion defines the versioned schema of this representation of an object. Servers should convert recognized schemas to the latest internal value, and may reject unrecognized values. More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#resources"
type: "string"
}
kind: {
description: "Kind is a string value representing the REST resource this object represents. Servers may infer this from the endpoint the client submits requests to. Cannot be updated. In CamelCase. More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#types-kinds"
type: "string"
}
metadata: {
type: "object"
}
}
}
}
}]
}
}`

crd := ctx.CompileString(crds)
g.Expect(crd.Err()).ToNot(HaveOccurred())

ir, err := convertCRD(crd)
g.Expect(err).ToNot(HaveOccurred())

specNode := ir.Schemas[0].Schema.LookupPath(cue.ParsePath("#NoSpecCaseSpec"))
g.Expect(specNode.Exists()).To(BeFalse())

name := ir.Schemas[0].Schema.LookupPath(cue.ParsePath("#NoSpecCase.metadata!.name!"))
g.Expect(name.Exists()).To(BeTrue())
namespace := ir.Schemas[0].Schema.LookupPath(cue.ParsePath("#NoSpecCase.metadata!.namespace!"))
g.Expect(namespace.Exists()).To(BeTrue())
}

func TestConvertCRD(t *testing.T) {
ctx := cuecontext.New()
g := NewWithT(t)
Expand Down

0 comments on commit 2b7d159

Please sign in to comment.