@@ -2,12 +2,16 @@ package applier
2
2
3
3
import (
4
4
"context"
5
+ "fmt"
5
6
"os"
6
7
"strings"
7
8
9
+ "k8s.io/apimachinery/pkg/api/meta"
8
10
"k8s.io/cli-runtime/pkg/genericclioptions"
9
11
"k8s.io/cli-runtime/pkg/printers"
10
12
"k8s.io/cli-runtime/pkg/resource"
13
+ "k8s.io/client-go/discovery"
14
+ "k8s.io/client-go/rest"
11
15
"k8s.io/kubectl/pkg/cmd/apply"
12
16
cmdDelete "k8s.io/kubectl/pkg/cmd/delete"
13
17
cmdutil "k8s.io/kubectl/pkg/cmd/util"
@@ -22,6 +26,8 @@ func NewDirectApplier() *DirectApplier {
22
26
}
23
27
24
28
func (d * DirectApplier ) Apply (ctx context.Context ,
29
+ restConfig * rest.Config ,
30
+ restMapper meta.RESTMapper ,
25
31
namespace string ,
26
32
manifest string ,
27
33
validate bool ,
@@ -32,16 +38,29 @@ func (d *DirectApplier) Apply(ctx context.Context,
32
38
Out : os .Stdout ,
33
39
ErrOut : os .Stderr ,
34
40
}
35
- restClient := genericclioptions .NewConfigFlags (true ).WithDeprecatedPasswordFlag ()
36
41
ioReader := strings .NewReader (manifest )
37
42
38
- b := resource .NewBuilder (restClient )
43
+ restClientGetter := & staticRESTClientGetter {
44
+ RESTMapper : restMapper ,
45
+ RESTConfig : restConfig ,
46
+ }
47
+ b := resource .NewBuilder (restClientGetter )
39
48
res := b .Unstructured ().Stream (ioReader , "manifestString" ).Do ()
40
49
infos , err := res .Infos ()
41
50
if err != nil {
42
51
return err
43
52
}
44
53
54
+ // Populate the namespace on any namespace-scoped objects
55
+ if namespace != "" {
56
+ visitor := resource .SetNamespace (namespace )
57
+ for _ , info := range infos {
58
+ if err := info .Visit (visitor ); err != nil {
59
+ return fmt .Errorf ("error from SetNamespace: %w" , err )
60
+ }
61
+ }
62
+ }
63
+
45
64
applyOpts := apply .NewApplyOptions (ioStreams )
46
65
applyOpts .Namespace = namespace
47
66
applyOpts .SetObjects (infos )
@@ -56,3 +75,31 @@ func (d *DirectApplier) Apply(ctx context.Context,
56
75
57
76
return applyOpts .Run ()
58
77
}
78
+
79
+ // staticRESTClientGetter returns a fixed RESTClient
80
+ type staticRESTClientGetter struct {
81
+ RESTConfig * rest.Config
82
+ DiscoveryClient discovery.CachedDiscoveryInterface
83
+ RESTMapper meta.RESTMapper
84
+ }
85
+
86
+ var _ resource.RESTClientGetter = & staticRESTClientGetter {}
87
+
88
+ func (s * staticRESTClientGetter ) ToRESTConfig () (* rest.Config , error ) {
89
+ if s .RESTConfig == nil {
90
+ return nil , fmt .Errorf ("RESTConfig not set" )
91
+ }
92
+ return s .RESTConfig , nil
93
+ }
94
+ func (s * staticRESTClientGetter ) ToDiscoveryClient () (discovery.CachedDiscoveryInterface , error ) {
95
+ if s .DiscoveryClient == nil {
96
+ return nil , fmt .Errorf ("DiscoveryClient not set" )
97
+ }
98
+ return s .DiscoveryClient , nil
99
+ }
100
+ func (s * staticRESTClientGetter ) ToRESTMapper () (meta.RESTMapper , error ) {
101
+ if s .RESTMapper == nil {
102
+ return nil , fmt .Errorf ("RESTMapper not set" )
103
+ }
104
+ return s .RESTMapper , nil
105
+ }
0 commit comments