Skip to content

Commit

Permalink
Add a configuration to exclude images
Browse files Browse the repository at this point in the history
Adds two configuration options that let the user skip images that are
either matching the configured image name or tag.

Related: stacklok#150
  • Loading branch information
jhrozek committed Jun 17, 2024
1 parent 7ab01ef commit f4f2b9f
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 6 deletions.
32 changes: 28 additions & 4 deletions pkg/utils/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ func FromCommand(cmd *cobra.Command) (*Config, error) {
type Config struct {
Platform string `yaml:"platform" mapstructure:"platform"`
GHActions GHActions `yaml:"ghactions" mapstructure:"ghactions"`
Images Images `yaml:"images" mapstructure:"images"`
}

// GHActions is the GitHub Actions configuration.
Expand All @@ -72,15 +73,39 @@ type Filter struct {
Exclude []string `yaml:"exclude" mapstructure:"exclude"`
}

// Images is the image configuration.
type Images struct {
ImageFilter `yaml:",inline" mapstructure:",inline"`
}

// ImageFilter is the image filter configuration.
type ImageFilter struct {
// ExcludeImages is a regex that must match in order for an image to be excluded and not pinned
ExcludeImages []string `yaml:"exclude_images" mapstructure:"exclude_images"`
ExcludeTags []string `yaml:"exclude_tags" mapstructure:"exclude_tags"`
}

// ParseConfigFile parses a configuration file.
func ParseConfigFile(configfile string) (*Config, error) {
bfs := osfs.New(".")
return ParseConfigFileFromFS(bfs, configfile)
}

func defaultConfig() *Config {
return &Config{
Platform: "linux/amd64",
Images: Images{
ImageFilter: ImageFilter{
ExcludeImages: []string{"scratch"},
ExcludeTags: []string{"latest"},
},
},
}
}

// ParseConfigFileFromFS parses a configuration file from a filesystem.
func ParseConfigFileFromFS(fs billy.Filesystem, configfile string) (*Config, error) {
cfg := &Config{}
cfg := defaultConfig()
cleancfgfile := filepath.Clean(configfile)
cfgF, err := fs.Open(cleancfgfile)
if err != nil {
Expand All @@ -94,10 +119,9 @@ func ParseConfigFileFromFS(fs billy.Filesystem, configfile string) (*Config, err

dec := yaml.NewDecoder(cfgF)
if err := dec.Decode(cfg); err != nil {
if err == io.EOF {
return cfg, nil
if err != io.EOF {
return nil, fmt.Errorf("failed to decode config file: %w", err)
}
return nil, fmt.Errorf("failed to decode config file: %w", err)
}

return cfg, nil
Expand Down
21 changes: 19 additions & 2 deletions pkg/utils/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ func TestParseConfigFile(t *testing.T) {
{
name: "FileNotFound",
fileName: "nonexistent.yaml",
expectedResult: &Config{},
expectedResult: defaultConfig(),
},
{
name: "InvalidYaml",
Expand All @@ -100,6 +100,11 @@ ghactions:
exclude:
- pattern1
- pattern2
images:
exclude_images:
- notthisone
exclude_tags:
- notthistag
`,
},
expectedResult: &Config{
Expand All @@ -109,13 +114,19 @@ ghactions:
Exclude: []string{"pattern1", "pattern2"},
},
},
Images: Images{
ImageFilter: ImageFilter{
ExcludeImages: []string{"notthisone"},
ExcludeTags: []string{"notthistag"},
},
},
},
},
{
name: "EmptyFile",
fileName: "empty.yaml",
fsContent: map[string]string{"empty.yaml": ""},
expectedResult: &Config{},
expectedResult: defaultConfig(),
},
}

Expand All @@ -141,6 +152,12 @@ ghactions:
if cfg.GHActions.Exclude != nil {
require.Equal(t, tt.expectedResult.GHActions.Exclude, cfg.GHActions.Exclude)
}
if cfg.Images.ExcludeImages != nil {
require.Equal(t, tt.expectedResult.Images.ExcludeImages, cfg.Images.ExcludeImages)
}
if cfg.Images.ExcludeTags != nil {
require.Equal(t, tt.expectedResult.Images.ExcludeTags, cfg.Images.ExcludeTags)
}
}
})
}
Expand Down

0 comments on commit f4f2b9f

Please sign in to comment.