@@ -2,12 +2,11 @@ package applier
2
2
3
3
import (
4
4
"context"
5
+ "io/ioutil"
5
6
"os"
6
- "strings"
7
7
8
8
"k8s.io/cli-runtime/pkg/genericclioptions"
9
9
"k8s.io/cli-runtime/pkg/printers"
10
- "k8s.io/cli-runtime/pkg/resource"
11
10
"k8s.io/kubectl/pkg/cmd/apply"
12
11
cmdDelete "k8s.io/kubectl/pkg/cmd/delete"
13
12
cmdutil "k8s.io/kubectl/pkg/cmd/util"
@@ -27,24 +26,41 @@ func (d *DirectApplier) Apply(ctx context.Context,
27
26
validate bool ,
28
27
extraArgs ... string ,
29
28
) error {
29
+
30
+ tmpFile , err := ioutil .TempFile ("" , "tmp-manifest-*.yaml" )
31
+ if err != nil {
32
+ return err
33
+ }
34
+ tmpFile .Write ([]byte (manifest ))
35
+ tmpFile .Close ()
36
+ defer os .Remove (tmpFile .Name ())
30
37
ioStreams := genericclioptions.IOStreams {
31
- In : os . Stdin ,
38
+ In : tmpFile ,
32
39
Out : os .Stdout ,
33
40
ErrOut : os .Stderr ,
34
41
}
42
+
35
43
restClient := genericclioptions .NewConfigFlags (true ).WithDeprecatedPasswordFlag ()
36
- ioReader := strings .NewReader (manifest )
44
+ f := cmdutil .NewFactory (restClient )
45
+ schema , err := f .Validator (validate )
46
+ if err != nil {
47
+ return err
48
+ }
49
+ applyOpts := apply .NewApplyOptions (ioStreams )
37
50
38
- b := resource .NewBuilder (restClient )
39
- res := b .Unstructured ().Stream (ioReader , "manifestString" ).Do ()
40
- infos , err := res .Infos ()
51
+ applyOpts .DynamicClient , err = f .DynamicClient ()
41
52
if err != nil {
42
53
return err
43
54
}
55
+ applyOpts .DeleteOptions = applyOpts .DeleteFlags .ToOptions (applyOpts .DynamicClient , applyOpts .IOStreams )
44
56
45
- applyOpts := apply .NewApplyOptions (ioStreams )
46
- applyOpts .Namespace = namespace
47
- applyOpts .SetObjects (infos )
57
+ applyOpts .Namespace , applyOpts .EnforceNamespace , err = f .ToRawKubeConfigLoader ().Namespace ()
58
+ if namespace != "" {
59
+ applyOpts .Namespace = namespace
60
+ }
61
+ applyOpts .Validator = schema
62
+ applyOpts .Builder = f .NewBuilder ()
63
+ applyOpts .Mapper , err = f .ToRESTMapper ()
48
64
applyOpts .ToPrinter = func (operation string ) (printers.ResourcePrinter , error ) {
49
65
applyOpts .PrintFlags .NamePrintFlags .Operation = operation
50
66
cmdutil .PrintFlagsWithDryRunStrategy (applyOpts .PrintFlags , applyOpts .DryRunStrategy )
@@ -53,6 +69,7 @@ func (d *DirectApplier) Apply(ctx context.Context,
53
69
applyOpts .DeleteOptions = & cmdDelete.DeleteOptions {
54
70
IOStreams : ioStreams ,
55
71
}
72
+ applyOpts .DeleteOptions .Filenames = []string {tmpFile .Name ()}
56
73
57
74
return applyOpts .Run ()
58
75
}
0 commit comments