@@ -74,12 +74,8 @@ func (s *apiScaffolder) InjectFS(fs machinery.Filesystem) {
74
74
func (s * apiScaffolder ) Scaffold () error {
75
75
fmt .Println ("Writing scaffold for you to edit..." )
76
76
77
- if err := s .scaffoldCreateAPIFromGolang (); err != nil {
78
- return fmt .Errorf ("error scaffolding APIs: %v" , err )
79
- }
80
-
81
- if err := s .scaffoldCreateAPIFromKustomize (); err != nil {
82
- return fmt .Errorf ("error scaffolding kustomize file for the new APIs: %v" , err )
77
+ if err := s .scaffoldCreateAPIFromPlugins (); err != nil {
78
+ return err
83
79
}
84
80
85
81
// Load the boilerplate
@@ -101,26 +97,28 @@ func (s *apiScaffolder) Scaffold() error {
101
97
return fmt .Errorf ("error updating APIs: %v" , err )
102
98
}
103
99
104
- if err := s .scafffoldControllerWithImage (scaffold ); err != nil {
105
- return fmt .Errorf ("error updating controller: %v" , err )
100
+ if err := scaffold .Execute (
101
+ & samples.CRDSample {Port : s .port },
102
+ ); err != nil {
103
+ return fmt .Errorf ("error updating config/samples: %v" , err )
106
104
}
107
105
108
- if err := s .updateMainEventRecorder (); err != nil {
109
- return fmt .Errorf ("error updating main.go: %v" , err )
106
+ controller := & controllers.Controller {
107
+ ControllerRuntimeVersion : golangv3scaffolds .ControllerRuntimeVersion ,
108
+ Image : s .image ,
110
109
}
111
-
112
- if err := s .updateManagerWithNewEnvKey (); err != nil {
113
- return fmt .Errorf ("error updating config/manager/manager.yaml with env key: %v" , err )
110
+ if err := scaffold .Execute (
111
+ controller ,
112
+ ); err != nil {
113
+ return fmt .Errorf ("error scaffolding controller: %v" , err )
114
114
}
115
115
116
- if err := s .updateManagerWithNewEnvVar ( ); err != nil {
117
- return fmt .Errorf ("error updating config/manager/manager.yaml with env var : %v" , err )
116
+ if err := s .updateControllerCode ( * controller ); err != nil {
117
+ return fmt .Errorf ("error updating controller : %v" , err )
118
118
}
119
119
120
- if err := scaffold .Execute (
121
- & samples.CRDSample {Port : s .port },
122
- ); err != nil {
123
- return fmt .Errorf ("error updating config/samples: %v" , err )
120
+ if err := s .updateMainByAddingEventRecorder (); err != nil {
121
+ return fmt .Errorf ("error updating main.go: %v" , err )
124
122
}
125
123
126
124
if err := scaffold .Execute (
@@ -129,62 +127,75 @@ func (s *apiScaffolder) Scaffold() error {
129
127
return fmt .Errorf ("error creating controllers/**_controller_test.go: %v" , err )
130
128
}
131
129
130
+ if err := s .addEnvVarIntoManager (); err != nil {
131
+ return err
132
+ }
133
+
132
134
return nil
133
135
}
134
136
135
- func (s * apiScaffolder ) updateManagerWithNewEnvKey () error {
137
+ // addEnvVarIntoManager will update the config/manager/manager.yaml by adding
138
+ // a new ENV VAR for to store the image informed which will be used in the
139
+ // controller to create the Pod for the Kind
140
+ func (s * apiScaffolder ) addEnvVarIntoManager () error {
136
141
managerPath := filepath .Join ("config" , "manager" , "manager.yaml" )
137
142
err := util .ReplaceInFile (managerPath , `env:` , `env:` )
138
143
if err != nil {
139
- err := util .InsertCode (managerPath , `name: manager` , `
140
- env:` )
141
- if err != nil {
144
+ if err := util .InsertCode (managerPath , `name: manager` , `
145
+ env:` ); err != nil {
142
146
return fmt .Errorf ("error scaffolding env key in config/manager/manager.yaml" )
143
147
}
144
148
}
145
149
150
+ if err = util .InsertCode (managerPath , `env:` ,
151
+ fmt .Sprintf (envVarTemplate , strings .ToUpper (s .resource .Kind ), s .image )); err != nil {
152
+ return fmt .Errorf ("error scaffolding env key in config/manager/manager.yaml" )
153
+ }
154
+
146
155
return nil
147
156
}
148
157
149
- func (s * apiScaffolder ) updateManagerWithNewEnvVar () error {
150
- managerPath := filepath .Join ("config" , "manager" , "manager.yaml" )
151
- err := util .InsertCode (managerPath , `env:` ,
152
- fmt .Sprintf (envVarTemplate , strings .ToUpper (s .resource .Kind ), s .image ))
153
- if err != nil {
154
- return fmt .Errorf ("error scaffolding env key in config/manager/manager.yaml" )
158
+ // scaffoldCreateAPIFromPlugins will reuse the code from the kustomize and base golang
159
+ // plugins to do the default scaffolds which an API is created
160
+ func (s * apiScaffolder ) scaffoldCreateAPIFromPlugins () error {
161
+ if err := s .scaffoldCreateAPIFromGolang (); err != nil {
162
+ return fmt .Errorf ("error scaffolding golang files for the new API: %v" , err )
163
+ }
164
+
165
+ if err := s .scaffoldCreateAPIFromKustomize (); err != nil {
166
+ return fmt .Errorf ("error scaffolding kustomize manifests for the new API: %v" , err )
155
167
}
156
168
return nil
157
169
}
158
170
159
171
// TODO: replace this implementation by creating its own MainUpdater
160
- // which will have its own controller template which set the recorder
161
- func (s * apiScaffolder ) updateMainEventRecorder () error {
172
+ // which will have its own controller template which set the recorder so that we can use it
173
+ // in the reconciliation to create an event inside for the finalizer
174
+ func (s * apiScaffolder ) updateMainByAddingEventRecorder () error {
162
175
defaultMainPath := "main.go"
163
- err := util .InsertCode (defaultMainPath , `Scheme: mgr.GetScheme(),` ,
164
- fmt .Sprintf (recorderTemplate , strings .ToLower (s .resource .Kind )))
165
- if err != nil {
166
- return fmt .Errorf ("error scaffolding event recorder while creating the controller in main.go: %v" , err )
167
- }
168
- return nil
169
- }
170
176
171
- func (s * apiScaffolder ) scafffoldControllerWithImage (scaffold * machinery.Scaffold ) error {
172
- controller := & controllers.Controller {ControllerRuntimeVersion : golangv3scaffolds .ControllerRuntimeVersion ,
173
- Image : s .image ,
174
- }
175
- if err := scaffold .Execute (
176
- controller ,
177
+ if err := util .InsertCode (
178
+ defaultMainPath ,
179
+ `Scheme: mgr.GetScheme(),` ,
180
+ fmt .Sprintf (recorderTemplate , strings .ToLower (s .resource .Kind )),
177
181
); err != nil {
178
- return fmt .Errorf ("error scaffolding controller : %v" , err )
182
+ return fmt .Errorf ("error scaffolding event recorder in %s : %v" , defaultMainPath , err )
179
183
}
180
184
181
- controllerPath := controller .Path
182
- if err := util .ReplaceInFile (controllerPath , "//TODO: scaffold container" ,
185
+ return nil
186
+ }
187
+
188
+ // updateControllerCode will update the code generate on the template to add the Container information
189
+ func (s * apiScaffolder ) updateControllerCode (controller controllers.Controller ) error {
190
+ if err := util .ReplaceInFile (
191
+ controller .Path ,
192
+ "//TODO: scaffold container" ,
183
193
fmt .Sprintf (containerTemplate , // value for the image
184
194
strings .ToLower (s .resource .Kind ), // value for the name of the container
185
195
),
186
196
); err != nil {
187
- return fmt .Errorf ("error scaffolding container in the controller: %v" , err )
197
+ return fmt .Errorf ("error scaffolding container in the controller path (%s): %v" ,
198
+ controller .Path , err )
188
199
}
189
200
190
201
// Scaffold the command if informed
@@ -200,53 +211,69 @@ func (s *apiScaffolder) scafffoldControllerWithImage(scaffold *machinery.Scaffol
200
211
// remove the first space to not fail in the go fmt ./...
201
212
res = strings .TrimLeft (res , " " )
202
213
203
- err := util .InsertCode (controllerPath , `SecurityContext: &corev1.SecurityContext{
214
+ if err := util .InsertCode (controller . Path , `SecurityContext: &corev1.SecurityContext{
204
215
RunAsNonRoot: &[]bool{true}[0],
205
216
AllowPrivilegeEscalation: &[]bool{false}[0],
206
217
Capabilities: &corev1.Capabilities{
207
218
Drop: []corev1.Capability{
208
219
"ALL",
209
220
},
210
221
},
211
- },` , fmt .Sprintf (commandTemplate , res ))
212
- if err != nil {
213
- return fmt . Errorf ( "error scaffolding command in the controller: %v" , err )
222
+ },` , fmt .Sprintf (commandTemplate , res )); err != nil {
223
+ return fmt . Errorf ( "error scaffolding command in the controller path (%s): %v" ,
224
+ controller . Path , err )
214
225
}
215
226
}
216
227
217
228
// Scaffold the port if informed
218
229
if len (s .port ) > 0 {
219
- err := util .InsertCode (controllerPath , `SecurityContext: &corev1.SecurityContext{
230
+ if err := util .InsertCode (
231
+ controller .Path ,
232
+ `SecurityContext: &corev1.SecurityContext{
220
233
RunAsNonRoot: &[]bool{true}[0],
221
234
AllowPrivilegeEscalation: &[]bool{false}[0],
222
235
Capabilities: &corev1.Capabilities{
223
236
Drop: []corev1.Capability{
224
237
"ALL",
225
238
},
226
239
},
227
- },` , fmt .Sprintf (portTemplate , strings .ToLower (s .resource .Kind ), strings .ToLower (s .resource .Kind )))
228
- if err != nil {
229
- return fmt .Errorf ("error scaffolding container port in the controller: %v" , err )
240
+ },` ,
241
+ fmt .Sprintf (
242
+ portTemplate ,
243
+ strings .ToLower (s .resource .Kind ),
244
+ strings .ToLower (s .resource .Kind )),
245
+ ); err != nil {
246
+ return fmt .Errorf ("error scaffolding container port in the controller path (%s): %v" ,
247
+ controller .Path ,
248
+ err )
230
249
}
231
250
}
232
251
233
252
if len (s .runAsUser ) > 0 {
234
- if err := util .InsertCode (controllerPath , `RunAsNonRoot: &[]bool{true}[0],` ,
253
+ if err := util .InsertCode (
254
+ controller .Path ,
255
+ `RunAsNonRoot: &[]bool{true}[0],` ,
235
256
fmt .Sprintf (runAsUserTemplate , s .runAsUser ),
236
257
); err != nil {
237
- return fmt .Errorf ("error scaffolding user-id in the controller: %v" , err )
258
+ return fmt .Errorf ("error scaffolding user-id in the controller path (%s): %v" ,
259
+ controller .Path , err )
238
260
}
239
261
}
262
+
240
263
return nil
241
264
}
242
265
243
266
func (s * apiScaffolder ) scaffoldCreateAPIFromKustomize () error {
244
267
// Now we need call the kustomize/v1 plugin to do its scaffolds when we create a new API
245
268
// todo: when we have the go/v4-alpha plugin we will also need to check what is the plugin used
246
269
// in the Project layout to know if we should use kustomize/v1 OR kustomize/v2-alpha
247
- kustomizeV1Scaffolder := kustomizev1scaffolds .NewAPIScaffolder (s .config ,
248
- s .resource , true )
270
+ kustomizeV1Scaffolder := kustomizev1scaffolds .NewAPIScaffolder (
271
+ s .config ,
272
+ s .resource ,
273
+ true ,
274
+ )
249
275
kustomizeV1Scaffolder .InjectFS (s .fs )
276
+
250
277
if err := kustomizeV1Scaffolder .Scaffold (); err != nil {
251
278
return fmt .Errorf ("error scaffolding kustomize files for the APIs: %v" , err )
252
279
}
0 commit comments