Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

feat: Add single create bundle command #728

Merged
merged 1 commit into from
May 23, 2024
Merged
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
482 changes: 482 additions & 0 deletions cmd/mindthegap/create/bundle/bundle.go

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions cmd/mindthegap/create/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (

"github.com/mesosphere/dkp-cli-runtime/core/output"

"github.com/mesosphere/mindthegap/cmd/mindthegap/create/bundle"
"github.com/mesosphere/mindthegap/cmd/mindthegap/create/helmbundle"
"github.com/mesosphere/mindthegap/cmd/mindthegap/create/imagebundle"
)
Expand All @@ -20,5 +21,6 @@ func NewCommand(out output.Output) *cobra.Command {

cmd.AddCommand(imagebundle.NewCommand(out))
cmd.AddCommand(helmbundle.NewCommand(out))
cmd.AddCommand(bundle.NewCommand(out))
return cmd
}
228 changes: 11 additions & 217 deletions cmd/mindthegap/create/helmbundle/helm_bundle.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,15 @@
package helmbundle

import (
"errors"
"fmt"
"os"
"path/filepath"
"sort"
"strconv"

"github.com/spf13/cobra"
"helm.sh/helm/v3/pkg/action"
"k8s.io/utils/ptr"

"github.com/mesosphere/dkp-cli-runtime/core/output"

"github.com/mesosphere/mindthegap/archive"
"github.com/mesosphere/mindthegap/cleanup"
"github.com/mesosphere/mindthegap/cmd/mindthegap/create/bundle"
"github.com/mesosphere/mindthegap/cmd/mindthegap/flags"
"github.com/mesosphere/mindthegap/cmd/mindthegap/utils"
"github.com/mesosphere/mindthegap/config"
"github.com/mesosphere/mindthegap/docker/registry"
"github.com/mesosphere/mindthegap/helm"
)

func NewCommand(out output.Output) *cobra.Command {
Expand All @@ -47,211 +37,13 @@ func NewCommand(out output.Output) *cobra.Command {
return nil
},
RunE: func(cmd *cobra.Command, args []string) error {
if !overwrite {
out.StartOperation("Checking if output file already exists")
_, err := os.Stat(outputFile)
switch {
case err == nil:
out.EndOperationWithStatus(output.Failure())
return fmt.Errorf(
"%s already exists: specify --overwrite to overwrite existing file",
outputFile,
)
case !errors.Is(err, os.ErrNotExist):
out.EndOperationWithStatus(output.Failure())
return fmt.Errorf(
"failed to check if output file %s already exists: %w",
outputFile,
err,
)
default:
out.EndOperationWithStatus(output.Success())
}
}

out.StartOperation("Parsing Helm chart bundle config")
cfg, err := config.ParseHelmChartsConfigFile(configFile)
if err != nil {
out.EndOperationWithStatus(output.Failure())
return err
}
out.EndOperationWithStatus(output.Success())
out.V(4).Infof("Helm charts config: %+v", cfg)

configFileAbs, err := filepath.Abs(configFile)
if err != nil {
return err
}

out.StartOperation("Creating temporary OCI registry directory")
outputFileAbs, err := filepath.Abs(outputFile)
if err != nil {
out.EndOperationWithStatus(output.Failure())
return fmt.Errorf(
"failed to determine where to create temporary directory: %w",
err,
)
}

cleaner := cleanup.NewCleaner()
defer cleaner.Cleanup()

tempRegistryDir, err := os.MkdirTemp(filepath.Dir(outputFileAbs), ".helm-bundle-*")
if err != nil {
out.EndOperationWithStatus(output.Failure())
return fmt.Errorf("failed to create temporary directory for OCI registry: %w", err)
}
cleaner.AddCleanupFn(func() { _ = os.RemoveAll(tempRegistryDir) })
out.EndOperationWithStatus(output.Success())

out.StartOperation("Starting temporary OCI registry")
reg, err := registry.NewRegistry(registry.Config{StorageDirectory: tempRegistryDir})
if err != nil {
out.EndOperationWithStatus(output.Failure())
return fmt.Errorf("failed to create local OCI registry: %w", err)
}
go func() {
if err := reg.ListenAndServe(); err != nil {
out.Error(err, "error serving OCI registry")
os.Exit(2)
}
}()
out.EndOperationWithStatus(output.Success())

out.StartOperation("Creating temporary chart storage directory")

tempHelmChartStorageDir, err := os.MkdirTemp("", ".helm-bundle-temp-storage-*")
if err != nil {
out.EndOperationWithStatus(output.Failure())
return fmt.Errorf(
"failed to create temporary directory for Helm chart storage: %w",
err,
)
}
cleaner.AddCleanupFn(func() { _ = os.RemoveAll(tempHelmChartStorageDir) })
out.EndOperationWithStatus(output.Success())

helmClient, helmCleanup := helm.NewClient(out)
cleaner.AddCleanupFn(func() { _ = helmCleanup() })

ociAddress := fmt.Sprintf("%s://%s/charts", helm.OCIScheme, reg.Address())

for repoName, repoConfig := range cfg.Repositories {
for chartName, chartVersions := range repoConfig.Charts {
sort.Strings(chartVersions)

out.StartOperation(
fmt.Sprintf(
"Fetching Helm chart %s (versions %v) from %s (%s)",
chartName,
chartVersions,
repoName,
repoConfig.RepoURL,
),
)
var opts []action.PullOpt
if repoConfig.Username != "" {
opts = append(
opts,
helm.UsernamePasswordOpt(repoConfig.Username, repoConfig.Password),
)
}
if !ptr.Deref(repoConfig.TLSVerify, true) {
opts = append(opts, helm.InsecureSkipTLSverifyOpt())
}
for _, chartVersion := range chartVersions {
downloaded, err := helmClient.GetChartFromRepo(
tempHelmChartStorageDir,
repoConfig.RepoURL,
chartName,
chartVersion,
[]helm.ConfigOpt{helm.RegistryClientConfigOpt()},
opts...,
)
if err != nil {
out.EndOperationWithStatus(output.Failure())
return fmt.Errorf("failed to create Helm chart bundle: %w", err)
}

if err := helmClient.PushHelmChartToOCIRegistry(
downloaded, ociAddress,
); err != nil {
out.EndOperationWithStatus(output.Failure())
return fmt.Errorf(
"failed to push Helm chart to temporary registry: %w",
err,
)
}

// Best effort cleanup of downloaded chart, will be cleaned up when the cleaner deletes the temporary
// directory anyway.
_ = os.Remove(downloaded)
}
out.EndOperationWithStatus(output.Success())
}
}
for _, chartURL := range cfg.ChartURLs {
out.StartOperation(fmt.Sprintf("Fetching Helm chart from URL %s", chartURL))
downloaded, err := helmClient.GetChartFromURL(
tempRegistryDir,
chartURL,
filepath.Dir(configFileAbs),
)
if err != nil {
out.EndOperationWithStatus(output.Failure())
return fmt.Errorf("failed to create Helm chart bundle: %w", err)
}

chrt, err := helm.LoadChart(downloaded)
if err != nil {
out.EndOperationWithStatus(output.Failure())
return fmt.Errorf(
"failed to extract Helm chart details from local chart: %w",
err,
)
}

_, ok := cfg.Repositories["local"]
if !ok {
cfg.Repositories["local"] = config.HelmRepositorySyncConfig{
Charts: make(map[string][]string, 1),
}
}
_, ok = cfg.Repositories["local"].Charts[chrt.Name()]
if !ok {
cfg.Repositories["local"].Charts[chrt.Name()] = make([]string, 0, 1)
}
cfg.Repositories["local"].Charts[chrt.Name()] = append(
cfg.Repositories["local"].Charts[chrt.Name()],
chrt.Metadata.Version,
)

if err := helmClient.PushHelmChartToOCIRegistry(
downloaded, ociAddress,
); err != nil {
out.EndOperationWithStatus(output.Failure())
return fmt.Errorf("failed to push Helm chart to temporary registry: %w", err)
}

// Best effort cleanup of downloaded chart, will be cleaned up when the cleaner deletes the temporary
// directory anyway.
_ = os.Remove(downloaded)

out.EndOperationWithStatus(output.Success())
}

if err := config.WriteSanitizedHelmChartsConfig(cfg, filepath.Join(tempRegistryDir, "charts.yaml")); err != nil {
return err
}

out.StartOperation(fmt.Sprintf("Archiving Helm charts to %s", outputFile))
if err := archive.ArchiveDirectory(tempRegistryDir, outputFile); err != nil {
out.EndOperationWithStatus(output.Failure())
return fmt.Errorf("failed to create Helm charts bundle tarball: %w", err)
}
out.EndOperationWithStatus(output.Success())

return nil
createBundleCmd := bundle.NewCommand(out)
createBundleCmd.SetArgs([]string{
"--helm-charts-file", configFile,
"--output-file", outputFile,
"--overwrite", strconv.FormatBool(overwrite),
})
return createBundleCmd.Execute()
},
}

Expand All @@ -266,5 +58,7 @@ func NewCommand(out output.Output) *cobra.Command {
// TODO Unhide this from DKP CLI once DKP supports OCI registry for Helm charts.
utils.AddCmdAnnotation(cmd, "exclude-from-dkp-cli", "true")

cmd.Deprecated = `"mindthegap create helm-bundle" is deprecated, please use "mindthegap create bundle" instead`

return cmd
}
Loading
Loading