Skip to content

Commit

Permalink
Refactoring template.RawYAMLBuilder properties
Browse files Browse the repository at this point in the history
  • Loading branch information
jlandowner committed May 28, 2024
1 parent 386485b commit 7e8c16f
Show file tree
Hide file tree
Showing 6 changed files with 20 additions and 81 deletions.
2 changes: 1 addition & 1 deletion internal/cmd/template/generate_workspace.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ func (o *generateWorkspaceOption) RunE(cmd *cobra.Command, args []string) error
}
o.Logr.Debug().Info(input)

unsts, err := template.NewRawYAMLBuilder(input, nil).Build()
unsts, err := template.NewRawYAMLBuilder(input).Build()
if err != nil {
return fmt.Errorf("failed to build template: %w", err)
}
Expand Down
2 changes: 1 addition & 1 deletion internal/cmd/template/generate_workspace_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -602,7 +602,7 @@ spec:
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
u, err := template.NewRawYAMLBuilder(tt.args.tmpl, nil).Build()
u, err := template.NewRawYAMLBuilder(tt.args.tmpl).Build()
if err != nil {
t.Errorf("dummyTemplateBuild() error = %v", err)
}
Expand Down
10 changes: 3 additions & 7 deletions internal/webhooks/workspace_webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ func (h *WorkspaceMutationWebhookHandler) mutateWorkspace(ctx context.Context, w
}

func (h *WorkspaceMutationWebhookHandler) migrateTmplServiceToNetworkRule(ctx context.Context, ws *cosmov1alpha1.Workspace, rawTmpl string, cfg cosmov1alpha1.Config) error {
unst, err := preTemplateBuild(*ws, rawTmpl)
unst, err := preTemplateBuild(rawTmpl)
if err != nil {
return err
}
Expand Down Expand Up @@ -134,12 +134,8 @@ func appendNetworkRuleIfNotExist(ws *cosmov1alpha1.Workspace, netRule cosmov1alp
ws.Spec.Network = append(ws.Spec.Network, netRule)
}

func preTemplateBuild(ws cosmov1alpha1.Workspace, rawTmpl string) ([]unstructured.Unstructured, error) {
var inst cosmov1alpha1.Instance
inst.SetName(ws.GetName())
inst.SetNamespace(ws.GetNamespace())

builder := template.NewRawYAMLBuilder(rawTmpl, &inst)
func preTemplateBuild(rawTmpl string) ([]unstructured.Unstructured, error) {
builder := template.NewRawYAMLBuilder(rawTmpl)
return builder.Build()
}

Expand Down
6 changes: 3 additions & 3 deletions pkg/template/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ type Builder interface {

func BuildObjects(tmplSpec cosmov1alpha1.TemplateSpec, inst cosmov1alpha1.InstanceObject) (objects []unstructured.Unstructured, err error) {
if tmplSpec.RawYaml != "" {
objects, err = NewRawYAMLBuilder(tmplSpec.RawYaml, inst).
ReplaceDefaultVars().
ReplaceCustomVars().
objects, err = NewRawYAMLBuilder(tmplSpec.RawYaml).
ReplaceDefaultVars(inst).
ReplaceCustomVars(inst).
Build()
if err != nil {
return nil, err
Expand Down
20 changes: 9 additions & 11 deletions pkg/template/rawyaml.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,11 @@ var (

type RawYAMLBuilder struct {
rawYaml string
inst cosmov1alpha1.InstanceObject
}

func NewRawYAMLBuilder(rawYaml string, inst cosmov1alpha1.InstanceObject) *RawYAMLBuilder {
func NewRawYAMLBuilder(rawYaml string) *RawYAMLBuilder {
return &RawYAMLBuilder{
rawYaml: rawYaml,
inst: inst,
}
}

Expand All @@ -49,19 +47,19 @@ func (t *RawYAMLBuilder) Build() ([]unstructured.Unstructured, error) {
return resources, nil
}

func (t *RawYAMLBuilder) ReplaceDefaultVars() *RawYAMLBuilder {
t.rawYaml = strings.ReplaceAll(t.rawYaml, DefaultVarsInstance, t.inst.GetName())
t.rawYaml = strings.ReplaceAll(t.rawYaml, DefaultVarsTemplate, t.inst.GetSpec().Template.Name)
func (t *RawYAMLBuilder) ReplaceDefaultVars(inst cosmov1alpha1.InstanceObject) *RawYAMLBuilder {
t.rawYaml = strings.ReplaceAll(t.rawYaml, DefaultVarsInstance, inst.GetName())
t.rawYaml = strings.ReplaceAll(t.rawYaml, DefaultVarsTemplate, inst.GetSpec().Template.Name)

if t.inst.GetScope() == meta.RESTScopeNamespace {
t.rawYaml = strings.ReplaceAll(t.rawYaml, DefaultVarsNamespace, t.inst.GetNamespace())
if inst.GetScope() == meta.RESTScopeNamespace {
t.rawYaml = strings.ReplaceAll(t.rawYaml, DefaultVarsNamespace, inst.GetNamespace())
}
return t
}

func (t *RawYAMLBuilder) ReplaceCustomVars() *RawYAMLBuilder {
if t.inst.GetSpec().Vars != nil {
for key, val := range t.inst.GetSpec().Vars {
func (t *RawYAMLBuilder) ReplaceCustomVars(inst cosmov1alpha1.InstanceObject) *RawYAMLBuilder {
if inst.GetSpec().Vars != nil {
for key, val := range inst.GetSpec().Vars {
key = FixupTemplateVarKey(key)
t.rawYaml = strings.ReplaceAll(t.rawYaml, key, val)
}
Expand Down
61 changes: 3 additions & 58 deletions pkg/template/rawyaml_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,25 +77,12 @@ metadata:
namespace: default
spec:
hello: world`,
inst: &cosmov1alpha1.Instance{
ObjectMeta: metav1.ObjectMeta{
Name: "cs1",
Namespace: "cosmo-user-tom",
},
Spec: cosmov1alpha1.InstanceSpec{
Template: cosmov1alpha1.TemplateRef{
Name: "code-server",
},
Override: cosmov1alpha1.OverrideSpec{},
Vars: map[string]string{"{{TEST}}": "OK"},
},
},
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := NewRawYAMLBuilder(tt.args.data, tt.args.inst); !reflect.DeepEqual(got, tt.want) {
if got := NewRawYAMLBuilder(tt.args.data); !reflect.DeepEqual(got, tt.want) {
t.Errorf("NewRawYAMLBuilder() = %v, want %v", got, tt.want)
}
})
Expand Down Expand Up @@ -315,7 +302,6 @@ data:
t.Run(tt.name, func(t *testing.T) {
tr := &RawYAMLBuilder{
rawYaml: tt.fields.data,
inst: tt.fields.inst,
}
got, err := tr.Build()
if (err != nil) != tt.wantErr {
Expand Down Expand Up @@ -359,19 +345,6 @@ func TestRawYAMLBuilder_ReplaceDefaultVars(t *testing.T) {
},
want: &RawYAMLBuilder{
rawYaml: "cs1-cosmo-user-tom-code-server",
inst: &cosmov1alpha1.Instance{
ObjectMeta: metav1.ObjectMeta{
Name: "cs1",
Namespace: "cosmo-user-tom",
},
Spec: cosmov1alpha1.InstanceSpec{
Template: cosmov1alpha1.TemplateRef{
Name: "code-server",
},
Override: cosmov1alpha1.OverrideSpec{},
Vars: map[string]string{"{{TEST}}": "OK"},
},
},
},
},
{
Expand All @@ -394,29 +367,15 @@ func TestRawYAMLBuilder_ReplaceDefaultVars(t *testing.T) {
},
want: &RawYAMLBuilder{
rawYaml: "cs1-cosmo-user-tom-code-server",
inst: &cosmov1alpha1.Instance{
ObjectMeta: metav1.ObjectMeta{
Name: "cs1",
Namespace: "cosmo-user-tom",
},
Spec: cosmov1alpha1.InstanceSpec{
Template: cosmov1alpha1.TemplateRef{
Name: "code-server",
},
Override: cosmov1alpha1.OverrideSpec{},
Vars: map[string]string{"TEST": "OK"},
},
},
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
tr := &RawYAMLBuilder{
rawYaml: tt.fields.rawYaml,
inst: tt.fields.inst,
}
if got := tr.ReplaceDefaultVars(); !reflect.DeepEqual(got, tt.want) {
if got := tr.ReplaceDefaultVars(tt.fields.inst); !reflect.DeepEqual(got, tt.want) {
t.Errorf("RawYAMLBuilder.ReplaceDefaultVars() = %v, want %v", got, tt.want)
}
})
Expand Down Expand Up @@ -453,29 +412,15 @@ func TestRawYAMLBuilder_ReplaceCustomVars(t *testing.T) {
},
want: &RawYAMLBuilder{
rawYaml: "{{INSTANCE}}-{{NAMESPACE}}-{{TEMPLATE}}-OK",
inst: &cosmov1alpha1.Instance{
ObjectMeta: metav1.ObjectMeta{
Name: "cs1",
Namespace: "cosmo-user-tom",
},
Spec: cosmov1alpha1.InstanceSpec{
Template: cosmov1alpha1.TemplateRef{
Name: "code-server",
},
Override: cosmov1alpha1.OverrideSpec{},
Vars: map[string]string{"{{TEST}}": "OK"},
},
},
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
tr := &RawYAMLBuilder{
rawYaml: tt.fields.rawYaml,
inst: tt.fields.inst,
}
if got := tr.ReplaceCustomVars(); !reflect.DeepEqual(got, tt.want) {
if got := tr.ReplaceCustomVars(tt.fields.inst); !reflect.DeepEqual(got, tt.want) {
t.Errorf("RawYAMLBuilder.ReplaceCustomVars() = %v, want %v", got, tt.want)
}
})
Expand Down

0 comments on commit 7e8c16f

Please sign in to comment.