Preserve existing namespace when using kubectl set --local

This commit is contained in:
Jordan Liggitt 2019-04-30 10:39:29 -04:00
parent 306740f81c
commit 2fae80c27c
3 changed files with 60 additions and 1 deletions

View File

@ -78,6 +78,43 @@ func TestSetEnvLocal(t *testing.T) {
}
}
func TestSetEnvLocalNamespace(t *testing.T) {
tf := cmdtesting.NewTestFactory().WithNamespace("test")
defer tf.Cleanup()
tf.Client = &fake.RESTClient{
GroupVersion: schema.GroupVersion{Version: ""},
NegotiatedSerializer: scheme.Codecs.WithoutConversion(),
Client: fake.CreateHTTPClient(func(req *http.Request) (*http.Response, error) {
t.Fatalf("unexpected request: %s %#v\n%#v", req.Method, req.URL, req)
return nil, nil
}),
}
tf.ClientConfigVal = &restclient.Config{ContentConfig: restclient.ContentConfig{GroupVersion: &schema.GroupVersion{Version: ""}}}
outputFormat := "yaml"
streams, _, buf, bufErr := genericclioptions.NewTestIOStreams()
opts := NewEnvOptions(streams)
opts.PrintFlags = genericclioptions.NewPrintFlags("").WithDefaultOutput(outputFormat).WithTypeSetter(scheme.Scheme)
opts.FilenameOptions = resource.FilenameOptions{
Filenames: []string{"../../../../test/fixtures/pkg/kubectl/cmd/set/namespaced-resource.yaml"},
}
opts.Local = true
err := opts.Complete(tf, NewCmdEnv(tf, streams), []string{"env=prod"})
assert.NoError(t, err)
err = opts.Validate()
assert.NoError(t, err)
err = opts.RunEnv()
assert.NoError(t, err)
if bufErr.Len() > 0 {
t.Errorf("unexpected error: %s", string(bufErr.String()))
}
if !strings.Contains(buf.String(), "namespace: existing-ns") {
t.Errorf("did not set env: %s", buf.String())
}
}
func TestSetMultiResourcesEnvLocal(t *testing.T) {
tf := cmdtesting.NewTestFactory().WithNamespace("test")
defer tf.Cleanup()

View File

@ -169,7 +169,12 @@ func (i *Info) String() string {
// Namespaced returns true if the object belongs to a namespace
func (i *Info) Namespaced() bool {
return i.Mapping != nil && i.Mapping.Scope.Name() == meta.RESTScopeNameNamespace
if i.Mapping != nil {
// if we have RESTMapper info, use it
return i.Mapping.Scope.Name() == meta.RESTScopeNameNamespace
}
// otherwise, use the presence of a namespace in the info as an indicator
return len(i.Namespace) > 0
}
// Watch returns server changes to this object after it was retrieved.

View File

@ -0,0 +1,17 @@
apiVersion: v1
kind: ReplicationController
metadata:
name: namespaced-rc
namespace: existing-ns
spec:
replicas: 1
selector:
app: mock
template:
metadata:
labels:
app: mock
spec:
containers:
- name: mock-container
image: k8s.gcr.io/pause:3.1