Skip to content

Commit a540df7

Browse files
author
Phillip Wittrock
authored
Merge pull request #13 from pwittrock/master
Use StatefulSets for controller-managers and support ShortName for CRD
2 parents f0fe674 + 954aa1c commit a540df7

File tree

9 files changed

+264
-162
lines changed

9 files changed

+264
-162
lines changed

cmd/internal/codegen/parse/apis.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,10 @@ type genUnversionedType struct {
3434

3535
func (b *APIs) parseAPIs() {
3636
apis := &codegen.APIs{
37-
Domain: b.Domain,
38-
Package: b.APIsPkg,
39-
Groups: map[string]*codegen.APIGroup{},
40-
Rules: b.Rules,
37+
Domain: b.Domain,
38+
Package: b.APIsPkg,
39+
Groups: map[string]*codegen.APIGroup{},
40+
Rules: b.Rules,
4141
Informers: b.Informers,
4242
}
4343

@@ -70,6 +70,7 @@ func (b *APIs) parseAPIs() {
7070
StatusStrategy: resource.StatusStrategy,
7171
Strategy: resource.Strategy,
7272
NonNamespaced: resource.NonNamespaced,
73+
ShortName: resource.ShortName,
7374
}
7475
apiVersion.Resources[kind] = apiResource
7576
// Set the package for the api version

cmd/internal/codegen/parse/index.go

Lines changed: 100 additions & 97 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ package parse
1919
import (
2020
"fmt"
2121
"log"
22-
"path/filepath"
2322
"strings"
2423

2524
"github.com/kubernetes-sigs/kubebuilder/cmd/internal/codegen"
@@ -71,8 +70,9 @@ func (b *APIs) parseIndex() {
7170

7271
rt := parseResourceTag(b.getResourceTag(c))
7372
r.Resource = rt.Resource
74-
r.REST = rt.REST
75-
r.Strategy = rt.Strategy
73+
r.ShortName = rt.ShortName
74+
//r.REST = rt.REST
75+
//r.Strategy = rt.Strategy
7676

7777
// Copy the Status strategy to mirror the non-status strategy
7878
r.StatusStrategy = strings.TrimSuffix(r.Strategy, "Strategy")
@@ -96,85 +96,86 @@ func (b *APIs) parseIndex() {
9696
b.ByGroupKindVersion[r.Group][r.Kind][r.Version] = r
9797
b.ByGroupVersionKind[r.Group][r.Version][r.Kind] = r
9898

99-
if !HasSubresource(c) {
100-
continue
101-
}
99+
//if !HasSubresource(c) {
100+
// continue
101+
//}
102102
r.Type = c
103-
r.Subresources = b.getSubresources(r)
103+
//r.Subresources = b.getSubresources(r)
104104
}
105105
}
106106

107-
func (b *APIs) getSubresources(c *codegen.APIResource) map[string]*codegen.APISubresource {
108-
r := map[string]*codegen.APISubresource{}
109-
subresources := b.getSubresourceTags(c.Type)
110-
111-
if len(subresources) == 0 {
112-
// Not a subresource
113-
return r
114-
}
115-
for _, subresource := range subresources {
116-
// Parse the values for each subresource
117-
tags := parseSubresourceTag(c, subresource)
118-
sr := &codegen.APISubresource{
119-
Kind: tags.Kind,
120-
Request: tags.RequestKind,
121-
Path: tags.Path,
122-
REST: tags.REST,
123-
Domain: b.Domain,
124-
Version: c.Version,
125-
Resource: c.Resource,
126-
Group: c.Group,
127-
}
128-
if !b.isInPackage(tags) {
129-
// Out of package Request types require an import and are prefixed with the
130-
// package name - e.g. v1.Scale
131-
sr.Request, sr.ImportPackage = b.getNameAndImport(tags)
132-
}
133-
if v, found := r[sr.Path]; found {
134-
log.Fatalf("Multiple subresources registered for path %s: %v %v",
135-
sr.Path, v, subresource)
136-
}
137-
r[sr.Path] = sr
138-
}
139-
return r
140-
}
107+
//func (b *APIs) getSubresources(c *codegen.APIResource) map[string]*codegen.APISubresource {
108+
// r := map[string]*codegen.APISubresource{}
109+
// subresources := b.getSubresourceTags(c.Type)
110+
//
111+
// if len(subresources) == 0 {
112+
// // Not a subresource
113+
// return r
114+
// }
115+
//for _, subresource := range subresources {
116+
// // Parse the values for each subresource
117+
// tags := parseSubresourceTag(c, subresource)
118+
// sr := &codegen.APISubresource{
119+
// Kind: tags.Kind,
120+
// Request: tags.RequestKind,
121+
// Path: tags.Path,
122+
// REST: tags.REST,
123+
// Domain: b.Domain,
124+
// Version: c.Version,
125+
// Resource: c.Resource,
126+
// Group: c.Group,
127+
// }
128+
// if !b.isInPackage(tags) {
129+
// // Out of package Request types require an import and are prefixed with the
130+
// // package name - e.g. v1.Scale
131+
// sr.Request, sr.ImportPackage = b.getNameAndImport(tags)
132+
// }
133+
// if v, found := r[sr.Path]; found {
134+
// log.Fatalf("Multiple subresources registered for path %s: %v %v",
135+
// sr.Path, v, subresource)
136+
// }
137+
// r[sr.Path] = sr
138+
//}
139+
// return r
140+
//}
141141

142142
// subresourceTags contains the tags present in a "+subresource=" comment
143-
type subresourceTags struct {
144-
Path string
145-
Kind string
146-
RequestKind string
147-
REST string
148-
}
149-
150-
func (b *APIs) getSubresourceTags(c *types.Type) []string {
151-
comments := Comments(c.CommentLines)
152-
return comments.getTags("subresource", ":")
153-
}
143+
//type subresourceTags struct {
144+
// Path string
145+
// Kind string
146+
// RequestKind string
147+
// REST string
148+
//}
149+
//
150+
//func (b *APIs) getSubresourceTags(c *types.Type) []string {
151+
// comments := Comments(c.CommentLines)
152+
// return comments.getTags("subresource", ":")
153+
//}
154154

155155
// Returns true if the subresource Request type is in the same package as the resource type
156-
func (b *APIs) isInPackage(tags subresourceTags) bool {
157-
return !strings.Contains(tags.RequestKind, ".")
158-
}
159-
160-
// GetNameAndImport converts
161-
func (b *APIs) getNameAndImport(tags subresourceTags) (string, string) {
162-
last := strings.LastIndex(tags.RequestKind, ".")
163-
importPackage := tags.RequestKind[:last]
164-
165-
// Set the request kind to the struct name
166-
tags.RequestKind = tags.RequestKind[last+1:]
167-
// Find the package
168-
pkg := filepath.Base(importPackage)
169-
// Prefix the struct name with the package it is in
170-
return strings.Join([]string{pkg, tags.RequestKind}, "."), importPackage
171-
}
156+
//func (b *APIs) isInPackage(tags subresourceTags) bool {
157+
// return !strings.Contains(tags.RequestKind, ".")
158+
//}
159+
//
160+
//// GetNameAndImport converts
161+
//func (b *APIs) getNameAndImport(tags subresourceTags) (string, string) {
162+
// last := strings.LastIndex(tags.RequestKind, ".")
163+
// importPackage := tags.RequestKind[:last]
164+
//
165+
// // Set the request kind to the struct name
166+
// tags.RequestKind = tags.RequestKind[last+1:]
167+
// // Find the package
168+
// pkg := filepath.Base(importPackage)
169+
// // Prefix the struct name with the package it is in
170+
// return strings.Join([]string{pkg, tags.RequestKind}, "."), importPackage
171+
//}
172172

173173
// resourceTags contains the tags present in a "+resource=" comment
174174
type resourceTags struct {
175-
Resource string
176-
REST string
177-
Strategy string
175+
Resource string
176+
REST string
177+
Strategy string
178+
ShortName string
178179
}
179180

180181
// ParseResourceTag parses the tags in a "+resource=" comment into a resourceTags struct
@@ -189,40 +190,42 @@ func parseResourceTag(tag string) resourceTags {
189190
}
190191
value := kv[1]
191192
switch kv[0] {
192-
case "rest":
193-
result.REST = value
193+
//case "rest":
194+
// result.REST = value
194195
case "path":
195196
result.Resource = value
196-
case "strategy":
197-
result.Strategy = value
197+
//case "strategy":
198+
// result.Strategy = value
199+
case "shortName":
200+
result.ShortName = value
198201
}
199202
}
200203
return result
201204
}
202205

203206
// ParseSubresourceTag parses the tags in a "+subresource=" comment into a subresourceTags struct
204-
func parseSubresourceTag(c *codegen.APIResource, tag string) subresourceTags {
205-
result := subresourceTags{}
206-
for _, elem := range strings.Split(tag, ",") {
207-
kv := strings.Split(elem, "=")
208-
if len(kv) != 2 {
209-
log.Fatalf("// +subresource: tags must be key value pairs. Expected "+
210-
"keys [request=<requestType>,rest=<restImplType>,path=<subresourcepath>] "+
211-
"Got string: [%s]", tag)
212-
}
213-
value := kv[1]
214-
switch kv[0] {
215-
case "request":
216-
result.RequestKind = value
217-
case "rest":
218-
result.REST = value
219-
case "path":
220-
// Strip the parent resource
221-
result.Path = strings.Replace(value, c.Resource+"/", "", -1)
222-
}
223-
}
224-
return result
225-
}
207+
//func parseSubresourceTag(c *codegen.APIResource, tag string) subresourceTags {
208+
// result := subresourceTags{}
209+
// for _, elem := range strings.Split(tag, ",") {
210+
// kv := strings.Split(elem, "=")
211+
// if len(kv) != 2 {
212+
// log.Fatalf("// +subresource: tags must be key value pairs. Expected "+
213+
// "keys [request=<requestType>,rest=<restImplType>,path=<subresourcepath>] "+
214+
// "Got string: [%s]", tag)
215+
// }
216+
// value := kv[1]
217+
// switch kv[0] {
218+
// case "request":
219+
// result.RequestKind = value
220+
// case "rest":
221+
// result.REST = value
222+
// case "path":
223+
// // Strip the parent resource
224+
// result.Path = strings.Replace(value, c.Resource+"/", "", -1)
225+
// }
226+
// }
227+
// return result
228+
//}
226229

227230
// getResourceTag returns the value of the "+resource=" comment tag
228231
func (b *APIs) getResourceTag(c *types.Type) string {

cmd/internal/codegen/parse/validation.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,10 @@ func (b *APIs) parseJSONSchemaProps() {
7171
} else {
7272
resource.CRD.Spec.Scope = "Namespaced"
7373
}
74+
75+
if len(resource.ShortName) > 0 {
76+
resource.CRD.Spec.Names.ShortNames = []string{resource.ShortName}
77+
}
7478
}
7579
}
7680
}

cmd/internal/codegen/types.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,8 @@ type APIResource struct {
155155
// NonNamespaced indicates that the resource kind is non namespaced
156156
NonNamespaced bool
157157

158+
ShortName string
159+
158160
JSONSchemaProps v1beta1.JSONSchemaProps
159161
CRD v1beta1.CustomResourceDefinition
160162
Validation string

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

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -32,14 +32,14 @@ type versionedGenerator struct {
3232

3333
var _ generator.Generator = &versionedGenerator{}
3434

35-
func hasSubresources(version *codegen.APIVersion) bool {
36-
for _, v := range version.Resources {
37-
if len(v.Subresources) != 0 {
38-
return true
39-
}
40-
}
41-
return false
42-
}
35+
//func hasSubresources(version *codegen.APIVersion) bool {
36+
// for _, v := range version.Resources {
37+
// if len(v.Subresources) != 0 {
38+
// return true
39+
// }
40+
// }
41+
// return false
42+
//}
4343

4444
func (d *versionedGenerator) Imports(c *generator.Context) []string {
4545
imports := []string{
@@ -49,9 +49,9 @@ func (d *versionedGenerator) Imports(c *generator.Context) []string {
4949
"k8s.io/apimachinery/pkg/runtime/schema",
5050
d.apigroup.Pkg.Path,
5151
}
52-
if hasSubresources(d.apiversion) {
53-
imports = append(imports, "k8s.io/apiserver/pkg/registry/rest")
54-
}
52+
//if hasSubresources(d.apiversion) {
53+
// imports = append(imports, "k8s.io/apiserver/pkg/registry/rest")
54+
//}
5555

5656
return imports
5757
}
@@ -120,6 +120,9 @@ var (
120120
Names: v1beta1.CustomResourceDefinitionNames{
121121
Kind: "{{.Kind}}",
122122
Plural: "{{.Resource}}",
123+
{{ if .ShortName -}}
124+
ShortNames: []string{"{{.ShortName}}"},
125+
{{ end -}}
123126
},
124127
{{ if .NonNamespaced -}}
125128
Scope: "Cluster",

cmd/kubebuilder/create/config/config.go

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ package config
1717
import (
1818
"fmt"
1919
"github.com/spf13/cobra"
20+
"log"
2021
"path/filepath"
2122
)
2223

@@ -32,6 +33,11 @@ var configCmd = &cobra.Command{
3233
- Controller Deployment
3334
`,
3435
Run: func(cmd *cobra.Command, args []string) {
36+
if controllerType != "statefulset" && controllerType != "deployment" {
37+
fmt.Printf(
38+
"Invalid value %s for --controller-type, must be statefulset or deployment\n", controllerType)
39+
return
40+
}
3541
if controllerImage == "" {
3642
fmt.Printf("Must specify --controller-image\n")
3743
return
@@ -41,15 +47,17 @@ var configCmd = &cobra.Command{
4147
return
4248
}
4349
CodeGenerator{}.Execute()
50+
log.Printf("Config written to hack/install.yaml")
4451
},
4552
}
4653

47-
var controllerImage string
48-
var name string
49-
var output string
54+
var (
55+
controllerType, controllerImage, name, output string
56+
)
5057

5158
func AddCreateConfig(cmd *cobra.Command) {
5259
cmd.AddCommand(configCmd)
60+
configCmd.Flags().StringVar(&controllerType, "controller-type", "statefulset", "either statefulset or deployment.")
5361
configCmd.Flags().StringVar(&controllerImage, "controller-image", "", "name of the controller container to run.")
5462
configCmd.Flags().StringVar(&name, "name", "", "name of the installation.")
5563
configCmd.Flags().StringVar(&output, "output", filepath.Join("hack", "install.yaml"), "location to write yaml to")

0 commit comments

Comments
 (0)