diff --git a/internal/pkg/plugininstaller/kubectl/installer.go b/internal/pkg/plugininstaller/kubectl/installer.go index 48da6865a..9296c17ba 100644 --- a/internal/pkg/plugininstaller/kubectl/installer.go +++ b/internal/pkg/plugininstaller/kubectl/installer.go @@ -12,20 +12,30 @@ import ( func ProcessByContent(action, content string) plugininstaller.BaseOperation { return func(options plugininstaller.RawOptions) error { - if content == "" { - return fmt.Errorf("kubectl content is empty") + reader, err := renderKubectlContent(content, options) + if err != nil { + return err } - reader := strings.NewReader(content) - return processByIOReader(action, reader) } } +func renderKubectlContent(content string, options plugininstaller.RawOptions) (io.Reader, error) { + content, err := template.New().FromContent(content).SetDefaultRender("kubectl", options).Render() + if err != nil { + return nil, err + } + if content == "" { + return nil, fmt.Errorf("kubectl content is empty") + } + + return strings.NewReader(content), nil +} + func ProcessByURL(action, url string) plugininstaller.BaseOperation { return func(options plugininstaller.RawOptions) error { - var err error - content, err := template.New().FromURL(url).String() + content, err := template.New().FromURL(url).SetDefaultRender("kubectl", options).Render() if err != nil { return err } diff --git a/internal/pkg/plugininstaller/kubectl/installer_test.go b/internal/pkg/plugininstaller/kubectl/installer_test.go index 3ed15181c..5980c8aef 100644 --- a/internal/pkg/plugininstaller/kubectl/installer_test.go +++ b/internal/pkg/plugininstaller/kubectl/installer_test.go @@ -1,7 +1,8 @@ -package kubectl_test +package kubectl import ( "fmt" + "io" "net/http" . "github.com/onsi/ginkgo/v2" @@ -9,10 +10,49 @@ import ( "github.com/onsi/gomega/ghttp" "github.com/devstream-io/devstream/internal/pkg/plugininstaller" - "github.com/devstream-io/devstream/internal/pkg/plugininstaller/kubectl" utilKubectl "github.com/devstream-io/devstream/pkg/util/kubectl" ) +var _ = Describe("renderKubectlContent", func() { + var ( + content string + options plugininstaller.RawOptions + ) + + BeforeEach(func() { + content = `metadata: + name: "[[ .app.name ]]" + namespace: "[[ .app.namespace ]]" + finalizers: + - resources-finalizer.argocd.argoproj.io +` + options = map[string]interface{}{ + "app": map[string]interface{}{ + "name": "app-name", + "namespace": "app-namespace", + }, + } + }) + + It("should render kubectl content", func() { + + contentExpected := `metadata: + name: "app-name" + namespace: "app-namespace" + finalizers: + - resources-finalizer.argocd.argoproj.io +` + reader, err := renderKubectlContent(content, options) + Expect(err).To(Succeed()) + + bytes, err := io.ReadAll(reader) + + Expect(err).To(Succeed()) + Expect(string(bytes)).To(Equal(contentExpected)) + }) + +}) + var _ = Describe("ProcessByContent", Ordered, func() { var options plugininstaller.RawOptions var s *ghttp.Server @@ -27,27 +67,27 @@ var _ = Describe("ProcessByContent", Ordered, func() { s.Close() }) It("should return error if content is empty", func() { - op := kubectl.ProcessByContent(utilKubectl.Apply, "") + op := ProcessByContent(utilKubectl.Apply, "") err := op(options) Expect(err).To(HaveOccurred()) }) It("action is kubectl apply", func() { - op := kubectl.ProcessByURL(utilKubectl.Apply, s.URL()) + op := ProcessByURL(utilKubectl.Apply, s.URL()) err := op(options) Expect(err).To(HaveOccurred()) }) It("action is kubectl create", func() { - op := kubectl.ProcessByURL(utilKubectl.Create, s.URL()) + op := ProcessByURL(utilKubectl.Create, s.URL()) err := op(options) Expect(err).To(HaveOccurred()) }) It("action is kubectl delete", func() { - op := kubectl.ProcessByURL(utilKubectl.Delete, s.URL()) + op := ProcessByURL(utilKubectl.Delete, s.URL()) err := op(options) Expect(err).To(HaveOccurred()) }) It("action is not support", func() { - op := kubectl.ProcessByURL("", s.URL()) + op := ProcessByURL("", s.URL()) err := op(options) Expect(err).To(HaveOccurred()) })