Skip to content

Commit

Permalink
Move subscription id lookup into pipelinerunoptions
Browse files Browse the repository at this point in the history
  • Loading branch information
janboll committed Nov 28, 2024
1 parent 02a4c06 commit dd702a7
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 23 deletions.
9 changes: 5 additions & 4 deletions tooling/templatize/cmd/pipeline/run/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,9 +97,10 @@ func (o *RunOptions) RunPipeline(ctx context.Context) error {
return err
}
return o.PipelineOptions.Pipeline.Run(ctx, &pipeline.PipelineRunOptions{
DryRun: o.DryRun,
Vars: variables,
Region: rolloutOptions.Region,
Step: o.PipelineOptions.Step,
DryRun: o.DryRun,
Vars: variables,
Region: rolloutOptions.Region,
Step: o.PipelineOptions.Step,
SubsciptionLookupFunc: pipeline.LookupSubscriptionID,
})
}
31 changes: 31 additions & 0 deletions tooling/templatize/pkg/aks/kubeconfig_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package aks

import (
"context"
"os"
"os/exec"
"testing"

"gotest.tools/assert"
)

func TestKubeConfig(t *testing.T) {
if os.Getenv("SVC_SUBSCRIPTION") == "" || os.Getenv("SVC_RG") == "" {
t.Skip("SVC_SUBSCRIPTION and SVC_RG are required for this test")
}

subscription := os.Getenv("SVC_SUBSCRIPTION")
rg := os.Getenv("SVC_RG")

config, err := GetKubeConfig(context.Background(), subscription, rg, "aks")

assert.NilError(t, err)
assert.Assert(t, config != "")

command := "kubectl"
cmd := exec.Command(command, "get", "pods")
cmd.Env = append(os.Environ(), "KUBECONFIG="+config)
output, err := cmd.CombinedOutput()
assert.NilError(t, err)
assert.Assert(t, len(output) > 0)
}
2 changes: 1 addition & 1 deletion tooling/templatize/pkg/pipeline/executiontarget.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
"github.com/Azure/ARO-HCP/tooling/templatize/pkg/aks"
)

func lookupSubscriptionID(ctx context.Context, subscriptionName string) (string, error) {
func LookupSubscriptionID(ctx context.Context, subscriptionName string) (string, error) {
// Create a new Azure identity client
cred, err := azidentity.NewDefaultAzureCredential(nil)
if err != nil {
Expand Down
15 changes: 6 additions & 9 deletions tooling/templatize/pkg/pipeline/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,19 +39,16 @@ func NewPipelineFromFile(pipelineFilePath string, vars config.Variables) (*Pipel
}

type PipelineRunOptions struct {
DryRun bool
Step string
Region string
Vars config.Variables
DryRun bool
Step string
Region string
Vars config.Variables
SubsciptionLookupFunc subsciptionLookup
}

func (p *Pipeline) Run(ctx context.Context, options *PipelineRunOptions) error {
logger := logr.FromContextOrDiscard(ctx)

if p.subsciptionLookupFunc == nil {
p.subsciptionLookupFunc = lookupSubscriptionID
}

// set working directory to the pipeline file directory for the
// duration of the execution so that all commands and file references
// within the pipeline file are resolved relative to the pipeline file
Expand All @@ -75,7 +72,7 @@ func (p *Pipeline) Run(ctx context.Context, options *PipelineRunOptions) error {

for _, rg := range p.ResourceGroups {
// prepare execution context
subscriptionID, err := p.subsciptionLookupFunc(ctx, rg.Subscription)
subscriptionID, err := options.SubsciptionLookupFunc(ctx, rg.Subscription)
if err != nil {
return fmt.Errorf("failed to lookup subscription ID for %q: %w", rg.Subscription, err)
}
Expand Down
9 changes: 5 additions & 4 deletions tooling/templatize/pkg/pipeline/run_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -174,12 +174,13 @@ func TestPipelineRun(t *testing.T) {
},
},
},
subsciptionLookupFunc: func(_ context.Context, _ string) (string, error) {
return "test", nil
},
}

err := pipeline.Run(context.Background(), &PipelineRunOptions{})
err := pipeline.Run(context.Background(), &PipelineRunOptions{
SubsciptionLookupFunc: func(_ context.Context, _ string) (string, error) {
return "test", nil
},
})

assert.NilError(t, err)
assert.Equal(t, foundOutput, "hello\n")
Expand Down
9 changes: 4 additions & 5 deletions tooling/templatize/pkg/pipeline/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,10 @@ import "context"
type subsciptionLookup func(context.Context, string) (string, error)

type Pipeline struct {
pipelineFilePath string
ServiceGroup string `yaml:"serviceGroup"`
RolloutName string `yaml:"rolloutName"`
ResourceGroups []*ResourceGroup `yaml:"resourceGroups"`
subsciptionLookupFunc subsciptionLookup
pipelineFilePath string
ServiceGroup string `yaml:"serviceGroup"`
RolloutName string `yaml:"rolloutName"`
ResourceGroups []*ResourceGroup `yaml:"resourceGroups"`
}

type ResourceGroup struct {
Expand Down

0 comments on commit dd702a7

Please sign in to comment.