Skip to content

Commit 494a506

Browse files
🌱 (deploy-image/v2-alpha): improve code implementation for better maintainability
1 parent aba89c5 commit 494a506

File tree

1 file changed

+87
-60
lines changed
  • pkg/plugins/golang/deploy-image/v1alpha1/scaffolds

1 file changed

+87
-60
lines changed

pkg/plugins/golang/deploy-image/v1alpha1/scaffolds/api.go

Lines changed: 87 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -74,12 +74,8 @@ func (s *apiScaffolder) InjectFS(fs machinery.Filesystem) {
7474
func (s *apiScaffolder) Scaffold() error {
7575
fmt.Println("Writing scaffold for you to edit...")
7676

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
8379
}
8480

8581
// Load the boilerplate
@@ -101,26 +97,28 @@ func (s *apiScaffolder) Scaffold() error {
10197
return fmt.Errorf("error updating APIs: %v", err)
10298
}
10399

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)
106104
}
107105

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,
110109
}
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)
114114
}
115115

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)
118118
}
119119

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)
124122
}
125123

126124
if err := scaffold.Execute(
@@ -129,62 +127,75 @@ func (s *apiScaffolder) Scaffold() error {
129127
return fmt.Errorf("error creating controllers/**_controller_test.go: %v", err)
130128
}
131129

130+
if err := s.addEnvVarIntoManager(); err != nil {
131+
return err
132+
}
133+
132134
return nil
133135
}
134136

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 {
136141
managerPath := filepath.Join("config", "manager", "manager.yaml")
137142
err := util.ReplaceInFile(managerPath, `env:`, `env:`)
138143
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 {
142146
return fmt.Errorf("error scaffolding env key in config/manager/manager.yaml")
143147
}
144148
}
145149

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+
146155
return nil
147156
}
148157

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)
155167
}
156168
return nil
157169
}
158170

159171
// 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 {
162175
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-
}
170176

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)),
177181
); err != nil {
178-
return fmt.Errorf("error scaffolding controller: %v", err)
182+
return fmt.Errorf("error scaffolding event recorder in %s: %v", defaultMainPath, err)
179183
}
180184

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",
183193
fmt.Sprintf(containerTemplate, // value for the image
184194
strings.ToLower(s.resource.Kind), // value for the name of the container
185195
),
186196
); 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)
188199
}
189200

190201
// Scaffold the command if informed
@@ -200,53 +211,69 @@ func (s *apiScaffolder) scafffoldControllerWithImage(scaffold *machinery.Scaffol
200211
// remove the first space to not fail in the go fmt ./...
201212
res = strings.TrimLeft(res, " ")
202213

203-
err := util.InsertCode(controllerPath, `SecurityContext: &corev1.SecurityContext{
214+
if err := util.InsertCode(controller.Path, `SecurityContext: &corev1.SecurityContext{
204215
RunAsNonRoot: &[]bool{true}[0],
205216
AllowPrivilegeEscalation: &[]bool{false}[0],
206217
Capabilities: &corev1.Capabilities{
207218
Drop: []corev1.Capability{
208219
"ALL",
209220
},
210221
},
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)
214225
}
215226
}
216227

217228
// Scaffold the port if informed
218229
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{
220233
RunAsNonRoot: &[]bool{true}[0],
221234
AllowPrivilegeEscalation: &[]bool{false}[0],
222235
Capabilities: &corev1.Capabilities{
223236
Drop: []corev1.Capability{
224237
"ALL",
225238
},
226239
},
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)
230249
}
231250
}
232251

233252
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],`,
235256
fmt.Sprintf(runAsUserTemplate, s.runAsUser),
236257
); 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)
238260
}
239261
}
262+
240263
return nil
241264
}
242265

243266
func (s *apiScaffolder) scaffoldCreateAPIFromKustomize() error {
244267
// Now we need call the kustomize/v1 plugin to do its scaffolds when we create a new API
245268
// todo: when we have the go/v4-alpha plugin we will also need to check what is the plugin used
246269
// 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+
)
249275
kustomizeV1Scaffolder.InjectFS(s.fs)
276+
250277
if err := kustomizeV1Scaffolder.Scaffold(); err != nil {
251278
return fmt.Errorf("error scaffolding kustomize files for the APIs: %v", err)
252279
}

0 commit comments

Comments
 (0)