From 666c22e6b571e9e81a2c0a862d023ded1e2e52c4 Mon Sep 17 00:00:00 2001 From: Alexander Noskov Date: Tue, 4 Aug 2020 14:24:16 -0500 Subject: [PATCH] Split kubectl apply method into two methods In some cases, we need to execute kubectl apply against raw YAML instead of the document. This patchset provides the ability to call ApplyDocs for Documents and ApplyYaml for YAML. Relates-To: #268 Change-Id: I4b61d4358ecf5b739b9957a009458ef51142c41f --- pkg/k8s/kubectl/interfaces.go | 3 ++- pkg/k8s/kubectl/kubectl.go | 28 ++++++++++++++++++++-------- pkg/k8s/kubectl/kubectl_test.go | 2 +- 3 files changed, 23 insertions(+), 10 deletions(-) diff --git a/pkg/k8s/kubectl/interfaces.go b/pkg/k8s/kubectl/interfaces.go index 28649f2ac..3e1dda550 100644 --- a/pkg/k8s/kubectl/interfaces.go +++ b/pkg/k8s/kubectl/interfaces.go @@ -23,6 +23,7 @@ import ( // Interface provides a abstraction layer built on top of kubectl libraries // to implement kubectl subcommands as kubectl apply type Interface interface { - Apply(docs []document.Document, ao *ApplyOptions) error + ApplyDocs(docs []document.Document, ao *ApplyOptions) error + ApplyYaml(yaml []byte, ao *ApplyOptions) error ApplyOptions() (*ApplyOptions, error) } diff --git a/pkg/k8s/kubectl/kubectl.go b/pkg/k8s/kubectl/kubectl.go index a41f5f30f..545ce1358 100644 --- a/pkg/k8s/kubectl/kubectl.go +++ b/pkg/k8s/kubectl/kubectl.go @@ -15,6 +15,7 @@ package kubectl import ( + "bytes" "os" "k8s.io/cli-runtime/pkg/genericclioptions" @@ -56,8 +57,21 @@ func (kubectl *Kubectl) WithBufferDir(bd string) *Kubectl { return kubectl } -// Apply is abstraction to kubectl apply command -func (kubectl *Kubectl) Apply(docs []document.Document, ao *ApplyOptions) error { +// ApplyDocs transform Document into YAML +// TODO: (anoskov) Add unit test for ApplyDocs, as of now it is calling ApplyYaml which meets the coverage test. +func (kubectl *Kubectl) ApplyDocs(docs []document.Document, ao *ApplyOptions) error { + buf := bytes.NewBuffer([]byte{}) + for _, doc := range docs { + err := utilyaml.WriteOut(buf, doc) + if err != nil { + return err + } + } + return kubectl.ApplyYaml(buf.Bytes(), ao) +} + +// ApplyYaml is abstraction to kubectl apply command +func (kubectl *Kubectl) ApplyYaml(yaml []byte, ao *ApplyOptions) error { tf, err := kubectl.TempFile(kubectl.bufferDir, "initinfra") if err != nil { return err @@ -70,13 +84,11 @@ func (kubectl *Kubectl) Apply(docs []document.Document, ao *ApplyOptions) error log.Fatalf("Failed to cleanup temporary file %s during kubectl apply", fName) } }(tf) + defer tf.Close() - for _, doc := range docs { - // Write out documents to temporary file - err = utilyaml.WriteOut(tf, doc) - if err != nil { - return err - } + _, err = tf.Write(yaml) + if err != nil { + return err } ao.SetSourceFiles([]string{tf.Name()}) return ao.Run() diff --git a/pkg/k8s/kubectl/kubectl_test.go b/pkg/k8s/kubectl/kubectl_test.go index d58967da3..8ed155674 100644 --- a/pkg/k8s/kubectl/kubectl_test.go +++ b/pkg/k8s/kubectl/kubectl_test.go @@ -108,6 +108,6 @@ func TestApply(t *testing.T) { } for _, test := range tests { kctl.FileSystem = test.fs - assert.Equal(t, kctl.Apply(docs, ao), test.expectedErr) + assert.Equal(t, kctl.ApplyDocs(docs, ao), test.expectedErr) } }