Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

wip: add images from helm chart template #380

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,4 @@ fileserver/
cmd/hauler/binaries
testdata/certs/
coverage.out
ebug
2 changes: 1 addition & 1 deletion cmd/hauler/cli/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -367,7 +367,7 @@ hauler store add chart rancher --repo https://releases.rancher.com/server-charts
return err
}

return store.AddChartCmd(ctx, o, s, args[0])
return store.AddChartCmd(ctx, o, s, args[0], rso, ro)
},
}
o.AddFlags(cmd)
Expand Down
74 changes: 57 additions & 17 deletions cmd/hauler/cli/store/add.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,20 @@
import (
"context"
"os"
"slices"
"strings"

"github.com/google/go-containerregistry/pkg/name"
"helm.sh/helm/v3/pkg/action"
"hauler.dev/go/hauler/pkg/artifacts/file/getter"

Check failure on line 10 in cmd/hauler/cli/store/add.go

View workflow job for this annotation

GitHub Actions / Unit Tests

other declaration of getter
"hauler.dev/go/hauler/pkg/consts"

Check failure on line 11 in cmd/hauler/cli/store/add.go

View workflow job for this annotation

GitHub Actions / Unit Tests

other declaration of consts
"helm.sh/helm/v3/pkg/chartutil"
"helm.sh/helm/v3/pkg/engine"

"hauler.dev/go/hauler/internal/flags"
"hauler.dev/go/hauler/pkg/apis/hauler.cattle.io/v1alpha1"
"hauler.dev/go/hauler/pkg/artifacts/file"
"hauler.dev/go/hauler/pkg/artifacts/file/getter"

Check failure on line 18 in cmd/hauler/cli/store/add.go

View workflow job for this annotation

GitHub Actions / Unit Tests

getter redeclared in this block

Check failure on line 18 in cmd/hauler/cli/store/add.go

View workflow job for this annotation

GitHub Actions / Unit Tests

"hauler.dev/go/hauler/pkg/artifacts/file/getter" imported and not used
"hauler.dev/go/hauler/pkg/consts"

Check failure on line 19 in cmd/hauler/cli/store/add.go

View workflow job for this annotation

GitHub Actions / Unit Tests

consts redeclared in this block

Check failure on line 19 in cmd/hauler/cli/store/add.go

View workflow job for this annotation

GitHub Actions / Unit Tests

"hauler.dev/go/hauler/pkg/consts" imported and not used
"hauler.dev/go/hauler/pkg/content/chart"
"hauler.dev/go/hauler/pkg/cosign"
"hauler.dev/go/hauler/pkg/log"
Expand Down Expand Up @@ -111,27 +116,16 @@
return nil
}

func AddChartCmd(ctx context.Context, o *flags.AddChartOpts, s *store.Layout, chartName string) error {
// TODO: Reduce duplicates between api chart and upstream helm opts
cfg := v1alpha1.Chart{
Name: chartName,
RepoURL: o.ChartOpts.RepoURL,
Version: o.ChartOpts.Version,
}

return storeChart(ctx, s, cfg, o.ChartOpts)
func AddChartCmd(ctx context.Context, o *flags.AddChartOpts, s *store.Layout, chartName string, rso *flags.StoreRootOpts, ro *flags.CliRootOpts) error {
return storeChart(ctx, s, chartName, o, rso, ro)
}

func storeChart(ctx context.Context, s *store.Layout, cfg v1alpha1.Chart, opts *action.ChartPathOptions) error {
func storeChart(ctx context.Context, s *store.Layout, chartName string, opts *flags.AddChartOpts, rso *flags.StoreRootOpts, ro *flags.CliRootOpts) error {
l := log.FromContext(ctx)

l.Infof("adding 'chart' [%s] to the store", cfg.Name)

// TODO: This shouldn't be necessary
opts.RepoURL = cfg.RepoURL
opts.Version = cfg.Version
l.Infof("adding 'chart' [%s] to the store", chartName)

chrt, err := chart.NewChart(cfg.Name, opts)
chrt, err := chart.NewChart(chartName, opts.ChartOpts)
if err != nil {
return err
}
Expand All @@ -151,5 +145,51 @@
}

l.Infof("successfully added 'chart' [%s]", ref.Name())

if opts.AddImages {
if opts.HelmValues != "" {
values, err := chartutil.ReadValuesFile(opts.HelmValues)
if err != nil {
return err
}
c.Values = values
}

kubeVersion := chartutil.KubeVersion{Version: "1", Major: "31", Minor: "3"}
l.Debugf("setting kubernetes version... [v%s.%s.%s]", kubeVersion.Version, kubeVersion.Major, kubeVersion.Minor)

values, err := chartutil.ToRenderValues(c, c.Values, chartutil.ReleaseOptions{Namespace: "hauler"}, &chartutil.Capabilities{KubeVersion: kubeVersion})
if err != nil {
return err
}

template, err := engine.Render(c, values)
if err != nil {
return err
}

images := []string{}

for _, manifest := range template {
m := strings.Split(manifest, "\n")
for _, l := range m {
l := strings.ReplaceAll(l, " ", "")
l = strings.ReplaceAll(l, "\"", "")
if strings.HasPrefix(l, "image:") {
images = append(images, l[6:])
}
}
}

slices.Sort(images)
images = slices.Compact(images)

l.Infof("successfully found images... %v", images)

for _, image := range images {
storeImage(ctx, s, v1alpha1.Image{Name: image, Platform: opts.Platform}, "", rso, ro)
}
}

return nil
}
3 changes: 2 additions & 1 deletion cmd/hauler/cli/store/sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,8 @@ func processContent(ctx context.Context, fi *os.File, o *flags.SyncOpts, s *stor

for _, ch := range cfg.Spec.Charts {
// TODO: Provide a way to configure syncs
err := storeChart(ctx, s, ch, &action.ChartPathOptions{})

err := storeChart(ctx, s, ch.Name, &flags.AddChartOpts{ChartOpts: &action.ChartPathOptions{RepoURL: ch.RepoURL, Version: ch.Version}}, rso, ro)
if err != nil {
return err
}
Expand Down
8 changes: 8 additions & 0 deletions internal/flags/add.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ type AddChartOpts struct {
*StoreRootOpts

ChartOpts *action.ChartPathOptions

AddImages bool
HelmValues string
Platform string
}

func (o *AddChartOpts) AddFlags(cmd *cobra.Command) {
Expand All @@ -46,4 +50,8 @@ func (o *AddChartOpts) AddFlags(cmd *cobra.Command) {
f.StringVar(&o.ChartOpts.KeyFile, "key-file", "", "(Optional) Location of the TLS Key to use for client authenication")
f.BoolVar(&o.ChartOpts.InsecureSkipTLSverify, "insecure-skip-tls-verify", false, "(Optional) Skip TLS certificate verification")
f.StringVar(&o.ChartOpts.CaFile, "ca-file", "", "(Optional) Location of CA Bundle to enable certification verification")

f.BoolVar(&o.AddImages, "add-images", false, "(Optional) Fetch images referenced in a helm chart (tech preview)")
f.StringVar(&o.HelmValues, "values", "", "(Optional) Specify helm chart values when fetching images (tech preview)")
f.StringVarP(&o.Platform, "platform", "p", "", "(Optional) Specifiy the platform of the image... i.e. linux/amd64 (defaults to all)")
}
Loading