From d6f262cd21f8fb305e90e0311e33c59ef9ac7150 Mon Sep 17 00:00:00 2001 From: John Esmet Date: Thu, 17 Jan 2019 22:25:36 +0000 Subject: [PATCH] Fix a bug where fetching the chart and stashing the result leads to badness. Only fetch chart metadata. No need to optimize this. --- ankh/main.go | 15 +++++------ helm/helm.go | 71 ++++++++++++++++++++++++++-------------------------- 2 files changed, 44 insertions(+), 42 deletions(-) diff --git a/ankh/main.go b/ankh/main.go index c5a68f5a..c906f325 100644 --- a/ankh/main.go +++ b/ankh/main.go @@ -78,15 +78,17 @@ func printContexts(ankhConfig *ankh.AnkhConfig) { } } -func fetchChartsAndPromptForMissing(ctx *ankh.ExecutionContext, ankhFile *ankh.AnkhFile) error { +func promptForMissingConfigs(ctx *ankh.ExecutionContext, ankhFile *ankh.AnkhFile) error { if ctx.NoPrompt { for i := 0; i < len(ankhFile.Charts); i++ { chart := &ankhFile.Charts[i] - err := helm.FetchChart(ctx, chart) + // Fetch and merge chart metadata + meta, err := helm.FetchChartMeta(ctx, chart) if err != nil { return fmt.Errorf("Error fetching chart \"%v\": %v", chart.Name, err) } + mergo.Merge(&chart.ChartMeta, meta) // This logic is unfortunately duplicated in this function. // The gist is that if ctx.Namespace is set then we have a @@ -149,13 +151,12 @@ func fetchChartsAndPromptForMissing(ctx *ankh.ExecutionContext, ankhFile *ankh.A ctx.Logger.Infof("Using chart \"%v\" from local path \"%v\"", chart.Name, chart.Path) } - // Now that we have either a version or a local path, fetch the chart. - // We may be able to read a namespace from the ankh.yaml meta file - // within the chart. - err := helm.FetchChart(ctx, chart) + // Now that we have either a version or a local path, fetch the chart metadata and merge it. + meta, err := helm.FetchChartMeta(ctx, chart) if err != nil { return fmt.Errorf("Error fetching chart \"%v\": %v", chart.Name, err) } + mergo.Merge(&chart.ChartMeta, meta) // If namespace is set on the command line, we'll use that as an // override later during executeChartsOnNamespace, so don't check @@ -406,7 +407,7 @@ func execute(ctx *ankh.ExecutionContext) { contexts = environment.Contexts } - err = fetchChartsAndPromptForMissing(ctx, &rootAnkhFile) + err = promptForMissingConfigs(ctx, &rootAnkhFile) check(err) if ctx.SlackChannel != "" { diff --git a/helm/helm.go b/helm/helm.go index f5ff81bb..08b1b442 100644 --- a/helm/helm.go +++ b/helm/helm.go @@ -18,7 +18,6 @@ import ( "github.com/appnexus/ankh/context" "github.com/appnexus/ankh/util" - "github.com/imdario/mergo" ) func explain(args []string) string { @@ -136,44 +135,40 @@ func findChartFilesImpl(ctx *ankh.ExecutionContext, chart ankh.Chart) (ankh.Char var findChartFiles = findChartFilesImpl var execContext = exec.Command -func FetchChart(ctx *ankh.ExecutionContext, chart *ankh.Chart) error { +func FetchChartMeta(ctx *ankh.ExecutionContext, chart *ankh.Chart) (ankh.ChartMeta, error) { + meta := ankh.ChartMeta{} + files, err := findChartFiles(ctx, *chart) if err != nil { - return err + return meta, err } - chart.Files = &files // Load `meta` from chart - _, metaErr := os.Stat(chart.Files.MetaPath) + _, metaErr := os.Stat(files.MetaPath) if metaErr == nil { - metaFile, err := os.Open(chart.Files.MetaPath) + metaFile, err := os.Open(files.MetaPath) if err != nil { - return fmt.Errorf("unable to process ankh.yaml file for chart '%s': %v", chart.Name, err) + return meta, fmt.Errorf("unable to process ankh.yaml file for chart '%s': %v", chart.Name, err) } metaFileContent, err := ioutil.ReadAll(metaFile) if err != nil { - return fmt.Errorf("unable to read contents of ankh.yaml file for chart '%s': %v", chart.Name, err) + return meta, fmt.Errorf("unable to read contents of ankh.yaml file for chart '%s': %v", chart.Name, err) } - meta := ankh.ChartMeta{} err = yaml.Unmarshal(metaFileContent, &meta) if err != nil { - return fmt.Errorf("unable to unmarshal yaml of ankh.yaml file for chart '%s': %v", chart.Name, err) + return meta, fmt.Errorf("unable to unmarshal yaml of ankh.yaml file for chart '%s': %v", chart.Name, err) } - mergo.Merge(&chart.ChartMeta, meta) + return meta, nil + } - chart.Files = &files - return nil + return meta, nil } func templateChart(ctx *ankh.ExecutionContext, chart ankh.Chart, namespace string) (string, error) { - if chart.Files == nil { - panic("No chart files present?") - } - currentContext := ctx.AnkhConfig.CurrentContext helmArgs := []string{ctx.AnkhConfig.Helm.Command, "template"} @@ -196,41 +191,47 @@ func templateChart(ctx *ankh.ExecutionContext, chart ankh.Chart, namespace strin helmArgs = append(helmArgs, "--set", chart.ChartMeta.TagKey+"="+*chart.Tag) } + files, err := findChartFiles(ctx, chart) + + if err != nil { + return "", err + } + // Load `values` from chart - _, valuesErr := os.Stat(chart.Files.AnkhValuesPath) + _, valuesErr := os.Stat(files.AnkhValuesPath) if valuesErr == nil { - if _, err := util.CreateReducedYAMLFile(chart.Files.AnkhValuesPath, currentContext.EnvironmentClass, true); err != nil { + if _, err := util.CreateReducedYAMLFile(files.AnkhValuesPath, currentContext.EnvironmentClass, true); err != nil { return "", fmt.Errorf("unable to process ankh-values.yaml file for chart '%s': %v", chart.Name, err) } - helmArgs = append(helmArgs, "-f", chart.Files.AnkhValuesPath) + helmArgs = append(helmArgs, "-f", files.AnkhValuesPath) } // Load `resource-profiles` from chart - _, resourceProfilesError := os.Stat(chart.Files.AnkhResourceProfilesPath) + _, resourceProfilesError := os.Stat(files.AnkhResourceProfilesPath) if resourceProfilesError == nil { - if _, err := util.CreateReducedYAMLFile(chart.Files.AnkhResourceProfilesPath, currentContext.ResourceProfile, true); err != nil { + if _, err := util.CreateReducedYAMLFile(files.AnkhResourceProfilesPath, currentContext.ResourceProfile, true); err != nil { return "", fmt.Errorf("unable to process ankh-resource-profiles.yaml file for chart '%s': %v", chart.Name, err) } - helmArgs = append(helmArgs, "-f", chart.Files.AnkhResourceProfilesPath) + helmArgs = append(helmArgs, "-f", files.AnkhResourceProfilesPath) } // Load `releases` from chart if currentContext.Release != "" { - _, releasesError := os.Stat(chart.Files.AnkhReleasesPath) + _, releasesError := os.Stat(files.AnkhReleasesPath) if releasesError == nil { - out, err := util.CreateReducedYAMLFile(chart.Files.AnkhReleasesPath, currentContext.Release, false) + out, err := util.CreateReducedYAMLFile(files.AnkhReleasesPath, currentContext.Release, false) if err != nil { return "", fmt.Errorf("unable to process ankh-releases.yaml file for chart '%s': %v", chart.Name, err) } if len(out) > 0 { - helmArgs = append(helmArgs, "-f", chart.Files.AnkhReleasesPath) + helmArgs = append(helmArgs, "-f", files.AnkhReleasesPath) } } } // Load `default-values` if chart.DefaultValues != nil { - defaultValuesPath := filepath.Join(chart.Files.Dir, "default-values.yaml") + defaultValuesPath := filepath.Join(files.Dir, "default-values.yaml") defaultValuesBytes, err := yaml.Marshal(chart.DefaultValues) if err != nil { return "", err @@ -250,7 +251,7 @@ func templateChart(ctx *ankh.ExecutionContext, chart ankh.Chart, namespace strin return "", fmt.Errorf("Failed to load `values` for chart %v: %v", chart.Name, err) } if values != nil { - valuesPath := filepath.Join(chart.Files.Dir, "values.yaml") + valuesPath := filepath.Join(files.Dir, "values.yaml") valuesBytes, err := yaml.Marshal(values) if err != nil { return "", err @@ -271,7 +272,7 @@ func templateChart(ctx *ankh.ExecutionContext, chart ankh.Chart, namespace strin return "", fmt.Errorf("Failed to load `resource-profiles` for chart %v: %v", chart.Name, err) } if values != nil { - resourceProfilesPath := filepath.Join(chart.Files.Dir, "resource-profiles.yaml") + resourceProfilesPath := filepath.Join(files.Dir, "resource-profiles.yaml") resourceProfilesBytes, err := yaml.Marshal(values) if err != nil { @@ -293,7 +294,7 @@ func templateChart(ctx *ankh.ExecutionContext, chart ankh.Chart, namespace strin return "", fmt.Errorf("Failed to load `releases` for chart %v: %v", chart.Name, err) } if values != nil { - releasesPath := filepath.Join(chart.Files.Dir, "releases.yaml") + releasesPath := filepath.Join(files.Dir, "releases.yaml") releasesBytes, err := yaml.Marshal(values) if err != nil { @@ -319,16 +320,16 @@ func templateChart(ctx *ankh.ExecutionContext, chart ankh.Chart, namespace strin return "", err } - ctx.Logger.Debugf("writing global values to %s", chart.Files.GlobalPath) + ctx.Logger.Debugf("writing global values to %s", files.GlobalPath) - if err := ioutil.WriteFile(chart.Files.GlobalPath, globalYamlBytes, 0644); err != nil { + if err := ioutil.WriteFile(files.GlobalPath, globalYamlBytes, 0644); err != nil { return "", err } - helmArgs = append(helmArgs, "-f", chart.Files.GlobalPath) + helmArgs = append(helmArgs, "-f", files.GlobalPath) } - helmArgs = append(helmArgs, chart.Files.ChartDir) + helmArgs = append(helmArgs, files.ChartDir) ctx.Logger.Debugf("running helm command %s", strings.Join(helmArgs, " ")) @@ -341,7 +342,7 @@ func templateChart(ctx *ankh.ExecutionContext, chart ankh.Chart, namespace strin helmCmd.Stdout = &stdout helmCmd.Stderr = &stderr - err := helmCmd.Run() + err = helmCmd.Run() var helmOutput, helmError = string(stdout.Bytes()), string(stderr.Bytes()) if err != nil { outputMsg := ""