Skip to content

Commit

Permalink
Merge pull request #1003 from aFlyBird0/fix-kubectl
Browse files Browse the repository at this point in the history
fix: forget to render kubectl options
  • Loading branch information
iyear authored Aug 18, 2022
2 parents 1237ab8 + ab0573c commit db8ed52
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 13 deletions.
22 changes: 16 additions & 6 deletions internal/pkg/plugininstaller/kubectl/installer.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
54 changes: 47 additions & 7 deletions internal/pkg/plugininstaller/kubectl/installer_test.go
Original file line number Diff line number Diff line change
@@ -1,18 +1,58 @@
package kubectl_test
package kubectl

import (
"fmt"
"io"
"net/http"

. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
"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
Expand All @@ -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())
})
Expand Down

0 comments on commit db8ed52

Please sign in to comment.