Skip to content
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,9 @@ func (o *K8sBaselineRolloutStageOptions) UnmarshalJSON(data []byte) error {
return nil
}

// K8sBaselineCleanStageOptions contains all configurable values for a K8S_BASELINE_CLEAN stage.
type K8sBaselineCleanStageOptions struct{}

// K8sResourcePatch represents a patch operation for a Kubernetes resource.
type K8sResourcePatch struct {
// The target of the patch operation.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -235,3 +235,95 @@ func generateBaselineManifests(appCfg *kubeconfig.KubernetesApplicationSpec, man

return baselineManifests, nil
}

func (p *Plugin) executeK8sMultiBaselineCleanStage(ctx context.Context, input *sdk.ExecuteStageInput[kubeconfig.KubernetesApplicationSpec], dts []*sdk.DeployTarget[kubeconfig.KubernetesDeployTargetConfig]) sdk.StageStatus {
lp := input.Client.LogPersister()

cfg, err := input.Request.TargetDeploymentSource.AppConfig()
if err != nil {
lp.Errorf("Failed while decoding application config (%v)", err)
return sdk.StageStatusFailure
}

deployTargetMap := make(map[string]*sdk.DeployTarget[kubeconfig.KubernetesDeployTargetConfig], len(dts))
for _, dt := range dts {
deployTargetMap[dt.Name] = dt
}

type targetConfig struct {
deployTarget *sdk.DeployTarget[kubeconfig.KubernetesDeployTargetConfig]
multiTarget *kubeconfig.KubernetesMultiTarget
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Warashi Please check from here, If I am on track.

}

targetConfigs := make([]targetConfig, 0, len(dts))
if len(cfg.Spec.Input.MultiTargets) == 0 {
for _, dt := range dts {
targetConfigs = append(targetConfigs, targetConfig{deployTarget: dt})
}
} else {
for _, mt := range cfg.Spec.Input.MultiTargets {
dt, ok := deployTargetMap[mt.Target.Name]
if !ok {
lp.Infof("Ignore multi target '%s': not matched any deployTarget", mt.Target.Name)
continue
}
targetConfigs = append(targetConfigs, targetConfig{deployTarget: dt, multiTarget: &mt})
}
}

eg, ctx := errgroup.WithContext(ctx)
for _, tc := range targetConfigs {
eg.Go(func() error {
lp.Infof("Start cleaning BASELINE variant on target %s", tc.deployTarget.Name)
if err := p.baselineClean(ctx, input, tc.deployTarget, tc.multiTarget, cfg); err != nil {
return fmt.Errorf("failed to clean BASELINE variant on target %s: %w", tc.deployTarget.Name, err)
}
return nil
})
}

if err := eg.Wait(); err != nil {
lp.Errorf("Failed while cleaning BASELINE variant (%v)", err)
return sdk.StageStatusFailure
}

return sdk.StageStatusSuccess
}

func (p *Plugin) baselineClean(
ctx context.Context,
input *sdk.ExecuteStageInput[kubeconfig.KubernetesApplicationSpec],
dt *sdk.DeployTarget[kubeconfig.KubernetesDeployTargetConfig],
multiTarget *kubeconfig.KubernetesMultiTarget,
cfg *sdk.ApplicationConfig[kubeconfig.KubernetesApplicationSpec],
) error {
lp := input.Client.LogPersister()

var (
appCfg = cfg.Spec
variantLabel = appCfg.VariantLabel.Key
baselineVariant = appCfg.VariantLabel.BaselineValue
)

toolRegistry := toolregistry.NewRegistry(input.Client.ToolRegistry())

// Resolve kubectl version: multiTarget > spec > deployTarget
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah yes!

kubectlVersion := cmp.Or(appCfg.Input.KubectlVersion, dt.Config.KubectlVersion)
if multiTarget != nil {
kubectlVersion = cmp.Or(multiTarget.KubectlVersion, kubectlVersion)
}

kubectlPath, err := toolRegistry.Kubectl(ctx, kubectlVersion)
if err != nil {
return fmt.Errorf("failed while getting kubectl tool: %w", err)
}

kubectl := provider.NewKubectl(kubectlPath)
applier := provider.NewApplier(kubectl, appCfg.Input, dt.Config, input.Logger)

if err := deleteVariantResources(ctx, lp, kubectl, dt.Config.KubeConfigPath, applier, input.Request.Deployment.ApplicationID, variantLabel, baselineVariant); err != nil {
return fmt.Errorf("unable to remove baseline resources: %w", err)
}

return nil
}
Loading
Loading