|
| 1 | +/* |
| 2 | +Copyright 2022 The Kubernetes Authors. |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +package scaffolds |
| 18 | + |
| 19 | +import ( |
| 20 | + "fmt" |
| 21 | + "os" |
| 22 | + "path/filepath" |
| 23 | + "strings" |
| 24 | + |
| 25 | + log "github.com/sirupsen/logrus" |
| 26 | + "sigs.k8s.io/kubebuilder/v4/pkg/plugins/optional/helm/v1alpha/scaffolds/internal/templates" |
| 27 | + |
| 28 | + "sigs.k8s.io/kubebuilder/v4/pkg/machinery" |
| 29 | + "sigs.k8s.io/kubebuilder/v4/pkg/plugins" |
| 30 | +) |
| 31 | + |
| 32 | +var _ plugins.Scaffolder = &initScaffolder{} |
| 33 | + |
| 34 | +type initScaffolder struct { |
| 35 | + fs machinery.Filesystem |
| 36 | +} |
| 37 | + |
| 38 | +// NewInitScaffolder returns a new Scaffolder for project initialization operations |
| 39 | +func NewInitScaffolder() plugins.Scaffolder { |
| 40 | + return &initScaffolder{} |
| 41 | +} |
| 42 | + |
| 43 | +// InjectFS implements cmdutil.Scaffolder |
| 44 | +func (s *initScaffolder) InjectFS(fs machinery.Filesystem) { |
| 45 | + s.fs = fs |
| 46 | +} |
| 47 | + |
| 48 | +// Scaffold implements cmdutil.Scaffolder |
| 49 | +func (s *initScaffolder) Scaffold() error { |
| 50 | + log.Println("Generating Helm Chart to distribute project") |
| 51 | + |
| 52 | + scaffold := machinery.NewScaffold(s.fs) |
| 53 | + |
| 54 | + err := scaffold.Execute( |
| 55 | + &templates.HelmChart{}, |
| 56 | + &templates.HelmValues{}, |
| 57 | + &templates.HelmIgnore{}, |
| 58 | + ) |
| 59 | + if err != nil { |
| 60 | + return err |
| 61 | + } |
| 62 | + |
| 63 | + // Copy all relevant files from config/ to chart/templates/ |
| 64 | + err = copyConfigFiles() |
| 65 | + if err != nil { |
| 66 | + return err |
| 67 | + } |
| 68 | + return nil |
| 69 | +} |
| 70 | + |
| 71 | +// Helper function to copy files from config/ to dist/chart/templates/ |
| 72 | +func copyConfigFiles() error { |
| 73 | + // Define the source directories and destination directories under dist/chart/templates |
| 74 | + configDirs := []struct { |
| 75 | + SrcDir string |
| 76 | + DestDir string |
| 77 | + SubDir string |
| 78 | + }{ |
| 79 | + {"config/rbac", "dist/chart/templates/rbac", "rbac"}, |
| 80 | + {"config/manager", "dist/chart/templates/manager", "controllerManager"}, |
| 81 | + {"config/webhook", "dist/chart/templates/webhook", "webhook"}, |
| 82 | + {"config/crd/bases", "dist/chart/templates/crds", "crd"}, |
| 83 | + {"config/prometheus", "dist/chart/templates/prometheus", "prometheus"}, |
| 84 | + {"config/certmanager", "dist/chart/templates/certmanager", "certmanager"}, |
| 85 | + {"config/network-policy", "dist/chart/templates/network-policy", "networkPolicy"}, |
| 86 | + } |
| 87 | + |
| 88 | + // Copy all YAML files in each directory |
| 89 | + for _, dir := range configDirs { |
| 90 | + files, err := filepath.Glob(filepath.Join(dir.SrcDir, "*.yaml")) |
| 91 | + if err != nil { |
| 92 | + return err |
| 93 | + } |
| 94 | + |
| 95 | + for _, srcFile := range files { |
| 96 | + destFile := filepath.Join(dir.DestDir, filepath.Base(srcFile)) |
| 97 | + err := copyFileWithHelmLogic(srcFile, destFile, dir.SubDir) |
| 98 | + if err != nil { |
| 99 | + return err |
| 100 | + } |
| 101 | + } |
| 102 | + } |
| 103 | + |
| 104 | + return nil |
| 105 | +} |
| 106 | + |
| 107 | +// copyFileWithHelmLogic reads the source file, wraps it with Helm logic, and writes it to the destination |
| 108 | +func copyFileWithHelmLogic(srcFile, destFile, subDir string) error { |
| 109 | + // Ensure the source file exists |
| 110 | + if _, err := os.Stat(srcFile); os.IsNotExist(err) { |
| 111 | + log.Printf("Source file does not exist: %s", srcFile) |
| 112 | + return err |
| 113 | + } |
| 114 | + |
| 115 | + // Read the source file |
| 116 | + content, err := os.ReadFile(srcFile) |
| 117 | + if err != nil { |
| 118 | + log.Printf("Error reading source file: %s", srcFile) |
| 119 | + return err |
| 120 | + } |
| 121 | + |
| 122 | + // Skip irrelevant files like kustomization.yaml |
| 123 | + if strings.HasSuffix(srcFile, "kustomization.yaml") || strings.HasSuffix(srcFile, "kustomizeconfig.yaml") { |
| 124 | + log.Printf("Skipping irrelevant file: %s", srcFile) |
| 125 | + return nil |
| 126 | + } |
| 127 | + |
| 128 | + // Wrap the content with Helm conditional logic based on the subdirectory (e.g., "rbac", "manager") |
| 129 | + wrappedContent := fmt.Sprintf("{{- if .Values.%s.create }}\n%s\n{{- end }}\n", subDir, string(content)) |
| 130 | + |
| 131 | + // Ensure the destination directory exists |
| 132 | + if err := os.MkdirAll(filepath.Dir(destFile), os.ModePerm); err != nil { |
| 133 | + return err |
| 134 | + } |
| 135 | + |
| 136 | + // Write the wrapped content to the destination file |
| 137 | + err = os.WriteFile(destFile, []byte(wrappedContent), os.ModePerm) |
| 138 | + if err != nil { |
| 139 | + log.Printf("Error writing destination file: %s", destFile) |
| 140 | + return err |
| 141 | + } |
| 142 | + |
| 143 | + log.Printf("Successfully copied %s to %s", srcFile, destFile) |
| 144 | + return nil |
| 145 | +} |
0 commit comments