Skip to content

Commit

Permalink
Support multiple values files (#56)
Browse files Browse the repository at this point in the history
* feat: support multiple values yaml files
* chore: remove unused parameters
  • Loading branch information
alemorcuq committed Mar 8, 2024
1 parent fa79460 commit e4f8260
Show file tree
Hide file tree
Showing 29 changed files with 138 additions and 112 deletions.
2 changes: 1 addition & 1 deletion cmd/dt/annotate/annotate.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ Use it cautiously. Very often the complete list of images cannot be guessed from
SilenceUsage: true,
SilenceErrors: true,
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
RunE: func(_ *cobra.Command, args []string) error {
chartPath := args[0]
l := cfg.Logger()

Expand Down
2 changes: 1 addition & 1 deletion cmd/dt/carvelize/carvelize.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func NewCmd(cfg *config.Config) *cobra.Command {
SilenceUsage: true,
SilenceErrors: true,
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
RunE: func(_ *cobra.Command, args []string) error {
chartPath := args[0]
l := cfg.Logger()
// Allows silencing called methods
Expand Down
4 changes: 2 additions & 2 deletions cmd/dt/carvelize_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ func (suite *CmdSuite) TestCarvelizeCommand() {
res := dt(args...)
res.AssertSuccess(t)

t.Run("Generates Carvel bundle", func(t *testing.T) {
t.Run("Generates Carvel bundle", func(_ *testing.T) {
newBundleData, err := os.ReadFile(filepath.Join(chartDir, carvel.CarvelBundleFilePath))
require.NoError(err)
var newBundle map[string]interface{}
Expand All @@ -76,7 +76,7 @@ func (suite *CmdSuite) TestCarvelizeCommand() {
require.Equal(expectedBundle, newBundle)
})

t.Run("Generates Carvel images", func(t *testing.T) {
t.Run("Generates Carvel images", func(_ *testing.T) {
newImagesData, err := os.ReadFile(filepath.Join(chartDir, carvel.CarvelImagesFilePath))
require.NoError(err)
var newImagesLock map[string]interface{}
Expand Down
2 changes: 1 addition & 1 deletion cmd/dt/chart.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ var chartCmd = &cobra.Command{
Short: "Helm chart management commands",
SilenceUsage: true,
SilenceErrors: true,
Run: func(cmd *cobra.Command, args []string) {
Run: func(cmd *cobra.Command, _ []string) {
_ = cmd.Help()
},
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/dt/images.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ var imagesCmd = &cobra.Command{
SilenceUsage: true,
SilenceErrors: true,
Short: "Container image management commands",
Run: func(cmd *cobra.Command, args []string) {
Run: func(cmd *cobra.Command, _ []string) {
_ = cmd.Help()
},
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/dt/info/info.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ func NewCmd(cfg *config.Config) *cobra.Command {
SilenceUsage: true,
SilenceErrors: true,
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
RunE: func(_ *cobra.Command, args []string) error {
chartPath := args[0]
l := cfg.Logger()
_, _ = chartPath, l
Expand Down
2 changes: 1 addition & 1 deletion cmd/dt/lock/lock.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ func NewCmd(cfg *config.Config) *cobra.Command {
SilenceUsage: true,
SilenceErrors: true,
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
RunE: func(_ *cobra.Command, args []string) error {
l := cfg.Logger()

chartPath := args[0]
Expand Down
2 changes: 1 addition & 1 deletion cmd/dt/pull/pull.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func NewCmd(cfg *config.Config) *cobra.Command {
Args: cobra.ExactArgs(1),
SilenceUsage: true,
SilenceErrors: true,
RunE: func(cmd *cobra.Command, args []string) error {
RunE: func(_ *cobra.Command, args []string) error {
chartPath := args[0]
l := cfg.Logger()

Expand Down
2 changes: 1 addition & 1 deletion cmd/dt/push/push.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ func NewCmd(cfg *config.Config) *cobra.Command {
Args: cobra.ExactArgs(1),
SilenceUsage: true,
SilenceErrors: true,
RunE: func(cmd *cobra.Command, args []string) error {
RunE: func(_ *cobra.Command, args []string) error {
l := cfg.Logger()

chartPath := args[0]
Expand Down
10 changes: 8 additions & 2 deletions cmd/dt/relocate/relocate.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ import (

// NewCmd builds a new relocate command
func NewCmd(cfg *config.Config) *cobra.Command {
return &cobra.Command{
valuesFiles := []string{"values.yaml"}
cmd := &cobra.Command{
Use: "relocate CHART_PATH OCI_URI",
Short: "Relocates a Helm chart",
Long: "Relocates a Helm chart into a new OCI registry. This command will replace the existing registry references with the new registry both in the Images.lock and values.yaml files",
Expand All @@ -20,7 +21,7 @@ func NewCmd(cfg *config.Config) *cobra.Command {
Args: cobra.ExactArgs(2),
SilenceUsage: true,
SilenceErrors: true,
RunE: func(cmd *cobra.Command, args []string) error {
RunE: func(_ *cobra.Command, args []string) error {
chartPath, repository := args[0], args[1]
if repository == "" {
return fmt.Errorf("repository cannot be empty")
Expand All @@ -33,6 +34,7 @@ func NewCmd(cfg *config.Config) *cobra.Command {
repository,
relocator.WithLog(l), relocator.Recursive,
relocator.WithAnnotationsKey(cfg.AnnotationsKey),
relocator.WithValuesFiles(valuesFiles...),
)
}); err != nil {
return l.Failf("failed to relocate Helm chart %q: %w", chartPath, err)
Expand All @@ -42,4 +44,8 @@ func NewCmd(cfg *config.Config) *cobra.Command {
return nil
},
}

cmd.PersistentFlags().StringSliceVar(&valuesFiles, "values", valuesFiles, "values files to relocate images (can specify multiple)")

return cmd
}
4 changes: 2 additions & 2 deletions cmd/dt/relocate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ func (suite *CmdSuite) TestRelocateCommand() {

scenarioDir := fmt.Sprintf("../../testdata/scenarios/%s", scenarioName)

renderLockedChart := func(chartDir string, scenarioName string, serverURL string) string {
renderLockedChart := func(chartDir string, _ string, serverURL string) string {

require.NoError(tu.RenderScenario(scenarioDir, chartDir,
map[string]interface{}{"ServerURL": serverURL, "Images": images, "Name": chartName, "RepositoryURL": serverURL},
Expand All @@ -51,7 +51,7 @@ func (suite *CmdSuite) TestRelocateCommand() {
suite.Require().NoError(os.WriteFile(filepath.Join(chartDir, "Images.lock"), []byte(data), 0644))
return chartDir
}
suite.T().Run("Relocate Helm chart", func(t *testing.T) {
suite.T().Run("Relocate Helm chart", func(_ *testing.T) {
relocateURL := "custom.repo.example.com"
originChart := renderLockedChart(sb.TempFile(), scenarioName, serverURL)
expectedRelocatedDir := renderLockedChart(sb.TempFile(), scenarioName, relocateURL)
Expand Down
2 changes: 1 addition & 1 deletion cmd/dt/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ var mainConfig = config.NewConfig()
func newRootCmd() *cobra.Command {
cmd := &cobra.Command{
Use: filepath.Base(os.Args[0]),
Run: func(cmd *cobra.Command, args []string) {
Run: func(cmd *cobra.Command, _ []string) {
_ = cmd.Help()
},
}
Expand Down
14 changes: 13 additions & 1 deletion cmd/dt/unwrap/unwrap.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ type Config struct {
FetchArtifacts bool
Auth Auth
ContainerRegistryAuth Auth
ValuesFiles []string

// Interactive enables interacting with the user
Interactive bool
Expand Down Expand Up @@ -196,6 +197,13 @@ func WithTempDirectory(tempDir string) func(c *Config) {
}
}

// WithValuesFiles configures the values files of the wrapped chart
func WithValuesFiles(files ...string) func(c *Config) {
return func(c *Config) {
c.ValuesFiles = files
}
}

// NewConfig returns a new WrapConfig with default values
func NewConfig(opts ...Option) *Config {
cfg := &Config{
Expand All @@ -204,6 +212,7 @@ func NewConfig(opts ...Option) *Config {
logger: logrus.NewSectionLogger(),
AnnotationsKey: imagelock.DefaultAnnotationsKey,
Platforms: []string{},
ValuesFiles: []string{"values.yaml"},
}

for _, opt := range opts {
Expand Down Expand Up @@ -269,7 +278,7 @@ func unwrapChart(inputChart, registryURL, pushChartURL string, opts ...Option) (
if err := l.ExecuteStep(fmt.Sprintf("Relocating %q with prefix %q", wrap.ChartDir(), registryURL), func() error {
return relocator.RelocateChartDir(
wrap.ChartDir(), registryURL, relocator.WithLog(l),
relocator.Recursive, relocator.WithAnnotationsKey(cfg.AnnotationsKey),
relocator.Recursive, relocator.WithAnnotationsKey(cfg.AnnotationsKey), relocator.WithValuesFiles(cfg.ValuesFiles...),
)
}); err != nil {
return "", l.Failf("failed to relocate %q: %w", chartPath, err)
Expand Down Expand Up @@ -430,6 +439,7 @@ func NewCmd(cfg *config.Config) *cobra.Command {
pushChartURL string
version string
)
valuesFiles := []string{"values.yaml"}
cmd := &cobra.Command{
Use: "unwrap FILE OCI_URI",
Short: "Unwraps a wrapped Helm chart",
Expand Down Expand Up @@ -460,6 +470,7 @@ func NewCmd(cfg *config.Config) *cobra.Command {
WithInsecure(cfg.Insecure),
WithTempDirectory(tempDir),
WithUsePlainHTTP(cfg.UsePlainHTTP),
WithValuesFiles(valuesFiles...),
)
if err != nil {
return err
Expand All @@ -477,6 +488,7 @@ func NewCmd(cfg *config.Config) *cobra.Command {
cmd.PersistentFlags().StringVar(&version, "version", version, "when unwrapping remote Helm charts from OCI, version to request")
cmd.PersistentFlags().StringVar(&pushChartURL, "push-chart-url", pushChartURL, "push the unwrapped Helm chart to the given URL")
cmd.PersistentFlags().BoolVar(&sayYes, "yes", sayYes, "respond 'yes' to any yes/no question")
cmd.PersistentFlags().StringSliceVar(&valuesFiles, "values", valuesFiles, "values files to relocate images (can specify multiple)")

return cmd
}
2 changes: 1 addition & 1 deletion cmd/dt/verify/verify.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ func NewCmd(cfg *config.Config) *cobra.Command {
Args: cobra.ExactArgs(1),
SilenceUsage: true,
SilenceErrors: true,
RunE: func(cmd *cobra.Command, args []string) error {
RunE: func(_ *cobra.Command, args []string) error {
chartPath := args[0]

l := cfg.Logger()
Expand Down
2 changes: 1 addition & 1 deletion cmd/dt/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ var Commit = ""
var versionCmd = &cobra.Command{
Use: "version",
Short: "Prints the version",
Run: func(cmd *cobra.Command, args []string) {
Run: func(_ *cobra.Command, _ []string) {
msg := fmt.Sprintf("Distribution Tooling for Helm %s\n", Version)
if BuildDate != "" {
msg += fmt.Sprintf("Built on: %s\n", BuildDate)
Expand Down
2 changes: 1 addition & 1 deletion internal/testutil/testutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ func RenderScenario(origin string, destDir string, data map[string]interface{})
return fmt.Errorf("faled to list template partials")
}
for _, p := range matches {
err := filepath.Walk(p, func(path string, info os.FileInfo, err error) error {
err := filepath.Walk(p, func(path string, info os.FileInfo, _ error) error {
if strings.HasSuffix(path, partialExtension) {
return nil
}
Expand Down
2 changes: 1 addition & 1 deletion internal/widgets/spinner.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func init() {

// ExecuteWithSpinner runs the provided function while executing spinner
func ExecuteWithSpinner(spinner *Spinner, message string, fn func() error) error {
return putils.RunWithSpinner(spinner.WithRemoveWhenDone(true).WithText(message), func(spinner *pterm.SpinnerPrinter) error {
return putils.RunWithSpinner(spinner.WithRemoveWhenDone(true).WithText(message), func(_ *pterm.SpinnerPrinter) error {
return fn()
})
}
Expand Down
20 changes: 15 additions & 5 deletions pkg/chartutils/chart.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ type Chart struct {
chart *chart.Chart
rootDir string
annotationsKey string
valuesFiles []string
}

// ChartFullPath returns the wrapped chart ChartFullPath
Expand Down Expand Up @@ -112,9 +113,13 @@ func (c *Chart) File(name string) *chart.File {
return getChartFile(c.chart, name)
}

// ValuesFile returns the values.yaml chart.File
func (c *Chart) ValuesFile() *chart.File {
return c.File("values.yaml")
// ValuesFiles returns all the values chart.File
func (c *Chart) ValuesFiles() []*chart.File {
files := make([]*chart.File, 0, len(c.valuesFiles))
for _, valuesFile := range c.valuesFiles {
files = append(files, c.File(valuesFile))
}
return files
}

// AbsFilePath returns the absolute path to the Chart relative file name
Expand All @@ -134,7 +139,7 @@ func (c *Chart) GetAnnotatedImages() (imagelock.ImageList, error) {

// Dependencies returns the chart dependencies
func (c *Chart) Dependencies() []*Chart {
cfg := NewConfiguration(WithAnnotationsKey(c.annotationsKey))
cfg := NewConfiguration(WithAnnotationsKey(c.annotationsKey), WithValuesFiles(c.valuesFiles...))
deps := make([]*Chart, 0)

for _, dep := range c.chart.Dependencies() {
Expand All @@ -160,5 +165,10 @@ func LoadChart(path string, opts ...Option) (*Chart, error) {
}

func newChart(c *chart.Chart, chartRoot string, cfg *Configuration) *Chart {
return &Chart{chart: c, rootDir: chartRoot, annotationsKey: cfg.AnnotationsKey}
return &Chart{
chart: c,
rootDir: chartRoot,
annotationsKey: cfg.AnnotationsKey,
valuesFiles: cfg.ValuesFiles,
}
}
5 changes: 3 additions & 2 deletions pkg/chartutils/chart_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,10 @@ func (suite *ChartUtilsTestSuite) TestLoadChart() {
}
})
t.Run("ValuesFile", func(t *testing.T) {
f := chart.ValuesFile()
f := chart.ValuesFiles()
require.NotNil(t, f)
assert.Equal(t, f.Name, "values.yaml")
assert.Equal(t, len(f), 1)
assert.Equal(t, f[0].Name, "values.yaml")
})
t.Run("Dependencies", func(t *testing.T) {
dependencies := chart.Dependencies()
Expand Down
4 changes: 2 additions & 2 deletions pkg/chartutils/images_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ func (suite *ChartUtilsTestSuite) TestPullImages() {

scenarioDir := fmt.Sprintf("../../testdata/scenarios/%s", scenarioName)

t.Run("Pulls images", func(t *testing.T) {
t.Run("Pulls images", func(_ *testing.T) {
chartDir := sb.TempFile()

require.NoError(tu.RenderScenario(scenarioDir, chartDir,
Expand All @@ -67,7 +67,7 @@ func (suite *ChartUtilsTestSuite) TestPullImages() {
}
})

t.Run("Error when no images in Images.lock", func(t *testing.T) {
t.Run("Error when no images in Images.lock", func(_ *testing.T) {
chartDir := sb.TempFile()

images := []tu.ImageData{}
Expand Down
9 changes: 9 additions & 0 deletions pkg/chartutils/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ type Configuration struct {
MaxRetries int
InsecureMode bool
Auth Auth
ValuesFiles []string
}

// WithInsecureMode configures Insecure transport
Expand Down Expand Up @@ -91,6 +92,7 @@ func NewConfiguration(opts ...Option) *Configuration {
MaxRetries: 3,
Log: silent.NewLogger(),
InsecureMode: false,
ValuesFiles: []string{"values.yaml"},
}
for _, opt := range opts {
opt(cfg)
Expand All @@ -115,3 +117,10 @@ func WithAnnotationsKey(str string) func(cfg *Configuration) {
cfg.AnnotationsKey = str
}
}

// WithValuesFiles customizes the values files in the chart
func WithValuesFiles(files ...string) func(cfg *Configuration) {
return func(cfg *Configuration) {
cfg.ValuesFiles = files
}
}
Loading

0 comments on commit e4f8260

Please sign in to comment.