mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-07-23 19:56:01 +00:00
Preserve existing namespace when using kubectl set --local
This commit is contained in:
parent
306740f81c
commit
2fae80c27c
@ -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) {
|
func TestSetMultiResourcesEnvLocal(t *testing.T) {
|
||||||
tf := cmdtesting.NewTestFactory().WithNamespace("test")
|
tf := cmdtesting.NewTestFactory().WithNamespace("test")
|
||||||
defer tf.Cleanup()
|
defer tf.Cleanup()
|
||||||
|
@ -169,7 +169,12 @@ func (i *Info) String() string {
|
|||||||
|
|
||||||
// Namespaced returns true if the object belongs to a namespace
|
// Namespaced returns true if the object belongs to a namespace
|
||||||
func (i *Info) Namespaced() bool {
|
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.
|
// Watch returns server changes to this object after it was retrieved.
|
||||||
|
17
test/fixtures/pkg/kubectl/cmd/set/namespaced-resource.yaml
vendored
Normal file
17
test/fixtures/pkg/kubectl/cmd/set/namespaced-resource.yaml
vendored
Normal 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
|
Loading…
Reference in New Issue
Block a user