Skip to content

Commit a3bf49d

Browse files
committed
Fix namespace setting with direct applier
The direct-applier was not setting the namespace correctly; we reuse the SetNamespace visitor to do so.
1 parent 0e49556 commit a3bf49d

File tree

5 files changed

+60
-9
lines changed

5 files changed

+60
-9
lines changed

pkg/patterns/declarative/pkg/applier/direct.go

Lines changed: 49 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,16 @@ package applier
22

33
import (
44
"context"
5+
"fmt"
56
"os"
67
"strings"
78

9+
"k8s.io/apimachinery/pkg/api/meta"
810
"k8s.io/cli-runtime/pkg/genericclioptions"
911
"k8s.io/cli-runtime/pkg/printers"
1012
"k8s.io/cli-runtime/pkg/resource"
13+
"k8s.io/client-go/discovery"
14+
"k8s.io/client-go/rest"
1115
"k8s.io/kubectl/pkg/cmd/apply"
1216
cmdDelete "k8s.io/kubectl/pkg/cmd/delete"
1317
cmdutil "k8s.io/kubectl/pkg/cmd/util"
@@ -22,6 +26,8 @@ func NewDirectApplier() *DirectApplier {
2226
}
2327

2428
func (d *DirectApplier) Apply(ctx context.Context,
29+
restConfig *rest.Config,
30+
restMapper meta.RESTMapper,
2531
namespace string,
2632
manifest string,
2733
validate bool,
@@ -32,16 +38,29 @@ func (d *DirectApplier) Apply(ctx context.Context,
3238
Out: os.Stdout,
3339
ErrOut: os.Stderr,
3440
}
35-
restClient := genericclioptions.NewConfigFlags(true).WithDeprecatedPasswordFlag()
3641
ioReader := strings.NewReader(manifest)
3742

38-
b := resource.NewBuilder(restClient)
43+
restClientGetter := &staticRESTClientGetter{
44+
RESTMapper: restMapper,
45+
RESTConfig: restConfig,
46+
}
47+
b := resource.NewBuilder(restClientGetter)
3948
res := b.Unstructured().Stream(ioReader, "manifestString").Do()
4049
infos, err := res.Infos()
4150
if err != nil {
4251
return err
4352
}
4453

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+
4564
applyOpts := apply.NewApplyOptions(ioStreams)
4665
applyOpts.Namespace = namespace
4766
applyOpts.SetObjects(infos)
@@ -56,3 +75,31 @@ func (d *DirectApplier) Apply(ctx context.Context,
5675

5776
return applyOpts.Run()
5877
}
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+
}

pkg/patterns/declarative/pkg/applier/exec.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ import (
2424
"strconv"
2525
"strings"
2626

27+
"k8s.io/apimachinery/pkg/api/meta"
28+
"k8s.io/client-go/rest"
2729
"sigs.k8s.io/controller-runtime/pkg/log"
2830
)
2931

@@ -49,7 +51,7 @@ func (console) Run(c *exec.Cmd) error {
4951
}
5052

5153
// Apply runs the kubectl apply with the provided manifest argument
52-
func (c *ExecKubectl) Apply(ctx context.Context, namespace string, manifest string, validate bool,
54+
func (c *ExecKubectl) Apply(ctx context.Context, restConfig *rest.Config, restMapper meta.RESTMapper, namespace string, manifest string, validate bool,
5355
extraArgs ...string) error {
5456
log := log.Log
5557

pkg/patterns/declarative/pkg/applier/exec_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ func TestKubectlApply(t *testing.T) {
8585
t.Run(test.name, func(t *testing.T) {
8686
cs := collector{Error: test.err}
8787
kubectl := &ExecKubectl{cmdSite: &cs}
88-
err := kubectl.Apply(context.Background(), test.namespace, test.manifest, test.validate, test.args...)
88+
err := kubectl.Apply(context.Background(), nil, nil, test.namespace, test.manifest, test.validate, test.args...)
8989

9090
if test.err != nil && err == nil {
9191
t.Error("expected error to occur")

pkg/patterns/declarative/pkg/applier/type.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,11 @@ package applier
22

33
import (
44
"context"
5+
6+
"k8s.io/apimachinery/pkg/api/meta"
7+
"k8s.io/client-go/rest"
58
)
69

710
type Applier interface {
8-
Apply(ctx context.Context, namespace string, manifest string, validate bool, extraArgs ...string) error
11+
Apply(ctx context.Context, restConfig *rest.Config, restMapper meta.RESTMapper, namespace string, manifest string, validate bool, extraArgs ...string) error
912
}

pkg/patterns/declarative/reconciler.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -256,7 +256,7 @@ func (r *Reconciler) reconcileExists(ctx context.Context, name types.NamespacedN
256256
}
257257
}
258258

259-
if err := r.kubectl.Apply(ctx, ns, manifestStr, r.options.validate, extraArgs...); err != nil {
259+
if err := r.kubectl.Apply(ctx, r.config, r.restMapper, ns, manifestStr, r.options.validate, extraArgs...); err != nil {
260260
log.Error(err, "applying manifest")
261261
return reconcile.Result{}, fmt.Errorf("error applying manifest: %v", err)
262262
}
@@ -564,14 +564,13 @@ func (r *Reconciler) CollectMetrics() bool {
564564
return r.options.metrics
565565
}
566566

567-
func GetObjectFromCluster(obj *manifest.Object, r *Reconciler) (*unstructured.
568-
Unstructured, error) {
567+
func GetObjectFromCluster(obj *manifest.Object, r *Reconciler) (*unstructured.Unstructured, error) {
569568
getOptions := metav1.GetOptions{}
570569
gvk := obj.GroupVersionKind()
571570

572571
mapping, err := r.restMapper.RESTMapping(obj.GroupKind(), gvk.Version)
573572
if err != nil {
574-
return nil, fmt.Errorf("unable to get mapping for resource: %w", err)
573+
return nil, fmt.Errorf("unable to get mapping for resource %v: %w", gvk, err)
575574
}
576575
ns := obj.UnstructuredObject().GetNamespace()
577576
unstruct, err := r.dynamicClient.Resource(mapping.Resource).Namespace(ns).Get(context.Background(),

0 commit comments

Comments
 (0)