mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-09-14 05:36:12 +00:00
Verify namespace is not set on root objects
This commit is contained in:
@@ -147,6 +147,15 @@ func streamYAMLTestData() (io.Reader, *api.PodList, *api.ServiceList) {
|
||||
return r, pods, svc
|
||||
}
|
||||
|
||||
func streamTestObject(obj runtime.Object) io.Reader {
|
||||
r, w := io.Pipe()
|
||||
go func() {
|
||||
defer w.Close()
|
||||
w.Write([]byte(runtime.EncodeOrDie(latest.Codec, obj)))
|
||||
}()
|
||||
return r
|
||||
}
|
||||
|
||||
type testVisitor struct {
|
||||
InjectErr error
|
||||
Infos []*Info
|
||||
@@ -617,6 +626,31 @@ func TestSingularObject(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestSingularRootScopedObject(t *testing.T) {
|
||||
node := &api.Node{ObjectMeta: api.ObjectMeta{Name: "test"}, Spec: api.NodeSpec{ExternalID: "test"}}
|
||||
r := streamTestObject(node)
|
||||
infos, err := NewBuilder(latest.RESTMapper, api.Scheme, fakeClient()).
|
||||
NamespaceParam("test").DefaultNamespace().
|
||||
Stream(r, "STDIN").
|
||||
Flatten().
|
||||
Do().Infos()
|
||||
|
||||
if err != nil || len(infos) != 1 {
|
||||
t.Fatalf("unexpected error: %v", err)
|
||||
}
|
||||
|
||||
if infos[0].Namespace != "" {
|
||||
t.Errorf("namespace should be empty: %#v", infos[0])
|
||||
}
|
||||
n, ok := infos[0].Object.(*api.Node)
|
||||
if !ok {
|
||||
t.Fatalf("unexpected object: %#v", infos[0].Object)
|
||||
}
|
||||
if n.Name != "test" || n.Namespace != "" {
|
||||
t.Errorf("unexpected object: %#v", n)
|
||||
}
|
||||
}
|
||||
|
||||
func TestListObject(t *testing.T) {
|
||||
pods, _ := testData()
|
||||
labelKey := api.LabelSelectorQueryParam(testapi.Version())
|
||||
|
@@ -474,6 +474,9 @@ func FilterNamespace(info *Info) error {
|
||||
// set. If info.Object is set, it will be mutated as well.
|
||||
func SetNamespace(namespace string) VisitorFunc {
|
||||
return func(info *Info) error {
|
||||
if !info.Namespaced() {
|
||||
return nil
|
||||
}
|
||||
if len(info.Namespace) == 0 {
|
||||
info.Namespace = namespace
|
||||
UpdateObjectNamespace(info)
|
||||
|
Reference in New Issue
Block a user