From 69eb024f779642d0801d533e7a53b5b338495536 Mon Sep 17 00:00:00 2001 From: Jan-Hendrik Boll Date: Thu, 5 Dec 2024 15:50:31 +0100 Subject: [PATCH] Add no persist toggle --- .../templatize/cmd/pipeline/run/options.go | 5 +++++ tooling/templatize/internal/end2end/e2e.go | 1 + tooling/templatize/pkg/pipeline/arm.go | 19 ++++++++----------- tooling/templatize/pkg/pipeline/run.go | 1 + 4 files changed, 15 insertions(+), 11 deletions(-) diff --git a/tooling/templatize/cmd/pipeline/run/options.go b/tooling/templatize/cmd/pipeline/run/options.go index beee7e737..5a866f37c 100644 --- a/tooling/templatize/cmd/pipeline/run/options.go +++ b/tooling/templatize/cmd/pipeline/run/options.go @@ -23,12 +23,14 @@ func BindOptions(opts *RawRunOptions, cmd *cobra.Command) error { return fmt.Errorf("failed to bind options: %w", err) } cmd.Flags().BoolVar(&opts.DryRun, "dry-run", opts.DryRun, "validate the pipeline without executing it") + cmd.Flags().BoolVar(&opts.NoPersist, "no-persist-tag", opts.NoPersist, "toggle if persist tag should not be set") return nil } type RawRunOptions struct { PipelineOptions *options.RawPipelineOptions DryRun bool + NoPersist bool } // validatedRunOptions is a private wrapper that enforces a call of Validate() before Complete() can be invoked. @@ -46,6 +48,7 @@ type ValidatedRunOptions struct { type completedRunOptions struct { PipelineOptions *options.PipelineOptions DryRun bool + NoPersist bool } type RunOptions struct { @@ -77,6 +80,7 @@ func (o *ValidatedRunOptions) Complete() (*RunOptions, error) { completedRunOptions: &completedRunOptions{ PipelineOptions: completed, DryRun: o.DryRun, + NoPersist: o.NoPersist, }, }, nil } @@ -102,5 +106,6 @@ func (o *RunOptions) RunPipeline(ctx context.Context) error { Region: rolloutOptions.Region, Step: o.PipelineOptions.Step, SubsciptionLookupFunc: pipeline.LookupSubscriptionID, + NoPersist: o.NoPersist, }) } diff --git a/tooling/templatize/internal/end2end/e2e.go b/tooling/templatize/internal/end2end/e2e.go index 6edb6d387..b48c1a145 100644 --- a/tooling/templatize/internal/end2end/e2e.go +++ b/tooling/templatize/internal/end2end/e2e.go @@ -125,6 +125,7 @@ func (e *e2eImpl) SetOSArgs() { "--pipeline-file", e.tmpdir + "/pipeline.yaml", "--config-file", e.tmpdir + "/config.yaml", "--deploy-env", "dev", + "--no-persist-tag", "--region", "westus3", } } diff --git a/tooling/templatize/pkg/pipeline/arm.go b/tooling/templatize/pkg/pipeline/arm.go index 5e72e312a..6e5bca60f 100644 --- a/tooling/templatize/pkg/pipeline/arm.go +++ b/tooling/templatize/pkg/pipeline/arm.go @@ -47,7 +47,7 @@ func (a *armClient) runArmStep(ctx context.Context, options *PipelineRunOptions, } // Ensure resourcegroup exists - err = a.ensureResourceGroupExists(ctx, rgName) + err = a.ensureResourceGroupExists(ctx, rgName, options.NoPersist) if err != nil { return nil, fmt.Errorf("failed to ensure resource group exists: %w", err) } @@ -85,23 +85,20 @@ func (a *armClient) runArmStep(ctx context.Context, options *PipelineRunOptions, return nil, nil } -func (a *armClient) ensureResourceGroupExists(ctx context.Context, rgName string) error { - // Create a new Azure identity client - cred, err := azidentity.NewDefaultAzureCredential(nil) - if err != nil { - return fmt.Errorf("failed to obtain a credential: %w", err) - } - +func (a *armClient) ensureResourceGroupExists(ctx context.Context, rgName string, rgNoPersist bool) error { // Create a new ARM client - client, err := armresources.NewResourceGroupsClient(a.SubscriptionID, cred, nil) + client, err := armresources.NewResourceGroupsClient(a.SubscriptionID, a.creds, nil) if err != nil { return fmt.Errorf("failed to create ARM client: %w", err) } // Check if the resource group exists // todo fill tags properly - tags := map[string]*string{ - "persist": to.Ptr("true"), + tags := map[string]*string{} + + if !rgNoPersist { + // if no-persist is set, don't set the persist tag, needs double negotiate, cause default should be true + tags["persist"] = to.Ptr("true") } _, err = client.Get(ctx, rgName, nil) if err != nil { diff --git a/tooling/templatize/pkg/pipeline/run.go b/tooling/templatize/pkg/pipeline/run.go index c376427d4..bf5e16675 100644 --- a/tooling/templatize/pkg/pipeline/run.go +++ b/tooling/templatize/pkg/pipeline/run.go @@ -44,6 +44,7 @@ type PipelineRunOptions struct { Region string Vars config.Variables SubsciptionLookupFunc subsciptionLookup + NoPersist bool } type armOutput map[string]any