Skip to content

Commit

Permalink
remove RG parsing magic func
Browse files Browse the repository at this point in the history
Signed-off-by: Gerd Oberlechner <[email protected]>
  • Loading branch information
geoberle committed Dec 18, 2024
1 parent f5d3ed6 commit ae92c51
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 40 deletions.
5 changes: 2 additions & 3 deletions tooling/templatize/pkg/pipeline/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,15 @@ import (
)

func (p *Pipeline) DeepCopy(newPipelineFilePath string) (*Pipeline, error) {
copy := new(Pipeline)
data, err := yaml.Marshal(p)
if err != nil {
return nil, fmt.Errorf("failed to marshal pipeline: %v", err)
}
err = yaml.Unmarshal(data, copy)

copy, err := NewPlainPipelineFromBytes(newPipelineFilePath, data)
if err != nil {
return nil, fmt.Errorf("failed to unmarshal pipeline: %v", err)
}
copy.pipelineFilePath = newPipelineFilePath
return copy, nil
}

Expand Down
7 changes: 1 addition & 6 deletions tooling/templatize/pkg/pipeline/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ import (
"os"
"path/filepath"

"gopkg.in/yaml.v3"

"github.com/go-logr/logr"

"github.com/Azure/ARO-HCP/tooling/templatize/pkg/config"
Expand All @@ -29,10 +27,7 @@ func NewPipelineFromFile(pipelineFilePath string, vars config.Variables) (*Pipel
return nil, fmt.Errorf("failed to get absolute path for pipeline file %q: %w", pipelineFilePath, err)
}

pipeline := &Pipeline{
pipelineFilePath: absPath,
}
err = yaml.Unmarshal(bytes, pipeline)
pipeline, err := NewPlainPipelineFromBytes(absPath, bytes)
if err != nil {
return nil, fmt.Errorf("failed to unmarshal pipeline file %w", err)
}
Expand Down
79 changes: 48 additions & 31 deletions tooling/templatize/pkg/pipeline/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,41 +24,58 @@ type ResourceGroup struct {
Steps []Step `yaml:"steps"`
}

func (rg *ResourceGroup) UnmarshalYAML(unmarshal func(interface{}) error) error {
rawRg := &struct {
Name string `yaml:"name"`
Subscription string `yaml:"subscription"`
AKSCluster string `yaml:"aksCluster,omitempty"`
Steps []any `yaml:"steps"`
func NewPlainPipelineFromBytes(filepath string, bytes []byte) (*Pipeline, error) {
rawPipeline := &struct {
ServiceGroup string `yaml:"serviceGroup"`
RolloutName string `yaml:"rolloutName"`
ResourceGroups []struct {
Name string `yaml:"name"`
Subscription string `yaml:"subscription"`
AKSCluster string `yaml:"aksCluster,omitempty"`
Steps []any `yaml:"steps"`
} `yaml:"resourceGroups"`
}{}
if err := unmarshal(&rawRg); err != nil {
return err
err := yaml.Unmarshal(bytes, rawPipeline)
if err != nil {
return nil, err
}
rg.Name = rawRg.Name
rg.Subscription = rawRg.Subscription
rg.AKSCluster = rawRg.AKSCluster
rg.Steps = make([]Step, len(rawRg.Steps))
for i, rawStep := range rawRg.Steps {
// unmarshal the map into a StepMeta
stepMeta := &StepMeta{}
err := mapToStruct(rawStep, stepMeta)
if err != nil {
return err
}
switch stepMeta.Action {
case "Shell":
rg.Steps[i] = &ShellStep{}
case "ARM":
rg.Steps[i] = &ARMStep{}
default:
return fmt.Errorf("unknown action type %s", stepMeta.Action)
}
err = mapToStruct(rawStep, rg.Steps[i])
if err != nil {
return err
pipeline := &Pipeline{
pipelineFilePath: filepath,
ServiceGroup: rawPipeline.ServiceGroup,
RolloutName: rawPipeline.RolloutName,
ResourceGroups: make([]*ResourceGroup, len(rawPipeline.ResourceGroups)),
}

for i, rawRg := range rawPipeline.ResourceGroups {
rg := &ResourceGroup{}
pipeline.ResourceGroups[i] = rg
rg.Name = rawRg.Name
rg.Subscription = rawRg.Subscription
rg.AKSCluster = rawRg.AKSCluster
rg.Steps = make([]Step, len(rawRg.Steps))
for i, rawStep := range rawRg.Steps {
// unmarshal the map into a StepMeta
stepMeta := &StepMeta{}
err := mapToStruct(rawStep, stepMeta)
if err != nil {
return nil, err
}
switch stepMeta.Action {
case "Shell":
rg.Steps[i] = &ShellStep{}
case "ARM":
rg.Steps[i] = &ARMStep{}
default:
return nil, fmt.Errorf("unknown action type %s", stepMeta.Action)
}
err = mapToStruct(rawStep, rg.Steps[i])
if err != nil {
return nil, err
}
}
}
return nil

return pipeline, nil
}

func mapToStruct(m any, s interface{}) error {
Expand Down

0 comments on commit ae92c51

Please sign in to comment.