Skip to content

Commit 80866f9

Browse files
authored
Merge pull request #4728 from Azure/templatize-config-render-settings
templatize: support settings.yaml for config rendering
2 parents e190ab6 + 6ff85bd commit 80866f9

File tree

6 files changed

+62
-25
lines changed

6 files changed

+62
-25
lines changed

tooling/templatize/cmd/configuration/explain/options.go

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ package explain
1717
import (
1818
"context"
1919
"fmt"
20-
"strconv"
2120

2221
"github.com/spf13/cobra"
2322

@@ -27,7 +26,7 @@ import (
2726

2827
func DefaultOptions() *RawOptions {
2928
return &RawOptions{
30-
Stamp: 1,
29+
Stamp: "1",
3130
}
3231
}
3332

@@ -38,7 +37,7 @@ func BindOptions(opts *RawOptions, cmd *cobra.Command) error {
3837
cmd.Flags().StringVar(&opts.Region, "region", opts.Region, "The name of the region to explain in.")
3938
cmd.Flags().StringVar(&opts.Ev2Cloud, "ev2-cloud", opts.Ev2Cloud, "Cloud to use for Ev2 configuration, useful for dev mode explanations.")
4039
cmd.Flags().StringVar(&opts.RegionShortSuffix, "region-short-suffix", opts.RegionShortSuffix, "Suffix to use for region short-name, useful for dev mode explanations.")
41-
cmd.Flags().IntVar(&opts.Stamp, "stamp", opts.Stamp, "Stamp value to use, useful for dev mode explanations.")
40+
cmd.Flags().StringVar(&opts.Stamp, "stamp", opts.Stamp, "Stamp value to use, useful for dev mode explanations.")
4241

4342
cmd.Flags().StringVar(&opts.Path, "path", opts.Path, "Path to the value needing explanation.")
4443

@@ -61,7 +60,7 @@ type RawOptions struct {
6160
Ev2Cloud string
6261
RegionShortOverride string
6362
RegionShortSuffix string
64-
Stamp int
63+
Stamp string
6564

6665
Path string
6766
}
@@ -85,7 +84,7 @@ type completedOptions struct {
8584
Ev2Cloud string
8685
RegionShortOverride string
8786
RegionShortSuffix string
88-
Stamp int
87+
Stamp string
8988
Path string
9089
}
9190

@@ -103,7 +102,7 @@ func (o *RawOptions) Validate() (*ValidatedOptions, error) {
103102
{flag: "service-config-file", name: "service configuration file", value: &o.ServiceConfigFile},
104103
{flag: "cloud", name: "cloud", value: &o.Cloud},
105104
{flag: "environment", name: "environment", value: &o.Environment},
106-
{flag: "region", name: "region", value: &o.Environment},
105+
{flag: "region", name: "region", value: &o.Region},
107106
{flag: "path", name: "path", value: &o.Path},
108107
} {
109108
if item.value == nil || *item.value == "" {
@@ -152,7 +151,7 @@ func (opts *Options) ExplainConfiguration(ctx context.Context) error {
152151
RegionReplacement: opts.Region,
153152
CloudReplacement: opts.Cloud,
154153
EnvironmentReplacement: opts.Environment,
155-
StampReplacement: strconv.Itoa(opts.Stamp),
154+
StampReplacement: opts.Stamp,
156155
Ev2Config: ev2Cfg,
157156
}
158157
for key, into := range map[string]*string{

tooling/templatize/cmd/configuration/render/cmd.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ func NewCommand() (*cobra.Command, error) {
3838
}
3939

4040
func runInspect(ctx context.Context, opts *RawOptions) error {
41-
validated, err := opts.Validate()
41+
validated, err := opts.Validate(ctx)
4242
if err != nil {
4343
return err
4444
}

tooling/templatize/cmd/configuration/render/options.go

Lines changed: 48 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ import (
2020
"io"
2121
"os"
2222
"path/filepath"
23-
"strconv"
2423

2524
"github.com/spf13/cobra"
2625

@@ -29,11 +28,13 @@ import (
2928
"github.com/Azure/ARO-Tools/config"
3029
"github.com/Azure/ARO-Tools/config/ev2config"
3130
"github.com/Azure/ARO-Tools/config/types"
31+
32+
"github.com/Azure/ARO-HCP/tooling/templatize/pkg/settings"
3233
)
3334

3435
func DefaultOptions() *RawOptions {
3536
return &RawOptions{
36-
Stamp: 1,
37+
Stamp: "1",
3738
Output: "-",
3839
}
3940
}
@@ -47,12 +48,14 @@ func BindOptions(opts *RawOptions, cmd *cobra.Command) error {
4748
cmd.Flags().StringVar(&opts.Output, "output", opts.Output, "Output file to render to. Set to '-' for stdout.")
4849
cmd.Flags().StringVar(&opts.Ev2Cloud, "ev2-cloud", opts.Ev2Cloud, "Cloud to use for Ev2 configuration, useful for dev mode rendering.")
4950
cmd.Flags().StringVar(&opts.RegionShortSuffix, "region-short-suffix", opts.RegionShortSuffix, "Suffix to use for region short-name, useful for dev mode rendering.")
50-
cmd.Flags().IntVar(&opts.Stamp, "stamp", opts.Stamp, "Stamp value to use, useful for dev mode rendering.")
51+
cmd.Flags().StringVar(&opts.Stamp, "stamp", opts.Stamp, "Stamp value to use, useful for dev mode rendering.")
5152
cmd.Flags().BoolVar(&opts.SkipSchemaValidation, "skip-schema-validation", opts.SkipSchemaValidation, "Skip JSON schema validation of the rendered config.")
53+
cmd.Flags().StringVar(&opts.DevSettingsFile, "dev-settings-file", opts.DevSettingsFile, "File to load environment details from. Uses --cloud and --environment to resolve defaults.")
5254

5355
for _, flag := range []string{
5456
"service-config-file",
5557
"config-file-override",
58+
"dev-settings-file",
5659
} {
5760
if err := cmd.MarkFlagFilename(flag); err != nil {
5861
return fmt.Errorf("failed to mark flag %q as a file: %w", flag, err)
@@ -71,9 +74,11 @@ type RawOptions struct {
7174
Ev2Cloud string
7275
RegionShortOverride string
7376
RegionShortSuffix string
74-
Stamp int
77+
Stamp string
7578
Output string
7679
SkipSchemaValidation bool
80+
81+
DevSettingsFile string
7782
}
7883

7984
// validatedOptions is a private wrapper that enforces a call of Validate() before Complete() can be invoked.
@@ -95,7 +100,7 @@ type completedOptions struct {
95100
Ev2Cloud string
96101
RegionShortOverride string
97102
RegionShortSuffix string
98-
Stamp int
103+
Stamp string
99104
Output io.WriteCloser
100105
SkipSchemaValidation bool
101106
}
@@ -105,7 +110,42 @@ type Options struct {
105110
*completedOptions
106111
}
107112

108-
func (o *RawOptions) Validate() (*ValidatedOptions, error) {
113+
func (o *RawOptions) Validate(ctx context.Context) (*ValidatedOptions, error) {
114+
if o.DevSettingsFile != "" {
115+
if o.Cloud == "" {
116+
return nil, fmt.Errorf("provide the cloud with --cloud when using --dev-settings-file")
117+
}
118+
if o.Environment == "" {
119+
return nil, fmt.Errorf("provide the environment with --environment when using --dev-settings-file")
120+
}
121+
122+
devSettings, err := settings.Load(o.DevSettingsFile)
123+
if err != nil {
124+
return nil, fmt.Errorf("failed to load developer settings: %w", err)
125+
}
126+
127+
env, err := devSettings.Resolve(ctx, o.Cloud, o.Environment)
128+
if err != nil {
129+
return nil, fmt.Errorf("failed to resolve environment %s in cloud %s: %w", o.Environment, o.Cloud, err)
130+
}
131+
132+
if o.Region == "" {
133+
o.Region = env.Region
134+
}
135+
if o.Ev2Cloud == "" {
136+
o.Ev2Cloud = env.Ev2Cloud
137+
}
138+
if o.RegionShortSuffix == "" {
139+
o.RegionShortSuffix = env.RegionShortSuffix
140+
}
141+
if o.RegionShortOverride == "" {
142+
o.RegionShortOverride = env.RegionShortOverride
143+
}
144+
if o.Stamp == "" {
145+
o.Stamp = env.Stamp
146+
}
147+
}
148+
109149
for _, item := range []struct {
110150
flag string
111151
name string
@@ -114,7 +154,7 @@ func (o *RawOptions) Validate() (*ValidatedOptions, error) {
114154
{flag: "service-config-file", name: "service configuration file", value: &o.ServiceConfigFile},
115155
{flag: "cloud", name: "cloud", value: &o.Cloud},
116156
{flag: "environment", name: "environment", value: &o.Environment},
117-
{flag: "region", name: "region", value: &o.Environment},
157+
{flag: "region", name: "region", value: &o.Region},
118158
{flag: "output", name: "output destination", value: &o.Output},
119159
} {
120160
if item.value == nil || *item.value == "" {
@@ -191,7 +231,7 @@ func (opts *Options) RenderServiceConfig(ctx context.Context) error {
191231
RegionReplacement: opts.Region,
192232
CloudReplacement: opts.Cloud,
193233
EnvironmentReplacement: opts.Environment,
194-
StampReplacement: strconv.Itoa(opts.Stamp),
234+
StampReplacement: opts.Stamp,
195235
Ev2Config: ev2Cfg,
196236
}
197237
for key, into := range map[string]*string{

tooling/templatize/cmd/configuration/validate/options.go

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ import (
2323
"os"
2424
"os/exec"
2525
"path/filepath"
26-
"strconv"
2726
"strings"
2827

2928
"github.com/go-logr/logr"
@@ -189,7 +188,7 @@ func (opts *Options) ValidateServiceConfig(ctx context.Context) error {
189188
for _, region := range ev2Contexts[cloud] {
190189
contexts[cloud][env] = append(contexts[cloud][env], RegionContext{
191190
Region: region,
192-
Stamp: 999, // we want to validate that all of our names still work, even with large stamp numbers
191+
Stamp: "99a", // we want to validate that all of our names still work, even with large stamps
193192
})
194193
}
195194
}
@@ -248,7 +247,7 @@ type RegionContext struct {
248247
Ev2Cloud string
249248
RegionShortOverride string
250249
RegionShortSuffix string
251-
Stamp int
250+
Stamp string
252251
}
253252

254253
func ValidateServiceConfig(
@@ -309,7 +308,7 @@ func ValidateServiceConfig(
309308
RegionReplacement: region,
310309
CloudReplacement: cloud,
311310
EnvironmentReplacement: environment,
312-
StampReplacement: strconv.Itoa(regionCtx.Stamp),
311+
StampReplacement: regionCtx.Stamp,
313312
Ev2Config: ev2Cfg,
314313
}
315314
for key, into := range map[string]*string{
@@ -534,7 +533,7 @@ func renderDiff(
534533
Stamp: regionCtx.Stamp,
535534
Output: previousConfig,
536535
}
537-
validated, err := opts.Validate()
536+
validated, err := opts.Validate(ctx)
538537
if err != nil {
539538
return fmt.Errorf("failed to validate render options: %w", err)
540539
}

tooling/templatize/cmd/rolloutoptions.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ package options
1717
import (
1818
"context"
1919
"fmt"
20-
"strconv"
2120

2221
"github.com/go-logr/logr"
2322
"github.com/spf13/cobra"
@@ -157,7 +156,7 @@ func (o *RawRolloutOptions) Validate(ctx context.Context) (*ValidatedRolloutOpti
157156
o.Region = region
158157
o.RegionShortSuffix = env.RegionShortSuffix
159158
o.RegionShortOverride = env.RegionShortOverride
160-
o.Stamp = strconv.Itoa(env.Stamp)
159+
o.Stamp = env.Stamp
161160
subscriptions = devSettings.Subscriptions
162161
}
163162

tooling/templatize/pkg/settings/settings.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ type Parameters struct {
4444
// Region is the region for which this environment is rendered.
4545
Region string `json:"region"`
4646
// CxStamp is the stamp for which this environment is rendered.
47-
CxStamp int `json:"cxStamp"`
47+
CxStamp string `json:"cxStamp"`
4848
// RegionShortOverride is a shell-ism that, when run through `echo`, outputs a replacement for the short region variable from the EV2 central config.
4949
RegionShortOverride string `json:"regionShortOverride"`
5050
// RegionShortSuffix is a shell-ism that, when run through `echo`, outputs a suffix for the short region variable.
@@ -66,7 +66,7 @@ type EnvironmentParameters struct {
6666
Ev2Cloud string
6767
Environment string
6868
Region string
69-
Stamp int
69+
Stamp string
7070
RegionShortOverride string
7171
RegionShortSuffix string
7272
}

0 commit comments

Comments
 (0)