Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
fix(exp-engine): decouple deploy from ActionConfig
Signed-off-by: Ilya Lesikov <ilya@lesikov.com>
  • Loading branch information
ilya-lesikov committed Jul 21, 2023
1 parent ac382cf commit a4b0850
Show file tree
Hide file tree
Showing 11 changed files with 54 additions and 62 deletions.
4 changes: 2 additions & 2 deletions cmd/werf/bundle/apply/apply.go
Expand Up @@ -159,7 +159,7 @@ func runApply(ctx context.Context) error {
}

actionConfig := new(action.Configuration)
if err := helm.InitActionConfig(ctx, common.GetOndemandKubeInitializer(), releaseName, namespace, helm_v3.Settings, 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 @@ -170,7 +170,7 @@ func runApply(ctx context.Context) error {
},
ReleasesHistoryMax: *commonCmdData.ReleasesHistoryMax,
RegistryClient: helmRegistryClient,
}, nil); err != nil {
}); err != nil {
return err
}

Expand Down
2 changes: 1 addition & 1 deletion cmd/werf/bundle/render/render.go
Expand Up @@ -154,7 +154,7 @@ func runRender(ctx context.Context) error {
releaseName := common.GetOptionalRelease(&commonCmdData)

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

Expand Down
7 changes: 3 additions & 4 deletions cmd/werf/common/helm.go
Expand Up @@ -7,7 +7,6 @@ import (
helm_v3 "helm.sh/helm/v3/cmd/helm"
"helm.sh/helm/v3/pkg/action"
"helm.sh/helm/v3/pkg/registry"
"helm.sh/helm/v3/pkg/werf/mutator"

"github.com/werf/kubedog/pkg/kube"
"github.com/werf/logboek"
Expand Down Expand Up @@ -55,10 +54,10 @@ func NewBundlesRegistryClient(ctx context.Context, commonCmdData *CmdData) (*bun
)
}

func NewActionConfig(ctx context.Context, kubeInitializer helm.KubeInitializer, releaseName, namespace string, commonCmdData *CmdData, registryClient *registry.Client, extraMutators []mutator.RuntimeResourceMutator) (*action.Configuration, error) {
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, releaseName, namespace, helm_v3.Settings, 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 @@ -69,7 +68,7 @@ func NewActionConfig(ctx context.Context, kubeInitializer helm.KubeInitializer,
},
ReleasesHistoryMax: *commonCmdData.ReleasesHistoryMax,
RegistryClient: registryClient,
}, extraMutators); err != nil {
}); err != nil {
return nil, err
}

Expand Down
52 changes: 37 additions & 15 deletions cmd/werf/converge/converge.go
Expand Up @@ -21,12 +21,16 @@ import (
"helm.sh/helm/v3/pkg/registry"
"helm.sh/helm/v3/pkg/release"
helmchart "helm.sh/helm/v3/pkg/werf/chart"
"helm.sh/helm/v3/pkg/werf/client"
"helm.sh/helm/v3/pkg/werf/history"
"helm.sh/helm/v3/pkg/werf/kubeclient"
"helm.sh/helm/v3/pkg/werf/log"
"helm.sh/helm/v3/pkg/werf/mutator"
"helm.sh/helm/v3/pkg/werf/plan"
helmresource "helm.sh/helm/v3/pkg/werf/resource"
"helm.sh/helm/v3/pkg/werf/resourcebuilder"
"helm.sh/helm/v3/pkg/werf/resourcetracker"
"helm.sh/helm/v3/pkg/werf/resourcewaiter"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"

"github.com/werf/kubedog/pkg/kube"
Expand Down Expand Up @@ -492,7 +496,7 @@ func run(ctx context.Context, containerBackend container_backend.ContainerBacken
}
}

actionConfig, err := common.NewActionConfig(ctx, common.GetOndemandKubeInitializer(), releaseName, namespace, &commonCmdData, helmRegistryClient, extraRuntimeResourceMutators)
actionConfig, err := common.NewActionConfig(ctx, common.GetOndemandKubeInitializer(), namespace, &commonCmdData, helmRegistryClient)
if err != nil {
return err
}
Expand All @@ -502,7 +506,7 @@ func run(ctx context.Context, containerBackend container_backend.ContainerBacken
return err
}

actionConfig, err = common.NewActionConfig(ctx, common.GetOndemandKubeInitializer(), releaseName, namespace, &commonCmdData, helmRegistryClient, extraRuntimeResourceMutators)
actionConfig, err = common.NewActionConfig(ctx, common.GetOndemandKubeInitializer(), namespace, &commonCmdData, helmRegistryClient)
if err != nil {
return err
}
Expand Down Expand Up @@ -534,9 +538,6 @@ func run(ctx context.Context, containerBackend container_backend.ContainerBacken
}
}

trackTimeout := *common.NewDuration(time.Duration(cmdData.Timeout) * time.Second)
actionConfig.Client.SetDeletionTimeout(int(trackTimeout))

// FIXME(ilya-lesikov): there is more chartpath options, are they needed?
chartPathOptions := action.ChartPathOptions{}
chartPathOptions.SetRegistryClient(actionConfig.RegistryClient)
Expand All @@ -548,6 +549,31 @@ func run(ctx context.Context, containerBackend container_backend.ContainerBacken
releaseNamespace = helm_v3.Settings.Namespace()
}

configGetter := helm_v3.Settings.GetConfigP()

deferredKubeClient := kubeclient.NewDeferredKubeClient(*configGetter)
if err := deferredKubeClient.Init(); err != nil {
return fmt.Errorf("error initializing deferred kube client: %w", err)
}

waiter := resourcewaiter.NewResourceWaiter(deferredKubeClient.Dynamic(), deferredKubeClient.Mapper())

statusProgressPeriod := time.Duration(*commonCmdData.StatusProgressPeriodSeconds) * time.Second
hooksStatusProgressPeriod := time.Duration(*commonCmdData.HooksStatusProgressPeriodSeconds) * time.Second
tracker := resourcetracker.NewResourceTracker(statusProgressPeriod, hooksStatusProgressPeriod)

cli, err := client.NewClient(deferredKubeClient.Static(), deferredKubeClient.Dynamic(), deferredKubeClient.Discovery(), deferredKubeClient.Mapper(), waiter)
if err != nil {
return fmt.Errorf("error creating client: %w", err)
}
cli.AddTargetResourceMutators(extraRuntimeResourceMutators...)
cli.AddTargetResourceMutators(
mutator.NewReplicasOnCreationMutator(),
mutator.NewReleaseMetadataMutator(releaseName, namespace),
)
trackTimeout := *common.NewDuration(time.Duration(cmdData.Timeout) * time.Second)
cli.SetDeletionTimeout(int(trackTimeout))

// FIXME(ilya-lesikov): move some of it out of lock release wrapper
return command_helpers.LockReleaseWrapper(ctx, releaseName, lockManager, func() error {
actionConfig.Releases.MaxHistory = *commonCmdData.ReleasesHistoryMax
Expand All @@ -556,10 +582,6 @@ func run(ctx context.Context, containerBackend container_backend.ContainerBacken
return fmt.Errorf("release name is invalid: %s", releaseNamespace)
}

if err := actionConfig.DeferredKubeClient.Init(); err != nil {
return fmt.Errorf("error initializing deferred kube client: %w", err)
}

hist, err := history.NewHistory(releaseName, releaseNamespace, actionConfig.Releases.Driver)
if err != nil {
return fmt.Errorf("error building history for release %q: %w", releaseName, err)
Expand Down Expand Up @@ -589,14 +611,14 @@ func run(ctx context.Context, containerBackend container_backend.ContainerBacken
},
},
},
actionConfig.Client.DiscoveryRESTMapper(),
actionConfig.Client.DiscoveryClient(),
cli.DiscoveryRESTMapper(),
cli.DiscoveryClient(),
)
if err := releaseNamespace.Validate(); err != nil {
return fmt.Errorf("error validating release namespace: %w", err)
}

deployResourceBuilder := resourcebuilder.NewDeployResourceBuilder(releaseNamespace, deployType, actionConfig.Client).
deployResourceBuilder := resourcebuilder.NewDeployResourceBuilder(releaseNamespace, deployType, cli).
WithLegacyPreloadedCRDs(chartTree.LegacyPreloadedCRDs()...).
WithLegacyHelmHooks(chartTree.LegacyHooks()...).
WithReleaseManifests(chartTree.LegacyResources())
Expand Down Expand Up @@ -835,7 +857,7 @@ func run(ctx context.Context, containerBackend container_backend.ContainerBacken
}()
}

deployReport, executeErr := plan.NewDeployPlanExecutor(deployPlan, releaseNamespace, actionConfig.Client, actionConfig.Tracker, actionConfig.Releases).WithTrackTimeout(trackTimeout).Execute(ctx)
deployReport, executeErr := plan.NewDeployPlanExecutor(deployPlan, releaseNamespace, cli, tracker, actionConfig.Releases).WithTrackTimeout(trackTimeout).Execute(ctx)
if executeErr != nil {
defer func() {
fmt.Fprintf(errStream, "\nRelease %q in namespace %q failed.\n", releaseName, releaseNamespace)
Expand All @@ -849,7 +871,7 @@ func run(ctx context.Context, containerBackend container_backend.ContainerBacken
Build()

// FIXME(ilya-lesikov): deploy report from this execute is not used
_, err = plan.NewDeployPlanExecutor(finalizeFailedDeployPlan, releaseNamespace, actionConfig.Client, actionConfig.Tracker, actionConfig.Releases).WithTrackTimeout(trackTimeout).WithReport(deployReport).Execute(ctx)
_, err = plan.NewDeployPlanExecutor(finalizeFailedDeployPlan, releaseNamespace, cli, tracker, actionConfig.Releases).WithTrackTimeout(trackTimeout).WithReport(deployReport).Execute(ctx)
if err != nil {
return multierror.Append(executeErr, fmt.Errorf("error finalizing failed deploy plan: %w", err))
}
Expand Down Expand Up @@ -962,7 +984,7 @@ func migrateHelm2ToHelm3(ctx context.Context, releaseName, namespace string, mai

logboek.Context(ctx).Default().LogOptionalLn()
if err := logboek.Context(ctx).LogProcess("Rendering helm 3 templates for the current project state").DoError(func() error {
actionConfig, err := common.NewActionConfig(ctx, common.GetOndemandKubeInitializer(), releaseName, namespace, &commonCmdData, helmRegistryClient, extraMutators)
actionConfig, err := common.NewActionConfig(ctx, common.GetOndemandKubeInitializer(), namespace, &commonCmdData, helmRegistryClient)
if err != nil {
return err
}
Expand Down
4 changes: 2 additions & 2 deletions cmd/werf/dismiss/dismiss.go
Expand Up @@ -209,7 +209,7 @@ func runDismiss(ctx context.Context) error {
}

actionConfig := new(action.Configuration)
if err := helm.InitActionConfig(ctx, common.GetOndemandKubeInitializer(), release, namespace, helm_v3.Settings, 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 @@ -219,7 +219,7 @@ func runDismiss(ctx context.Context) error {
},
ReleasesHistoryMax: *commonCmdData.ReleasesHistoryMax,
RegistryClient: helmRegistryClient,
}, nil); err != nil {
}); err != nil {
return err
}

Expand Down
4 changes: 2 additions & 2 deletions cmd/werf/helm/helm.go
Expand Up @@ -200,7 +200,7 @@ func NewCmd(ctx context.Context) (*cobra.Command, error) {

common.SetupOndemandKubeInitializer(*_commonCmdData.KubeContext, *_commonCmdData.KubeConfig, *_commonCmdData.KubeConfigBase64, *_commonCmdData.KubeConfigPathMergeList)

helm.InitActionConfig(ctx, common.GetOndemandKubeInitializer(), "", namespace, helm_v3.Settings, 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 All @@ -210,7 +210,7 @@ func NewCmd(ctx context.Context) (*cobra.Command, error) {
ConfigDataBase64: *_commonCmdData.KubeConfigBase64,
},
ReleasesHistoryMax: *_commonCmdData.ReleasesHistoryMax,
}, nil)
})

if oldRunE != nil {
return oldRunE(cmd, args)
Expand Down
4 changes: 2 additions & 2 deletions cmd/werf/helm/migrate2to3.go
Expand Up @@ -140,10 +140,10 @@ func runMigrate2To3(ctx context.Context) error {
}

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

Expand Down
2 changes: 1 addition & 1 deletion cmd/werf/render/render.go
Expand Up @@ -416,7 +416,7 @@ func runRender(ctx context.Context, imagesToProcess build.ImagesToProcess) error
wc.SetServiceValues(vals)
}

actionConfig, err := common.NewActionConfig(ctx, common.GetOndemandKubeInitializer(), releaseName, namespace, &commonCmdData, helmRegistryClient, nil)
actionConfig, err := common.NewActionConfig(ctx, common.GetOndemandKubeInitializer(), namespace, &commonCmdData, helmRegistryClient)
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Expand Up @@ -347,6 +347,6 @@ replace (
github.com/helm/helm-2to3 => github.com/werf/3p-helm-2to3 v0.0.0-20230313155428-cf9dd655c0e3 // switch back to upstream when merged: https://github.com/helm/helm-2to3/pull/224
github.com/jaguilar/vt100 => github.com/tonistiigi/vt100 v0.0.0-20190402012908-ad4c4a574305 // upstream not maintained
github.com/maorfr/helm-plugin-utils => github.com/werf/3p-helm-plugin-utils v0.6.1-0.20230313152239-057595ce9d57 // switch back to upstream when merged: https://github.com/maorfr/helm-plugin-utils/pull/17
helm.sh/helm/v3 => github.com/werf/3p-helm/v3 v3.0.0-20230720150643-13bccc57bd78
helm.sh/helm/v3 => github.com/werf/3p-helm/v3 v3.0.0-20230721124034-069d446f230a
k8s.io/helm => github.com/werf/helm v0.0.0-20210202111118-81e74d46da0f
)
4 changes: 2 additions & 2 deletions go.sum
Expand Up @@ -1740,8 +1740,8 @@ github.com/weppos/publicsuffix-go v0.5.0/go.mod h1:z3LCPQ38eedDQSwmsSRW4Y7t2L8Ln
github.com/weppos/publicsuffix-go v0.20.1-0.20221209102050-40d9c30084b3 h1:ypyhoprZWFzU0ydOBv3I5SS7/jLFJ+ujPAU+BD/EVFM=
github.com/werf/3p-helm-2to3 v0.0.0-20230313155428-cf9dd655c0e3 h1:pdKLiuiUoOFswc0HV6GX1+JHgP14HqE035xl2DQJi8A=
github.com/werf/3p-helm-2to3 v0.0.0-20230313155428-cf9dd655c0e3/go.mod h1:lTSQBLomjl67m4HQMbg3BHKWes3pOmjNBg8yygXYugk=
github.com/werf/3p-helm/v3 v3.0.0-20230720150643-13bccc57bd78 h1:ZuW0Ljr9SatWfJV7XU1k6ebVHVNUCCcWOh5O+eKr0g0=
github.com/werf/3p-helm/v3 v3.0.0-20230720150643-13bccc57bd78/go.mod h1:vH6ckglZT6mhJefVLbUM9759Zt/wQnqqRe0BY4jmmHM=
github.com/werf/3p-helm/v3 v3.0.0-20230721124034-069d446f230a h1:Rl/eBUlHoPqd0DQo4HXBo851ct/3JOW2fMA15I/2jBE=
github.com/werf/3p-helm/v3 v3.0.0-20230721124034-069d446f230a/go.mod h1:vH6ckglZT6mhJefVLbUM9759Zt/wQnqqRe0BY4jmmHM=
github.com/werf/copy-recurse v0.2.7 h1:3FTOarbJ9uhFLi75oeUCioK9zxZwuV7o28kuUBPDZPM=
github.com/werf/copy-recurse v0.2.7/go.mod h1:6Ypb+qN+hRBJgoCgEkX1vpbqcQ+8q69BQ3hi8s8Y6Qc=
github.com/werf/helm v0.0.0-20210202111118-81e74d46da0f h1:81YscYTF9mmTf0ULOsCmm42YWQp+qWDzWi1HjWniZrg=
Expand Down
31 changes: 1 addition & 30 deletions pkg/deploy/helm/init.go
Expand Up @@ -16,16 +16,10 @@ import (
"helm.sh/helm/v3/pkg/registry"
"helm.sh/helm/v3/pkg/release"
"helm.sh/helm/v3/pkg/storage/driver"
"helm.sh/helm/v3/pkg/werf/client"
"helm.sh/helm/v3/pkg/werf/kubeclient"
"helm.sh/helm/v3/pkg/werf/mutator"
"helm.sh/helm/v3/pkg/werf/resourcetracker"
"helm.sh/helm/v3/pkg/werf/resourcewaiter"
"sigs.k8s.io/yaml"

"github.com/werf/kubedog/pkg/kube"
"github.com/werf/logboek"
"github.com/werf/werf/pkg/util"
)

const FEATURE_TOGGLE_ENV_EXPERIMENTAL_DEPLOY_ENGINE = "WERF_EXPERIMENTAL_DEPLOY_ENGINE"
Expand All @@ -38,7 +32,7 @@ type InitActionConfigOptions struct {
RegistryClient *registry.Client
}

func InitActionConfig(ctx context.Context, kubeInitializer KubeInitializer, releaseName, namespace string, envSettings *cli.EnvSettings, actionConfig *action.Configuration, opts InitActionConfigOptions, extraMutators []mutator.RuntimeResourceMutator) error {
func InitActionConfig(ctx context.Context, kubeInitializer KubeInitializer, namespace string, envSettings *cli.EnvSettings, actionConfig *action.Configuration, opts InitActionConfigOptions) error {
configGetter, err := kube.NewKubeConfigGetter(kube.KubeConfigGetterOptions{
KubeConfigOptions: opts.KubeConfigOptions,
Namespace: namespace,
Expand Down Expand Up @@ -80,29 +74,6 @@ func InitActionConfig(ctx context.Context, kubeInitializer KubeInitializer, rele
actionConfig.RegistryClient = opts.RegistryClient
}

if util.GetBoolEnvironmentDefaultFalse(FEATURE_TOGGLE_ENV_EXPERIMENTAL_DEPLOY_ENGINE) {
deferredKubeClient := kubeclient.NewDeferredKubeClient(configGetter)

waiter := resourcewaiter.NewResourceWaiter(deferredKubeClient.Dynamic(), deferredKubeClient.Mapper())

tracker := resourcetracker.NewResourceTracker(opts.StatusProgressPeriod, opts.HooksStatusProgressPeriod)

cli, err := client.NewClient(deferredKubeClient.Static(), deferredKubeClient.Dynamic(), deferredKubeClient.Discovery(), deferredKubeClient.Mapper(), waiter)
if err != nil {
return fmt.Errorf("error creating client: %w", err)
}
cli.AddTargetResourceMutators(extraMutators...)
cli.AddTargetResourceMutators(
mutator.NewReplicasOnCreationMutator(),
mutator.NewReleaseMetadataMutator(releaseName, namespace),
)

actionConfig.DeferredKubeClient = deferredKubeClient
actionConfig.Waiter = waiter
actionConfig.Tracker = tracker
actionConfig.Client = cli
}

return nil
}

Expand Down

0 comments on commit a4b0850

Please sign in to comment.