@@ -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,36 @@ func (d *DirectApplier) Apply(ctx context.Context,
27
26
validate bool ,
28
27
extraArgs ... string ,
29
28
) error {
29
+ tmpFile , err := ioutil .TempFile ("" , "tmp-manifest" )
30
+ if err != nil {
31
+ return err
32
+ }
33
+ tmpFile .Write ([]byte (manifest ))
34
+ tmpFile .Close ()
35
+ tmpFile , err = os .Open (tmpFile .Name ())
36
+ defer os .Remove (tmpFile .Name ())
37
+ os .Stdin = tmpFile
38
+
30
39
ioStreams := genericclioptions.IOStreams {
31
40
In : os .Stdin ,
32
41
Out : os .Stdout ,
33
42
ErrOut : os .Stderr ,
34
43
}
35
- restClient := genericclioptions .NewConfigFlags (true ).WithDeprecatedPasswordFlag ()
36
- ioReader := strings .NewReader (manifest )
37
44
38
- b := resource . NewBuilder ( restClient )
39
- res := b . Unstructured (). Stream ( ioReader , "manifestString" ). Do ( )
40
- infos , err := res . Infos ( )
45
+ restClient := genericclioptions . NewConfigFlags ( true ). WithDeprecatedPasswordFlag ( )
46
+ f := cmdutil . NewFactory ( restClient )
47
+ schema , err := f . Validator ( validate )
41
48
if err != nil {
42
49
return err
43
50
}
44
-
45
51
applyOpts := apply .NewApplyOptions (ioStreams )
46
- applyOpts .Namespace = namespace
47
- applyOpts .SetObjects (infos )
52
+ applyOpts .Namespace , applyOpts .EnforceNamespace , err = f .ToRawKubeConfigLoader ().Namespace ()
53
+ if namespace != "" {
54
+ applyOpts .Namespace = namespace
55
+ }
56
+ applyOpts .Validator = schema
57
+ applyOpts .Builder = f .NewBuilder ()
58
+ applyOpts .Mapper , err = f .ToRESTMapper ()
48
59
applyOpts .ToPrinter = func (operation string ) (printers.ResourcePrinter , error ) {
49
60
applyOpts .PrintFlags .NamePrintFlags .Operation = operation
50
61
cmdutil .PrintFlagsWithDryRunStrategy (applyOpts .PrintFlags , applyOpts .DryRunStrategy )
@@ -53,6 +64,7 @@ func (d *DirectApplier) Apply(ctx context.Context,
53
64
applyOpts .DeleteOptions = & cmdDelete.DeleteOptions {
54
65
IOStreams : ioStreams ,
55
66
}
67
+ applyOpts .DeleteOptions .Filenames = []string {"-" }
56
68
57
69
return applyOpts .Run ()
58
70
}
0 commit comments