k3s/pkg/controller/endpoint/endpoints_controller_test.go

569 lines
23 KiB
Go
Raw Normal View History

2014-06-06 23:40:48 +00:00
/*
Copyright 2014 The Kubernetes Authors.
2014-06-06 23:40:48 +00:00
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
2014-06-23 18:32:11 +00:00
package endpoint
2014-06-06 23:40:48 +00:00
import (
"fmt"
"net/http"
"net/http/httptest"
2014-06-06 23:40:48 +00:00
"testing"
2015-08-05 22:03:47 +00:00
"k8s.io/kubernetes/pkg/api/testapi"
"k8s.io/kubernetes/pkg/api/unversioned"
2016-11-18 20:50:17 +00:00
"k8s.io/kubernetes/pkg/api/v1"
endptspkg "k8s.io/kubernetes/pkg/api/v1/endpoints"
2016-10-04 21:20:07 +00:00
"k8s.io/kubernetes/pkg/apimachinery/registered"
_ "k8s.io/kubernetes/pkg/apimachinery/registered"
"k8s.io/kubernetes/pkg/client/cache"
2016-11-18 20:50:17 +00:00
clientset "k8s.io/kubernetes/pkg/client/clientset_generated/release_1_5"
2016-02-12 18:58:43 +00:00
"k8s.io/kubernetes/pkg/client/restclient"
"k8s.io/kubernetes/pkg/controller"
2015-08-05 22:03:47 +00:00
"k8s.io/kubernetes/pkg/runtime"
"k8s.io/kubernetes/pkg/util/intstr"
utiltesting "k8s.io/kubernetes/pkg/util/testing"
2014-06-06 23:40:48 +00:00
)
var alwaysReady = func() bool { return true }
2016-08-15 22:25:59 +00:00
var emptyNodeName string
func addPods(store cache.Store, namespace string, nPods int, nPorts int, nNotReady int) {
for i := 0; i < nPods+nNotReady; i++ {
2016-11-18 20:50:17 +00:00
p := &v1.Pod{
TypeMeta: unversioned.TypeMeta{APIVersion: registered.GroupOrDie(v1.GroupName).GroupVersion.String()},
ObjectMeta: v1.ObjectMeta{
2015-04-16 23:18:02 +00:00
Namespace: namespace,
Name: fmt.Sprintf("pod%d", i),
Labels: map[string]string{"foo": "bar"},
},
2016-11-18 20:50:17 +00:00
Spec: v1.PodSpec{
Containers: []v1.Container{{Ports: []v1.ContainerPort{}}},
},
2016-11-18 20:50:17 +00:00
Status: v1.PodStatus{
PodIP: fmt.Sprintf("1.2.3.%d", 4+i),
2016-11-18 20:50:17 +00:00
Conditions: []v1.PodCondition{
2015-02-02 18:51:52 +00:00
{
2016-11-18 20:50:17 +00:00
Type: v1.PodReady,
Status: v1.ConditionTrue,
2015-02-02 18:51:52 +00:00
},
},
},
}
if i >= nPods {
2016-11-18 20:50:17 +00:00
p.Status.Conditions[0].Status = v1.ConditionFalse
}
for j := 0; j < nPorts; j++ {
2015-03-13 15:16:41 +00:00
p.Spec.Containers[0].Ports = append(p.Spec.Containers[0].Ports,
2016-11-18 20:50:17 +00:00
v1.ContainerPort{Name: fmt.Sprintf("port%d", i), ContainerPort: int32(8080 + j)})
}
2015-04-16 23:18:02 +00:00
store.Add(p)
}
}
type serverResponse struct {
statusCode int
obj interface{}
}
func makeTestServer(t *testing.T, namespace string, endpointsResponse serverResponse) (*httptest.Server, *utiltesting.FakeHandler) {
fakeEndpointsHandler := utiltesting.FakeHandler{
StatusCode: endpointsResponse.statusCode,
ResponseBody: runtime.EncodeOrDie(testapi.Default.Codec(), endpointsResponse.obj.(runtime.Object)),
}
mux := http.NewServeMux()
mux.Handle(testapi.Default.ResourcePath("endpoints", namespace, ""), &fakeEndpointsHandler)
mux.Handle(testapi.Default.ResourcePath("endpoints/", namespace, ""), &fakeEndpointsHandler)
mux.HandleFunc("/", func(res http.ResponseWriter, req *http.Request) {
t.Errorf("unexpected request: %v", req.RequestURI)
res.WriteHeader(http.StatusNotFound)
})
return httptest.NewServer(mux), &fakeEndpointsHandler
}
2014-11-18 17:49:00 +00:00
func TestSyncEndpointsItemsPreserveNoSelector(t *testing.T) {
2016-11-18 20:50:17 +00:00
ns := v1.NamespaceDefault
2015-04-16 23:18:02 +00:00
testServer, endpointsHandler := makeTestServer(t, ns,
2016-11-18 20:50:17 +00:00
serverResponse{http.StatusOK, &v1.Endpoints{
ObjectMeta: v1.ObjectMeta{
2014-11-18 17:49:00 +00:00
Name: "foo",
2015-04-16 23:18:02 +00:00
Namespace: ns,
2014-11-18 17:49:00 +00:00
ResourceVersion: "1",
},
2016-11-18 20:50:17 +00:00
Subsets: []v1.EndpointSubset{{
Addresses: []v1.EndpointAddress{{IP: "6.7.8.9", NodeName: &emptyNodeName}},
Ports: []v1.EndpointPort{{Port: 1000}},
}},
}})
defer testServer.Close()
2016-11-18 20:50:17 +00:00
client := clientset.NewForConfigOrDie(&restclient.Config{Host: testServer.URL, ContentConfig: restclient.ContentConfig{GroupVersion: &registered.GroupOrDie(v1.GroupName).GroupVersion}})
endpoints := NewEndpointControllerFromClient(client, controller.NoResyncPeriodFunc)
endpoints.podStoreSynced = alwaysReady
2016-11-18 20:50:17 +00:00
endpoints.serviceStore.Indexer.Add(&v1.Service{
ObjectMeta: v1.ObjectMeta{Name: "foo", Namespace: ns},
Spec: v1.ServiceSpec{Ports: []v1.ServicePort{{Port: 80}}},
2015-04-16 23:18:02 +00:00
})
endpoints.syncService(ns + "/foo")
endpointsHandler.ValidateRequestCount(t, 0)
}
2015-04-24 21:16:27 +00:00
func TestCheckLeftoverEndpoints(t *testing.T) {
2016-11-18 20:50:17 +00:00
ns := v1.NamespaceDefault
2015-04-24 21:16:27 +00:00
// Note that this requests *all* endpoints, therefore the NamespaceAll
// below.
2016-11-18 20:50:17 +00:00
testServer, _ := makeTestServer(t, v1.NamespaceAll,
serverResponse{http.StatusOK, &v1.EndpointsList{
ListMeta: unversioned.ListMeta{
2015-04-24 21:16:27 +00:00
ResourceVersion: "1",
},
2016-11-18 20:50:17 +00:00
Items: []v1.Endpoints{{
ObjectMeta: v1.ObjectMeta{
2015-04-24 21:16:27 +00:00
Name: "foo",
Namespace: ns,
ResourceVersion: "1",
},
2016-11-18 20:50:17 +00:00
Subsets: []v1.EndpointSubset{{
Addresses: []v1.EndpointAddress{{IP: "6.7.8.9", NodeName: &emptyNodeName}},
Ports: []v1.EndpointPort{{Port: 1000}},
2015-04-24 21:16:27 +00:00
}},
}},
}})
defer testServer.Close()
2016-11-18 20:50:17 +00:00
client := clientset.NewForConfigOrDie(&restclient.Config{Host: testServer.URL, ContentConfig: restclient.ContentConfig{GroupVersion: &registered.GroupOrDie(v1.GroupName).GroupVersion}})
endpoints := NewEndpointControllerFromClient(client, controller.NoResyncPeriodFunc)
endpoints.podStoreSynced = alwaysReady
2015-04-24 21:16:27 +00:00
endpoints.checkLeftoverEndpoints()
if e, a := 1, endpoints.queue.Len(); e != a {
t.Fatalf("Expected %v, got %v", e, a)
}
got, _ := endpoints.queue.Get()
if e, a := ns+"/foo", got; e != a {
t.Errorf("Expected %v, got %v", e, a)
}
}
func TestSyncEndpointsProtocolTCP(t *testing.T) {
2015-04-16 23:18:02 +00:00
ns := "other"
testServer, endpointsHandler := makeTestServer(t, ns,
2016-11-18 20:50:17 +00:00
serverResponse{http.StatusOK, &v1.Endpoints{
ObjectMeta: v1.ObjectMeta{
Name: "foo",
2015-04-16 23:18:02 +00:00
Namespace: ns,
ResourceVersion: "1",
},
2016-11-18 20:50:17 +00:00
Subsets: []v1.EndpointSubset{{
Addresses: []v1.EndpointAddress{{IP: "6.7.8.9", NodeName: &emptyNodeName}},
Ports: []v1.EndpointPort{{Port: 1000, Protocol: "TCP"}},
}},
}})
defer testServer.Close()
2016-11-18 20:50:17 +00:00
client := clientset.NewForConfigOrDie(&restclient.Config{Host: testServer.URL, ContentConfig: restclient.ContentConfig{GroupVersion: &registered.GroupOrDie(v1.GroupName).GroupVersion}})
endpoints := NewEndpointControllerFromClient(client, controller.NoResyncPeriodFunc)
endpoints.podStoreSynced = alwaysReady
2016-04-07 12:15:21 +00:00
addPods(endpoints.podStore.Indexer, ns, 1, 1, 0)
2016-11-18 20:50:17 +00:00
endpoints.serviceStore.Indexer.Add(&v1.Service{
ObjectMeta: v1.ObjectMeta{Name: "foo", Namespace: ns},
Spec: v1.ServiceSpec{
2015-04-16 23:18:02 +00:00
Selector: map[string]string{},
2016-11-18 20:50:17 +00:00
Ports: []v1.ServicePort{{Port: 80, TargetPort: intstr.FromInt(8080), Protocol: "TCP"}},
2015-04-16 23:18:02 +00:00
},
})
endpoints.syncService(ns + "/foo")
endpointsHandler.ValidateRequestCount(t, 2)
2016-11-18 20:50:17 +00:00
data := runtime.EncodeOrDie(testapi.Default.Codec(), &v1.Endpoints{
ObjectMeta: v1.ObjectMeta{
Name: "foo",
Namespace: ns,
ResourceVersion: "1",
},
2016-11-18 20:50:17 +00:00
Subsets: []v1.EndpointSubset{{
Addresses: []v1.EndpointAddress{{IP: "1.2.3.4", NodeName: &emptyNodeName, TargetRef: &v1.ObjectReference{Kind: "Pod", Name: "pod0", Namespace: ns}}},
Ports: []v1.EndpointPort{{Port: 8080, Protocol: "TCP"}},
}},
})
endpointsHandler.ValidateRequest(t, testapi.Default.ResourcePath("endpoints", ns, "foo"), "PUT", &data)
}
func TestSyncEndpointsProtocolUDP(t *testing.T) {
2015-04-16 23:18:02 +00:00
ns := "other"
testServer, endpointsHandler := makeTestServer(t, ns,
2016-11-18 20:50:17 +00:00
serverResponse{http.StatusOK, &v1.Endpoints{
ObjectMeta: v1.ObjectMeta{
Name: "foo",
2015-04-16 23:18:02 +00:00
Namespace: ns,
ResourceVersion: "1",
},
2016-11-18 20:50:17 +00:00
Subsets: []v1.EndpointSubset{{
Addresses: []v1.EndpointAddress{{IP: "6.7.8.9", NodeName: &emptyNodeName}},
Ports: []v1.EndpointPort{{Port: 1000, Protocol: "UDP"}},
}},
2014-11-18 17:49:00 +00:00
}})
defer testServer.Close()
2016-11-18 20:50:17 +00:00
client := clientset.NewForConfigOrDie(&restclient.Config{Host: testServer.URL, ContentConfig: restclient.ContentConfig{GroupVersion: &registered.GroupOrDie(v1.GroupName).GroupVersion}})
endpoints := NewEndpointControllerFromClient(client, controller.NoResyncPeriodFunc)
endpoints.podStoreSynced = alwaysReady
2016-04-07 12:15:21 +00:00
addPods(endpoints.podStore.Indexer, ns, 1, 1, 0)
2016-11-18 20:50:17 +00:00
endpoints.serviceStore.Indexer.Add(&v1.Service{
ObjectMeta: v1.ObjectMeta{Name: "foo", Namespace: ns},
Spec: v1.ServiceSpec{
2015-04-16 23:18:02 +00:00
Selector: map[string]string{},
2016-11-18 20:50:17 +00:00
Ports: []v1.ServicePort{{Port: 80, TargetPort: intstr.FromInt(8080), Protocol: "UDP"}},
2015-04-16 23:18:02 +00:00
},
})
endpoints.syncService(ns + "/foo")
endpointsHandler.ValidateRequestCount(t, 2)
2016-11-18 20:50:17 +00:00
data := runtime.EncodeOrDie(testapi.Default.Codec(), &v1.Endpoints{
ObjectMeta: v1.ObjectMeta{
Name: "foo",
Namespace: ns,
ResourceVersion: "1",
},
2016-11-18 20:50:17 +00:00
Subsets: []v1.EndpointSubset{{
Addresses: []v1.EndpointAddress{{IP: "1.2.3.4", NodeName: &emptyNodeName, TargetRef: &v1.ObjectReference{Kind: "Pod", Name: "pod0", Namespace: ns}}},
Ports: []v1.EndpointPort{{Port: 8080, Protocol: "UDP"}},
}},
})
endpointsHandler.ValidateRequest(t, testapi.Default.ResourcePath("endpoints", ns, "foo"), "PUT", &data)
2014-11-18 17:49:00 +00:00
}
func TestSyncEndpointsItemsEmptySelectorSelectsAll(t *testing.T) {
2015-04-16 23:18:02 +00:00
ns := "other"
testServer, endpointsHandler := makeTestServer(t, ns,
2016-11-18 20:50:17 +00:00
serverResponse{http.StatusOK, &v1.Endpoints{
ObjectMeta: v1.ObjectMeta{
2014-11-18 17:49:00 +00:00
Name: "foo",
2015-04-16 23:18:02 +00:00
Namespace: ns,
2014-11-18 17:49:00 +00:00
ResourceVersion: "1",
},
2016-11-18 20:50:17 +00:00
Subsets: []v1.EndpointSubset{},
2014-11-18 17:49:00 +00:00
}})
defer testServer.Close()
2016-11-18 20:50:17 +00:00
client := clientset.NewForConfigOrDie(&restclient.Config{Host: testServer.URL, ContentConfig: restclient.ContentConfig{GroupVersion: &registered.GroupOrDie(v1.GroupName).GroupVersion}})
endpoints := NewEndpointControllerFromClient(client, controller.NoResyncPeriodFunc)
endpoints.podStoreSynced = alwaysReady
2016-04-07 12:15:21 +00:00
addPods(endpoints.podStore.Indexer, ns, 1, 1, 0)
2016-11-18 20:50:17 +00:00
endpoints.serviceStore.Indexer.Add(&v1.Service{
ObjectMeta: v1.ObjectMeta{Name: "foo", Namespace: ns},
Spec: v1.ServiceSpec{
2015-04-16 23:18:02 +00:00
Selector: map[string]string{},
2016-11-18 20:50:17 +00:00
Ports: []v1.ServicePort{{Port: 80, Protocol: "TCP", TargetPort: intstr.FromInt(8080)}},
2015-04-16 23:18:02 +00:00
},
})
endpoints.syncService(ns + "/foo")
2016-11-18 20:50:17 +00:00
data := runtime.EncodeOrDie(testapi.Default.Codec(), &v1.Endpoints{
ObjectMeta: v1.ObjectMeta{
2014-11-18 17:49:00 +00:00
Name: "foo",
2015-04-16 23:18:02 +00:00
Namespace: ns,
2014-11-18 17:49:00 +00:00
ResourceVersion: "1",
},
2016-11-18 20:50:17 +00:00
Subsets: []v1.EndpointSubset{{
Addresses: []v1.EndpointAddress{{IP: "1.2.3.4", NodeName: &emptyNodeName, TargetRef: &v1.ObjectReference{Kind: "Pod", Name: "pod0", Namespace: ns}}},
Ports: []v1.EndpointPort{{Port: 8080, Protocol: "TCP"}},
}},
2014-11-18 17:49:00 +00:00
})
endpointsHandler.ValidateRequest(t, testapi.Default.ResourcePath("endpoints", ns, "foo"), "PUT", &data)
2014-11-18 17:49:00 +00:00
}
func TestSyncEndpointsItemsEmptySelectorSelectsAllNotReady(t *testing.T) {
ns := "other"
testServer, endpointsHandler := makeTestServer(t, ns,
2016-11-18 20:50:17 +00:00
serverResponse{http.StatusOK, &v1.Endpoints{
ObjectMeta: v1.ObjectMeta{
Name: "foo",
Namespace: ns,
ResourceVersion: "1",
},
2016-11-18 20:50:17 +00:00
Subsets: []v1.EndpointSubset{},
}})
defer testServer.Close()
2016-11-18 20:50:17 +00:00
client := clientset.NewForConfigOrDie(&restclient.Config{Host: testServer.URL, ContentConfig: restclient.ContentConfig{GroupVersion: &registered.GroupOrDie(v1.GroupName).GroupVersion}})
endpoints := NewEndpointControllerFromClient(client, controller.NoResyncPeriodFunc)
endpoints.podStoreSynced = alwaysReady
2016-04-07 12:15:21 +00:00
addPods(endpoints.podStore.Indexer, ns, 0, 1, 1)
2016-11-18 20:50:17 +00:00
endpoints.serviceStore.Indexer.Add(&v1.Service{
ObjectMeta: v1.ObjectMeta{Name: "foo", Namespace: ns},
Spec: v1.ServiceSpec{
Selector: map[string]string{},
2016-11-18 20:50:17 +00:00
Ports: []v1.ServicePort{{Port: 80, Protocol: "TCP", TargetPort: intstr.FromInt(8080)}},
},
})
endpoints.syncService(ns + "/foo")
2016-11-18 20:50:17 +00:00
data := runtime.EncodeOrDie(testapi.Default.Codec(), &v1.Endpoints{
ObjectMeta: v1.ObjectMeta{
Name: "foo",
Namespace: ns,
ResourceVersion: "1",
},
2016-11-18 20:50:17 +00:00
Subsets: []v1.EndpointSubset{{
NotReadyAddresses: []v1.EndpointAddress{{IP: "1.2.3.4", NodeName: &emptyNodeName, TargetRef: &v1.ObjectReference{Kind: "Pod", Name: "pod0", Namespace: ns}}},
Ports: []v1.EndpointPort{{Port: 8080, Protocol: "TCP"}},
}},
})
endpointsHandler.ValidateRequest(t, testapi.Default.ResourcePath("endpoints", ns, "foo"), "PUT", &data)
}
func TestSyncEndpointsItemsEmptySelectorSelectsAllMixed(t *testing.T) {
ns := "other"
testServer, endpointsHandler := makeTestServer(t, ns,
2016-11-18 20:50:17 +00:00
serverResponse{http.StatusOK, &v1.Endpoints{
ObjectMeta: v1.ObjectMeta{
Name: "foo",
Namespace: ns,
ResourceVersion: "1",
},
2016-11-18 20:50:17 +00:00
Subsets: []v1.EndpointSubset{},
}})
defer testServer.Close()
2016-11-18 20:50:17 +00:00
client := clientset.NewForConfigOrDie(&restclient.Config{Host: testServer.URL, ContentConfig: restclient.ContentConfig{GroupVersion: &registered.GroupOrDie(v1.GroupName).GroupVersion}})
endpoints := NewEndpointControllerFromClient(client, controller.NoResyncPeriodFunc)
endpoints.podStoreSynced = alwaysReady
2016-04-07 12:15:21 +00:00
addPods(endpoints.podStore.Indexer, ns, 1, 1, 1)
2016-11-18 20:50:17 +00:00
endpoints.serviceStore.Indexer.Add(&v1.Service{
ObjectMeta: v1.ObjectMeta{Name: "foo", Namespace: ns},
Spec: v1.ServiceSpec{
Selector: map[string]string{},
2016-11-18 20:50:17 +00:00
Ports: []v1.ServicePort{{Port: 80, Protocol: "TCP", TargetPort: intstr.FromInt(8080)}},
},
})
endpoints.syncService(ns + "/foo")
2016-11-18 20:50:17 +00:00
data := runtime.EncodeOrDie(testapi.Default.Codec(), &v1.Endpoints{
ObjectMeta: v1.ObjectMeta{
Name: "foo",
Namespace: ns,
ResourceVersion: "1",
},
2016-11-18 20:50:17 +00:00
Subsets: []v1.EndpointSubset{{
Addresses: []v1.EndpointAddress{{IP: "1.2.3.4", NodeName: &emptyNodeName, TargetRef: &v1.ObjectReference{Kind: "Pod", Name: "pod0", Namespace: ns}}},
NotReadyAddresses: []v1.EndpointAddress{{IP: "1.2.3.5", NodeName: &emptyNodeName, TargetRef: &v1.ObjectReference{Kind: "Pod", Name: "pod1", Namespace: ns}}},
Ports: []v1.EndpointPort{{Port: 8080, Protocol: "TCP"}},
}},
})
endpointsHandler.ValidateRequest(t, testapi.Default.ResourcePath("endpoints", ns, "foo"), "PUT", &data)
}
func TestSyncEndpointsItemsPreexisting(t *testing.T) {
2015-04-16 23:18:02 +00:00
ns := "bar"
testServer, endpointsHandler := makeTestServer(t, ns,
2016-11-18 20:50:17 +00:00
serverResponse{http.StatusOK, &v1.Endpoints{
ObjectMeta: v1.ObjectMeta{
2014-10-22 17:02:02 +00:00
Name: "foo",
2015-04-16 23:18:02 +00:00
Namespace: ns,
ResourceVersion: "1",
},
2016-11-18 20:50:17 +00:00
Subsets: []v1.EndpointSubset{{
Addresses: []v1.EndpointAddress{{IP: "6.7.8.9", NodeName: &emptyNodeName}},
Ports: []v1.EndpointPort{{Port: 1000}},
}},
}})
defer testServer.Close()
2016-11-18 20:50:17 +00:00
client := clientset.NewForConfigOrDie(&restclient.Config{Host: testServer.URL, ContentConfig: restclient.ContentConfig{GroupVersion: &registered.GroupOrDie(v1.GroupName).GroupVersion}})
endpoints := NewEndpointControllerFromClient(client, controller.NoResyncPeriodFunc)
endpoints.podStoreSynced = alwaysReady
2016-04-07 12:15:21 +00:00
addPods(endpoints.podStore.Indexer, ns, 1, 1, 0)
2016-11-18 20:50:17 +00:00
endpoints.serviceStore.Indexer.Add(&v1.Service{
ObjectMeta: v1.ObjectMeta{Name: "foo", Namespace: ns},
Spec: v1.ServiceSpec{
2015-04-16 23:18:02 +00:00
Selector: map[string]string{"foo": "bar"},
2016-11-18 20:50:17 +00:00
Ports: []v1.ServicePort{{Port: 80, Protocol: "TCP", TargetPort: intstr.FromInt(8080)}},
2015-04-16 23:18:02 +00:00
},
})
endpoints.syncService(ns + "/foo")
2016-11-18 20:50:17 +00:00
data := runtime.EncodeOrDie(testapi.Default.Codec(), &v1.Endpoints{
ObjectMeta: v1.ObjectMeta{
2014-10-22 17:02:02 +00:00
Name: "foo",
2015-04-16 23:18:02 +00:00
Namespace: ns,
ResourceVersion: "1",
},
2016-11-18 20:50:17 +00:00
Subsets: []v1.EndpointSubset{{
Addresses: []v1.EndpointAddress{{IP: "1.2.3.4", NodeName: &emptyNodeName, TargetRef: &v1.ObjectReference{Kind: "Pod", Name: "pod0", Namespace: ns}}},
Ports: []v1.EndpointPort{{Port: 8080, Protocol: "TCP"}},
}},
})
endpointsHandler.ValidateRequest(t, testapi.Default.ResourcePath("endpoints", ns, "foo"), "PUT", &data)
}
func TestSyncEndpointsItemsPreexistingIdentical(t *testing.T) {
2016-11-18 20:50:17 +00:00
ns := v1.NamespaceDefault
testServer, endpointsHandler := makeTestServer(t, v1.NamespaceDefault,
serverResponse{http.StatusOK, &v1.Endpoints{
ObjectMeta: v1.ObjectMeta{
ResourceVersion: "1",
2015-04-16 23:18:02 +00:00
Name: "foo",
Namespace: ns,
},
2016-11-18 20:50:17 +00:00
Subsets: []v1.EndpointSubset{{
Addresses: []v1.EndpointAddress{{IP: "1.2.3.4", NodeName: &emptyNodeName, TargetRef: &v1.ObjectReference{Kind: "Pod", Name: "pod0", Namespace: ns}}},
Ports: []v1.EndpointPort{{Port: 8080, Protocol: "TCP"}},
}},
}})
defer testServer.Close()
2016-11-18 20:50:17 +00:00
client := clientset.NewForConfigOrDie(&restclient.Config{Host: testServer.URL, ContentConfig: restclient.ContentConfig{GroupVersion: &registered.GroupOrDie(v1.GroupName).GroupVersion}})
endpoints := NewEndpointControllerFromClient(client, controller.NoResyncPeriodFunc)
endpoints.podStoreSynced = alwaysReady
2016-11-18 20:50:17 +00:00
addPods(endpoints.podStore.Indexer, v1.NamespaceDefault, 1, 1, 0)
endpoints.serviceStore.Indexer.Add(&v1.Service{
ObjectMeta: v1.ObjectMeta{Name: "foo", Namespace: v1.NamespaceDefault},
Spec: v1.ServiceSpec{
2015-04-16 23:18:02 +00:00
Selector: map[string]string{"foo": "bar"},
2016-11-18 20:50:17 +00:00
Ports: []v1.ServicePort{{Port: 80, Protocol: "TCP", TargetPort: intstr.FromInt(8080)}},
2015-04-16 23:18:02 +00:00
},
})
endpoints.syncService(ns + "/foo")
2016-11-18 20:50:17 +00:00
endpointsHandler.ValidateRequest(t, testapi.Default.ResourcePath("endpoints", v1.NamespaceDefault, "foo"), "GET", nil)
}
func TestSyncEndpointsItems(t *testing.T) {
2015-04-16 23:18:02 +00:00
ns := "other"
testServer, endpointsHandler := makeTestServer(t, ns,
2016-11-18 20:50:17 +00:00
serverResponse{http.StatusOK, &v1.Endpoints{}})
defer testServer.Close()
2016-11-18 20:50:17 +00:00
client := clientset.NewForConfigOrDie(&restclient.Config{Host: testServer.URL, ContentConfig: restclient.ContentConfig{GroupVersion: &registered.GroupOrDie(v1.GroupName).GroupVersion}})
endpoints := NewEndpointControllerFromClient(client, controller.NoResyncPeriodFunc)
endpoints.podStoreSynced = alwaysReady
2016-04-07 12:15:21 +00:00
addPods(endpoints.podStore.Indexer, ns, 3, 2, 0)
addPods(endpoints.podStore.Indexer, "blah", 5, 2, 0) // make sure these aren't found!
2016-11-18 20:50:17 +00:00
endpoints.serviceStore.Indexer.Add(&v1.Service{
ObjectMeta: v1.ObjectMeta{Name: "foo", Namespace: ns},
Spec: v1.ServiceSpec{
2015-04-16 23:18:02 +00:00
Selector: map[string]string{"foo": "bar"},
2016-11-18 20:50:17 +00:00
Ports: []v1.ServicePort{
{Name: "port0", Port: 80, Protocol: "TCP", TargetPort: intstr.FromInt(8080)},
{Name: "port1", Port: 88, Protocol: "TCP", TargetPort: intstr.FromInt(8088)},
2015-04-16 23:18:02 +00:00
},
},
})
endpoints.syncService("other/foo")
2016-11-18 20:50:17 +00:00
expectedSubsets := []v1.EndpointSubset{{
Addresses: []v1.EndpointAddress{
{IP: "1.2.3.4", NodeName: &emptyNodeName, TargetRef: &v1.ObjectReference{Kind: "Pod", Name: "pod0", Namespace: ns}},
{IP: "1.2.3.5", NodeName: &emptyNodeName, TargetRef: &v1.ObjectReference{Kind: "Pod", Name: "pod1", Namespace: ns}},
{IP: "1.2.3.6", NodeName: &emptyNodeName, TargetRef: &v1.ObjectReference{Kind: "Pod", Name: "pod2", Namespace: ns}},
},
2016-11-18 20:50:17 +00:00
Ports: []v1.EndpointPort{
2015-03-13 15:16:41 +00:00
{Name: "port0", Port: 8080, Protocol: "TCP"},
{Name: "port1", Port: 8088, Protocol: "TCP"},
},
}}
2016-11-18 20:50:17 +00:00
data := runtime.EncodeOrDie(testapi.Default.Codec(), &v1.Endpoints{
ObjectMeta: v1.ObjectMeta{
ResourceVersion: "",
},
Subsets: endptspkg.SortSubsets(expectedSubsets),
})
// endpointsHandler should get 2 requests - one for "GET" and the next for "POST".
endpointsHandler.ValidateRequestCount(t, 2)
endpointsHandler.ValidateRequest(t, testapi.Default.ResourcePath("endpoints", ns, ""), "POST", &data)
2014-06-06 23:40:48 +00:00
}
2015-04-16 23:18:02 +00:00
func TestSyncEndpointsItemsWithLabels(t *testing.T) {
ns := "other"
testServer, endpointsHandler := makeTestServer(t, ns,
2016-11-18 20:50:17 +00:00
serverResponse{http.StatusOK, &v1.Endpoints{}})
defer testServer.Close()
2016-11-18 20:50:17 +00:00
client := clientset.NewForConfigOrDie(&restclient.Config{Host: testServer.URL, ContentConfig: restclient.ContentConfig{GroupVersion: &registered.GroupOrDie(v1.GroupName).GroupVersion}})
endpoints := NewEndpointControllerFromClient(client, controller.NoResyncPeriodFunc)
endpoints.podStoreSynced = alwaysReady
2016-04-07 12:15:21 +00:00
addPods(endpoints.podStore.Indexer, ns, 3, 2, 0)
2015-04-16 23:18:02 +00:00
serviceLabels := map[string]string{"foo": "bar"}
2016-11-18 20:50:17 +00:00
endpoints.serviceStore.Indexer.Add(&v1.Service{
ObjectMeta: v1.ObjectMeta{
2015-04-16 23:18:02 +00:00
Name: "foo",
Namespace: ns,
Labels: serviceLabels,
},
2016-11-18 20:50:17 +00:00
Spec: v1.ServiceSpec{
2015-04-16 23:18:02 +00:00
Selector: map[string]string{"foo": "bar"},
2016-11-18 20:50:17 +00:00
Ports: []v1.ServicePort{
{Name: "port0", Port: 80, Protocol: "TCP", TargetPort: intstr.FromInt(8080)},
{Name: "port1", Port: 88, Protocol: "TCP", TargetPort: intstr.FromInt(8088)},
},
},
2015-04-16 23:18:02 +00:00
})
endpoints.syncService(ns + "/foo")
2016-11-18 20:50:17 +00:00
expectedSubsets := []v1.EndpointSubset{{
Addresses: []v1.EndpointAddress{
{IP: "1.2.3.4", NodeName: &emptyNodeName, TargetRef: &v1.ObjectReference{Kind: "Pod", Name: "pod0", Namespace: ns}},
{IP: "1.2.3.5", NodeName: &emptyNodeName, TargetRef: &v1.ObjectReference{Kind: "Pod", Name: "pod1", Namespace: ns}},
{IP: "1.2.3.6", NodeName: &emptyNodeName, TargetRef: &v1.ObjectReference{Kind: "Pod", Name: "pod2", Namespace: ns}},
},
2016-11-18 20:50:17 +00:00
Ports: []v1.EndpointPort{
{Name: "port0", Port: 8080, Protocol: "TCP"},
{Name: "port1", Port: 8088, Protocol: "TCP"},
},
}}
2016-11-18 20:50:17 +00:00
data := runtime.EncodeOrDie(testapi.Default.Codec(), &v1.Endpoints{
ObjectMeta: v1.ObjectMeta{
ResourceVersion: "",
2015-04-16 23:18:02 +00:00
Labels: serviceLabels,
},
Subsets: endptspkg.SortSubsets(expectedSubsets),
})
// endpointsHandler should get 2 requests - one for "GET" and the next for "POST".
endpointsHandler.ValidateRequestCount(t, 2)
endpointsHandler.ValidateRequest(t, testapi.Default.ResourcePath("endpoints", ns, ""), "POST", &data)
}
func TestSyncEndpointsItemsPreexistingLabelsChange(t *testing.T) {
2015-04-16 23:18:02 +00:00
ns := "bar"
testServer, endpointsHandler := makeTestServer(t, ns,
2016-11-18 20:50:17 +00:00
serverResponse{http.StatusOK, &v1.Endpoints{
ObjectMeta: v1.ObjectMeta{
Name: "foo",
2015-04-16 23:18:02 +00:00
Namespace: ns,
ResourceVersion: "1",
Labels: map[string]string{
"foo": "bar",
},
},
2016-11-18 20:50:17 +00:00
Subsets: []v1.EndpointSubset{{
Addresses: []v1.EndpointAddress{{IP: "6.7.8.9", NodeName: &emptyNodeName}},
Ports: []v1.EndpointPort{{Port: 1000}},
}},
}})
defer testServer.Close()
2016-11-18 20:50:17 +00:00
client := clientset.NewForConfigOrDie(&restclient.Config{Host: testServer.URL, ContentConfig: restclient.ContentConfig{GroupVersion: &registered.GroupOrDie(v1.GroupName).GroupVersion}})
endpoints := NewEndpointControllerFromClient(client, controller.NoResyncPeriodFunc)
endpoints.podStoreSynced = alwaysReady
2016-04-07 12:15:21 +00:00
addPods(endpoints.podStore.Indexer, ns, 1, 1, 0)
2015-04-16 23:18:02 +00:00
serviceLabels := map[string]string{"baz": "blah"}
2016-11-18 20:50:17 +00:00
endpoints.serviceStore.Indexer.Add(&v1.Service{
ObjectMeta: v1.ObjectMeta{
2015-04-16 23:18:02 +00:00
Name: "foo",
Namespace: ns,
Labels: serviceLabels,
},
2016-11-18 20:50:17 +00:00
Spec: v1.ServiceSpec{
2015-04-16 23:18:02 +00:00
Selector: map[string]string{"foo": "bar"},
2016-11-18 20:50:17 +00:00
Ports: []v1.ServicePort{{Port: 80, Protocol: "TCP", TargetPort: intstr.FromInt(8080)}},
2015-04-16 23:18:02 +00:00
},
})
endpoints.syncService(ns + "/foo")
2016-11-18 20:50:17 +00:00
data := runtime.EncodeOrDie(testapi.Default.Codec(), &v1.Endpoints{
ObjectMeta: v1.ObjectMeta{
Name: "foo",
2015-04-16 23:18:02 +00:00
Namespace: ns,
ResourceVersion: "1",
2015-04-16 23:18:02 +00:00
Labels: serviceLabels,
},
2016-11-18 20:50:17 +00:00
Subsets: []v1.EndpointSubset{{
Addresses: []v1.EndpointAddress{{IP: "1.2.3.4", NodeName: &emptyNodeName, TargetRef: &v1.ObjectReference{Kind: "Pod", Name: "pod0", Namespace: ns}}},
Ports: []v1.EndpointPort{{Port: 8080, Protocol: "TCP"}},
}},
})
endpointsHandler.ValidateRequest(t, testapi.Default.ResourcePath("endpoints", ns, "foo"), "PUT", &data)
}