Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix cross-namespace LIST and WATCH #4395

Merged
merged 4 commits into from
Feb 17, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
82 changes: 63 additions & 19 deletions cmd/integration/integration.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ limitations under the License.
package main

import (
"fmt"
"io/ioutil"
"net"
"net/http"
Expand Down Expand Up @@ -278,18 +279,19 @@ func runReplicationControllerTest(c *client.Client) {
}

glog.Infof("Creating replication controllers")
if _, err := c.ReplicationControllers(api.NamespaceDefault).Create(&controller); err != nil {
updated, err := c.ReplicationControllers("test").Create(&controller)
if err != nil {
glog.Fatalf("Unexpected error: %v", err)
}
glog.Infof("Done creating replication controllers")

// Give the controllers some time to actually create the pods
if err := wait.Poll(time.Second, time.Second*30, client.ControllerHasDesiredReplicas(c, &controller)); err != nil {
if err := wait.Poll(time.Second, time.Second*30, client.ControllerHasDesiredReplicas(c, updated)); err != nil {
glog.Fatalf("FAILED: pods never created %v", err)
}

// wait for minions to indicate they have info about the desired pods
pods, err := c.Pods(api.NamespaceDefault).List(labels.Set(controller.Spec.Selector).AsSelector())
pods, err := c.Pods("test").List(labels.Set(updated.Spec.Selector).AsSelector())
if err != nil {
glog.Fatalf("FAILED: unable to get pods to list: %v", err)
}
Expand All @@ -311,12 +313,15 @@ func runAPIVersionsTest(c *client.Client) {
glog.Infof("Version test passed")
}

func runSelfLinkTest(c *client.Client) {
func runSelfLinkTestOnNamespace(c *client.Client, namespace string) {
var svc api.Service
err := c.Post().Resource("services").Body(
err := c.Post().
NamespaceIfScoped(namespace, len(namespace) > 0).
Resource("services").Body(
&api.Service{
ObjectMeta: api.ObjectMeta{
Name: "selflinktest",
Name: "selflinktest",
Namespace: namespace,
Labels: map[string]string{
"name": "selflinktest",
},
Expand All @@ -335,18 +340,19 @@ func runSelfLinkTest(c *client.Client) {
if err != nil {
glog.Fatalf("Failed creating selflinktest service: %v", err)
}
err = c.Get().AbsPath(svc.SelfLink).Do().Into(&svc)
// TODO: this is not namespace aware
err = c.Get().RequestURI(svc.SelfLink).Do().Into(&svc)
if err != nil {
glog.Fatalf("Failed listing service with supplied self link '%v': %v", svc.SelfLink, err)
}

var svcList api.ServiceList
err = c.Get().Resource("services").Do().Into(&svcList)
err = c.Get().NamespaceIfScoped(namespace, len(namespace) > 0).Resource("services").Do().Into(&svcList)
if err != nil {
glog.Fatalf("Failed listing services: %v", err)
}

err = c.Get().AbsPath(svcList.SelfLink).Do().Into(&svcList)
err = c.Get().RequestURI(svcList.SelfLink).Do().Into(&svcList)
if err != nil {
glog.Fatalf("Failed listing services with supplied self link '%v': %v", svcList.SelfLink, err)
}
Expand All @@ -358,16 +364,16 @@ func runSelfLinkTest(c *client.Client) {
continue
}
found = true
err = c.Get().AbsPath(item.SelfLink).Do().Into(&svc)
err = c.Get().RequestURI(item.SelfLink).Do().Into(&svc)
if err != nil {
glog.Fatalf("Failed listing service with supplied self link '%v': %v", item.SelfLink, err)
}
break
}
if !found {
glog.Fatalf("never found selflinktest service")
glog.Fatalf("never found selflinktest service in namespace %s", namespace)
}
glog.Infof("Self link test passed")
glog.Infof("Self link test passed in namespace %s", namespace)

// TODO: Should test PUT at some point, too.
}
Expand Down Expand Up @@ -518,7 +524,7 @@ func runMasterServiceTest(client *client.Client) {
}

func runServiceTest(client *client.Client) {
pod := api.Pod{
pod := &api.Pod{
ObjectMeta: api.ObjectMeta{
Name: "foo",
Labels: map[string]string{
Expand All @@ -543,14 +549,14 @@ func runServiceTest(client *client.Client) {
PodIP: "1.2.3.4",
},
}
_, err := client.Pods(api.NamespaceDefault).Create(&pod)
pod, err := client.Pods(api.NamespaceDefault).Create(pod)
if err != nil {
glog.Fatalf("Failed to create pod: %v, %v", pod, err)
}
if err := wait.Poll(time.Second, time.Second*20, podExists(client, pod.Namespace, pod.Name)); err != nil {
glog.Fatalf("FAILED: pod never started running %v", err)
}
svc1 := api.Service{
svc1 := &api.Service{
ObjectMeta: api.ObjectMeta{Name: "service1"},
Spec: api.ServiceSpec{
Selector: map[string]string{
Expand All @@ -561,15 +567,33 @@ func runServiceTest(client *client.Client) {
SessionAffinity: "None",
},
}
_, err = client.Services(api.NamespaceDefault).Create(&svc1)
svc1, err = client.Services(api.NamespaceDefault).Create(svc1)
if err != nil {
glog.Fatalf("Failed to create service: %v, %v", svc1, err)
}

// create an identical service in the default namespace
svc3 := &api.Service{
ObjectMeta: api.ObjectMeta{Name: "service1"},
Spec: api.ServiceSpec{
Selector: map[string]string{
"name": "thisisalonglabel",
},
Port: 8080,
Protocol: "TCP",
SessionAffinity: "None",
},
}
svc3, err = client.Services("other").Create(svc3)
if err != nil {
glog.Fatalf("Failed to create service: %v, %v", svc3, err)
}

if err := wait.Poll(time.Second, time.Second*20, endpointsSet(client, svc1.Namespace, svc1.Name, 1)); err != nil {
glog.Fatalf("FAILED: unexpected endpoints: %v", err)
}
// A second service with the same port.
svc2 := api.Service{
svc2 := &api.Service{
ObjectMeta: api.ObjectMeta{Name: "service2"},
Spec: api.ServiceSpec{
Selector: map[string]string{
Expand All @@ -580,13 +604,30 @@ func runServiceTest(client *client.Client) {
SessionAffinity: "None",
},
}
_, err = client.Services(api.NamespaceDefault).Create(&svc2)
svc2, err = client.Services(api.NamespaceDefault).Create(svc2)
if err != nil {
glog.Fatalf("Failed to create service: %v, %v", svc2, err)
}
if err := wait.Poll(time.Second, time.Second*20, endpointsSet(client, svc2.Namespace, svc2.Name, 1)); err != nil {
glog.Fatalf("FAILED: unexpected endpoints: %v", err)
}

if ok, err := endpointsSet(client, svc3.Namespace, svc3.Name, 0)(); !ok || err != nil {
glog.Fatalf("FAILED: service in other namespace should have no endpoints: %v %v", ok, err)
}

svcList, err := client.Services(api.NamespaceAll).List(labels.Everything())
if err != nil {
glog.Fatalf("Failed to list services across namespaces: %v", err)
}
names := util.NewStringSet()
for _, svc := range svcList.Items {
names.Insert(fmt.Sprintf("%s/%s", svc.Namespace, svc.Name))
}
if !names.HasAll("default/kubernetes", "default/kubernetes-ro", "default/service1", "default/service2", "other/service1") {
glog.Fatalf("Unexpected service list: %#v", names)
}

glog.Info("Service test passed.")
}

Expand Down Expand Up @@ -623,7 +664,10 @@ func main() {
runServiceTest,
runAPIVersionsTest,
runMasterServiceTest,
runSelfLinkTest,
func(c *client.Client) {
runSelfLinkTestOnNamespace(c, "")
runSelfLinkTestOnNamespace(c, "other")
},
}
var wg sync.WaitGroup
wg.Add(len(testFuncs))
Expand Down
5 changes: 5 additions & 0 deletions hack/test-cmd.sh
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,11 @@ for version in "${kube_api_versions[@]}"; do
kubectl describe pod redis-master "${kube_flags[@]}" | grep -q 'Name:.*redis-master'
kubectl delete -f examples/guestbook/redis-master.json "${kube_flags[@]}"

# make calls in another namespace
kubectl create --namespace=other -f examples/guestbook/redis-master.json "${kube_flags[@]}"
kubectl get pod --namespace=other redis-master
kubectl delete pod --namespace=other redis-master

kube::log::status "Testing kubectl(${version}:services)"
kubectl get services "${kube_flags[@]}"
kubectl create -f examples/guestbook/frontend-service.json "${kube_flags[@]}"
Expand Down