Skip to content

Commit 1e01523

Browse files
authored
Merge pull request kubernetes#77267 from liggitt/kubectl-set-local-namespace
Preserve existing namespace when using kubectl set --local
2 parents 0ec6f0f + 2fae80c commit 1e01523

File tree

3 files changed

+60
-1
lines changed

3 files changed

+60
-1
lines changed

pkg/kubectl/cmd/set/set_env_test.go

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,43 @@ func TestSetEnvLocal(t *testing.T) {
7878
}
7979
}
8080

81+
func TestSetEnvLocalNamespace(t *testing.T) {
82+
tf := cmdtesting.NewTestFactory().WithNamespace("test")
83+
defer tf.Cleanup()
84+
85+
tf.Client = &fake.RESTClient{
86+
GroupVersion: schema.GroupVersion{Version: ""},
87+
NegotiatedSerializer: scheme.Codecs.WithoutConversion(),
88+
Client: fake.CreateHTTPClient(func(req *http.Request) (*http.Response, error) {
89+
t.Fatalf("unexpected request: %s %#v\n%#v", req.Method, req.URL, req)
90+
return nil, nil
91+
}),
92+
}
93+
tf.ClientConfigVal = &restclient.Config{ContentConfig: restclient.ContentConfig{GroupVersion: &schema.GroupVersion{Version: ""}}}
94+
outputFormat := "yaml"
95+
96+
streams, _, buf, bufErr := genericclioptions.NewTestIOStreams()
97+
opts := NewEnvOptions(streams)
98+
opts.PrintFlags = genericclioptions.NewPrintFlags("").WithDefaultOutput(outputFormat).WithTypeSetter(scheme.Scheme)
99+
opts.FilenameOptions = resource.FilenameOptions{
100+
Filenames: []string{"../../../../test/fixtures/pkg/kubectl/cmd/set/namespaced-resource.yaml"},
101+
}
102+
opts.Local = true
103+
104+
err := opts.Complete(tf, NewCmdEnv(tf, streams), []string{"env=prod"})
105+
assert.NoError(t, err)
106+
err = opts.Validate()
107+
assert.NoError(t, err)
108+
err = opts.RunEnv()
109+
assert.NoError(t, err)
110+
if bufErr.Len() > 0 {
111+
t.Errorf("unexpected error: %s", string(bufErr.String()))
112+
}
113+
if !strings.Contains(buf.String(), "namespace: existing-ns") {
114+
t.Errorf("did not set env: %s", buf.String())
115+
}
116+
}
117+
81118
func TestSetMultiResourcesEnvLocal(t *testing.T) {
82119
tf := cmdtesting.NewTestFactory().WithNamespace("test")
83120
defer tf.Cleanup()

staging/src/k8s.io/cli-runtime/pkg/resource/visitor.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,12 @@ func (i *Info) String() string {
169169

170170
// Namespaced returns true if the object belongs to a namespace
171171
func (i *Info) Namespaced() bool {
172-
return i.Mapping != nil && i.Mapping.Scope.Name() == meta.RESTScopeNameNamespace
172+
if i.Mapping != nil {
173+
// if we have RESTMapper info, use it
174+
return i.Mapping.Scope.Name() == meta.RESTScopeNameNamespace
175+
}
176+
// otherwise, use the presence of a namespace in the info as an indicator
177+
return len(i.Namespace) > 0
173178
}
174179

175180
// Watch returns server changes to this object after it was retrieved.
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
apiVersion: v1
2+
kind: ReplicationController
3+
metadata:
4+
name: namespaced-rc
5+
namespace: existing-ns
6+
spec:
7+
replicas: 1
8+
selector:
9+
app: mock
10+
template:
11+
metadata:
12+
labels:
13+
app: mock
14+
spec:
15+
containers:
16+
- name: mock-container
17+
image: k8s.gcr.io/pause:3.1

0 commit comments

Comments
 (0)