airshipctl/pkg/k8s/kubectl/apply_options.go
Kostiantyn Kalynovskyi 9afeccb785 [#20] Add abstraction to kubernetes interactions
Object Cluster will be a entry point to intractions with kubernetes
It will provide:
  - kubectl abstraction which will emulate kubectl apply
and other commands (in the future)
  - kubernets client-go kubernetes interface which has all operations,
including CRUD to kubernetes apps, core, extentions, etc... objects

Change-Id: Ie2961f68a160e720c264c622c1124f283bded161
2020-02-25 12:21:37 -06:00

107 lines
2.6 KiB
Go

package kubectl
import (
"k8s.io/cli-runtime/pkg/genericclioptions"
"k8s.io/cli-runtime/pkg/printers"
"k8s.io/kubectl/pkg/cmd/apply"
cmdutil "k8s.io/kubectl/pkg/cmd/util"
)
// ApplyOptions is a abstraction layer
// to ApplyOptions of kubectl/apply package
type ApplyOptions struct {
ApplyOptions *apply.ApplyOptions
}
func (ao *ApplyOptions) SetDryRun(dryRun bool) {
ao.ApplyOptions.DryRun = dryRun
}
func (ao *ApplyOptions) SetPrune(label string) {
if label != "" {
ao.ApplyOptions.Prune = true
ao.ApplyOptions.Selector = label
} else {
ao.ApplyOptions.Prune = false
}
}
// SetSourceFiles sets files to read for kubectl apply command
func (ao *ApplyOptions) SetSourceFiles(fileNames []string) {
ao.ApplyOptions.DeleteOptions.Filenames = fileNames
}
func (ao *ApplyOptions) Run() error {
return ao.ApplyOptions.Run()
}
// NewApplyOptions is a helper function that Creates ApplyOptions of kubectl apply module
// Values set here, are default, and do not conflict with each other, can be used if you
// need `kubectl apply` functionality without calling executing command in shell
// To function properly, you may need to specify files from where to read the resources:
// SetSourceFiles of returned object has to be used for that
func NewApplyOptions(f cmdutil.Factory, streams genericclioptions.IOStreams) (*ApplyOptions, error) {
o := apply.NewApplyOptions(streams)
o.ServerSideApply = false
o.ForceConflicts = false
o.ToPrinter = func(operation string) (printers.ResourcePrinter, error) {
o.PrintFlags.NamePrintFlags.Operation = operation
if o.DryRun {
err := o.PrintFlags.Complete("%s (dry run)")
if err != nil {
return nil, err
}
}
if o.ServerDryRun {
err := o.PrintFlags.Complete("%s (server dry run)")
if err != nil {
return nil, err
}
}
return o.PrintFlags.ToPrinter()
}
var err error
o.Recorder, err = o.RecordFlags.ToRecorder()
if err != nil {
return nil, err
}
o.DiscoveryClient, err = f.ToDiscoveryClient()
if err != nil {
return nil, err
}
dynamicClient, err := f.DynamicClient()
if err != nil {
return nil, err
}
o.DeleteOptions = o.DeleteFlags.ToOptions(dynamicClient, o.IOStreams)
// This can only fail if ToDiscoverClient() function fails
o.OpenAPISchema, err = f.OpenAPISchema()
if err != nil {
return nil, err
}
o.Validator, err = f.Validator(false)
if err != nil {
return nil, err
}
o.Builder = f.NewBuilder()
o.Mapper, err = f.ToRESTMapper()
if err != nil {
return nil, err
}
o.DynamicClient = dynamicClient
o.Namespace, o.EnforceNamespace, err = f.ToRawKubeConfigLoader().Namespace()
if err != nil {
return nil, err
}
return &ApplyOptions{ApplyOptions: o}, nil
}