Skip to content

Commit ffaa806

Browse files
[feat] fail on missing images (#150)
* add option to fail on missing valuesPaths * refactor check to happen in viper bootstrap * add option to fail on unavailable images in helm charts
1 parent b451c29 commit ffaa806

File tree

4 files changed

+24
-15
lines changed

4 files changed

+24
-15
lines changed

example/helmper.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ parser:
66
disableImageDetection: false
77
useCustomValues: false
88
failOnMissingValues: false
9+
failOnMissingImages: false
910
mirrors:
1011
- registry: docker.io
1112
mirror: example.azurecr.io/docker/

internal/bootstrap/viper.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ type ParserConfigSection struct {
7979
DisableImageDetection bool `yaml:"disableImageDetection"`
8080
UseCustomValues bool `yaml:"useCustomValues"`
8181
FailOnMissingValues bool `yaml:"failOnMissingValues"`
82+
FailOnMissingImages bool `yaml:"failOnMissingImages"`
8283
}
8384

8485
type MirrorConfigSection struct {

internal/program.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -116,9 +116,10 @@ func program(ctx context.Context, _ []string, viper *viper.Viper, settings *cli.
116116
// STEP 2: Find images in Helm Charts and dependencies
117117
slog.Debug("Starting parsing user specified chart(s) for images..")
118118
co := helm.ChartOption{
119-
ChartCollection: charts,
120-
IdentifyImages: !parserConfig.DisableImageDetection,
121-
UseCustomValues: parserConfig.UseCustomValues,
119+
ChartCollection: charts,
120+
IdentifyImages: !parserConfig.DisableImageDetection,
121+
UseCustomValues: parserConfig.UseCustomValues,
122+
FailOnMissingImages: parserConfig.FailOnMissingImages,
122123

123124
Mirrors: bootstrap.ConvertToHelmMirrors(mirrorConfig),
124125
Images: images,
@@ -152,7 +153,6 @@ func program(ctx context.Context, _ []string, viper *viper.Viper, settings *cli.
152153

153154
// Step 4: Import charts to registries
154155
if importConfig.Import.Enabled {
155-
ctx := context.WithoutCancel(ctx)
156156
err := helm.ChartImportOption{
157157
Data: mCharts,
158158
All: all,

pkg/helm/chartOption.go

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,10 @@ import (
2626
)
2727

2828
type ChartOption struct {
29-
ChartCollection *ChartCollection
30-
IdentifyImages bool
31-
UseCustomValues bool
29+
ChartCollection *ChartCollection
30+
IdentifyImages bool
31+
UseCustomValues bool
32+
FailOnMissingImages bool
3233

3334
Mirrors []Mirror
3435
Images []image.Image
@@ -417,14 +418,17 @@ func (co *ChartOption) Run(ctx context.Context, setters ...Option) (ChartData, e
417418
return channel
418419
}
419420

420-
imageCollector := func(imgs <-chan *imageInfo) ChartData {
421+
imageCollector := func(imgs <-chan *imageInfo) (ChartData, error) {
421422
chartImageHelmValuesMap := make(ChartData)
422423
id := 0
423424

424425
for i := range imgs {
425426
if !i.available {
426427
str := i.image.String()
427428
slog.Info("Image not available. will be excluded from import...", slog.String("image", str))
429+
if co.FailOnMissingImages {
430+
return nil, xerrors.New("image not available")
431+
}
428432
continue
429433
}
430434

@@ -453,30 +457,33 @@ func (co *ChartOption) Run(ctx context.Context, setters ...Option) (ChartData, e
453457
id = id + 1
454458
}
455459

456-
return chartImageHelmValuesMap
460+
return chartImageHelmValuesMap, nil
457461
}
458462

459-
workload := func(c *ChartCollection) ChartData {
463+
workload := func(c *ChartCollection) (ChartData, error) {
460464
if co.IdentifyImages {
461465
return imageCollector(
462466
imageGenerator(
463467
chartGenerator(c),
464468
),
465469
)
466470
}
467-
return chartCollector(chartGenerator(c))
471+
return chartCollector(chartGenerator(c)), nil
468472
}
469473

470-
cd := workload(co.ChartCollection)
474+
cd, err := workload(co.ChartCollection)
475+
if err != nil {
476+
return nil, err
477+
}
471478

472479
if err := eg.Wait(); err != nil {
473-
return ChartData{}, err
480+
return nil, err
474481
}
475482

476483
// Replace mirrors for further processing
477-
err := replaceWithMirrors(&cd, co.Mirrors)
484+
err = replaceWithMirrors(&cd, co.Mirrors)
478485
if err != nil {
479-
return ChartData{}, err
486+
return nil, err
480487
}
481488

482489
if len(co.Images) > 0 {

0 commit comments

Comments
 (0)