-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
Copy pathgenerate.go
444 lines (386 loc) · 13.2 KB
/
generate.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
/*
Copyright 2023 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package internal
import (
"errors"
"fmt"
"os"
"os/exec"
"strings"
log "github.com/sirupsen/logrus"
"github.com/spf13/afero"
"sigs.k8s.io/kubebuilder/v4/pkg/config"
"sigs.k8s.io/kubebuilder/v4/pkg/config/store"
"sigs.k8s.io/kubebuilder/v4/pkg/config/store/yaml"
"sigs.k8s.io/kubebuilder/v4/pkg/machinery"
"sigs.k8s.io/kubebuilder/v4/pkg/model/resource"
"sigs.k8s.io/kubebuilder/v4/pkg/plugin"
"sigs.k8s.io/kubebuilder/v4/pkg/plugin/util"
"sigs.k8s.io/kubebuilder/v4/pkg/plugins/golang/deploy-image/v1alpha1"
"sigs.k8s.io/kubebuilder/v4/pkg/plugins/optional/grafana/v1alpha"
hemlv1alpha "sigs.k8s.io/kubebuilder/v4/pkg/plugins/optional/helm/v1alpha"
)
// Generate store the required info for the command
type Generate struct {
InputDir string
OutputDir string
}
// Generate handles the migration and scaffolding process.
func (opts *Generate) Generate() error {
config, err := loadProjectConfig(opts.InputDir)
if err != nil {
return err
}
if opts.OutputDir == "" {
cwd, err := os.Getwd()
if err != nil {
return fmt.Errorf("failed to get working directory: %w", err)
}
opts.OutputDir = cwd
if _, err := os.Stat(opts.OutputDir); err == nil {
log.Warn("Using current working directory to re-scaffold the project")
log.Warn("This directory will be cleaned up and all files removed before the re-generation")
// Ensure we clean the correct directory
log.Info("Cleaning directory:", opts.OutputDir)
// Use an absolute path to target files directly
cleanupCmd := fmt.Sprintf("rm -rf %s/*", opts.OutputDir)
err = util.RunCmd("Running cleanup", "sh", "-c", cleanupCmd)
if err != nil {
log.Error("Cleanup failed:", err)
return err
}
}
}
if err := createDirectory(opts.OutputDir); err != nil {
return err
}
if err := changeWorkingDirectory(opts.OutputDir); err != nil {
return err
}
if err := kubebuilderInit(config); err != nil {
return err
}
if err := kubebuilderEdit(config); err != nil {
return err
}
if err := kubebuilderCreate(config); err != nil {
return err
}
if err := migrateGrafanaPlugin(config, opts.InputDir, opts.OutputDir); err != nil {
return err
}
if hasHelmPlugin(config) {
if err := kubebuilderHelmEdit(); err != nil {
return err
}
}
return migrateDeployImagePlugin(config)
}
// Validate ensures the options are valid and kubebuilder is installed.
func (opts *Generate) Validate() error {
var err error
opts.InputDir, err = getInputPath(opts.InputDir)
if err != nil {
return err
}
_, err = exec.LookPath("kubebuilder")
if err != nil {
return fmt.Errorf("kubebuilder not found in the path: %w", err)
}
return nil
}
// Helper function to load the project configuration.
func loadProjectConfig(inputDir string) (store.Store, error) {
config := yaml.New(machinery.Filesystem{FS: afero.NewOsFs()})
if err := config.LoadFrom(fmt.Sprintf("%s/%s", inputDir, yaml.DefaultPath)); err != nil {
return nil, fmt.Errorf("failed to load PROJECT file: %w", err)
}
return config, nil
}
// Helper function to create the output directory.
func createDirectory(outputDir string) error {
if err := os.MkdirAll(outputDir, 0755); err != nil {
return fmt.Errorf("failed to create output directory %s: %w", outputDir, err)
}
return nil
}
// Helper function to change the current working directory.
func changeWorkingDirectory(outputDir string) error {
if err := os.Chdir(outputDir); err != nil {
return fmt.Errorf("failed to change the working directory to %s: %w", outputDir, err)
}
return nil
}
// Initializes the project with Kubebuilder.
func kubebuilderInit(store store.Store) error {
args := append([]string{"init"}, getInitArgs(store)...)
return util.RunCmd("kubebuilder init", "kubebuilder", args...)
}
// Edits the project to enable or disable multigroup layout.
func kubebuilderEdit(store store.Store) error {
if store.Config().IsMultiGroup() {
args := []string{"edit", "--multigroup"}
return util.RunCmd("kubebuilder edit", "kubebuilder", args...)
}
return nil
}
// Creates APIs and Webhooks for the project.
func kubebuilderCreate(store store.Store) error {
resources, err := store.Config().GetResources()
if err != nil {
return fmt.Errorf("failed to get resources: %w", err)
}
// First, scaffold all APIs
for _, r := range resources {
if err := createAPI(r); err != nil {
return fmt.Errorf("failed to create API for %s/%s/%s: %w", r.Group, r.Version, r.Kind, err)
}
}
// Then, scaffold all webhooks
// We cannot create a webhook for an API that does not exist
for _, r := range resources {
if err := createWebhook(r); err != nil {
return fmt.Errorf("failed to create webhook for %s/%s/%s: %w", r.Group, r.Version, r.Kind, err)
}
}
return nil
}
// Migrates the Grafana plugin.
func migrateGrafanaPlugin(store store.Store, src, des string) error {
var grafanaPlugin struct{}
err := store.Config().DecodePluginConfig(plugin.KeyFor(v1alpha.Plugin{}), grafanaPlugin)
if errors.As(err, &config.PluginKeyNotFoundError{}) {
log.Info("Grafana plugin not found, skipping migration")
return nil
} else if err != nil {
return fmt.Errorf("failed to decode grafana plugin config: %w", err)
}
if err := kubebuilderGrafanaEdit(); err != nil {
return err
}
if err := grafanaConfigMigrate(src, des); err != nil {
return err
}
return kubebuilderGrafanaEdit()
}
// Migrates the Deploy Image plugin.
func migrateDeployImagePlugin(store store.Store) error {
var deployImagePlugin v1alpha1.PluginConfig
err := store.Config().DecodePluginConfig(plugin.KeyFor(v1alpha1.Plugin{}), &deployImagePlugin)
if errors.As(err, &config.PluginKeyNotFoundError{}) {
log.Info("Deploy-image plugin not found, skipping migration")
return nil
} else if err != nil {
return fmt.Errorf("failed to decode deploy-image plugin config: %w", err)
}
for _, r := range deployImagePlugin.Resources {
if err := createAPIWithDeployImage(r); err != nil {
return fmt.Errorf("failed to create API with deploy-image: %w", err)
}
}
return nil
}
// Creates an API with Deploy Image plugin.
func createAPIWithDeployImage(resource v1alpha1.ResourceData) error {
args := append([]string{"create", "api"}, getGVKFlagsFromDeployImage(resource)...)
args = append(args, getDeployImageOptions(resource)...)
return util.RunCmd("kubebuilder create api", "kubebuilder", args...)
}
// Helper function to get input path.
func getInputPath(inputPath string) (string, error) {
if inputPath == "" {
cwd, err := os.Getwd()
if err != nil {
return "", fmt.Errorf("failed to get working directory: %w", err)
}
inputPath = cwd
}
projectPath := fmt.Sprintf("%s/%s", inputPath, yaml.DefaultPath)
if _, err := os.Stat(projectPath); os.IsNotExist(err) {
return "", fmt.Errorf("project path %s does not exist: %w", projectPath, err)
}
return inputPath, nil
}
// Helper function to get Init arguments for Kubebuilder.
func getInitArgs(store store.Store) []string {
var args []string
plugins := store.Config().GetPluginChain()
if len(plugins) > 0 {
args = append(args, "--plugins", strings.Join(plugins, ","))
}
if domain := store.Config().GetDomain(); domain != "" {
args = append(args, "--domain", domain)
}
return args
}
// Gets the GVK flags for a resource.
func getGVKFlags(resource resource.Resource) []string {
var args []string
if resource.Plural != "" {
args = append(args, "--plural", resource.Plural)
}
if resource.Group != "" {
args = append(args, "--group", resource.Group)
}
if resource.Version != "" {
args = append(args, "--version", resource.Version)
}
if resource.Kind != "" {
args = append(args, "--kind", resource.Kind)
}
return args
}
// Gets the GVK flags for a Deploy Image resource.
func getGVKFlagsFromDeployImage(resource v1alpha1.ResourceData) []string {
var args []string
if resource.Group != "" {
args = append(args, "--group", resource.Group)
}
if resource.Version != "" {
args = append(args, "--version", resource.Version)
}
if resource.Kind != "" {
args = append(args, "--kind", resource.Kind)
}
return args
}
// Gets the options for a Deploy Image resource.
func getDeployImageOptions(resource v1alpha1.ResourceData) []string {
var args []string
if resource.Options.Image != "" {
args = append(args, fmt.Sprintf("--image=%s", resource.Options.Image))
}
if resource.Options.ContainerCommand != "" {
args = append(args, fmt.Sprintf("--image-container-command=%s", resource.Options.ContainerCommand))
}
if resource.Options.ContainerPort != "" {
args = append(args, fmt.Sprintf("--image-container-port=%s", resource.Options.ContainerPort))
}
if resource.Options.RunAsUser != "" {
args = append(args, fmt.Sprintf("--run-as-user=%s", resource.Options.RunAsUser))
}
args = append(args, fmt.Sprintf("--plugins=%s", plugin.KeyFor(v1alpha1.Plugin{})))
return args
}
// Creates an API resource.
func createAPI(resource resource.Resource) error {
args := append([]string{"create", "api"}, getGVKFlags(resource)...)
args = append(args, getAPIResourceFlags(resource)...)
// Add the external API path flag if the resource is external
if resource.IsExternal() {
args = append(args, "--external-api-path", resource.Path)
args = append(args, "--external-api-domain", resource.Domain)
}
return util.RunCmd("kubebuilder create api", "kubebuilder", args...)
}
// Gets flags for API resource creation.
func getAPIResourceFlags(resource resource.Resource) []string {
var args []string
if resource.API == nil || resource.API.IsEmpty() {
args = append(args, "--resource=false")
} else {
args = append(args, "--resource")
if resource.API.Namespaced {
args = append(args, "--namespaced")
} else {
args = append(args, "--namespaced=false")
}
}
if resource.Controller {
args = append(args, "--controller")
} else {
args = append(args, "--controller=false")
}
return args
}
// Creates a webhook resource.
func createWebhook(resource resource.Resource) error {
if resource.Webhooks == nil || resource.Webhooks.IsEmpty() {
return nil
}
args := append([]string{"create", "webhook"}, getGVKFlags(resource)...)
args = append(args, getWebhookResourceFlags(resource)...)
return util.RunCmd("kubebuilder create webhook", "kubebuilder", args...)
}
// Gets flags for webhook creation.
func getWebhookResourceFlags(resource resource.Resource) []string {
var args []string
if resource.IsExternal() {
args = append(args, "--external-api-path", resource.Path)
args = append(args, "--external-api-domain", resource.Domain)
}
if resource.HasValidationWebhook() {
args = append(args, "--programmatic-validation")
}
if resource.HasDefaultingWebhook() {
args = append(args, "--defaulting")
}
if resource.HasConversionWebhook() {
args = append(args, "--conversion")
if len(resource.Webhooks.Spoke) > 0 {
for _, spoke := range resource.Webhooks.Spoke {
args = append(args, "--spoke", spoke)
}
}
}
return args
}
// Copies files from source to destination.
func copyFile(src, des string) error {
bytesRead, err := os.ReadFile(src)
if err != nil {
return fmt.Errorf("source file path %s does not exist: %w", src, err)
}
return os.WriteFile(des, bytesRead, 0755)
}
// Migrates Grafana configuration files.
func grafanaConfigMigrate(src, des string) error {
grafanaConfig := fmt.Sprintf("%s/grafana/custom-metrics/config.yaml", src)
if _, err := os.Stat(grafanaConfig); os.IsNotExist(err) {
return fmt.Errorf("Grafana config path %s does not exist: %w", grafanaConfig, err)
}
return copyFile(grafanaConfig, fmt.Sprintf("%s/grafana/custom-metrics/config.yaml", des))
}
// Edits the project to include the Grafana plugin.
func kubebuilderGrafanaEdit() error {
args := []string{"edit", "--plugins", plugin.KeyFor(v1alpha.Plugin{})}
if err := util.RunCmd("kubebuilder edit", "kubebuilder", args...); err != nil {
return fmt.Errorf("failed to run edit subcommand for Grafana plugin: %w", err)
}
return nil
}
// Edits the project to include the Helm plugin.
func kubebuilderHelmEdit() error {
args := []string{"edit", "--plugins", plugin.KeyFor(hemlv1alpha.Plugin{})}
if err := util.RunCmd("kubebuilder edit", "kubebuilder", args...); err != nil {
return fmt.Errorf("failed to run edit subcommand for Helm plugin: %w", err)
}
return nil
}
// hasHelmPlugin checks if the Helm plugin is present by inspecting the plugin chain or configuration.
func hasHelmPlugin(cfg store.Store) bool {
var pluginConfig map[string]interface{}
// Decode the Helm plugin configuration to check if it's present
err := cfg.Config().DecodePluginConfig(plugin.KeyFor(hemlv1alpha.Plugin{}), &pluginConfig)
if err != nil {
// If the Helm plugin is not found, return false
if errors.As(err, &config.PluginKeyNotFoundError{}) {
return false
}
// Log other errors if needed
log.Errorf("Error decoding Helm plugin config: %v", err)
return false
}
// Helm plugin is present
return true
}