Skip to content

Commit 2ca8b7a

Browse files
authored
Merge pull request #269 from pmorie/status-subresource
Support for status subresource
2 parents 964a6b0 + bc21162 commit 2ca8b7a

File tree

8 files changed

+54
-12
lines changed

8 files changed

+54
-12
lines changed

cmd/internal/codegen/parse/crd.go

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,14 @@ func (b *APIs) parseCRDs() {
8080
resource.CRD.Spec.Names.Categories = strings.Split(categoriesTag, ",")
8181
}
8282

83+
if HasStatusSubresource(resource.Type) {
84+
subresources := &v1beta1.CustomResourceSubresources{
85+
Status: &v1beta1.CustomResourceSubresourceStatus{},
86+
}
87+
resource.CRD.Spec.Subresources = subresources
88+
resource.HasStatusSubresource = true
89+
}
90+
8391
if len(resource.ShortName) > 0 {
8492
resource.CRD.Spec.Names.ShortNames = []string{resource.ShortName}
8593
}
@@ -318,7 +326,7 @@ func (b *APIs) parseArrayValidation(t *types.Type, found sets.String, comments [
318326

319327
type objectTemplateArgs struct {
320328
v1beta1.JSONSchemaProps
321-
Fields map[string]string
329+
Fields map[string]string
322330
Required []string
323331
}
324332

@@ -514,7 +522,7 @@ func (b *APIs) getMembers(t *types.Type, found sets.String) (map[string]v1beta1.
514522

515523
// Inline "inline" structs
516524
if strat == "inline" {
517-
m, r, re:= b.getMembers(member.Type, found)
525+
m, r, re := b.getMembers(member.Type, found)
518526
for n, v := range m {
519527
members[n] = v
520528
}

cmd/internal/codegen/parse/util.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,20 @@ func HasSubresource(t *types.Type) bool {
121121
return false
122122
}
123123

124+
// HasStatusSubresource returns true if t is an APIResource annotated with
125+
// +kubebuilder:subresource:status.
126+
func HasStatusSubresource(t *types.Type) bool {
127+
if !IsAPIResource(t) {
128+
return false
129+
}
130+
for _, c := range t.CommentLines {
131+
if strings.Contains(c, "+kubebuilder:subresource:status") {
132+
return true
133+
}
134+
}
135+
return false
136+
}
137+
124138
// HasCategories returns true if t is an APIResource annotated with
125139
// +kubebuilder:categories.
126140
func HasCategories(t *types.Type) bool {

cmd/internal/codegen/types.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,8 @@ type APIResource struct {
164164
ValidationComments string
165165
// DocAnnotation is a map of annotations by name for doc. e.g. warning, notes message
166166
DocAnnotation map[string]string
167+
// HasStatusSubresource indicates that the resource has a status subresource
168+
HasStatusSubresource bool
167169
}
168170

169171
type APISubresource struct {

cmd/kubebuilder-gen/internal/resourcegen/versioned_generator.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,11 @@ var (
136136
Validation: &v1beta1.CustomResourceValidation{
137137
OpenAPIV3Schema: &{{.Validation}},
138138
},
139+
{{ if .HasStatusSubresource -}}
140+
Subresources: &v1beta1.CustomResourceSubresources{
141+
Status: &v1beta1.CustomResourceSubresourceStatus{},
142+
},
143+
{{ end -}}
139144
},
140145
}
141146
{{ end -}}

cmd/kubebuilder/create/resource/resource.go

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -26,15 +26,16 @@ import (
2626
)
2727

2828
type resourceTemplateArgs struct {
29-
BoilerPlate string
30-
Domain string
31-
Group string
32-
Version string
33-
Kind string
34-
Resource string
35-
Repo string
36-
PluralizedKind string
37-
NonNamespacedKind bool
29+
BoilerPlate string
30+
Domain string
31+
Group string
32+
Version string
33+
Kind string
34+
Resource string
35+
Repo string
36+
PluralizedKind string
37+
NonNamespacedKind bool
38+
HasStatusSubresource bool
3839
}
3940

4041
func doResource(dir string, args resourceTemplateArgs) bool {

cmd/kubebuilder/create/resource/run.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,7 @@ func createResource(boilerplate string) {
109109
util.Repo,
110110
inflect.NewDefaultRuleset().Pluralize(createutil.KindName),
111111
nonNamespacedKind,
112+
false,
112113
}
113114

114115
dir, err := os.Getwd()

pkg/gen/apis/doc.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@ const (
2323
// Resource annotates a type as a resource
2424
Resource = "// +kubebuilder:resource:path="
2525

26+
// StatusSubresource annotates a type as having a status subresource
27+
StatusSubresource = "// +kubebuilder:subresource:status"
28+
2629
// Categories annotates a type as belonging to a comma-delimited list of
2730
// categories
2831
Categories = "// +kubebuilder:categories="

test.sh

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,8 @@ function generate_crd_resources {
155155

156156
header_text "editing generated files to simulate a user"
157157
sed -i -e '/type Bee struct/ i \
158-
// +kubebuilder:categories=foo,bar
158+
// +kubebuilder:categories=foo,bar\
159+
// +kubebuilder:subresource:status
159160
' pkg/apis/insect/v1beta1/bee_types.go
160161

161162
sed -i -e '/type BeeController struct {/ i \
@@ -194,6 +195,8 @@ spec:
194195
kind: Bee
195196
plural: bees
196197
scope: Namespaced
198+
subresources:
199+
status: {}
197200
validation:
198201
openAPIV3Schema:
199202
properties:
@@ -290,6 +293,8 @@ spec:
290293
kind: Bee
291294
plural: bees
292295
scope: Namespaced
296+
subresources:
297+
status: {}
293298
validation:
294299
openAPIV3Schema:
295300
properties:
@@ -440,6 +445,8 @@ spec:
440445
kind: Bee
441446
plural: bees
442447
scope: Namespaced
448+
subresources:
449+
status: {}
443450
validation:
444451
openAPIV3Schema:
445452
properties:
@@ -534,6 +541,7 @@ function test_crd_validation {
534541
Comment []byte \`json:"comment,omitempty"\`\
535542
' pkg/apis/got/v1beta1/house_types.go
536543

544+
header_text "calling kubebuilder generate"
537545
kubebuilder generate
538546
header_text "generating and testing CRD..."
539547
kubebuilder create config --crds --output crd-validation.yaml

0 commit comments

Comments
 (0)