From 7e8c16f3d2055f218a0d5f4b6956e0575dc120a0 Mon Sep 17 00:00:00 2001 From: jlandowner Date: Mon, 27 May 2024 08:24:44 +0900 Subject: [PATCH] Refactoring template.RawYAMLBuilder properties --- internal/cmd/template/generate_workspace.go | 2 +- .../cmd/template/generate_workspace_test.go | 2 +- internal/webhooks/workspace_webhook.go | 10 +-- pkg/template/builder.go | 6 +- pkg/template/rawyaml.go | 20 +++--- pkg/template/rawyaml_test.go | 61 +------------------ 6 files changed, 20 insertions(+), 81 deletions(-) diff --git a/internal/cmd/template/generate_workspace.go b/internal/cmd/template/generate_workspace.go index a7e2c0d0..083f086b 100644 --- a/internal/cmd/template/generate_workspace.go +++ b/internal/cmd/template/generate_workspace.go @@ -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) } diff --git a/internal/cmd/template/generate_workspace_test.go b/internal/cmd/template/generate_workspace_test.go index 991eaa2d..67e22f49 100644 --- a/internal/cmd/template/generate_workspace_test.go +++ b/internal/cmd/template/generate_workspace_test.go @@ -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) } diff --git a/internal/webhooks/workspace_webhook.go b/internal/webhooks/workspace_webhook.go index b06e8460..5e4e7627 100644 --- a/internal/webhooks/workspace_webhook.go +++ b/internal/webhooks/workspace_webhook.go @@ -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 } @@ -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() } diff --git a/pkg/template/builder.go b/pkg/template/builder.go index ffe75988..8e0ff39c 100644 --- a/pkg/template/builder.go +++ b/pkg/template/builder.go @@ -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 diff --git a/pkg/template/rawyaml.go b/pkg/template/rawyaml.go index bf30180e..4aab3ac1 100644 --- a/pkg/template/rawyaml.go +++ b/pkg/template/rawyaml.go @@ -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, } } @@ -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) } diff --git a/pkg/template/rawyaml_test.go b/pkg/template/rawyaml_test.go index 72c81e2e..9ae33f08 100644 --- a/pkg/template/rawyaml_test.go +++ b/pkg/template/rawyaml_test.go @@ -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) } }) @@ -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 { @@ -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"}, - }, - }, }, }, { @@ -394,19 +367,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"}, - }, - }, }, }, } @@ -414,9 +374,8 @@ func TestRawYAMLBuilder_ReplaceDefaultVars(t *testing.T) { 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) } }) @@ -453,19 +412,6 @@ 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"}, - }, - }, }, }, } @@ -473,9 +419,8 @@ func TestRawYAMLBuilder_ReplaceCustomVars(t *testing.T) { 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) } })