Skip to content

Commit

Permalink
fix(helm): fix "missing registry client" error in werf-helm-* commands
Browse files Browse the repository at this point in the history
Signed-off-by: Timofey Kirillov <timofey.kirillov@flant.com>
  • Loading branch information
distorhead committed Sep 22, 2022
1 parent 787d5b3 commit 414dd38
Show file tree
Hide file tree
Showing 14 changed files with 100 additions and 58 deletions.
7 changes: 4 additions & 3 deletions cmd/werf/bundle/apply/apply.go
Expand Up @@ -139,7 +139,7 @@ func runApply(ctx context.Context) error {

helm_v3.Settings.Debug = *commonCmdData.LogDebug

helmRegistryClientHandle, err := common.NewHelmRegistryClientHandle(ctx, &commonCmdData)
helmRegistryClient, err := common.NewHelmRegistryClient(ctx, *commonCmdData.DockerConfig, *commonCmdData.InsecureHelmDependencies)
if err != nil {
return fmt.Errorf("unable to create helm registry client: %w", err)
}
Expand All @@ -150,7 +150,7 @@ func runApply(ctx context.Context) error {
}

actionConfig := new(action.Configuration)
if err := helm.InitActionConfig(ctx, common.GetOndemandKubeInitializer(), *commonCmdData.Namespace, helm_v3.Settings, helmRegistryClientHandle, actionConfig, helm.InitActionConfigOptions{
if err := helm.InitActionConfig(ctx, common.GetOndemandKubeInitializer(), *commonCmdData.Namespace, helm_v3.Settings, actionConfig, helm.InitActionConfigOptions{
StatusProgressPeriod: time.Duration(*commonCmdData.StatusProgressPeriodSeconds) * time.Second,
HooksStatusProgressPeriod: time.Duration(*commonCmdData.HooksStatusProgressPeriodSeconds) * time.Second,
KubeConfigOptions: kube.KubeConfigOptions{
Expand All @@ -160,6 +160,7 @@ func runApply(ctx context.Context) error {
ConfigPathMergeList: *commonCmdData.KubeConfigPathMergeList,
},
ReleasesHistoryMax: *commonCmdData.ReleasesHistoryMax,
RegistryClient: helmRegistryClient,
}); err != nil {
return err
}
Expand Down Expand Up @@ -190,7 +191,7 @@ func runApply(ctx context.Context) error {

secretsManager := secrets_manager.NewSecretsManager(secrets_manager.SecretsManagerOptions{DisableSecretsDecryption: *commonCmdData.IgnoreSecretKey})

bundle, err := chart_extender.NewBundle(ctx, bundleTmpDir, helm_v3.Settings, helmRegistryClientHandle, secretsManager, chart_extender.BundleOptions{
bundle, err := chart_extender.NewBundle(ctx, bundleTmpDir, helm_v3.Settings, helmRegistryClient, secretsManager, chart_extender.BundleOptions{
SecretValueFiles: common.GetSecretValues(&commonCmdData),
BuildChartDependenciesOpts: command_helpers.BuildChartDependenciesOptions{IgnoreInvalidAnnotationsAndLabels: true},
IgnoreInvalidAnnotationsAndLabels: true,
Expand Down
4 changes: 2 additions & 2 deletions cmd/werf/bundle/export/export.go
Expand Up @@ -312,12 +312,12 @@ func runExport(ctx context.Context, imagesToProcess build.ImagesToProcess) error
logboek.LogOptionalLn()
}

helmRegistryClientHandle, err := common.NewHelmRegistryClientHandle(ctx, &commonCmdData)
helmRegistryClient, err := common.NewHelmRegistryClient(ctx, *commonCmdData.DockerConfig, *commonCmdData.InsecureHelmDependencies)
if err != nil {
return fmt.Errorf("unable to create helm registry client: %w", err)
}

wc := chart_extender.NewWerfChart(ctx, giterminismManager, nil, chartDir, helm_v3.Settings, helmRegistryClientHandle, chart_extender.WerfChartOptions{
wc := chart_extender.NewWerfChart(ctx, giterminismManager, nil, chartDir, helm_v3.Settings, helmRegistryClient, chart_extender.WerfChartOptions{
ExtraAnnotations: userExtraAnnotations,
ExtraLabels: userExtraLabels,
IgnoreInvalidAnnotationsAndLabels: true,
Expand Down
4 changes: 2 additions & 2 deletions cmd/werf/bundle/publish/publish.go
Expand Up @@ -317,7 +317,7 @@ func runPublish(ctx context.Context, imagesToProcess build.ImagesToProcess) erro
logboek.LogOptionalLn()
}

helmRegistryClientHandle, err := common.NewHelmRegistryClientHandle(ctx, &commonCmdData)
helmRegistryClient, err := common.NewHelmRegistryClient(ctx, *commonCmdData.DockerConfig, *commonCmdData.InsecureHelmDependencies)
if err != nil {
return err
}
Expand All @@ -327,7 +327,7 @@ func runPublish(ctx context.Context, imagesToProcess build.ImagesToProcess) erro
return err
}

wc := chart_extender.NewWerfChart(ctx, giterminismManager, nil, chartDir, helm_v3.Settings, helmRegistryClientHandle, chart_extender.WerfChartOptions{
wc := chart_extender.NewWerfChart(ctx, giterminismManager, nil, chartDir, helm_v3.Settings, helmRegistryClient, chart_extender.WerfChartOptions{
ExtraAnnotations: userExtraAnnotations,
ExtraLabels: userExtraLabels,
IgnoreInvalidAnnotationsAndLabels: true,
Expand Down
6 changes: 3 additions & 3 deletions cmd/werf/bundle/render/render.go
Expand Up @@ -144,13 +144,13 @@ func runRender(ctx context.Context) error {

helm_v3.Settings.Debug = *commonCmdData.LogDebug

helmRegistryClientHandle, err := common.NewHelmRegistryClientHandle(ctx, &commonCmdData)
helmRegistryClient, err := common.NewHelmRegistryClient(ctx, *commonCmdData.DockerConfig, *commonCmdData.InsecureHelmDependencies)
if err != nil {
return fmt.Errorf("unable to create helm registry client: %w", err)
}

actionConfig := new(action.Configuration)
if err := helm.InitActionConfig(ctx, nil, *commonCmdData.Namespace, helm_v3.Settings, helmRegistryClientHandle, actionConfig, helm.InitActionConfigOptions{}); err != nil {
if err := helm.InitActionConfig(ctx, nil, *commonCmdData.Namespace, helm_v3.Settings, actionConfig, helm.InitActionConfigOptions{RegistryClient: helmRegistryClient}); err != nil {
return err
}

Expand Down Expand Up @@ -189,7 +189,7 @@ func runRender(ctx context.Context) error {

secretsManager := secrets_manager.NewSecretsManager(secrets_manager.SecretsManagerOptions{DisableSecretsDecryption: *commonCmdData.IgnoreSecretKey})

bundle, err := chart_extender.NewBundle(ctx, bundleDir, helm_v3.Settings, helmRegistryClientHandle, secretsManager, chart_extender.BundleOptions{
bundle, err := chart_extender.NewBundle(ctx, bundleDir, helm_v3.Settings, helmRegistryClient, secretsManager, chart_extender.BundleOptions{
SecretValueFiles: common.GetSecretValues(&commonCmdData),
BuildChartDependenciesOpts: command_helpers.BuildChartDependenciesOptions{IgnoreInvalidAnnotationsAndLabels: false},
IgnoreInvalidAnnotationsAndLabels: false,
Expand Down
37 changes: 21 additions & 16 deletions cmd/werf/common/helm.go
Expand Up @@ -2,11 +2,8 @@ package common

import (
"context"
"path/filepath"
"time"

"github.com/docker/cli/cli/config"
"github.com/docker/docker/pkg/homedir"
helm_v3 "helm.sh/helm/v3/cmd/helm"
"helm.sh/helm/v3/pkg/action"
"helm.sh/helm/v3/pkg/registry"
Expand All @@ -15,45 +12,52 @@ import (
"github.com/werf/logboek"
bundles_registry "github.com/werf/werf/pkg/deploy/bundles/registry"
"github.com/werf/werf/pkg/deploy/helm"
"github.com/werf/werf/pkg/docker"
"github.com/werf/werf/pkg/util"
)

func NewHelmRegistryClientHandle(ctx context.Context, commonCmdData *CmdData) (*registry.Client, error) {
func NewHelmRegistryClient(ctx context.Context, dockerConfig string, insecureHelmDependencies bool) (*registry.Client, error) {
client, err := NewHelmRegistryClientWithoutInit(ctx)
if err != nil {
return nil, err
}

InitHelmRegistryClient(client, dockerConfig, insecureHelmDependencies)

return client, nil
}

func NewHelmRegistryClientWithoutInit(ctx context.Context) (*registry.Client, error) {
return registry.NewClient(
registry.ClientOptCredentialsFile(getDockerConfigCredentialsFile(*commonCmdData.DockerConfig)),
registry.ClientOptDebug(logboek.Context(ctx).Debug().IsAccepted()),
registry.ClientOptInsecure(*commonCmdData.InsecureHelmDependencies),
registry.ClientOptWriter(logboek.Context(ctx).OutStream()),
)
}

func InitHelmRegistryClient(registryClient *registry.Client, dockerConfig string, insecureHelmDependencies bool) {
registry.ClientOptCredentialsFile(docker.GetDockerConfigCredentialsFile(dockerConfig))(registryClient)
registry.ClientOptInsecure(insecureHelmDependencies)(registryClient)
}

func NewBundlesRegistryClient(ctx context.Context, commonCmdData *CmdData) (*bundles_registry.Client, error) {
debug := logboek.Context(ctx).Debug().IsAccepted()
insecure := util.GetBoolEnvironmentDefaultFalse("WERF_BUNDLE_INSECURE_REGISTRY")
skipTlsVerify := util.GetBoolEnvironmentDefaultFalse("WERF_BUNDLE_SKIP_TLS_VERIFY_REGISTRY")
out := logboek.Context(ctx).OutStream()

return bundles_registry.NewClient(
bundles_registry.ClientOptCredentialsFile(getDockerConfigCredentialsFile(*commonCmdData.DockerConfig)),
bundles_registry.ClientOptCredentialsFile(docker.GetDockerConfigCredentialsFile(*commonCmdData.DockerConfig)),
bundles_registry.ClientOptDebug(debug),
bundles_registry.ClientOptInsecure(insecure),
bundles_registry.ClientOptSkipTlsVerify(skipTlsVerify),
bundles_registry.ClientOptWriter(out),
)
}

func getDockerConfigCredentialsFile(configDir string) string {
if configDir == "" {
return filepath.Join(homedir.Get(), ".docker", config.ConfigFileName)
} else {
return filepath.Join(configDir, config.ConfigFileName)
}
}

func NewActionConfig(ctx context.Context, kubeInitializer helm.KubeInitializer, namespace string, commonCmdData *CmdData, registryClient *registry.Client) (*action.Configuration, error) {
actionConfig := new(action.Configuration)

if err := helm.InitActionConfig(ctx, kubeInitializer, namespace, helm_v3.Settings, registryClient, actionConfig, helm.InitActionConfigOptions{
if err := helm.InitActionConfig(ctx, kubeInitializer, namespace, helm_v3.Settings, actionConfig, helm.InitActionConfigOptions{
StatusProgressPeriod: time.Duration(*commonCmdData.StatusProgressPeriodSeconds) * time.Second,
HooksStatusProgressPeriod: time.Duration(*commonCmdData.HooksStatusProgressPeriodSeconds) * time.Second,
KubeConfigOptions: kube.KubeConfigOptions{
Expand All @@ -63,6 +67,7 @@ func NewActionConfig(ctx context.Context, kubeInitializer helm.KubeInitializer,
ConfigPathMergeList: *commonCmdData.KubeConfigPathMergeList,
},
ReleasesHistoryMax: *commonCmdData.ReleasesHistoryMax,
RegistryClient: registryClient,
}); err != nil {
return nil, err
}
Expand Down
10 changes: 5 additions & 5 deletions cmd/werf/converge/converge.go
Expand Up @@ -381,12 +381,12 @@ func run(ctx context.Context, containerBackend container_backend.ContainerBacken
lockManager = m
}

helmRegistryClientHandle, err := common.NewHelmRegistryClientHandle(ctx, &commonCmdData)
helmRegistryClient, err := common.NewHelmRegistryClient(ctx, *commonCmdData.DockerConfig, *commonCmdData.InsecureHelmDependencies)
if err != nil {
return fmt.Errorf("unable to create helm registry client: %w", err)
}

wc := chart_extender.NewWerfChart(ctx, giterminismManager, secretsManager, chartDir, helm_v3.Settings, helmRegistryClientHandle, chart_extender.WerfChartOptions{
wc := chart_extender.NewWerfChart(ctx, giterminismManager, secretsManager, chartDir, helm_v3.Settings, helmRegistryClient, chart_extender.WerfChartOptions{
SecretValueFiles: common.GetSecretValues(&commonCmdData),
ExtraAnnotations: userExtraAnnotations,
ExtraLabels: userExtraLabels,
Expand Down Expand Up @@ -435,17 +435,17 @@ func run(ctx context.Context, containerBackend container_backend.ContainerBacken
FileValues: common.GetSetFile(&commonCmdData),
}

actionConfig, err := common.NewActionConfig(ctx, common.GetOndemandKubeInitializer(), namespace, &commonCmdData, helmRegistryClientHandle)
actionConfig, err := common.NewActionConfig(ctx, common.GetOndemandKubeInitializer(), namespace, &commonCmdData, helmRegistryClient)
if err != nil {
return err
}
maintenanceHelper := createMaintenanceHelper(ctx, actionConfig, kubeConfigOptions)

if err := migrateHelm2ToHelm3(ctx, releaseName, namespace, maintenanceHelper, wc.ChainPostRenderer, valueOpts, filepath.Join(giterminismManager.ProjectDir(), chartDir), helmRegistryClientHandle); err != nil {
if err := migrateHelm2ToHelm3(ctx, releaseName, namespace, maintenanceHelper, wc.ChainPostRenderer, valueOpts, filepath.Join(giterminismManager.ProjectDir(), chartDir), helmRegistryClient); err != nil {
return err
}

actionConfig, err = common.NewActionConfig(ctx, common.GetOndemandKubeInitializer(), namespace, &commonCmdData, helmRegistryClientHandle)
actionConfig, err = common.NewActionConfig(ctx, common.GetOndemandKubeInitializer(), namespace, &commonCmdData, helmRegistryClient)
if err != nil {
return err
}
Expand Down
7 changes: 4 additions & 3 deletions cmd/werf/dismiss/dismiss.go
Expand Up @@ -204,12 +204,12 @@ func runDismiss(ctx context.Context) error {
return fmt.Errorf("getting helm chart dir failed: %w", err)
}

helmRegistryClientHandle, err := common.NewHelmRegistryClientHandle(ctx, &commonCmdData)
helmRegistryClient, err := common.NewHelmRegistryClient(ctx, *commonCmdData.DockerConfig, *commonCmdData.InsecureHelmDependencies)
if err != nil {
return fmt.Errorf("unable to create helm registry client: %w", err)
}

wc := chart_extender.NewWerfChart(ctx, giterminismManager, nil, chartDir, helm_v3.Settings, helmRegistryClientHandle, chart_extender.WerfChartOptions{
wc := chart_extender.NewWerfChart(ctx, giterminismManager, nil, chartDir, helm_v3.Settings, helmRegistryClient, chart_extender.WerfChartOptions{
IgnoreInvalidAnnotationsAndLabels: true,
})

Expand All @@ -221,7 +221,7 @@ func runDismiss(ctx context.Context) error {
}

actionConfig := new(action.Configuration)
if err := helm.InitActionConfig(ctx, common.GetOndemandKubeInitializer(), namespace, helm_v3.Settings, helmRegistryClientHandle, actionConfig, helm.InitActionConfigOptions{
if err := helm.InitActionConfig(ctx, common.GetOndemandKubeInitializer(), namespace, helm_v3.Settings, actionConfig, helm.InitActionConfigOptions{
StatusProgressPeriod: time.Duration(*commonCmdData.StatusProgressPeriodSeconds) * time.Second,
HooksStatusProgressPeriod: time.Duration(*commonCmdData.HooksStatusProgressPeriodSeconds) * time.Second,
KubeConfigOptions: kube.KubeConfigOptions{
Expand All @@ -230,6 +230,7 @@ func runDismiss(ctx context.Context) error {
ConfigDataBase64: *commonCmdData.KubeConfigBase64,
},
ReleasesHistoryMax: *commonCmdData.ReleasesHistoryMax,
RegistryClient: helmRegistryClient,
}); err != nil {
return err
}
Expand Down
23 changes: 13 additions & 10 deletions cmd/werf/helm/helm.go
Expand Up @@ -36,17 +36,23 @@ func IsHelm3Mode() bool {
return os.Getenv("WERF_HELM3_MODE") == "1"
}

func NewCmd(ctx context.Context) *cobra.Command {
func NewCmd(ctx context.Context) (*cobra.Command, error) {
var namespace string
actionConfig := new(action.Configuration)

ctx = common.NewContextWithCmdData(ctx, &_commonCmdData)
cmd := common.SetCommandContext(ctx, &cobra.Command{
Use: "helm",
Short: "Manage application deployment with helm",
SilenceUsage: true,
})

registryClient, err := common.NewHelmRegistryClientWithoutInit(ctx)
if err != nil {
return nil, fmt.Errorf("unable to create helm registry client: %w", err)
}

actionConfig := new(action.Configuration)
actionConfig.RegistryClient = registryClient

wc := chart_extender.NewWerfChartStub(ctx, false)

loader.GlobalLoadOptions = &loader.LoadOptions{
Expand Down Expand Up @@ -164,14 +170,11 @@ func NewCmd(ctx context.Context) *cobra.Command {
wc.SetStubServiceValues(vals)
}

common.SetupOndemandKubeInitializer(*_commonCmdData.KubeContext, *_commonCmdData.KubeConfig, *_commonCmdData.KubeConfigBase64, *_commonCmdData.KubeConfigPathMergeList)
common.InitHelmRegistryClient(registryClient, *_commonCmdData.DockerConfig, *_commonCmdData.InsecureHelmDependencies)

helmRegistryClientHandle, err := common.NewHelmRegistryClientHandle(ctx, &_commonCmdData)
if err != nil {
return fmt.Errorf("unable to create helm registry client: %w", err)
}
common.SetupOndemandKubeInitializer(*_commonCmdData.KubeContext, *_commonCmdData.KubeConfig, *_commonCmdData.KubeConfigBase64, *_commonCmdData.KubeConfigPathMergeList)

helm.InitActionConfig(ctx, common.GetOndemandKubeInitializer(), namespace, helm_v3.Settings, helmRegistryClientHandle, actionConfig, helm.InitActionConfigOptions{
helm.InitActionConfig(ctx, common.GetOndemandKubeInitializer(), namespace, helm_v3.Settings, actionConfig, helm.InitActionConfigOptions{
StatusProgressPeriod: time.Duration(*_commonCmdData.StatusProgressPeriodSeconds) * time.Second,
HooksStatusProgressPeriod: time.Duration(*_commonCmdData.HooksStatusProgressPeriodSeconds) * time.Second,
KubeConfigOptions: kube.KubeConfigOptions{
Expand Down Expand Up @@ -200,7 +203,7 @@ func NewCmd(ctx context.Context) *cobra.Command {
cmd.PersistentFlags().Parse(os.Args[1:])
}

return cmd
return cmd, nil
}

func secretCmd(ctx context.Context) *cobra.Command {
Expand Down
2 changes: 1 addition & 1 deletion cmd/werf/helm/install.go
Expand Up @@ -5,7 +5,7 @@ import (
"os"

"github.com/spf13/cobra"
"helm.sh/helm/v3/cmd/helm"
helm_v3 "helm.sh/helm/v3/cmd/helm"
"helm.sh/helm/v3/pkg/action"

"github.com/werf/werf/cmd/werf/common"
Expand Down
7 changes: 5 additions & 2 deletions cmd/werf/helm/migrate2to3.go
Expand Up @@ -134,13 +134,16 @@ func runMigrate2To3(ctx context.Context) error {
ConfigPathMergeList: *migrate2To3CommonCmdData.KubeConfigPathMergeList,
}

helmRegistryClientHandler, err := common.NewHelmRegistryClientHandle(ctx, &migrate2To3CommonCmdData)
helmRegistryClient, err := common.NewHelmRegistryClient(ctx, *migrate2To3CommonCmdData.DockerConfig, *migrate2To3CommonCmdData.InsecureHelmDependencies)
if err != nil {
return fmt.Errorf("unable to create helm registry client: %w", err)
}

actionConfig := new(action.Configuration)
if err := helm.InitActionConfig(ctx, common.GetOndemandKubeInitializer(), targetNamespace, helm_v3.Settings, helmRegistryClientHandler, actionConfig, helm.InitActionConfigOptions{KubeConfigOptions: kubeConfigOptions}); err != nil {
if err := helm.InitActionConfig(ctx, common.GetOndemandKubeInitializer(), targetNamespace, helm_v3.Settings, actionConfig, helm.InitActionConfigOptions{
KubeConfigOptions: kubeConfigOptions,
RegistryClient: helmRegistryClient,
}); err != nil {
return err
}

Expand Down
19 changes: 14 additions & 5 deletions cmd/werf/main.go
Expand Up @@ -72,7 +72,11 @@ func main() {
common.TerminateWithError(fmt.Sprintf("process exterminator initialization failed: %s", err), 1)
}

rootCmd := constructRootCmd(ctx)
rootCmd, err := constructRootCmd(ctx)
if err != nil {
common.ShutdownTelemetry(ctx, 1)
common.TerminateWithError(err.Error(), 1)
}

setupTelemetryInit(rootCmd)

Expand All @@ -89,9 +93,14 @@ func main() {
common.ShutdownTelemetry(ctx, 0)
}

func constructRootCmd(ctx context.Context) *cobra.Command {
func constructRootCmd(ctx context.Context) (*cobra.Command, error) {
helmCmd, err := helm.NewCmd(ctx)
if err != nil {
return nil, fmt.Errorf("unable to init helm commands: %w", err)
}

if filepath.Base(os.Args[0]) == "helm" || helm.IsHelm3Mode() {
return helm.NewCmd(ctx)
return helmCmd, nil
}

rootCmd := common.SetCommandContext(ctx, &cobra.Command{
Expand Down Expand Up @@ -140,7 +149,7 @@ Find more information at https://werf.io`),
configCmd(ctx),
managedImagesCmd(ctx),
hostCmd(ctx),
helm.NewCmd(ctx),
helmCmd,
crCmd(ctx),
kubectl.NewCmd(ctx),
},
Expand All @@ -160,7 +169,7 @@ Find more information at https://werf.io`),

templates.ActsAsRootCommand(rootCmd, *groups...)

return rootCmd
return rootCmd, nil
}

func dockerComposeCmd(ctx context.Context) *cobra.Command {
Expand Down

0 comments on commit 414dd38

Please sign in to comment.