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
This commit is contained in:
Alexander Noskov 2020-08-04 14:24:16 -05:00
parent 53e4f7ffc6
commit 666c22e6b5
3 changed files with 23 additions and 10 deletions

View File

@ -23,6 +23,7 @@ import (
// Interface provides a abstraction layer built on top of kubectl libraries // Interface provides a abstraction layer built on top of kubectl libraries
// to implement kubectl subcommands as kubectl apply // to implement kubectl subcommands as kubectl apply
type Interface interface { 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) ApplyOptions() (*ApplyOptions, error)
} }

View File

@ -15,6 +15,7 @@
package kubectl package kubectl
import ( import (
"bytes"
"os" "os"
"k8s.io/cli-runtime/pkg/genericclioptions" "k8s.io/cli-runtime/pkg/genericclioptions"
@ -56,8 +57,21 @@ func (kubectl *Kubectl) WithBufferDir(bd string) *Kubectl {
return kubectl return kubectl
} }
// Apply is abstraction to kubectl apply command // ApplyDocs transform Document into YAML
func (kubectl *Kubectl) Apply(docs []document.Document, ao *ApplyOptions) error { // 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") tf, err := kubectl.TempFile(kubectl.bufferDir, "initinfra")
if err != nil { if err != nil {
return err 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) log.Fatalf("Failed to cleanup temporary file %s during kubectl apply", fName)
} }
}(tf) }(tf)
defer tf.Close() defer tf.Close()
for _, doc := range docs { _, err = tf.Write(yaml)
// Write out documents to temporary file if err != nil {
err = utilyaml.WriteOut(tf, doc) return err
if err != nil {
return err
}
} }
ao.SetSourceFiles([]string{tf.Name()}) ao.SetSourceFiles([]string{tf.Name()})
return ao.Run() return ao.Run()

View File

@ -108,6 +108,6 @@ func TestApply(t *testing.T) {
} }
for _, test := range tests { for _, test := range tests {
kctl.FileSystem = test.fs kctl.FileSystem = test.fs
assert.Equal(t, kctl.Apply(docs, ao), test.expectedErr) assert.Equal(t, kctl.ApplyDocs(docs, ao), test.expectedErr)
} }
} }