From c8745f9796025ca476b0772284e8b66b49718f9f Mon Sep 17 00:00:00 2001 From: Christoffer Nissen Date: Wed, 30 Oct 2024 23:27:38 +0100 Subject: [PATCH] [bug] add charts with no images to ChartOption.Run return value (#138) --- internal/program_test.go | 2 +- pkg/helm/chart.go | 22 +++++++++++++++++++--- pkg/helm/chartImportOption.go | 5 ++--- pkg/helm/chartOption.go | 7 +++++++ 4 files changed, 29 insertions(+), 7 deletions(-) diff --git a/internal/program_test.go b/internal/program_test.go index 785beef..10ef690 100644 --- a/internal/program_test.go +++ b/internal/program_test.go @@ -1089,7 +1089,7 @@ func TestFindImagesInHelmChartsOnKyvernoChart(t *testing.T) { t.Error(err) } - expectedChartCount := 1 + expectedChartCount := 2 expectedImageCount := 10 // Act diff --git a/pkg/helm/chart.go b/pkg/helm/chart.go index 37a29fb..8364ec3 100644 --- a/pkg/helm/chart.go +++ b/pkg/helm/chart.go @@ -76,7 +76,6 @@ func (c Chart) addToHelmRepositoryFile(settings *cli.EnvSettings) (bool, error) } func (c Chart) CountDependencies(settings *cli.EnvSettings) (int, error) { - HelmDriver := "configmap" actionConfig := new(action.Configuration) if err := actionConfig.Init(settings.RESTClientGetter(), settings.Namespace(), HelmDriver, slog.Info); err != nil { @@ -304,8 +303,25 @@ func (c Chart) Pull(settings *cli.EnvSettings) (string, error) { tarPattern := fmt.Sprintf("%s-*%s*.tgz", chartPath, c.Version) if file.FileExists(chartPath) { - slog.Info("Reusing existing archieve for chart", slog.String("chart", c.Name), slog.String("path", chartPath)) - return chartPath, nil + path := filepath.Join(chartPath, chartutil.ChartsDir, c.Name, chartutil.ChartfileName) + if file.FileExists(path) { + // Check chart is the correct version + meta, err := chartutil.LoadChartfile(path) + if err != nil { + return "", err + } + + if meta.Version == c.Version { + slog.Info("Reusing existing achieve for chart", slog.String("chart", c.Name), slog.String("path", chartPath)) + return chartPath, nil + } + + slog.Info("Deleting existing achieve for chart", slog.String("chart", c.Name), slog.String("desired version", c.Version), slog.String("found version", meta.Version), slog.String("path", chartPath)) + err = os.RemoveAll(chartPath) + if err != nil { + return "", err + } + } } if foundPath, ok := findFile(tarPattern); ok { diff --git a/pkg/helm/chartImportOption.go b/pkg/helm/chartImportOption.go index ed75a13..5304617 100644 --- a/pkg/helm/chartImportOption.go +++ b/pkg/helm/chartImportOption.go @@ -64,17 +64,16 @@ func (io *IdentifyImportOption) Run(_ context.Context) (RegistryChartStatus, Reg row := table.Row{sc.Value("index_import_charts"), fmt.Sprintf("charts/%s", c.Name), c.Version} for _, r := range io.Registries { - existsInRegistry := registry.Exists(context.TODO(), n, v, []*registry.Registry{r})[r.URL] - elem := m1[r] if elem == nil { // init map elem = make(map[*Chart]bool, 0) m1[r] = elem } + + existsInRegistry := registry.Exists(context.TODO(), n, v, []*registry.Registry{r})[r.URL] b := io.All || !existsInRegistry elem[&c] = b - if b { sc.Inc(r.URL + "charts") } diff --git a/pkg/helm/chartOption.go b/pkg/helm/chartOption.go index 4f49f08..199f3ba 100644 --- a/pkg/helm/chartOption.go +++ b/pkg/helm/chartOption.go @@ -488,5 +488,12 @@ func (co *ChartOption) Run(ctx context.Context, setters ...Option) (ChartData, e cd[placeHolder] = m } + // Make sure we parse Charts with no images as well + for _, c := range co.ChartCollection.Charts { + if cd[*c] == nil { + cd[*c] = make(map[*image.Image][]string) + } + } + return cd, nil }