forked from kubernetes-sigs/kubebuilder-declarative-pattern
/
direct.go
58 lines (50 loc) · 1.4 KB
/
direct.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
package applier
import (
"context"
"os"
"strings"
"k8s.io/cli-runtime/pkg/genericclioptions"
"k8s.io/cli-runtime/pkg/printers"
"k8s.io/cli-runtime/pkg/resource"
"k8s.io/kubectl/pkg/cmd/apply"
cmdDelete "k8s.io/kubectl/pkg/cmd/delete"
cmdutil "k8s.io/kubectl/pkg/cmd/util"
)
type DirectApplier struct {
a apply.ApplyOptions
}
func NewDirectApplier() *DirectApplier {
return &DirectApplier{}
}
func (d *DirectApplier) Apply(ctx context.Context,
namespace string,
manifest string,
validate bool,
extraArgs ...string,
) error {
ioStreams := genericclioptions.IOStreams{
In: os.Stdin,
Out: os.Stdout,
ErrOut: os.Stderr,
}
restClient := genericclioptions.NewConfigFlags(true).WithDeprecatedPasswordFlag()
ioReader := strings.NewReader(manifest)
b := resource.NewBuilder(restClient)
res := b.Unstructured().Stream(ioReader, "manifestString").Do()
infos, err := res.Infos()
if err != nil {
return err
}
applyOpts := apply.NewApplyOptions(ioStreams)
applyOpts.Namespace = namespace
applyOpts.SetObjects(infos)
applyOpts.ToPrinter = func(operation string) (printers.ResourcePrinter, error) {
applyOpts.PrintFlags.NamePrintFlags.Operation = operation
cmdutil.PrintFlagsWithDryRunStrategy(applyOpts.PrintFlags, applyOpts.DryRunStrategy)
return applyOpts.PrintFlags.ToPrinter()
}
applyOpts.DeleteOptions = &cmdDelete.DeleteOptions{
IOStreams: ioStreams,
}
return applyOpts.Run()
}