2015-05-12 21:35:34 +00:00
|
|
|
/*
|
|
|
|
Copyright 2015 The Kubernetes Authors All rights reserved.
|
|
|
|
|
|
|
|
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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
2015-05-18 17:03:30 +00:00
|
|
|
"net/http"
|
2015-05-12 21:35:34 +00:00
|
|
|
"path"
|
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
kapi "github.com/GoogleCloudPlatform/kubernetes/pkg/api"
|
2015-05-18 17:03:30 +00:00
|
|
|
"github.com/GoogleCloudPlatform/kubernetes/pkg/client/cache"
|
2015-05-12 21:35:34 +00:00
|
|
|
"github.com/coreos/go-etcd/etcd"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
|
|
|
type fakeEtcdClient struct {
|
|
|
|
// TODO: Convert this to real fs to better simulate etcd behavior.
|
|
|
|
writes map[string]string
|
|
|
|
}
|
|
|
|
|
2015-05-18 17:03:30 +00:00
|
|
|
func (ec *fakeEtcdClient) Set(key, value string, ttl uint64) (*etcd.Response, error) {
|
|
|
|
ec.writes[key] = value
|
2015-05-12 21:35:34 +00:00
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
2015-05-18 17:03:30 +00:00
|
|
|
func (ec *fakeEtcdClient) Delete(key string, recursive bool) (*etcd.Response, error) {
|
2015-05-12 21:35:34 +00:00
|
|
|
for p := range ec.writes {
|
2015-05-18 17:03:30 +00:00
|
|
|
if (recursive && strings.HasPrefix(p, key)) || (!recursive && p == key) {
|
2015-05-12 21:35:34 +00:00
|
|
|
delete(ec.writes, p)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
2015-05-18 17:03:30 +00:00
|
|
|
func (ec *fakeEtcdClient) RawGet(key string, sort, recursive bool) (*etcd.RawResponse, error) {
|
|
|
|
count := 0
|
|
|
|
for path := range ec.writes {
|
|
|
|
if strings.HasPrefix(path, key) {
|
|
|
|
count++
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if count == 0 {
|
|
|
|
return &etcd.RawResponse{StatusCode: http.StatusNotFound}, nil
|
|
|
|
}
|
|
|
|
return &etcd.RawResponse{StatusCode: http.StatusOK}, nil
|
|
|
|
}
|
|
|
|
|
2015-05-12 21:35:34 +00:00
|
|
|
const (
|
2015-05-18 17:03:30 +00:00
|
|
|
testDomain = "cluster.local."
|
2015-05-13 20:33:38 +00:00
|
|
|
basePath = "/skydns/local/cluster"
|
|
|
|
serviceSubDomain = "svc"
|
2015-05-12 21:35:34 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func newKube2Sky(ec etcdClient) *kube2sky {
|
|
|
|
return &kube2sky{
|
|
|
|
etcdClient: ec,
|
|
|
|
domain: testDomain,
|
|
|
|
etcdMutationTimeout: time.Second,
|
2015-05-18 17:03:30 +00:00
|
|
|
endpointsStore: cache.NewStore(cache.MetaNamespaceKeyFunc),
|
|
|
|
servicesStore: cache.NewStore(cache.MetaNamespaceKeyFunc),
|
2015-05-12 21:35:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-13 20:33:38 +00:00
|
|
|
func getEtcdOldStylePath(name, namespace string) string {
|
|
|
|
return path.Join(basePath, namespace, name)
|
2015-05-12 21:35:34 +00:00
|
|
|
}
|
|
|
|
|
2015-05-13 20:33:38 +00:00
|
|
|
func getEtcdNewStylePath(name, namespace string) string {
|
|
|
|
return path.Join(basePath, serviceSubDomain, namespace, name)
|
2015-05-12 21:35:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type hostPort struct {
|
|
|
|
Host string `json:"host"`
|
|
|
|
Port int `json:"port"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func getHostPort(service *kapi.Service) *hostPort {
|
|
|
|
return &hostPort{
|
|
|
|
Host: service.Spec.PortalIP,
|
|
|
|
Port: service.Spec.Ports[0].Port,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func getHostPortFromString(data string) (*hostPort, error) {
|
|
|
|
var res hostPort
|
|
|
|
err := json.Unmarshal([]byte(data), &res)
|
|
|
|
return &res, err
|
|
|
|
}
|
|
|
|
|
2015-05-13 20:33:38 +00:00
|
|
|
func assertDnsServiceEntryInEtcd(t *testing.T, ec *fakeEtcdClient, serviceName, namespace string, expectedHostPort *hostPort) {
|
|
|
|
oldStyleKey := getEtcdOldStylePath(serviceName, namespace)
|
|
|
|
val, exists := ec.writes[oldStyleKey]
|
|
|
|
require.True(t, exists)
|
|
|
|
actualHostPort, err := getHostPortFromString(val)
|
|
|
|
require.NoError(t, err)
|
|
|
|
assert.Equal(t, actualHostPort, expectedHostPort)
|
|
|
|
|
|
|
|
newStyleKey := getEtcdNewStylePath(serviceName, namespace)
|
|
|
|
val, exists = ec.writes[newStyleKey]
|
|
|
|
require.True(t, exists)
|
|
|
|
actualHostPort, err = getHostPortFromString(val)
|
|
|
|
require.NoError(t, err)
|
|
|
|
assert.Equal(t, actualHostPort, expectedHostPort)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestHeadlessService(t *testing.T) {
|
|
|
|
const (
|
|
|
|
testService = "testService"
|
|
|
|
testNamespace = "default"
|
|
|
|
)
|
|
|
|
ec := &fakeEtcdClient{make(map[string]string)}
|
|
|
|
k2s := newKube2Sky(ec)
|
|
|
|
service := kapi.Service{
|
|
|
|
ObjectMeta: kapi.ObjectMeta{
|
2015-05-18 17:03:30 +00:00
|
|
|
Name: testService,
|
|
|
|
Namespace: testNamespace,
|
|
|
|
},
|
|
|
|
Spec: kapi.ServiceSpec{
|
|
|
|
PortalIP: "None",
|
|
|
|
Ports: []kapi.ServicePort{
|
|
|
|
{Port: 80},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
assert.NoError(t, k2s.servicesStore.Add(&service))
|
|
|
|
endpoints := kapi.Endpoints{
|
|
|
|
ObjectMeta: service.ObjectMeta,
|
|
|
|
Subsets: []kapi.EndpointSubset{
|
|
|
|
{
|
|
|
|
Addresses: []kapi.EndpointAddress{
|
|
|
|
{IP: "10.0.0.1"},
|
|
|
|
{IP: "10.0.0.2"},
|
|
|
|
},
|
|
|
|
Ports: []kapi.EndpointPort{
|
|
|
|
{Port: 80},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Addresses: []kapi.EndpointAddress{
|
|
|
|
{IP: "10.0.0.3"},
|
|
|
|
{IP: "10.0.0.4"},
|
|
|
|
},
|
|
|
|
Ports: []kapi.EndpointPort{
|
|
|
|
{Port: 8080},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
// We expect 4 records with "svc" subdomain and 4 records without
|
|
|
|
// "svc" subdomain.
|
|
|
|
expectedDNSRecords := 8
|
|
|
|
assert.NoError(t, k2s.endpointsStore.Add(&endpoints))
|
|
|
|
k2s.newService(&service)
|
|
|
|
assert.Equal(t, expectedDNSRecords, len(ec.writes))
|
|
|
|
k2s.removeService(&service)
|
|
|
|
assert.Empty(t, ec.writes)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestHeadlessServiceEndpointsUpdate(t *testing.T) {
|
|
|
|
const (
|
|
|
|
testService = "testService"
|
|
|
|
testNamespace = "default"
|
|
|
|
)
|
|
|
|
ec := &fakeEtcdClient{make(map[string]string)}
|
|
|
|
k2s := newKube2Sky(ec)
|
|
|
|
service := kapi.Service{
|
|
|
|
ObjectMeta: kapi.ObjectMeta{
|
|
|
|
Name: testService,
|
|
|
|
Namespace: testNamespace,
|
|
|
|
},
|
|
|
|
Spec: kapi.ServiceSpec{
|
|
|
|
PortalIP: "None",
|
|
|
|
Ports: []kapi.ServicePort{
|
|
|
|
{Port: 80},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
assert.NoError(t, k2s.servicesStore.Add(&service))
|
|
|
|
endpoints := kapi.Endpoints{
|
|
|
|
ObjectMeta: service.ObjectMeta,
|
|
|
|
Subsets: []kapi.EndpointSubset{
|
|
|
|
{
|
|
|
|
Addresses: []kapi.EndpointAddress{
|
|
|
|
{IP: "10.0.0.1"},
|
|
|
|
{IP: "10.0.0.2"},
|
|
|
|
},
|
|
|
|
Ports: []kapi.EndpointPort{
|
|
|
|
{Port: 80},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
expectedDNSRecords := 4
|
|
|
|
assert.NoError(t, k2s.endpointsStore.Add(&endpoints))
|
|
|
|
k2s.newService(&service)
|
|
|
|
assert.Equal(t, expectedDNSRecords, len(ec.writes))
|
|
|
|
endpoints.Subsets = append(endpoints.Subsets,
|
|
|
|
kapi.EndpointSubset{
|
|
|
|
Addresses: []kapi.EndpointAddress{
|
|
|
|
{IP: "10.0.0.3"},
|
|
|
|
{IP: "10.0.0.4"},
|
|
|
|
},
|
|
|
|
Ports: []kapi.EndpointPort{
|
|
|
|
{Port: 8080},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
)
|
|
|
|
expectedDNSRecords = 8
|
|
|
|
k2s.handleEndpointAdd(&endpoints)
|
|
|
|
|
|
|
|
assert.Equal(t, expectedDNSRecords, len(ec.writes))
|
|
|
|
k2s.removeService(&service)
|
|
|
|
assert.Empty(t, ec.writes)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestHeadlessServiceWithDelayedEndpointsAddition(t *testing.T) {
|
|
|
|
const (
|
|
|
|
testService = "testService"
|
|
|
|
testNamespace = "default"
|
|
|
|
)
|
|
|
|
ec := &fakeEtcdClient{make(map[string]string)}
|
|
|
|
k2s := newKube2Sky(ec)
|
|
|
|
service := kapi.Service{
|
|
|
|
ObjectMeta: kapi.ObjectMeta{
|
|
|
|
Name: testService,
|
2015-05-13 20:33:38 +00:00
|
|
|
Namespace: testNamespace,
|
|
|
|
},
|
2015-05-18 17:03:30 +00:00
|
|
|
Spec: kapi.ServiceSpec{
|
|
|
|
PortalIP: "None",
|
|
|
|
Ports: []kapi.ServicePort{
|
|
|
|
{Port: 80},
|
|
|
|
},
|
|
|
|
},
|
2015-05-13 20:33:38 +00:00
|
|
|
}
|
2015-05-18 17:03:30 +00:00
|
|
|
assert.NoError(t, k2s.servicesStore.Add(&service))
|
|
|
|
// Headless service DNS records should not be created since
|
|
|
|
// corresponding endpoints object doesn't exist.
|
2015-05-13 20:33:38 +00:00
|
|
|
k2s.newService(&service)
|
|
|
|
assert.Empty(t, ec.writes)
|
2015-05-18 17:03:30 +00:00
|
|
|
|
|
|
|
// Add an endpoints object for the service.
|
|
|
|
endpoints := kapi.Endpoints{
|
|
|
|
ObjectMeta: service.ObjectMeta,
|
|
|
|
Subsets: []kapi.EndpointSubset{
|
|
|
|
{
|
|
|
|
Addresses: []kapi.EndpointAddress{
|
|
|
|
{IP: "10.0.0.1"},
|
|
|
|
{IP: "10.0.0.2"},
|
|
|
|
},
|
|
|
|
Ports: []kapi.EndpointPort{
|
|
|
|
{Port: 80},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Addresses: []kapi.EndpointAddress{
|
|
|
|
{IP: "10.0.0.3"},
|
|
|
|
{IP: "10.0.0.4"},
|
|
|
|
},
|
|
|
|
Ports: []kapi.EndpointPort{
|
|
|
|
{Port: 8080},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
// We expect 4 records with "svc" subdomain and 4 records without
|
|
|
|
// "svc" subdomain.
|
|
|
|
expectedDNSRecords := 8
|
|
|
|
k2s.handleEndpointAdd(&endpoints)
|
|
|
|
assert.Equal(t, expectedDNSRecords, len(ec.writes))
|
2015-05-13 20:33:38 +00:00
|
|
|
}
|
|
|
|
|
2015-05-18 17:03:30 +00:00
|
|
|
// TODO: Test service updates for headless services.
|
|
|
|
// TODO: Test headless service addition with delayed endpoints addition
|
|
|
|
|
2015-05-12 21:35:34 +00:00
|
|
|
func TestAddSinglePortService(t *testing.T) {
|
|
|
|
const (
|
|
|
|
testService = "testService"
|
|
|
|
testNamespace = "default"
|
|
|
|
)
|
|
|
|
ec := &fakeEtcdClient{make(map[string]string)}
|
|
|
|
k2s := newKube2Sky(ec)
|
|
|
|
service := kapi.Service{
|
|
|
|
ObjectMeta: kapi.ObjectMeta{
|
|
|
|
Name: testService,
|
|
|
|
Namespace: testNamespace,
|
|
|
|
},
|
|
|
|
Spec: kapi.ServiceSpec{
|
|
|
|
Ports: []kapi.ServicePort{
|
|
|
|
{
|
|
|
|
Port: 80,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
PortalIP: "1.2.3.4",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
k2s.newService(&service)
|
|
|
|
expectedValue := getHostPort(&service)
|
2015-05-13 20:33:38 +00:00
|
|
|
assertDnsServiceEntryInEtcd(t, ec, testService, testNamespace, expectedValue)
|
2015-05-12 21:35:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestUpdateSinglePortService(t *testing.T) {
|
|
|
|
const (
|
|
|
|
testService = "testService"
|
|
|
|
testNamespace = "default"
|
|
|
|
)
|
|
|
|
ec := &fakeEtcdClient{make(map[string]string)}
|
|
|
|
k2s := newKube2Sky(ec)
|
|
|
|
service := kapi.Service{
|
|
|
|
ObjectMeta: kapi.ObjectMeta{
|
|
|
|
Name: testService,
|
|
|
|
Namespace: testNamespace,
|
|
|
|
},
|
|
|
|
Spec: kapi.ServiceSpec{
|
|
|
|
Ports: []kapi.ServicePort{
|
|
|
|
{
|
|
|
|
Port: 80,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
PortalIP: "1.2.3.4",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
k2s.newService(&service)
|
2015-05-13 20:33:38 +00:00
|
|
|
assert.Len(t, ec.writes, 2)
|
2015-05-12 21:35:34 +00:00
|
|
|
service.Spec.PortalIP = "0.0.0.0"
|
|
|
|
k2s.newService(&service)
|
|
|
|
expectedValue := getHostPort(&service)
|
2015-05-13 20:33:38 +00:00
|
|
|
assertDnsServiceEntryInEtcd(t, ec, testService, testNamespace, expectedValue)
|
2015-05-12 21:35:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestDeleteSinglePortService(t *testing.T) {
|
|
|
|
const (
|
|
|
|
testService = "testService"
|
|
|
|
testNamespace = "default"
|
|
|
|
)
|
|
|
|
ec := &fakeEtcdClient{make(map[string]string)}
|
|
|
|
k2s := newKube2Sky(ec)
|
|
|
|
service := kapi.Service{
|
|
|
|
ObjectMeta: kapi.ObjectMeta{
|
|
|
|
Name: testService,
|
|
|
|
Namespace: testNamespace,
|
|
|
|
},
|
|
|
|
Spec: kapi.ServiceSpec{
|
|
|
|
Ports: []kapi.ServicePort{
|
|
|
|
{
|
|
|
|
Port: 80,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
PortalIP: "1.2.3.4",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
// Add the service
|
|
|
|
k2s.newService(&service)
|
2015-05-13 20:33:38 +00:00
|
|
|
// two entries should get created, one with the svc subdomain (new-style)
|
|
|
|
// , and one without the svc subdomain (old-style)
|
|
|
|
assert.Len(t, ec.writes, 2)
|
2015-05-12 21:35:34 +00:00
|
|
|
// Delete the service
|
|
|
|
k2s.removeService(&service)
|
|
|
|
assert.Empty(t, ec.writes)
|
|
|
|
}
|
2015-05-18 17:03:30 +00:00
|
|
|
|
|
|
|
func TestBuildDNSName(t *testing.T) {
|
|
|
|
expectedDNSName := "name.ns.svc.cluster.local."
|
|
|
|
assert.Equal(t, expectedDNSName, buildDNSNameString("local.", "cluster", "svc", "ns", "name"))
|
|
|
|
newExpectedDNSName := "00.name.ns.svc.cluster.local."
|
|
|
|
assert.Equal(t, newExpectedDNSName, buildDNSNameString(expectedDNSName, "00"))
|
|
|
|
}
|