Skip to content

Commit

Permalink
Refactor lock.Create
Browse files Browse the repository at this point in the history
  • Loading branch information
juamedgod committed Jan 23, 2024
1 parent a5ee138 commit 9a700f6
Show file tree
Hide file tree
Showing 5 changed files with 7 additions and 41 deletions.
3 changes: 2 additions & 1 deletion cmd/dt/carvelize/carvelize.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (

"github.com/spf13/cobra"
"github.com/vmware-labs/distribution-tooling-for-helm/cmd/dt/config"
"github.com/vmware-labs/distribution-tooling-for-helm/cmd/dt/lock"
"github.com/vmware-labs/distribution-tooling-for-helm/cmd/dt/verify"
"github.com/vmware-labs/distribution-tooling-for-helm/pkg/carvel"
"github.com/vmware-labs/distribution-tooling-for-helm/pkg/chartutils"
Expand Down Expand Up @@ -54,7 +55,7 @@ func NewCmd(cfg *config.Config) *cobra.Command {
err := l.ExecuteStep(
"Images.lock file does not exist. Generating it from annotations...",
func() error {
return chartutils.CreateImagesLock(chartPath,
return lock.Create(chartPath,
lockFile, silentLog, imagelock.WithAnnotationsKey(cfg.AnnotationsKey), imagelock.WithInsecure(cfg.Insecure),
)
},
Expand Down
5 changes: 3 additions & 2 deletions cmd/dt/lock/lock.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ func NewCmd(cfg *config.Config) *cobra.Command {
return fmt.Errorf("failed to obtain Images.lock location: %w", err)
}
if err := l.ExecuteStep("Generating Images.lock from annotations...", func() error {
return createImagesLock(chartPath, outputFile, log.SilentLog, imagelock.WithPlatforms(platforms),
return Create(chartPath, outputFile, log.SilentLog, imagelock.WithPlatforms(platforms),
imagelock.WithAnnotationsKey(cfg.AnnotationsKey),
imagelock.WithInsecure(cfg.Insecure))
}); err != nil {
Expand All @@ -62,7 +62,8 @@ func NewCmd(cfg *config.Config) *cobra.Command {
return cmd
}

func createImagesLock(chartPath string, outputFile string, l log.Logger, opts ...imagelock.Option) error {
// Create generates an Images.lock file from the chart annotations
func Create(chartPath string, outputFile string, l log.Logger, opts ...imagelock.Option) error {
l.Infof("Generating images lock for Helm chart %q", chartPath)

lock, err := imagelock.GenerateFromChart(chartPath, opts...)
Expand Down
3 changes: 2 additions & 1 deletion cmd/dt/wrap/wrap.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/spf13/cobra"
"github.com/vmware-labs/distribution-tooling-for-helm/cmd/dt/carvelize"
"github.com/vmware-labs/distribution-tooling-for-helm/cmd/dt/config"
"github.com/vmware-labs/distribution-tooling-for-helm/cmd/dt/lock"
"github.com/vmware-labs/distribution-tooling-for-helm/cmd/dt/pull"
"github.com/vmware-labs/distribution-tooling-for-helm/internal/widgets"
"github.com/vmware-labs/distribution-tooling-for-helm/pkg/artifacts"
Expand Down Expand Up @@ -252,7 +253,7 @@ func validateWrapLock(wrap wrapping.Wrap, cfg *Config) error {
if err := l.ExecuteStep(
"Images.lock file does not exist. Generating it from annotations...",
func() error {
return chart.CreateImagesLock(log.SilentLog,
return lock.Create(chart.RootDir(), lockFile, log.SilentLog,
imagelock.WithAnnotationsKey(cfg.AnnotationsKey),
imagelock.WithInsecure(cfg.Insecure),
imagelock.WithPlatforms(cfg.Platforms),
Expand Down
8 changes: 0 additions & 8 deletions pkg/chartutils/chart.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (

"github.com/vmware-labs/distribution-tooling-for-helm/pkg/artifacts"
"github.com/vmware-labs/distribution-tooling-for-helm/pkg/imagelock"
dlog "github.com/vmware-labs/distribution-tooling-for-helm/pkg/log"
"github.com/vmware-labs/distribution-tooling-for-helm/pkg/utils"

"helm.sh/helm/v3/pkg/chart"
Expand All @@ -26,13 +25,6 @@ func (c *Chart) ChartFullPath() string {
return c.chart.ChartFullPath()
}

// CreateImagesLock creates the Images.lock file for the chart
func (c *Chart) CreateImagesLock(l dlog.Logger, opts ...imagelock.Option) error {
chartPath := c.ChartDir()
outputFile := c.LockFilePath()
return CreateImagesLock(chartPath, outputFile, l, opts...)
}

// Name returns the name of the chart
func (c *Chart) Name() string {
return c.chart.Name()
Expand Down
29 changes: 0 additions & 29 deletions pkg/chartutils/chartutils.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package chartutils

import (
"archive/tar"
"bytes"
"context"
"errors"
"fmt"
Expand All @@ -15,7 +14,6 @@ import (
"github.com/vmware-labs/distribution-tooling-for-helm/pkg/imagelock"
"github.com/vmware-labs/distribution-tooling-for-helm/pkg/utils"

dlog "github.com/vmware-labs/distribution-tooling-for-helm/pkg/log"
"gopkg.in/yaml.v3"

"helm.sh/helm/v3/pkg/chart"
Expand Down Expand Up @@ -185,30 +183,3 @@ func ReadLockFromChart(chartPath string) (*imagelock.ImagesLock, error) {
}
return imagelock.FromYAMLFile(f)
}

// CreateImagesLock generates an Images.lock file from the chart annotations
func CreateImagesLock(chartPath string, outputFile string, l dlog.Logger, opts ...imagelock.Option) error {
l.Infof("Generating images lock for Helm chart %q", chartPath)

lock, err := imagelock.GenerateFromChart(chartPath, opts...)

if err != nil {
return fmt.Errorf("failed to load Helm chart: %v", err)
}

if len(lock.Images) == 0 {
l.Warnf("Did not find any image annotations at Helm chart %q", chartPath)
}

buff := &bytes.Buffer{}
if err = lock.ToYAML(buff); err != nil {
return fmt.Errorf("failed to write Images.lock file: %v", err)
}

if err := os.WriteFile(outputFile, buff.Bytes(), 0666); err != nil {
return fmt.Errorf("failed to write lock to %q: %w", outputFile, err)
}

l.Infof("Images.lock file written to %q", outputFile)
return nil
}

0 comments on commit 9a700f6

Please sign in to comment.