Skip to content

Commit

Permalink
fmt
Browse files Browse the repository at this point in the history
Signed-off-by: Gerd Oberlechner <[email protected]>
  • Loading branch information
geoberle committed Nov 13, 2024
1 parent 2ceaadf commit 33671c1
Show file tree
Hide file tree
Showing 14 changed files with 80 additions and 65 deletions.
19 changes: 10 additions & 9 deletions tooling/templatize/cmd/generate/cmd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,22 @@ import (
"github.com/google/go-cmp/cmp"
"github.com/stretchr/testify/assert"

options "github.com/Azure/ARO-HCP/tooling/templatize/cmd"
"github.com/Azure/ARO-HCP/tooling/templatize/internal/config"
)

func TestExecuteTemplate(t *testing.T) {
for _, testCase := range []struct {
name string
config config.Variables
input string
name string
vars config.Variables
input string

expected string
expectedError bool
}{
{
name: "happy case generates a file",
config: config.Variables{
vars: config.Variables{
"region_maestro_keyvault": "kv",
"region_eventgrid_namespace": "ns",
},
Expand All @@ -36,7 +37,7 @@ param maestroEventGridMaxClientSessionsPerAuthName = 4`,
},
{
name: "referencing unset variable errors",
config: config.Variables{
vars: config.Variables{
"region_maestro_keyvault": "kv",
},
input: `param maestroKeyVaultName = '{{ .region_maestro_keyvault }}'
Expand All @@ -49,10 +50,10 @@ param maestroEventGridMaxClientSessionsPerAuthName = 4`,
output := &bytes.Buffer{}
opts := GenerationOptions{
completedGenerationOptions: &completedGenerationOptions{
Config: testCase.config,
Input: fstest.MapFS{"test": &fstest.MapFile{Data: []byte(testCase.input)}},
InputFile: "test",
Output: &nopCloser{Writer: output},
InputFS: fstest.MapFS{"test": &fstest.MapFile{Data: []byte(testCase.input)}},
InputFile: "test",
OutputFile: &nopCloser{Writer: output},
RolloutOptions: options.NewRolloutOptions(testCase.vars),
},
}
err := opts.ExecuteTemplate()
Expand Down
19 changes: 7 additions & 12 deletions tooling/templatize/cmd/generate/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ package generate

import (
"fmt"
"io"
"io/fs"
"log"
"os"
"path/filepath"
"text/template"
Expand Down Expand Up @@ -60,10 +60,10 @@ type ValidatedGenerationOptions struct {
// completedGenerationOptions is a private wrapper that enforces a call of Complete() before config generation can be invoked.
type completedGenerationOptions struct {
*ValidatedGenerationOptions
CompletedRolloutOptions *options.RolloutOptions
InputFS fs.FS
InputFile string
OutputFile *os.File
*options.RolloutOptions
InputFS fs.FS
InputFile string
OutputFile io.Writer
}

type GenerationOptions struct {
Expand Down Expand Up @@ -110,7 +110,7 @@ func (o *ValidatedGenerationOptions) Complete() (*GenerationOptions, error) {
return &GenerationOptions{
completedGenerationOptions: &completedGenerationOptions{
ValidatedGenerationOptions: o,
CompletedRolloutOptions: completed,
RolloutOptions: completed,
InputFS: os.DirFS(filepath.Dir(o.Input)),
InputFile: inputFile,
OutputFile: outputFile,
Expand All @@ -130,10 +130,5 @@ func (opts *GenerationOptions) ExecuteTemplate() error {
return err
}

defer func() {
if err := opts.OutputFile.Close(); err != nil {
log.Printf("error closing output: %v\n", err)
}
}()
return tmpl.Option("missingkey=error").ExecuteTemplate(opts.OutputFile, opts.InputFile, opts.CompletedRolloutOptions.Config)
return tmpl.Option("missingkey=error").ExecuteTemplate(opts.OutputFile, opts.InputFile, opts.RolloutOptions.Config)
}
10 changes: 6 additions & 4 deletions tooling/templatize/cmd/generate/options_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,15 @@ import (
func TestRawOptions(t *testing.T) {
tmpdir := t.TempDir()
opts := &RawGenerationOptions{
RawOptions: options.RawOptions{
ConfigFile: "../../testdata/config.yaml",
Cloud: "public",
DeployEnv: "dev",
RolloutOptions: &options.RawRolloutOptions{
Region: "uksouth",
RegionShort: "abcde",
Stamp: "fghij",
BaseOptions: &options.RawOptions{
ConfigFile: "../../testdata/config.yaml",
Cloud: "public",
DeployEnv: "dev",
},
},
Input: "../../testdata/helm.sh",
Output: fmt.Sprintf("%s/helm.sh", tmpdir),
Expand Down
File renamed without changes.
9 changes: 6 additions & 3 deletions tooling/templatize/cmd/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ func DefaultOptions() *RawOptions {
}

func BindOptions(opts *RawOptions, cmd *cobra.Command) error {
cmd.Flags().StringVar(&opts.ConfigFile, "config-file", opts.ConfigFile, "config file path")
cmd.Flags().StringVarP(&opts.ConfigFile, "config-file", "c", opts.ConfigFile, "config file path")
cmd.Flags().StringVar(&opts.Cloud, "cloud", opts.Cloud, "the cloud (public, fairfax)")
cmd.Flags().StringVar(&opts.DeployEnv, "deploy-env", opts.DeployEnv, "the deploy environment")
cmd.Flags().StringVarP(&opts.DeployEnv, "deploy-env", "e", opts.DeployEnv, "the deploy environment")
return nil
}

Expand Down Expand Up @@ -52,7 +52,10 @@ type ValidatedOptions struct {

func (o *ValidatedOptions) Complete() (*Options, error) {
configProvider := config.NewConfigProvider(o.ConfigFile)
configProvider.Validate(o.Cloud, o.DeployEnv)
err := configProvider.Validate(o.Cloud, o.DeployEnv)
if err != nil {
return nil, fmt.Errorf("failed to validate config: %w", err)
}

return &Options{
completedOptions: &completedOptions{
Expand Down
29 changes: 22 additions & 7 deletions tooling/templatize/cmd/rolloutoptions.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,14 @@ func DefaultRolloutOptions() *RawRolloutOptions {
}
}

func NewRolloutOptions(config config.Variables) *RolloutOptions {
return &RolloutOptions{
completedRolloutOptions: &completedRolloutOptions{
Config: config,
},
}
}

func EV2RolloutOptions() *RawRolloutOptions {
return &RawRolloutOptions{
Region: "$location()",
Expand All @@ -23,11 +31,14 @@ func EV2RolloutOptions() *RawRolloutOptions {
}

func BindRolloutOptions(opts *RawRolloutOptions, cmd *cobra.Command) error {
cmd.Flags().StringVar(&opts.Region, "region", opts.Region, "resources location")
err := BindOptions(opts.BaseOptions, cmd)
if err != nil {
return fmt.Errorf("failed to bind options: %w", err)
}
cmd.Flags().StringVarP(&opts.Region, "region", "r", opts.Region, "resources location")
cmd.Flags().StringVar(&opts.RegionShort, "region-short", opts.RegionShort, "short region string")
cmd.Flags().StringVar(&opts.Stamp, "stamp", opts.Stamp, "stamp")
cmd.Flags().StringVarP(&opts.Stamp, "stamp", "s", opts.Stamp, "stamp")
cmd.Flags().StringToStringVar(&opts.ExtraVars, "extra-args", opts.ExtraVars, "Extra arguments to be used config templating")
BindOptions(opts.BaseOptions, cmd)
return nil
}

Expand All @@ -53,8 +64,9 @@ type ValidatedRolloutOptions struct {

type completedRolloutOptions struct {
*ValidatedRolloutOptions
CompletedOptions *Options
Config config.Variables
Options *Options

Config config.Variables
}

type RolloutOptions struct {
Expand Down Expand Up @@ -91,12 +103,15 @@ func (o *ValidatedRolloutOptions) Complete() (*RolloutOptions, error) {
for k, v := range o.ExtraVars {
extraVars[k] = v
}
variables.AddNested("extraVars", extraVars)
err = variables.AddNested("extraVars", extraVars)
if err != nil {
return nil, fmt.Errorf("failed to add extraVars: %w", err)
}

return &RolloutOptions{
completedRolloutOptions: &completedRolloutOptions{
ValidatedRolloutOptions: o,
CompletedOptions: completed,
Options: completed,
Config: variables,
},
}, nil
Expand Down
6 changes: 3 additions & 3 deletions tooling/templatize/cmd/run/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ import (
func NewCommand() *cobra.Command {
opts := DefaultOptions()
cmd := &cobra.Command{
Use: "run",
Short: "run",
Long: "run",
Use: "run-pipeline",
Short: "run a pipeline.yaml file towards a target cluster or infrastructure",
Long: "run a pipeline.yaml file towards a target cluster or infrastructure",
RunE: func(cmd *cobra.Command, args []string) error {
return runPipeline(opts)
},
Expand Down
14 changes: 7 additions & 7 deletions tooling/templatize/cmd/run/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ func BindOptions(opts *RawRunOptions, cmd *cobra.Command) error {
if err != nil {
return fmt.Errorf("failed to bind options: %w", err)
}
cmd.Flags().StringVar(&opts.PipelineFile, "pipeline-config", opts.PipelineFile, "pipeline file path")
cmd.Flags().StringVarP(&opts.PipelineFile, "pipeline-config", "p", opts.PipelineFile, "pipeline file path")

for _, flag := range []string{"pipeline-config"} {
if err := cmd.MarkFlagFilename(flag); err != nil {
Expand Down Expand Up @@ -51,8 +51,8 @@ type ValidatedRunOptions struct {
// completedRunOptions is a private wrapper that enforces a call of Complete() before config generation can be invoked.
type completedRunOptions struct {
*ValidatedRunOptions
CompletedRolloutOptions *options.RolloutOptions
Pipeline *pipeline.Pipeline
RolloutOptions *options.RolloutOptions
Pipeline *pipeline.Pipeline
}

type RunOptions struct {
Expand Down Expand Up @@ -87,15 +87,15 @@ func (o *ValidatedRunOptions) Complete() (*RunOptions, error) {

return &RunOptions{
completedRunOptions: &completedRunOptions{
ValidatedRunOptions: o,
CompletedRolloutOptions: completed,
Pipeline: pipeline,
ValidatedRunOptions: o,
RolloutOptions: completed,
Pipeline: pipeline,
},
}, nil
}

func (o *RunOptions) RunPipeline() error {
variables, err := o.CompletedRolloutOptions.CompletedOptions.ConfigProvider.GetVariables(
variables, err := o.RolloutOptions.Options.ConfigProvider.GetVariables(
o.Cloud,
o.DeployEnv,
o.Region,
Expand Down
10 changes: 5 additions & 5 deletions tooling/templatize/internal/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@ import (

func TestConfigProvider(t *testing.T) {
region := "uksouth"
regionStamp := "1"
cxStamp := "cx"
regionShort := "uks"
stamp := "1"

configProvider := NewConfigProvider("../../testdata/config.yaml", region, regionStamp, cxStamp)
configProvider := NewConfigProvider("../../testdata/config.yaml")

variables, err := configProvider.GetVariables("public", "int", map[string]string{})
variables, err := configProvider.GetVariables("public", "int", region, NewConfigEvaluationContext(region, regionShort, stamp))
assert.NoError(t, err)
assert.NotNil(t, variables)

Expand All @@ -28,7 +28,7 @@ func TestConfigProvider(t *testing.T) {
assert.Equal(t, "aro-hcp-int.azurecr.io/maestro-server:the-stable-one", variables["maestro_image"])

// key is in the config file, default, varaible value
assert.Equal(t, fmt.Sprintf("hcp-underlay-%s-%s", region, regionStamp), variables["region_resourcegroup"])
assert.Equal(t, fmt.Sprintf("hcp-underlay-%s-%s", region, stamp), variables["region_resourcegroup"])
}

func TestInterfaceToVariable(t *testing.T) {
Expand Down
2 changes: 1 addition & 1 deletion tooling/templatize/internal/config/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ func (co *VariableOverrides) GetCloud(cloud string) (*CloudVariableOverride, err
if cloudOverride, ok := co.Overrides[cloud]; ok {
return cloudOverride, nil
}
return nil, fmt.Errorf("the cloud %s is not found in %s", cloud, co)
return nil, fmt.Errorf("the cloud %s is not found in config", cloud)
}

func (vo *VariableOverrides) GetCloudOverrides(cloud string) Variables {
Expand Down
6 changes: 2 additions & 4 deletions tooling/templatize/internal/ev2/mapping.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,14 @@ package ev2
import (
"fmt"
"strings"

"github.com/Azure/ARO-HCP/tooling/templatize/internal/config"
)

func EV2Mapping(input config.Variables, prefix []string) (map[string]string, map[string]interface{}) {
func EV2Mapping(input map[string]interface{}, prefix []string) (map[string]string, map[string]interface{}) {
output := map[string]string{}
replaced := map[string]interface{}{}
for key, value := range input {
nestedKey := append(prefix, key)
nested, ok := value.(config.Variables)
nested, ok := value.(map[string]interface{})
if ok {
flattened, replacement := EV2Mapping(nested, nestedKey)
for index, what := range flattened {
Expand Down
3 changes: 2 additions & 1 deletion tooling/templatize/internal/ev2/mapping_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@ import (
"fmt"
"testing"

"github.com/Azure/ARO-HCP/tooling/templatize/internal/config"
"gotest.tools/assert"
)

func TestMapping(t *testing.T) {
testData := map[string]interface{}{
testData := config.Variables{
"key1": "value1",
"key2": 42,
"key3": true,
Expand Down
10 changes: 5 additions & 5 deletions tooling/templatize/testdata/config.yaml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
defaults:
tenantId: "72f988bf-86f1-41af-91ab-2d7cd011db47"
region_resourcegroup: hcp-underlay-{{ .ctx.region }}-{{ .ctx.regionStamp }}
region_eventgrid_namespace: maestro-eventgrid-{{ .ctx.region }}-{{ .ctx.regionStamp }}
region_resourcegroup: hcp-underlay-{{ .ctx.region }}-{{ .ctx.stamp }}
region_eventgrid_namespace: maestro-eventgrid-{{ .ctx.region }}-{{ .ctx.stamp }}
aks_name: aro-hcp-aks
maestro_msi: "maestro-server"
clouds:
Expand All @@ -12,9 +12,9 @@ clouds:
environments:
dev:
defaults:
region_resourcegroup: hcp-underlay-{{ .ctx.region }}-{{ .ctx.regionStamp }}
region_maestro_keyvault: maestro-kv-{{ .ctx.region }}-{{ .ctx.regionStamp }}
svc_resourcegroup: hcp-underlay-{{ .ctx.region }}-svc-{{ .ctx.regionStamp }}
region_resourcegroup: hcp-underlay-{{ .ctx.region }}-{{ .ctx.stamp }}
region_maestro_keyvault: maestro-kv-{{ .ctx.region }}-{{ .ctx.stamp }}
svc_resourcegroup: hcp-underlay-{{ .ctx.region }}-svc-{{ .ctx.stamp }}
maestro_helm_chart: ../maestro/deploy/helm/server
maestro_image: aro-hcp-dev.azurecr.io/maestro-server:the-new-one
int:
Expand Down
8 changes: 4 additions & 4 deletions tooling/templatize/testdata/zz_fixture_TestRawOptions.sh
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
# copy from maestro/Makefile#L14
deploy-server:
TENANT_ID="72f988bf-86f1-41af-91ab-2d7cd011db47"
REGION_RG="hcp-underlay-uksouth-1"
EVENTGRID_NS="maestro-eventgrid-uksouth-1"
MAESTRO_KV="maestro-kv-uksouth-1"
SERVICE_RG="hcp-underlay-uksouth-svc-1"
REGION_RG="hcp-underlay-uksouth-fghij"
EVENTGRID_NS="maestro-eventgrid-uksouth-fghij"
MAESTRO_KV="maestro-kv-uksouth-fghij"
SERVICE_RG="hcp-underlay-uksouth-svc-fghij"
AKS="aro-hcp-aks"
MAESTRO_MI="maestro-server"
HELM_CHART="../maestro/deploy/helm/server"
Expand Down

0 comments on commit 33671c1

Please sign in to comment.