@@ -81,13 +81,10 @@ func (s *Crd) CustomRender() ([]byte, error) {
81
81
82
82
// controller-tools' generators read and make crds for all apis in pkg/apis,
83
83
// 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.
87
84
if ! cache .fileExists (path ) && projutil .IsOperatorGo () {
88
85
g := & crdgenerator.Generator {
89
86
RootPath : s .AbsProjectPath ,
90
- Domain : "placeholder " , // Our crds don't use this value.
87
+ Domain : strings . SplitN ( s . Resource . FullGroup , ". " , 2 )[ 1 ],
91
88
OutputDir : "." ,
92
89
SkipMapValidation : false ,
93
90
OutFs : cache ,
@@ -101,19 +98,18 @@ func (s *Crd) CustomRender() ([]byte, error) {
101
98
}
102
99
103
100
dstCrd := newCrdForResource (s .Resource )
104
- var (
105
- b []byte
106
- err error
107
- )
108
101
// Get our generated crd's from the in-memory fs. If it doesn't exist in the
109
102
// fs, the corresponding API does not exist yet, so scaffold a fresh crd
110
103
// 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 ) {
115
107
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
+ }
117
113
crd := new (apiextv1beta1.CustomResourceDefinition )
118
114
if err = yaml .Unmarshal (b , crd ); err != nil {
119
115
return nil , err
@@ -133,9 +129,25 @@ func (s *Crd) CustomRender() ([]byte, error) {
133
129
}
134
130
}
135
131
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
+ }
136
137
}
137
138
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 )
139
151
}
140
152
141
153
func newCrdForResource (r * Resource ) * apiextv1beta1.CustomResourceDefinition {
@@ -148,15 +160,22 @@ func newCrdForResource(r *Resource) *apiextv1beta1.CustomResourceDefinition {
148
160
Name : r .Resource + "." + r .FullGroup ,
149
161
},
150
162
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 ),
158
165
Scope : apiextv1beta1 .NamespaceScoped ,
159
166
Version : r .Version ,
167
+ Subresources : & apiextv1beta1.CustomResourceSubresources {
168
+ Status : & apiextv1beta1.CustomResourceSubresourceStatus {},
169
+ },
160
170
},
161
171
}
162
172
}
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
+ }
0 commit comments