2019-11-06 09:44:10 -06:00
|
|
|
package kubectl
|
|
|
|
|
|
|
|
import (
|
|
|
|
"os"
|
|
|
|
|
|
|
|
"k8s.io/cli-runtime/pkg/genericclioptions"
|
|
|
|
cmdutil "k8s.io/kubectl/pkg/cmd/util"
|
|
|
|
"sigs.k8s.io/kustomize/v3/pkg/fs"
|
|
|
|
|
|
|
|
"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
|
|
|
|
FileSystem
|
|
|
|
// Directory to buffer documents before passing them to kubectl commands
|
|
|
|
// default is empty, this means that /tmp dir will be used
|
|
|
|
bufferDir string
|
|
|
|
}
|
|
|
|
|
2020-02-20 20:34:30 +00:00
|
|
|
// NewKubectl builds an instance
|
2019-11-06 09:44:10 -06:00
|
|
|
// 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: Buffer{FileSystem: fs.MakeRealFS()},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (kubectl *Kubectl) WithBufferDir(bd string) *Kubectl {
|
|
|
|
kubectl.bufferDir = bd
|
|
|
|
return kubectl
|
|
|
|
}
|
|
|
|
|
|
|
|
// Apply is abstraction to kubectl apply command
|
2020-02-20 20:34:30 +00:00
|
|
|
func (kubectl *Kubectl) Apply(docs []document.Document, ao *ApplyOptions) error {
|
2019-11-06 09:44:10 -06:00
|
|
|
tf, err := kubectl.TempFile(kubectl.bufferDir, "initinfra")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
defer func(f 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
|
|
|
|
}
|
|
|
|
}
|
2020-02-20 20:34:30 +00:00
|
|
|
ao.SetSourceFiles([]string{tf.Name()})
|
2019-11-06 09:44:10 -06:00
|
|
|
return ao.Run()
|
|
|
|
}
|
|
|
|
|
|
|
|
// ApplyOptions is a wrapper over kubectl ApplyOptions, used to build
|
|
|
|
// new options from the factory and iostreams defined in Kubectl container
|
2020-02-20 20:34:30 +00:00
|
|
|
func (kubectl *Kubectl) ApplyOptions() (*ApplyOptions, error) {
|
2019-11-06 09:44:10 -06:00
|
|
|
return NewApplyOptions(kubectl.Factory, kubectl.IOStreams)
|
|
|
|
}
|