|
| 1 | +package preprocessing |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "os" |
| 6 | + "path/filepath" |
| 7 | + "strings" |
| 8 | + |
| 9 | + "github.com/Azure/draft/pkg/safeguards" |
| 10 | + log "github.com/sirupsen/logrus" |
| 11 | + "helm.sh/helm/v3/pkg/chart" |
| 12 | + "helm.sh/helm/v3/pkg/chart/loader" |
| 13 | + "helm.sh/helm/v3/pkg/engine" |
| 14 | + "sigs.k8s.io/kustomize/api/krusty" |
| 15 | + "sigs.k8s.io/kustomize/api/types" |
| 16 | + "sigs.k8s.io/kustomize/kyaml/filesys" |
| 17 | +) |
| 18 | + |
| 19 | +// Given a Helm chart directory or file, renders all templates and writes them to the specified directory |
| 20 | +func RenderHelmChart(isFile bool, mainChartPath, tempDir string) ([]safeguards.ManifestFile, error) { |
| 21 | + if isFile { // Get the directory that the Chart.yaml lives in |
| 22 | + mainChartPath = filepath.Dir(mainChartPath) |
| 23 | + } |
| 24 | + |
| 25 | + mainChart, err := loader.Load(mainChartPath) |
| 26 | + if err != nil { |
| 27 | + return nil, fmt.Errorf("failed to load main chart: %s", err) |
| 28 | + } |
| 29 | + |
| 30 | + loadedCharts := make(map[string]*chart.Chart) // map of chart path to chart object |
| 31 | + loadedCharts[mainChartPath] = mainChart |
| 32 | + |
| 33 | + // Load subcharts and dependencies |
| 34 | + for _, dep := range mainChart.Metadata.Dependencies { |
| 35 | + // Resolve the chart path based on the main chart's directory |
| 36 | + chartPath := filepath.Join(mainChartPath, dep.Repository[len("file://"):]) |
| 37 | + chartPath = filepath.Clean(chartPath) |
| 38 | + |
| 39 | + subChart, err := loader.Load(chartPath) |
| 40 | + if err != nil { |
| 41 | + return nil, fmt.Errorf("failed to load chart: %s", err) |
| 42 | + } |
| 43 | + loadedCharts[chartPath] = subChart |
| 44 | + } |
| 45 | + |
| 46 | + var manifestFiles []safeguards.ManifestFile |
| 47 | + for chartPath, chart := range loadedCharts { |
| 48 | + valuesPath := filepath.Join(chartPath, "values.yaml") // Enforce that values.yaml must be at same level as Chart.yaml |
| 49 | + mergedValues, err := getValues(chart, valuesPath) |
| 50 | + if err != nil { |
| 51 | + return nil, fmt.Errorf("failed to load values: %s", err) |
| 52 | + } |
| 53 | + e := engine.Engine{Strict: true} |
| 54 | + renderedFiles, err := e.Render(chart, mergedValues) |
| 55 | + if err != nil { |
| 56 | + return nil, fmt.Errorf("failed to render chart: %s", err) |
| 57 | + } |
| 58 | + |
| 59 | + // Write each rendered file to the output directory with the same name as in templates/ |
| 60 | + for renderedPath, content := range renderedFiles { |
| 61 | + outputFilePath := filepath.Join(tempDir, filepath.Base(renderedPath)) |
| 62 | + if err := os.WriteFile(outputFilePath, []byte(content), 0644); err != nil { |
| 63 | + return nil, fmt.Errorf("failed to write manifest file: %s", err) |
| 64 | + } |
| 65 | + manifestFiles = append(manifestFiles, safeguards.ManifestFile{Name: filepath.Base(renderedPath), Path: outputFilePath}) |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + return manifestFiles, nil |
| 70 | +} |
| 71 | + |
| 72 | +// CreateTempDir creates a temporary directory on the user's file system for rendering templates |
| 73 | +func CreateTempDir(p string) error { |
| 74 | + err := os.MkdirAll(p, 0755) |
| 75 | + if err != nil { |
| 76 | + log.Fatal(err) |
| 77 | + } |
| 78 | + |
| 79 | + return err |
| 80 | +} |
| 81 | + |
| 82 | +// IsKustomize checks whether a given path should be treated as a kustomize project |
| 83 | +func IsKustomize(p string) bool { |
| 84 | + var err error |
| 85 | + if safeguards.IsYAML(p) { |
| 86 | + return strings.Contains(p, "kustomization.yaml") |
| 87 | + } else if _, err = os.Stat(filepath.Join(p, "kustomization.yaml")); err == nil { |
| 88 | + return true |
| 89 | + } else if _, err = os.Stat(filepath.Join(p, "kustomization.yml")); err == nil { |
| 90 | + return true |
| 91 | + } |
| 92 | + return false |
| 93 | +} |
| 94 | + |
| 95 | +// Given a kustomization manifest file within kustomizationPath, RenderKustomizeManifest will render templates out to tempDir |
| 96 | +func RenderKustomizeManifest(kustomizationPath, tempDir string) ([]safeguards.ManifestFile, error) { |
| 97 | + log.Debugf("Rendering kustomization.yaml...") |
| 98 | + if safeguards.IsYAML(kustomizationPath) { |
| 99 | + kustomizationPath = filepath.Dir(kustomizationPath) |
| 100 | + } |
| 101 | + |
| 102 | + options := &krusty.Options{ |
| 103 | + Reorder: "none", |
| 104 | + LoadRestrictions: types.LoadRestrictionsRootOnly, |
| 105 | + PluginConfig: &types.PluginConfig{}, |
| 106 | + } |
| 107 | + k := krusty.MakeKustomizer(options) |
| 108 | + |
| 109 | + // Run the build to generate the manifests |
| 110 | + kustomizeFS := filesys.MakeFsOnDisk() |
| 111 | + resMap, err := k.Run(kustomizeFS, kustomizationPath) |
| 112 | + if err != nil { |
| 113 | + return nil, fmt.Errorf("Error building manifests: %s\n", err.Error()) |
| 114 | + } |
| 115 | + |
| 116 | + // Output the manifests |
| 117 | + var manifestFiles []safeguards.ManifestFile |
| 118 | + kindMap := make(map[string]int) |
| 119 | + for _, res := range resMap.Resources() { |
| 120 | + yamlRes, err := res.AsYAML() |
| 121 | + if err != nil { |
| 122 | + return nil, fmt.Errorf("Error converting resource to YAML: %s\n", err.Error()) |
| 123 | + } |
| 124 | + |
| 125 | + // index of every kind of manifest for outputRenderPath |
| 126 | + kindMap[res.GetKind()] += 1 |
| 127 | + outputRenderPath := filepath.Join(tempDir, strings.ToLower(res.GetKind())) + fmt.Sprintf("-%d.yaml", kindMap[res.GetKind()]) |
| 128 | + |
| 129 | + err = kustomizeFS.WriteFile(outputRenderPath, yamlRes) |
| 130 | + if err != nil { |
| 131 | + return nil, fmt.Errorf("Error writing yaml resource: %s\n", err.Error()) |
| 132 | + } |
| 133 | + |
| 134 | + // write yamlRes to dir |
| 135 | + manifestFiles = append(manifestFiles, safeguards.ManifestFile{ |
| 136 | + Name: res.GetName(), |
| 137 | + Path: outputRenderPath, |
| 138 | + }) |
| 139 | + } |
| 140 | + |
| 141 | + return manifestFiles, nil |
| 142 | +} |
0 commit comments