
Instead of endpoints being a flat list, it is now a list of "subsets" where each is a struct of {Addresses, Ports}. To generate the list of endpoints you need to take union of the Cartesian products of the subsets. This is compact in the vast majority of cases, yet still represents named ports and corner cases (e.g. each pod has a different port number). This also stores subsets in a deterministic order (sorted by hash) to avoid spurious updates and comparison problems. This is a fully compatible change - old objects and clients will keepworking as long as they don't need the new functionality. This is the prep for multi-port Services, which will add API to produce endpoints in this new structure.
138 lines
4.2 KiB
Go
138 lines
4.2 KiB
Go
/*
|
|
Copyright 2015 Google Inc. 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 v1beta3_test
|
|
|
|
import (
|
|
"reflect"
|
|
"testing"
|
|
|
|
current "github.com/GoogleCloudPlatform/kubernetes/pkg/api/v1beta3"
|
|
"github.com/GoogleCloudPlatform/kubernetes/pkg/runtime"
|
|
"github.com/GoogleCloudPlatform/kubernetes/pkg/util"
|
|
)
|
|
|
|
func roundTrip(t *testing.T, obj runtime.Object) runtime.Object {
|
|
data, err := current.Codec.Encode(obj)
|
|
if err != nil {
|
|
t.Errorf("%v\n %#v", err, obj)
|
|
return nil
|
|
}
|
|
obj2 := reflect.New(reflect.TypeOf(obj).Elem()).Interface().(runtime.Object)
|
|
err = current.Codec.DecodeInto(data, obj2)
|
|
if err != nil {
|
|
t.Errorf("%v\nData: %s\nSource: %#v", err, string(data), obj)
|
|
return nil
|
|
}
|
|
return obj2
|
|
}
|
|
|
|
func TestSetDefaultService(t *testing.T) {
|
|
svc := ¤t.Service{}
|
|
obj2 := roundTrip(t, runtime.Object(svc))
|
|
svc2 := obj2.(*current.Service)
|
|
if svc2.Spec.Protocol != current.ProtocolTCP {
|
|
t.Errorf("Expected default protocol :%s, got: %s", current.ProtocolTCP, svc2.Spec.Protocol)
|
|
}
|
|
if svc2.Spec.SessionAffinity != current.AffinityTypeNone {
|
|
t.Errorf("Expected default sesseion affinity type:%s, got: %s", current.AffinityTypeNone, svc2.Spec.SessionAffinity)
|
|
}
|
|
}
|
|
|
|
func TestSetDefaultSecret(t *testing.T) {
|
|
s := ¤t.Secret{}
|
|
obj2 := roundTrip(t, runtime.Object(s))
|
|
s2 := obj2.(*current.Secret)
|
|
|
|
if s2.Type != current.SecretTypeOpaque {
|
|
t.Errorf("Expected secret type %v, got %v", current.SecretTypeOpaque, s2.Type)
|
|
}
|
|
}
|
|
|
|
func TestSetDefaulEndpointsProtocol(t *testing.T) {
|
|
in := ¤t.Endpoints{Subsets: []current.EndpointSubset{
|
|
{Ports: []current.EndpointPort{{}, {Protocol: "UDP"}, {}}},
|
|
}}
|
|
obj := roundTrip(t, runtime.Object(in))
|
|
out := obj.(*current.Endpoints)
|
|
|
|
for i := range out.Subsets {
|
|
for j := range out.Subsets[i].Ports {
|
|
if in.Subsets[i].Ports[j].Protocol == "" {
|
|
if out.Subsets[i].Ports[j].Protocol != current.ProtocolTCP {
|
|
t.Errorf("Expected protocol %s, got %s", current.ProtocolTCP, out.Subsets[i].Ports[j].Protocol)
|
|
}
|
|
} else {
|
|
if out.Subsets[i].Ports[j].Protocol != in.Subsets[i].Ports[j].Protocol {
|
|
t.Errorf("Expected protocol %s, got %s", in.Subsets[i].Ports[j].Protocol, out.Subsets[i].Ports[j].Protocol)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestSetDefaulServiceTargetPort(t *testing.T) {
|
|
in := ¤t.Service{Spec: current.ServiceSpec{Port: 1234}}
|
|
obj := roundTrip(t, runtime.Object(in))
|
|
out := obj.(*current.Service)
|
|
if out.Spec.TargetPort.Kind != util.IntstrInt || out.Spec.TargetPort.IntVal != 1234 {
|
|
t.Errorf("Expected TargetPort to be defaulted, got %s", out.Spec.TargetPort)
|
|
}
|
|
|
|
in = ¤t.Service{Spec: current.ServiceSpec{Port: 1234, TargetPort: util.NewIntOrStringFromInt(5678)}}
|
|
obj = roundTrip(t, runtime.Object(in))
|
|
out = obj.(*current.Service)
|
|
if out.Spec.TargetPort.Kind != util.IntstrInt || out.Spec.TargetPort.IntVal != 5678 {
|
|
t.Errorf("Expected TargetPort to be unchanged, got %s", out.Spec.TargetPort)
|
|
}
|
|
}
|
|
|
|
func TestSetDefaultNamespace(t *testing.T) {
|
|
s := ¤t.Namespace{}
|
|
obj2 := roundTrip(t, runtime.Object(s))
|
|
s2 := obj2.(*current.Namespace)
|
|
|
|
if s2.Status.Phase != current.NamespaceActive {
|
|
t.Errorf("Expected phase %v, got %v", current.NamespaceActive, s2.Status.Phase)
|
|
}
|
|
}
|
|
|
|
func TestSetDefaultPodSpecHostNetwork(t *testing.T) {
|
|
portNum := 8080
|
|
s := current.PodSpec{}
|
|
s.HostNetwork = true
|
|
s.Containers = []current.Container{
|
|
{
|
|
Ports: []current.ContainerPort{
|
|
{
|
|
ContainerPort: portNum,
|
|
},
|
|
},
|
|
},
|
|
}
|
|
pod := ¤t.Pod{
|
|
Spec: s,
|
|
}
|
|
obj2 := roundTrip(t, runtime.Object(pod))
|
|
pod2 := obj2.(*current.Pod)
|
|
s2 := pod2.Spec
|
|
|
|
hostPortNum := s2.Containers[0].Ports[0].HostPort
|
|
if hostPortNum != portNum {
|
|
t.Errorf("Expected container port to be defaulted, was made %d instead of %d", hostPortNum, portNum)
|
|
}
|
|
}
|