Skip to content

Commit cdb791c

Browse files
committed
add names and only overwrite dstCrd when a file at path was generated
remove status field from resulting struct
1 parent 82c09e2 commit cdb791c

File tree

2 files changed

+42
-27
lines changed

2 files changed

+42
-27
lines changed

pkg/scaffold/crd.go

+40-21
Original file line numberDiff line numberDiff line change
@@ -81,13 +81,10 @@ func (s *Crd) CustomRender() ([]byte, error) {
8181

8282
// controller-tools' generators read and make crds for all apis in pkg/apis,
8383
// so generate crds in a cached, in-memory fs to extract the data we need.
84-
// Note that controller-tools' generator makes different assumptions about
85-
// how crd field values are structured, so we don't want to use the generated
86-
// files directly. This generator will fail if not in a Go project.
8784
if !cache.fileExists(path) && projutil.IsOperatorGo() {
8885
g := &crdgenerator.Generator{
8986
RootPath: s.AbsProjectPath,
90-
Domain: "placeholder", // Our crds don't use this value.
87+
Domain: strings.SplitN(s.Resource.FullGroup, ".", 2)[1],
9188
OutputDir: ".",
9289
SkipMapValidation: false,
9390
OutFs: cache,
@@ -101,19 +98,18 @@ func (s *Crd) CustomRender() ([]byte, error) {
10198
}
10299

103100
dstCrd := newCrdForResource(s.Resource)
104-
var (
105-
b []byte
106-
err error
107-
)
108101
// Get our generated crd's from the in-memory fs. If it doesn't exist in the
109102
// fs, the corresponding API does not exist yet, so scaffold a fresh crd
110103
// without a validation spec.
111-
// If it does, and a local crd exists, append the validation spec. Otherwise,
112-
// generate a fresh crd with the generated validation spec.
113-
b, err = afero.ReadFile(cache, path)
114-
if err != nil && !os.IsNotExist(err) {
104+
// If the crd exists in the fs, and a local crd exists, append the validation
105+
// spec. If a local crd does not exist, use the generated crd.
106+
if _, err := cache.Stat(path); err != nil && !os.IsNotExist(err) {
115107
return nil, err
116-
} else {
108+
} else if err == nil {
109+
b, err := afero.ReadFile(cache, path)
110+
if err != nil {
111+
return nil, err
112+
}
117113
crd := new(apiextv1beta1.CustomResourceDefinition)
118114
if err = yaml.Unmarshal(b, crd); err != nil {
119115
return nil, err
@@ -133,9 +129,25 @@ func (s *Crd) CustomRender() ([]byte, error) {
133129
}
134130
}
135131
dstCrd.Spec.Validation = crd.Spec.Validation.DeepCopy()
132+
// controller-tools does not set ListKind or Singular names.
133+
dstCrd.Spec.Names = getCrdNamesForResource(s.Resource)
134+
dstCrd.Spec.Subresources = &apiextv1beta1.CustomResourceSubresources{
135+
Status: &apiextv1beta1.CustomResourceSubresourceStatus{},
136+
}
136137
}
137138

138-
return yaml.Marshal(dstCrd)
139+
b, err := yaml.Marshal(dstCrd)
140+
if err != nil {
141+
return nil, err
142+
}
143+
// Remove the "status" field from yaml data, which causes a
144+
// resource creation error.
145+
crdMap := make(map[string]interface{})
146+
if err = yaml.Unmarshal(b, &crdMap); err != nil {
147+
return nil, err
148+
}
149+
delete(crdMap, "status")
150+
return yaml.Marshal(&crdMap)
139151
}
140152

141153
func newCrdForResource(r *Resource) *apiextv1beta1.CustomResourceDefinition {
@@ -148,15 +160,22 @@ func newCrdForResource(r *Resource) *apiextv1beta1.CustomResourceDefinition {
148160
Name: r.Resource + "." + r.FullGroup,
149161
},
150162
Spec: apiextv1beta1.CustomResourceDefinitionSpec{
151-
Group: r.FullGroup,
152-
Names: apiextv1beta1.CustomResourceDefinitionNames{
153-
Kind: r.Kind,
154-
ListKind: r.Kind + "List",
155-
Plural: r.Resource,
156-
Singular: r.LowerKind,
157-
},
163+
Group: r.FullGroup,
164+
Names: getCrdNamesForResource(r),
158165
Scope: apiextv1beta1.NamespaceScoped,
159166
Version: r.Version,
167+
Subresources: &apiextv1beta1.CustomResourceSubresources{
168+
Status: &apiextv1beta1.CustomResourceSubresourceStatus{},
169+
},
160170
},
161171
}
162172
}
173+
174+
func getCrdNamesForResource(r *Resource) apiextv1beta1.CustomResourceDefinitionNames {
175+
return apiextv1beta1.CustomResourceDefinitionNames{
176+
Kind: r.Kind,
177+
ListKind: r.Kind + "List",
178+
Plural: r.Resource,
179+
Singular: r.LowerKind,
180+
}
181+
}

pkg/scaffold/crd_test.go

+2-6
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,8 @@ spec:
6868
plural: memcacheds
6969
singular: memcached
7070
scope: Namespaced
71+
subresources:
72+
status: {}
7173
validation:
7274
openAPIV3Schema:
7375
properties:
@@ -95,10 +97,4 @@ spec:
9597
- nodes
9698
type: object
9799
version: v1alpha1
98-
status:
99-
acceptedNames:
100-
kind: ""
101-
plural: ""
102-
conditions: null
103-
storedVersions: null
104100
`

0 commit comments

Comments
 (0)