airshipctl/pkg/k8s/kubectl/kubectl.go
Kostiantyn Kalynovskyi 76a280ead0 Add Kustomize document filesystem layer
Add document filesystem object to avoid direct kustomize imports by
other packages in airshipctl, document.filesystem also extends
kustomize filesystem with temporary file methods.

Relates-To: #11
Closes: #11

Change-Id: Ia8034048d80d79d5996dce0e283828644fbef906
2020-03-04 20:04:35 +00:00

75 lines
2.0 KiB
Go

package kubectl
import (
"os"
"k8s.io/cli-runtime/pkg/genericclioptions"
cmdutil "k8s.io/kubectl/pkg/cmd/util"
"opendev.org/airship/airshipctl/pkg/document"
"opendev.org/airship/airshipctl/pkg/log"
utilyaml "opendev.org/airship/airshipctl/pkg/util/yaml"
)
// Kubectl container holds Factory, Streams and FileSystem to
// interact with upstream kubectl objects and serves as abstraction to kubectl project
type Kubectl struct {
cmdutil.Factory
genericclioptions.IOStreams
document.FileSystem
// Directory to buffer documents before passing them to kubectl commands
// default is empty, this means that /tmp dir will be used
bufferDir string
}
// NewKubectl builds an instance
// of Kubectl struct from Path to kubeconfig file
func NewKubectl(f cmdutil.Factory) *Kubectl {
return &Kubectl{
Factory: f,
IOStreams: genericclioptions.IOStreams{
In: os.Stdin,
Out: os.Stdout,
ErrOut: os.Stderr,
},
FileSystem: document.NewDocumentFs(),
}
}
func (kubectl *Kubectl) WithBufferDir(bd string) *Kubectl {
kubectl.bufferDir = bd
return kubectl
}
// Apply is abstraction to kubectl apply command
func (kubectl *Kubectl) Apply(docs []document.Document, ao *ApplyOptions) error {
tf, err := kubectl.TempFile(kubectl.bufferDir, "initinfra")
if err != nil {
return err
}
defer func(f document.File) {
fName := f.Name()
dErr := kubectl.RemoveAll(fName)
if dErr != nil {
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
}
}
ao.SetSourceFiles([]string{tf.Name()})
return ao.Run()
}
// ApplyOptions is a wrapper over kubectl ApplyOptions, used to build
// new options from the factory and iostreams defined in Kubectl container
func (kubectl *Kubectl) ApplyOptions() (*ApplyOptions, error) {
return NewApplyOptions(kubectl.Factory, kubectl.IOStreams)
}