Skip to content

Commit

Permalink
Add support for arbitrary resource annotations
Browse files Browse the repository at this point in the history
  • Loading branch information
matthchr committed Aug 10, 2021
1 parent 7619d96 commit 62d6c85
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 0 deletions.
23 changes: 23 additions & 0 deletions hack/generator/pkg/astmodel/resource_type.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ type ResourceType struct {
properties PropertySet
functions map[string]Function
testcases map[string]TestCase
annotations []string // TODO: Consider ensuring that these are actually kubebuilder annotations.
InterfaceImplementer
}

Expand Down Expand Up @@ -256,6 +257,7 @@ func (resource *ResourceType) Equals(other Type) bool {
len(resource.functions) != len(otherResource.functions) ||
!TypeEquals(resource.spec, otherResource.spec) ||
!TypeEquals(resource.status, otherResource.status) ||
len(resource.annotations) != len(otherResource.annotations) ||
!resource.InterfaceImplementer.Equals(otherResource.InterfaceImplementer) {
return false
}
Expand Down Expand Up @@ -286,6 +288,14 @@ func (resource *ResourceType) Equals(other Type) bool {
}
}

// Check same annotations present
for i, ourAnnotation := range resource.annotations {
otherAnnotation := otherResource.annotations[i]
if ourAnnotation != otherAnnotation {
return false
}
}

return true
}

Expand Down Expand Up @@ -422,6 +432,13 @@ func (resource *ResourceType) WithOwner(owner *TypeName) *ResourceType {
return result
}

// WithAnnotation adds the annotation to the resource and returns a copy of the resource
func (resource *ResourceType) WithAnnotation(annotation string) *ResourceType {
result := resource.copy()
result.annotations = append(result.annotations, annotation)
return result
}

// RequiredPackageReferences returns a list of packages required by this
func (resource *ResourceType) RequiredPackageReferences() *PackageReferenceSet {
references := NewPackageReferenceSet(MetaV1PackageReference)
Expand Down Expand Up @@ -506,6 +523,11 @@ func (resource *ResourceType) AsDeclarations(codeGenerationContext *CodeGenerati
astbuilder.AddComment(&comments, "// +kubebuilder:storageversion")
}

// Add any custom kubebuilder annotations
if len(resource.annotations) > 0 {
astbuilder.AddComments(&comments, resource.annotations)
}

astbuilder.AddWrappedComments(&comments, declContext.Description, 200)
AddValidationComments(&comments, declContext.Validations)

Expand Down Expand Up @@ -613,6 +635,7 @@ func (resource *ResourceType) copy() *ResourceType {
properties: make(map[PropertyName]*PropertyDefinition),
functions: make(map[string]Function),
testcases: make(map[string]TestCase),
annotations: append([]string(nil), resource.annotations...),
InterfaceImplementer: resource.InterfaceImplementer.copy(),
}

Expand Down
17 changes: 17 additions & 0 deletions hack/generator/pkg/astmodel/resource_type_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,3 +147,20 @@ func TestResourceType_WithoutStatusProperty_Panics(t *testing.T) {
base := NewResourceType(emptySpec, emptyStatus)
g.Expect(func() { base.WithoutProperty("Status") }).To(Panic())
}

/*
* WithAnnotation() tests
*/

func TestResourceType_WithAnnotation_ReturnsExpectedInstance(t *testing.T) {
g := NewGomegaWithT(t)

annotation := "kubebuilder:annotation:whatever"
base := NewResourceType(emptySpec, emptyStatus)
updated := base.WithAnnotation(annotation)

g.Expect(base).NotTo(Equal(updated)) // Ensure the original wasn't modified
g.Expect(base.Equals(updated)).To(BeFalse())
g.Expect(updated.annotations).To(HaveLen(1))
g.Expect(updated.annotations[0]).To(Equal(annotation))
}

0 comments on commit 62d6c85

Please sign in to comment.