Commit 186818d7 authored by Tim Hockin's avatar Tim Hockin

WIP: Implement multi-port Services

parent 9ed87612
...@@ -5722,4 +5722,4 @@ ...@@ -5722,4 +5722,4 @@
} }
} }
} }
} }
\ No newline at end of file
...@@ -6,8 +6,9 @@ metadata: ...@@ -6,8 +6,9 @@ metadata:
kubernetes.io/cluster-service: "true" kubernetes.io/cluster-service: "true"
name: monitoring-grafana name: monitoring-grafana
spec: spec:
targetPort: 80 ports:
port: 80 - port: 80
targetPort: 80
selector: selector:
name: influxGrafana name: influxGrafana
kubernetes.io/cluster-service: "true" kubernetes.io/cluster-service: "true"
......
...@@ -6,8 +6,9 @@ metadata: ...@@ -6,8 +6,9 @@ metadata:
kubernetes.io/cluster-service: "true" kubernetes.io/cluster-service: "true"
name: monitoring-heapster name: monitoring-heapster
spec: spec:
targetPort: 8082 ports:
port: 80 - port: 80
targetPort: 8082
selector: selector:
name: heapster name: heapster
kubernetes.io/cluster-service: "true" kubernetes.io/cluster-service: "true"
......
...@@ -5,8 +5,9 @@ metadata: ...@@ -5,8 +5,9 @@ metadata:
name: influxGrafana name: influxGrafana
name: monitoring-influxdb name: monitoring-influxdb
spec: spec:
targetPort: 8086 ports:
port: 80 - port: 80
targetPort: 8086
selector: selector:
name: influxGrafana name: influxGrafana
......
...@@ -5,8 +5,9 @@ metadata: ...@@ -5,8 +5,9 @@ metadata:
name: influxGrafana name: influxGrafana
name: monitoring-influxdb-ui name: monitoring-influxdb-ui
spec: spec:
targetPort: 8083 ports:
port: 80 - port: 80
targetPort: 8083
selector: selector:
name: influxGrafana name: influxGrafana
......
...@@ -465,12 +465,14 @@ func runSelfLinkTestOnNamespace(c *client.Client, namespace string) { ...@@ -465,12 +465,14 @@ func runSelfLinkTestOnNamespace(c *client.Client, namespace string) {
}, },
}, },
Spec: api.ServiceSpec{ Spec: api.ServiceSpec{
Port: 12345,
// This is here because validation requires it. // This is here because validation requires it.
Selector: map[string]string{ Selector: map[string]string{
"foo": "bar", "foo": "bar",
}, },
Protocol: "TCP", Ports: []api.ServicePort{{
Port: 12345,
Protocol: "TCP",
}},
SessionAffinity: "None", SessionAffinity: "None",
}, },
} }
...@@ -527,12 +529,14 @@ func runAtomicPutTest(c *client.Client) { ...@@ -527,12 +529,14 @@ func runAtomicPutTest(c *client.Client) {
}, },
}, },
Spec: api.ServiceSpec{ Spec: api.ServiceSpec{
Port: 12345,
// This is here because validation requires it. // This is here because validation requires it.
Selector: map[string]string{ Selector: map[string]string{
"foo": "bar", "foo": "bar",
}, },
Protocol: "TCP", Ports: []api.ServicePort{{
Port: 12345,
Protocol: "TCP",
}},
SessionAffinity: "None", SessionAffinity: "None",
}, },
} }
...@@ -606,12 +610,14 @@ func runPatchTest(c *client.Client) { ...@@ -606,12 +610,14 @@ func runPatchTest(c *client.Client) {
}, },
}, },
Spec: api.ServiceSpec{ Spec: api.ServiceSpec{
Port: 12345,
// This is here because validation requires it. // This is here because validation requires it.
Selector: map[string]string{ Selector: map[string]string{
"foo": "bar", "foo": "bar",
}, },
Protocol: "TCP", Ports: []api.ServicePort{{
Port: 12345,
Protocol: "TCP",
}},
SessionAffinity: "None", SessionAffinity: "None",
}, },
} }
...@@ -747,8 +753,10 @@ func runServiceTest(client *client.Client) { ...@@ -747,8 +753,10 @@ func runServiceTest(client *client.Client) {
Selector: map[string]string{ Selector: map[string]string{
"name": "thisisalonglabel", "name": "thisisalonglabel",
}, },
Port: 8080, Ports: []api.ServicePort{{
Protocol: "TCP", Port: 8080,
Protocol: "TCP",
}},
SessionAffinity: "None", SessionAffinity: "None",
}, },
} }
...@@ -764,8 +772,10 @@ func runServiceTest(client *client.Client) { ...@@ -764,8 +772,10 @@ func runServiceTest(client *client.Client) {
Selector: map[string]string{ Selector: map[string]string{
"name": "thisisalonglabel", "name": "thisisalonglabel",
}, },
Port: 8080, Ports: []api.ServicePort{{
Protocol: "TCP", Port: 8080,
Protocol: "TCP",
}},
SessionAffinity: "None", SessionAffinity: "None",
}, },
} }
...@@ -784,8 +794,10 @@ func runServiceTest(client *client.Client) { ...@@ -784,8 +794,10 @@ func runServiceTest(client *client.Client) {
Selector: map[string]string{ Selector: map[string]string{
"name": "thisisalonglabel", "name": "thisisalonglabel",
}, },
Port: 8080, Ports: []api.ServicePort{{
Protocol: "TCP", Port: 8080,
Protocol: "TCP",
}},
SessionAffinity: "None", SessionAffinity: "None",
}, },
} }
......
...@@ -8,11 +8,15 @@ ...@@ -8,11 +8,15 @@
} }
}, },
"spec": { "spec": {
"port": 8080, "ports": [
"protocol": "TCP", {
"port": 8080,
"protocol": "TCP",
"targetPort": 8080
}
],
"selector": { "selector": {
"name": "nettest" "name": "nettest"
}, }
"targetPort": 8080,
} }
} }
...@@ -5,7 +5,8 @@ metadata: ...@@ -5,7 +5,8 @@ metadata:
name: cassandra name: cassandra
name: cassandra name: cassandra
spec: spec:
targetPort: 9042 ports:
port: 9042 - port: 9042
targetPort: 9042
selector: selector:
name: cassandra name: cassandra
...@@ -8,9 +8,13 @@ ...@@ -8,9 +8,13 @@
} }
}, },
"spec":{ "spec":{
"port":3000, "ports": [
"containerPort":"http-server", {
"protocol":"TCP", "port":3000,
"targetPort":"http-server",
"protocol":"TCP"
}
],
"selector":{ "selector":{
"name":"guestbook" "name":"guestbook"
} }
......
...@@ -9,9 +9,13 @@ ...@@ -9,9 +9,13 @@
} }
}, },
"spec":{ "spec":{
"port":6379, "ports": [
"containerPort":"redis-server", {
"protocol":"TCP", "port":6379,
"targetPort":"redis-server",
"protocol":"TCP"
}
],
"selector":{ "selector":{
"name":"redis", "name":"redis",
"role":"master" "role":"master"
......
...@@ -9,9 +9,13 @@ ...@@ -9,9 +9,13 @@
} }
}, },
"spec":{ "spec":{
"port":6379, "ports": [
"containerPort":"redis-server", {
"protocol":"TCP", "port":6379,
"targetPort":"redis-server",
"protocol":"TCP"
}
],
"selector":{ "selector":{
"name":"redis", "name":"redis",
"role":"slave" "role":"slave"
......
...@@ -8,9 +8,13 @@ ...@@ -8,9 +8,13 @@
} }
}, },
"spec":{ "spec":{
"port":80, "ports": [
"containerPort":80, {
"protocol":"TCP", "port":80,
"targetPort":80,
"protocol":"TCP"
}
],
"selector":{ "selector":{
"name":"frontend" "name":"frontend"
} }
......
...@@ -8,9 +8,13 @@ ...@@ -8,9 +8,13 @@
} }
}, },
"spec":{ "spec":{
"port":6379, "ports": [
"containerPort":6379, {
"protocol":"TCP", "port":6379,
"targetPort":6379,
"protocol":"TCP"
}
],
"selector":{ "selector":{
"name":"redis-master" "name":"redis-master"
} }
......
...@@ -8,9 +8,13 @@ ...@@ -8,9 +8,13 @@
} }
}, },
"spec":{ "spec":{
"port":6379, "ports": [
"containerPort":6379, {
"protocol":"TCP", "port":6379,
"targetPort":6379,
"protocol":"TCP"
}
],
"selector":{ "selector":{
"name":"redis-slave" "name":"redis-slave"
} }
......
...@@ -5,7 +5,8 @@ metadata: ...@@ -5,7 +5,8 @@ metadata:
name: hazelcast name: hazelcast
name: hazelcast name: hazelcast
spec: spec:
targetPort: 5701 ports:
port: 5701 - port: 5701
targetPort: 5701
selector: selector:
name: hazelcast name: hazelcast
...@@ -5,8 +5,9 @@ metadata: ...@@ -5,8 +5,9 @@ metadata:
name: mysql name: mysql
name: mysql name: mysql
spec: spec:
targetPort: 3306 ports:
port: 3306 - port: 3306
targetPort: 3306
selector: selector:
name: mysql name: mysql
...@@ -5,8 +5,9 @@ metadata: ...@@ -5,8 +5,9 @@ metadata:
name: wpfrontend name: wpfrontend
name: wpfrontend name: wpfrontend
spec: spec:
targetPort: 80 ports:
port: 80 - port: 80
targetPort: 80
selector: selector:
name: wpfrontend name: wpfrontend
...@@ -6,7 +6,8 @@ metadata: ...@@ -6,7 +6,8 @@ metadata:
role: service role: service
name: redis-sentinel name: redis-sentinel
spec: spec:
targetPort: 26379 ports:
port: 26379 - port: 26379
targetPort: 26379
selector: selector:
redis-sentinel: "true" redis-sentinel: "true"
...@@ -6,8 +6,9 @@ metadata: ...@@ -6,8 +6,9 @@ metadata:
name: rethinkdb-admin name: rethinkdb-admin
namespace: rethinkdb namespace: rethinkdb
spec: spec:
targetPort: 8080 ports:
port: 8080 - port: 8080
targetPort: 8080
selector: selector:
db: rethinkdb db: rethinkdb
role: admin role: admin
...@@ -6,7 +6,8 @@ metadata: ...@@ -6,7 +6,8 @@ metadata:
name: rethinkdb-driver name: rethinkdb-driver
namespace: rethinkdb namespace: rethinkdb
spec: spec:
targetPort: 28015 ports:
port: 28015 - port: 28015
targetPort: 28015
selector: selector:
db: rethinkdb db: rethinkdb
...@@ -3,16 +3,14 @@ kind: Service ...@@ -3,16 +3,14 @@ kind: Service
metadata: metadata:
name: nginx-example name: nginx-example
spec: spec:
# the container on each pod to connect to, can be a name ports:
# (e.g. 'www') or a number (e.g. 80) - port: 8000 # the port that this service should serve on
targetPort: 80 # the container on each pod to connect to, can be a name
# the port that this service should serve on # (e.g. 'www') or a number (e.g. 80)
port: 8000 targetPort: 80
protocol: TCP protocol: TCP
# just like the selector in the replication controller, # just like the selector in the replication controller,
# but this time it identifies the set of pods to load balance # but this time it identifies the set of pods to load balance
# traffic to. # traffic to.
selector: selector:
name: nginx name: nginx
...@@ -97,12 +97,27 @@ func (set addressSet) Insert(addr *api.EndpointAddress) { ...@@ -97,12 +97,27 @@ func (set addressSet) Insert(addr *api.EndpointAddress) {
func hashAddresses(addrs addressSet) string { func hashAddresses(addrs addressSet) string {
// Flatten the list of addresses into a string so it can be used as a // Flatten the list of addresses into a string so it can be used as a
// map key. // map key. Unfortunately, DeepHashObject is implemented in terms of
// spew, and spew does not handle non-primitive mapo keys well. So
// first we collapse it into a slice, sort the slice, then hash that.
slice := []*api.EndpointAddress{}
for k := range addrs {
slice = append(slice, k)
}
sort.Sort(addrPtrsByIP(slice))
hasher := md5.New() hasher := md5.New()
util.DeepHashObject(hasher, addrs) util.DeepHashObject(hasher, slice)
return hex.EncodeToString(hasher.Sum(nil)[0:]) return hex.EncodeToString(hasher.Sum(nil)[0:])
} }
type addrPtrsByIP []*api.EndpointAddress
func (sl addrPtrsByIP) Len() int { return len(sl) }
func (sl addrPtrsByIP) Swap(i, j int) { sl[i], sl[j] = sl[j], sl[i] }
func (sl addrPtrsByIP) Less(i, j int) bool {
return bytes.Compare([]byte(sl[i].IP), []byte(sl[j].IP)) < 0
}
// SortSubsets sorts an array of EndpointSubset objects in place. For ease of // SortSubsets sorts an array of EndpointSubset objects in place. For ease of
// use it returns the input slice. // use it returns the input slice.
func SortSubsets(subsets []api.EndpointSubset) []api.EndpointSubset { func SortSubsets(subsets []api.EndpointSubset) []api.EndpointSubset {
......
...@@ -216,11 +216,18 @@ func FuzzerFor(t *testing.T, version string, src rand.Source) *fuzz.Fuzzer { ...@@ -216,11 +216,18 @@ func FuzzerFor(t *testing.T, version string, src rand.Source) *fuzz.Fuzzer {
}, },
func(ss *api.ServiceSpec, c fuzz.Continue) { func(ss *api.ServiceSpec, c fuzz.Continue) {
c.FuzzNoCustom(ss) // fuzz self without calling this function again c.FuzzNoCustom(ss) // fuzz self without calling this function again
switch ss.TargetPort.Kind { if len(ss.Ports) == 0 {
case util.IntstrInt: // There must be at least 1 port.
ss.TargetPort.IntVal = 1 + ss.TargetPort.IntVal%65535 // non-zero ss.Ports = append(ss.Ports, api.ServicePort{})
case util.IntstrString: c.Fuzz(&ss.Ports[0])
ss.TargetPort.StrVal = "x" + ss.TargetPort.StrVal // non-empty }
for i := range ss.Ports {
switch ss.Ports[i].TargetPort.Kind {
case util.IntstrInt:
ss.Ports[i].TargetPort.IntVal = 1 + ss.Ports[i].TargetPort.IntVal%65535 // non-zero
case util.IntstrString:
ss.Ports[i].TargetPort.StrVal = "x" + ss.Ports[i].TargetPort.StrVal // non-empty
}
} }
}, },
) )
......
...@@ -863,12 +863,8 @@ type ServiceStatus struct{} ...@@ -863,12 +863,8 @@ type ServiceStatus struct{}
// ServiceSpec describes the attributes that a user creates on a service // ServiceSpec describes the attributes that a user creates on a service
type ServiceSpec struct { type ServiceSpec struct {
// Port is the TCP or UDP port that will be made available to each pod for connecting to the pods // Required: The list of ports that are exposed by this service.
// proxied by this service. Ports []ServicePort `json:"ports"`
Port int `json:"port"`
// Required: Supports "TCP" and "UDP".
Protocol Protocol `json:"protocol,omitempty"`
// This service will route traffic to pods having labels matching this selector. If empty or not present, // This service will route traffic to pods having labels matching this selector. If empty or not present,
// the service is assumed to have endpoints set by an external process and Kubernetes will not modify // the service is assumed to have endpoints set by an external process and Kubernetes will not modify
...@@ -891,17 +887,32 @@ type ServiceSpec struct { ...@@ -891,17 +887,32 @@ type ServiceSpec struct {
// For hostnames, the user will use a CNAME record (instead of using an A record with the IP) // For hostnames, the user will use a CNAME record (instead of using an A record with the IP)
PublicIPs []string `json:"publicIPs,omitempty"` PublicIPs []string `json:"publicIPs,omitempty"`
// TargetPort is the name or number of the port on the container to direct traffic to.
// This is useful if the containers the service points to have multiple open ports.
// Optional: If unspecified, the first port on the container will be used.
// As of v1beta3 this field will become required in the internal API,
// and the versioned APIs must provide a default value.
TargetPort util.IntOrString `json:"targetPort,omitempty"`
// Required: Supports "ClientIP" and "None". Used to maintain session affinity. // Required: Supports "ClientIP" and "None". Used to maintain session affinity.
SessionAffinity AffinityType `json:"sessionAffinity,omitempty"` SessionAffinity AffinityType `json:"sessionAffinity,omitempty"`
} }
type ServicePort struct {
// Optional if only one ServicePort is defined on this service: The
// name of this port within the service. This must be a DNS_LABEL.
// All ports within a ServiceSpec must have unique names. This maps to
// the 'Name' field in EndpointPort objects.
Name string `json:"name"`
// The IP protocol for this port. Supports "TCP" and "UDP".
Protocol Protocol `json:"protocol"`
// The port that will be exposed on the service.
Port int `json:"port"`
// Optional: The target port on pods selected by this service. If this
// is a string, it will be looked up as a named port in the target
// Pod's container ports. If this is not specified, the first port on
// the destination pod will be used. This behavior is deprecated. As
// of v1beta3 the default value is the sames as the Port field (an
// identity map).
TargetPort util.IntOrString `json:"targetPort"`
}
// Service is a named abstraction of software service (for example, mysql) consisting of local port // Service is a named abstraction of software service (for example, mysql) consisting of local port
// (for example 3306) that the proxy listens on, and the selector that determines which pods // (for example 3306) that the proxy listens on, and the selector that determines which pods
// will answer requests sent through the proxy. // will answer requests sent through the proxy.
......
...@@ -703,14 +703,28 @@ func init() { ...@@ -703,14 +703,28 @@ func init() {
return err return err
} }
out.Port = in.Spec.Port // Produce legacy fields.
out.Protocol = Protocol(in.Spec.Protocol) if len(in.Spec.Ports) > 0 {
out.PortName = in.Spec.Ports[0].Name
out.Port = in.Spec.Ports[0].Port
out.Protocol = Protocol(in.Spec.Ports[0].Protocol)
out.ContainerPort = in.Spec.Ports[0].TargetPort
}
// Copy modern fields.
for i := range in.Spec.Ports {
out.Ports = append(out.Ports, ServicePort{
Name: in.Spec.Ports[i].Name,
Port: in.Spec.Ports[i].Port,
Protocol: Protocol(in.Spec.Ports[i].Protocol),
ContainerPort: in.Spec.Ports[i].TargetPort,
})
}
if err := s.Convert(&in.Spec.Selector, &out.Selector, 0); err != nil { if err := s.Convert(&in.Spec.Selector, &out.Selector, 0); err != nil {
return err return err
} }
out.CreateExternalLoadBalancer = in.Spec.CreateExternalLoadBalancer out.CreateExternalLoadBalancer = in.Spec.CreateExternalLoadBalancer
out.PublicIPs = in.Spec.PublicIPs out.PublicIPs = in.Spec.PublicIPs
out.ContainerPort = in.Spec.TargetPort
out.PortalIP = in.Spec.PortalIP out.PortalIP = in.Spec.PortalIP
if err := s.Convert(&in.Spec.SessionAffinity, &out.SessionAffinity, 0); err != nil { if err := s.Convert(&in.Spec.SessionAffinity, &out.SessionAffinity, 0); err != nil {
return err return err
...@@ -729,14 +743,31 @@ func init() { ...@@ -729,14 +743,31 @@ func init() {
return err return err
} }
out.Spec.Port = in.Port if len(in.Ports) == 0 && in.Port != 0 {
out.Spec.Protocol = newer.Protocol(in.Protocol) // Use legacy fields to produce modern fields.
out.Spec.Ports = append(out.Spec.Ports, newer.ServicePort{
Name: in.PortName,
Port: in.Port,
Protocol: newer.Protocol(in.Protocol),
TargetPort: in.ContainerPort,
})
} else {
// Use modern fields, ignore legacy.
for i := range in.Ports {
out.Spec.Ports = append(out.Spec.Ports, newer.ServicePort{
Name: in.Ports[i].Name,
Port: in.Ports[i].Port,
Protocol: newer.Protocol(in.Ports[i].Protocol),
TargetPort: in.Ports[i].ContainerPort,
})
}
}
if err := s.Convert(&in.Selector, &out.Spec.Selector, 0); err != nil { if err := s.Convert(&in.Selector, &out.Spec.Selector, 0); err != nil {
return err return err
} }
out.Spec.CreateExternalLoadBalancer = in.CreateExternalLoadBalancer out.Spec.CreateExternalLoadBalancer = in.CreateExternalLoadBalancer
out.Spec.PublicIPs = in.PublicIPs out.Spec.PublicIPs = in.PublicIPs
out.Spec.TargetPort = in.ContainerPort
out.Spec.PortalIP = in.PortalIP out.Spec.PortalIP = in.PortalIP
if err := s.Convert(&in.SessionAffinity, &out.Spec.SessionAffinity, 0); err != nil { if err := s.Convert(&in.SessionAffinity, &out.Spec.SessionAffinity, 0); err != nil {
return err return err
......
...@@ -284,6 +284,191 @@ func TestServiceEmptySelector(t *testing.T) { ...@@ -284,6 +284,191 @@ func TestServiceEmptySelector(t *testing.T) {
} }
} }
func TestServicePorts(t *testing.T) {
testCases := []struct {
given current.Service
expected newer.Service
roundtrip current.Service
}{
{
given: current.Service{
TypeMeta: current.TypeMeta{
ID: "legacy-with-defaults",
},
Port: 111,
Protocol: current.ProtocolTCP,
},
expected: newer.Service{
Spec: newer.ServiceSpec{Ports: []newer.ServicePort{{
Port: 111,
Protocol: newer.ProtocolTCP,
}}},
},
roundtrip: current.Service{
Ports: []current.ServicePort{{
Port: 111,
Protocol: current.ProtocolTCP,
}},
},
},
{
given: current.Service{
TypeMeta: current.TypeMeta{
ID: "legacy-full",
},
PortName: "p",
Port: 111,
Protocol: current.ProtocolTCP,
ContainerPort: util.NewIntOrStringFromString("p"),
},
expected: newer.Service{
Spec: newer.ServiceSpec{Ports: []newer.ServicePort{{
Name: "p",
Port: 111,
Protocol: newer.ProtocolTCP,
TargetPort: util.NewIntOrStringFromString("p"),
}}},
},
roundtrip: current.Service{
Ports: []current.ServicePort{{
Name: "p",
Port: 111,
Protocol: current.ProtocolTCP,
ContainerPort: util.NewIntOrStringFromString("p"),
}},
},
},
{
given: current.Service{
TypeMeta: current.TypeMeta{
ID: "both",
},
PortName: "p",
Port: 111,
Protocol: current.ProtocolTCP,
ContainerPort: util.NewIntOrStringFromString("p"),
Ports: []current.ServicePort{{
Name: "q",
Port: 222,
Protocol: current.ProtocolUDP,
ContainerPort: util.NewIntOrStringFromInt(93),
}},
},
expected: newer.Service{
Spec: newer.ServiceSpec{Ports: []newer.ServicePort{{
Name: "q",
Port: 222,
Protocol: newer.ProtocolUDP,
TargetPort: util.NewIntOrStringFromInt(93),
}}},
},
roundtrip: current.Service{
Ports: []current.ServicePort{{
Name: "q",
Port: 222,
Protocol: current.ProtocolUDP,
ContainerPort: util.NewIntOrStringFromInt(93),
}},
},
},
{
given: current.Service{
TypeMeta: current.TypeMeta{
ID: "one",
},
Ports: []current.ServicePort{{
Name: "p",
Port: 111,
Protocol: current.ProtocolUDP,
ContainerPort: util.NewIntOrStringFromInt(93),
}},
},
expected: newer.Service{
Spec: newer.ServiceSpec{Ports: []newer.ServicePort{{
Name: "p",
Port: 111,
Protocol: newer.ProtocolUDP,
TargetPort: util.NewIntOrStringFromInt(93),
}}},
},
roundtrip: current.Service{
Ports: []current.ServicePort{{
Name: "p",
Port: 111,
Protocol: current.ProtocolUDP,
ContainerPort: util.NewIntOrStringFromInt(93),
}},
},
},
{
given: current.Service{
TypeMeta: current.TypeMeta{
ID: "two",
},
Ports: []current.ServicePort{{
Name: "p",
Port: 111,
Protocol: current.ProtocolUDP,
ContainerPort: util.NewIntOrStringFromInt(93),
}, {
Name: "q",
Port: 222,
Protocol: current.ProtocolTCP,
ContainerPort: util.NewIntOrStringFromInt(76),
}},
},
expected: newer.Service{
Spec: newer.ServiceSpec{Ports: []newer.ServicePort{{
Name: "p",
Port: 111,
Protocol: newer.ProtocolUDP,
TargetPort: util.NewIntOrStringFromInt(93),
}, {
Name: "q",
Port: 222,
Protocol: newer.ProtocolTCP,
TargetPort: util.NewIntOrStringFromInt(76),
}}},
},
roundtrip: current.Service{
Ports: []current.ServicePort{{
Name: "p",
Port: 111,
Protocol: current.ProtocolUDP,
ContainerPort: util.NewIntOrStringFromInt(93),
}, {
Name: "q",
Port: 222,
Protocol: current.ProtocolTCP,
ContainerPort: util.NewIntOrStringFromInt(76),
}},
},
},
}
for i, tc := range testCases {
// Convert versioned -> internal.
got := newer.Service{}
if err := Convert(&tc.given, &got); err != nil {
t.Errorf("[Case: %d] Unexpected error: %v", i, err)
continue
}
if !reflect.DeepEqual(got.Spec.Ports, tc.expected.Spec.Ports) {
t.Errorf("[Case: %d] Expected %v, got %v", i, tc.expected.Spec.Ports, got.Spec.Ports)
}
// Convert internal -> versioned.
got2 := current.Service{}
if err := Convert(&got, &got2); err != nil {
t.Errorf("[Case: %d] Unexpected error: %v", i, err)
continue
}
if !reflect.DeepEqual(got2.Ports, tc.roundtrip.Ports) {
t.Errorf("[Case: %d] Expected %v, got %v", i, tc.roundtrip.Ports, got2.Ports)
}
}
}
func TestPullPolicyConversion(t *testing.T) { func TestPullPolicyConversion(t *testing.T) {
table := []struct { table := []struct {
versioned current.PullPolicy versioned current.PullPolicy
...@@ -527,7 +712,7 @@ func TestEndpointsConversion(t *testing.T) { ...@@ -527,7 +712,7 @@ func TestEndpointsConversion(t *testing.T) {
continue continue
} }
if got2.Protocol != tc.given.Protocol || !newer.Semantic.DeepEqual(got2.Endpoints, tc.given.Endpoints) { if got2.Protocol != tc.given.Protocol || !newer.Semantic.DeepEqual(got2.Endpoints, tc.given.Endpoints) {
t.Errorf("[Case: %d] Expected %#v, got %#v", i, tc.given.Endpoints, got2.Endpoints) t.Errorf("[Case: %d] Expected %s %#v, got %s %#v", i, tc.given.Protocol, tc.given.Endpoints, got2.Protocol, got2.Endpoints)
} }
} }
} }
......
...@@ -68,6 +68,14 @@ func init() { ...@@ -68,6 +68,14 @@ func init() {
obj.SessionAffinity = AffinityTypeNone obj.SessionAffinity = AffinityTypeNone
} }
}, },
func(obj *ServicePort) {
if obj.Protocol == "" {
obj.Protocol = ProtocolTCP
}
if obj.ContainerPort == util.NewIntOrStringFromInt(0) || obj.ContainerPort == util.NewIntOrStringFromString("") {
obj.ContainerPort = util.NewIntOrStringFromInt(obj.Port)
}
},
func(obj *PodSpec) { func(obj *PodSpec) {
if obj.DNSPolicy == "" { if obj.DNSPolicy == "" {
obj.DNSPolicy = DNSClusterFirst obj.DNSPolicy = DNSClusterFirst
......
...@@ -22,6 +22,7 @@ import ( ...@@ -22,6 +22,7 @@ import (
current "github.com/GoogleCloudPlatform/kubernetes/pkg/api/v1beta1" current "github.com/GoogleCloudPlatform/kubernetes/pkg/api/v1beta1"
"github.com/GoogleCloudPlatform/kubernetes/pkg/runtime" "github.com/GoogleCloudPlatform/kubernetes/pkg/runtime"
"github.com/GoogleCloudPlatform/kubernetes/pkg/util"
) )
func roundTrip(t *testing.T, obj runtime.Object) runtime.Object { func roundTrip(t *testing.T, obj runtime.Object) runtime.Object {
...@@ -148,3 +149,35 @@ func TestSetDefaultContainerManifestHostNetwork(t *testing.T) { ...@@ -148,3 +149,35 @@ func TestSetDefaultContainerManifestHostNetwork(t *testing.T) {
t.Errorf("Expected container port to be defaulted, was made %d instead of %d", hostPortNum, portNum) t.Errorf("Expected container port to be defaulted, was made %d instead of %d", hostPortNum, portNum)
} }
} }
func TestSetDefaultServicePort(t *testing.T) {
// Unchanged if set.
in := &current.Service{Ports: []current.ServicePort{{Protocol: "UDP", Port: 9376, ContainerPort: util.NewIntOrStringFromInt(118)}}}
out := roundTrip(t, runtime.Object(in)).(*current.Service)
if out.Ports[0].Protocol != current.ProtocolUDP {
t.Errorf("Expected protocol %s, got %s", current.ProtocolUDP, out.Ports[0].Protocol)
}
if out.Ports[0].ContainerPort != in.Ports[0].ContainerPort {
t.Errorf("Expected port %d, got %d", in.Ports[0].ContainerPort, out.Ports[0].ContainerPort)
}
// Defaulted.
in = &current.Service{Ports: []current.ServicePort{{Protocol: "", Port: 9376, ContainerPort: util.NewIntOrStringFromInt(0)}}}
out = roundTrip(t, runtime.Object(in)).(*current.Service)
if out.Ports[0].Protocol != current.ProtocolTCP {
t.Errorf("Expected protocol %s, got %s", current.ProtocolTCP, out.Ports[0].Protocol)
}
if out.Ports[0].ContainerPort != util.NewIntOrStringFromInt(in.Ports[0].Port) {
t.Errorf("Expected port %d, got %v", in.Ports[0].Port, out.Ports[0].ContainerPort)
}
// Defaulted.
in = &current.Service{Ports: []current.ServicePort{{Protocol: "", Port: 9376, ContainerPort: util.NewIntOrStringFromString("")}}}
out = roundTrip(t, runtime.Object(in)).(*current.Service)
if out.Ports[0].Protocol != current.ProtocolTCP {
t.Errorf("Expected protocol %s, got %s", current.ProtocolTCP, out.Ports[0].Protocol)
}
if out.Ports[0].ContainerPort != util.NewIntOrStringFromInt(in.Ports[0].Port) {
t.Errorf("Expected port %d, got %v", in.Ports[0].Port, out.Ports[0].ContainerPort)
}
}
...@@ -718,14 +718,21 @@ type Service struct { ...@@ -718,14 +718,21 @@ type Service struct {
// Required. // Required.
Port int `json:"port" description:"port exposed by the service"` Port int `json:"port" description:"port exposed by the service"`
// Optional: The name of the first port.
PortName string `json:"portName,omitempty" description:"the name of the first port; optional"`
// Optional: Defaults to "TCP". // Optional: Defaults to "TCP".
Protocol Protocol `json:"protocol,omitempty" description:"protocol for port; must be UDP or TCP; TCP if unspecified"` Protocol Protocol `json:"protocol,omitempty" description:"protocol for port; must be UDP or TCP; TCP if unspecified"`
// ContainerPort is the name or number of the port on the container to direct traffic to.
// This is useful if the containers the service points to have multiple open ports.
// Optional: If unspecified, the first port on the container will be used.
ContainerPort util.IntOrString `json:"containerPort,omitempty" description:"number or name of the port to access on the containers belonging to pods targeted by the service; defaults to the container's first open port"`
// This service's labels. // This service's labels.
Labels map[string]string `json:"labels,omitempty" description:"map of string keys and values that can be used to organize and categorize services"` Labels map[string]string `json:"labels,omitempty" description:"map of string keys and values that can be used to organize and categorize services"`
// This service will route traffic to pods having labels matching this selector. If null, no endpoints will be automatically created. If empty, all pods will be selected. // This service will route traffic to pods having labels matching this selector. If null, no endpoints will be automatically created. If empty, all pods will be selected.
Selector map[string]string `json:"selector" description:"label keys and values that must match in order to receive traffic for this service; if empty, all pods are selected, if not specified, endpoints must be manually specified"` Selector map[string]string `json:"selector" description:"label keys and values that must match in order to receive traffic for this service; if empty, all pods are selected, if not specified, endpoints must be manually specified"`
// An external load balancer should be set up via the cloud-provider // An external load balancer should be set up via the cloud-provider
CreateExternalLoadBalancer bool `json:"createExternalLoadBalancer,omitempty" description:"set up a cloud-provider-specific load balancer on an external IP"` CreateExternalLoadBalancer bool `json:"createExternalLoadBalancer,omitempty" description:"set up a cloud-provider-specific load balancer on an external IP"`
...@@ -733,11 +740,6 @@ type Service struct { ...@@ -733,11 +740,6 @@ type Service struct {
// users to handle external traffic that arrives at a node. // users to handle external traffic that arrives at a node.
PublicIPs []string `json:"publicIPs,omitempty" description:"externally visible IPs (e.g. load balancers) that should be proxied to this service"` PublicIPs []string `json:"publicIPs,omitempty" description:"externally visible IPs (e.g. load balancers) that should be proxied to this service"`
// ContainerPort is the name or number of the port on the container to direct traffic to.
// This is useful if the containers the service points to have multiple open ports.
// Optional: If unspecified, the first port on the container will be used.
ContainerPort util.IntOrString `json:"containerPort,omitempty" description:"number or name of the port to access on the containers belonging to pods targeted by the service; defaults to the container's first open port"`
// PortalIP is usually assigned by the master. If specified by the user // PortalIP is usually assigned by the master. If specified by the user
// we will try to respect it or else fail the request. This field can // we will try to respect it or else fail the request. This field can
// not be changed by updates. // not be changed by updates.
...@@ -750,6 +752,35 @@ type Service struct { ...@@ -750,6 +752,35 @@ type Service struct {
// Optional: Supports "ClientIP" and "None". Used to maintain session affinity. // Optional: Supports "ClientIP" and "None". Used to maintain session affinity.
SessionAffinity AffinityType `json:"sessionAffinity,omitempty" description:"enable client IP based session affinity; must be ClientIP or None; defaults to None"` SessionAffinity AffinityType `json:"sessionAffinity,omitempty" description:"enable client IP based session affinity; must be ClientIP or None; defaults to None"`
// Optional: Ports to expose on the service. If this field is
// specified, the legacy fields (Port, PortName, Protocol, and
// ContainerPort) will be overwritten by the first member of this
// array. If this field is not specified, it will be populated from
// the legacy fields.
Ports []ServicePort `json:"ports" description:"ports to be exposed on the service"`
}
type ServicePort struct {
// Required: The name of this port within the service. This must be a
// DNS_LABEL. All ports within a ServiceSpec must have unique names.
// This maps to the 'Name' field in EndpointPort objects.
Name string `json:"name" description:"the name of this port; optional if only one port is defined"`
// Optional: The IP protocol for this port. Supports "TCP" and "UDP",
// default is TCP.
Protocol Protocol `json:"protocol" description:"the protocol used by this port; must be UDP or TCP; TCP if unspecified"`
// Required: The port that will be exposed.
Port int `json:"port" description:"the port number that is exposed"`
// Optional: The port number on the target pod to direct traffic to.
// This is useful if the containers the service points to have multiple
// open ports. If this is a string, it will be looked up as a named
// port in the target Pod's container ports. If unspecified, the value
// of Port is used (an identity map) - note this is a different default
// than Service.ContainerPort.
ContainerPort util.IntOrString `json:"containerPort" description:"the port to access on the containers belonging to pods targeted by the service; defaults to the service port"`
} }
// EndpointObjectReference is a reference to an object exposing the endpoint // EndpointObjectReference is a reference to an object exposing the endpoint
......
...@@ -635,14 +635,28 @@ func init() { ...@@ -635,14 +635,28 @@ func init() {
return err return err
} }
out.Port = in.Spec.Port // Produce legacy fields.
out.Protocol = Protocol(in.Spec.Protocol) if len(in.Spec.Ports) > 0 {
out.PortName = in.Spec.Ports[0].Name
out.Port = in.Spec.Ports[0].Port
out.Protocol = Protocol(in.Spec.Ports[0].Protocol)
out.ContainerPort = in.Spec.Ports[0].TargetPort
}
// Copy modern fields.
for i := range in.Spec.Ports {
out.Ports = append(out.Ports, ServicePort{
Name: in.Spec.Ports[i].Name,
Port: in.Spec.Ports[i].Port,
Protocol: Protocol(in.Spec.Ports[i].Protocol),
ContainerPort: in.Spec.Ports[i].TargetPort,
})
}
if err := s.Convert(&in.Spec.Selector, &out.Selector, 0); err != nil { if err := s.Convert(&in.Spec.Selector, &out.Selector, 0); err != nil {
return err return err
} }
out.CreateExternalLoadBalancer = in.Spec.CreateExternalLoadBalancer out.CreateExternalLoadBalancer = in.Spec.CreateExternalLoadBalancer
out.PublicIPs = in.Spec.PublicIPs out.PublicIPs = in.Spec.PublicIPs
out.ContainerPort = in.Spec.TargetPort
out.PortalIP = in.Spec.PortalIP out.PortalIP = in.Spec.PortalIP
if err := s.Convert(&in.Spec.SessionAffinity, &out.SessionAffinity, 0); err != nil { if err := s.Convert(&in.Spec.SessionAffinity, &out.SessionAffinity, 0); err != nil {
return err return err
...@@ -661,14 +675,31 @@ func init() { ...@@ -661,14 +675,31 @@ func init() {
return err return err
} }
out.Spec.Port = in.Port if len(in.Ports) == 0 && in.Port != 0 {
out.Spec.Protocol = newer.Protocol(in.Protocol) // Use legacy fields to produce modern fields.
out.Spec.Ports = append(out.Spec.Ports, newer.ServicePort{
Name: in.PortName,
Port: in.Port,
Protocol: newer.Protocol(in.Protocol),
TargetPort: in.ContainerPort,
})
} else {
// Use modern fields, ignore legacy.
for i := range in.Ports {
out.Spec.Ports = append(out.Spec.Ports, newer.ServicePort{
Name: in.Ports[i].Name,
Port: in.Ports[i].Port,
Protocol: newer.Protocol(in.Ports[i].Protocol),
TargetPort: in.Ports[i].ContainerPort,
})
}
}
if err := s.Convert(&in.Selector, &out.Spec.Selector, 0); err != nil { if err := s.Convert(&in.Selector, &out.Spec.Selector, 0); err != nil {
return err return err
} }
out.Spec.CreateExternalLoadBalancer = in.CreateExternalLoadBalancer out.Spec.CreateExternalLoadBalancer = in.CreateExternalLoadBalancer
out.Spec.PublicIPs = in.PublicIPs out.Spec.PublicIPs = in.PublicIPs
out.Spec.TargetPort = in.ContainerPort
out.Spec.PortalIP = in.PortalIP out.Spec.PortalIP = in.PortalIP
if err := s.Convert(&in.SessionAffinity, &out.Spec.SessionAffinity, 0); err != nil { if err := s.Convert(&in.SessionAffinity, &out.Spec.SessionAffinity, 0); err != nil {
return err return err
......
...@@ -18,6 +18,7 @@ package v1beta2_test ...@@ -18,6 +18,7 @@ package v1beta2_test
import ( import (
"encoding/json" "encoding/json"
"reflect"
"testing" "testing"
newer "github.com/GoogleCloudPlatform/kubernetes/pkg/api" newer "github.com/GoogleCloudPlatform/kubernetes/pkg/api"
...@@ -58,6 +59,191 @@ func TestServiceEmptySelector(t *testing.T) { ...@@ -58,6 +59,191 @@ func TestServiceEmptySelector(t *testing.T) {
} }
} }
func TestServicePorts(t *testing.T) {
testCases := []struct {
given current.Service
expected newer.Service
roundtrip current.Service
}{
{
given: current.Service{
TypeMeta: current.TypeMeta{
ID: "legacy-with-defaults",
},
Port: 111,
Protocol: current.ProtocolTCP,
},
expected: newer.Service{
Spec: newer.ServiceSpec{Ports: []newer.ServicePort{{
Port: 111,
Protocol: newer.ProtocolTCP,
}}},
},
roundtrip: current.Service{
Ports: []current.ServicePort{{
Port: 111,
Protocol: current.ProtocolTCP,
}},
},
},
{
given: current.Service{
TypeMeta: current.TypeMeta{
ID: "legacy-full",
},
PortName: "p",
Port: 111,
Protocol: current.ProtocolTCP,
ContainerPort: util.NewIntOrStringFromString("p"),
},
expected: newer.Service{
Spec: newer.ServiceSpec{Ports: []newer.ServicePort{{
Name: "p",
Port: 111,
Protocol: newer.ProtocolTCP,
TargetPort: util.NewIntOrStringFromString("p"),
}}},
},
roundtrip: current.Service{
Ports: []current.ServicePort{{
Name: "p",
Port: 111,
Protocol: current.ProtocolTCP,
ContainerPort: util.NewIntOrStringFromString("p"),
}},
},
},
{
given: current.Service{
TypeMeta: current.TypeMeta{
ID: "both",
},
PortName: "p",
Port: 111,
Protocol: current.ProtocolTCP,
ContainerPort: util.NewIntOrStringFromString("p"),
Ports: []current.ServicePort{{
Name: "q",
Port: 222,
Protocol: current.ProtocolUDP,
ContainerPort: util.NewIntOrStringFromInt(93),
}},
},
expected: newer.Service{
Spec: newer.ServiceSpec{Ports: []newer.ServicePort{{
Name: "q",
Port: 222,
Protocol: newer.ProtocolUDP,
TargetPort: util.NewIntOrStringFromInt(93),
}}},
},
roundtrip: current.Service{
Ports: []current.ServicePort{{
Name: "q",
Port: 222,
Protocol: current.ProtocolUDP,
ContainerPort: util.NewIntOrStringFromInt(93),
}},
},
},
{
given: current.Service{
TypeMeta: current.TypeMeta{
ID: "one",
},
Ports: []current.ServicePort{{
Name: "p",
Port: 111,
Protocol: current.ProtocolUDP,
ContainerPort: util.NewIntOrStringFromInt(93),
}},
},
expected: newer.Service{
Spec: newer.ServiceSpec{Ports: []newer.ServicePort{{
Name: "p",
Port: 111,
Protocol: newer.ProtocolUDP,
TargetPort: util.NewIntOrStringFromInt(93),
}}},
},
roundtrip: current.Service{
Ports: []current.ServicePort{{
Name: "p",
Port: 111,
Protocol: current.ProtocolUDP,
ContainerPort: util.NewIntOrStringFromInt(93),
}},
},
},
{
given: current.Service{
TypeMeta: current.TypeMeta{
ID: "two",
},
Ports: []current.ServicePort{{
Name: "p",
Port: 111,
Protocol: current.ProtocolUDP,
ContainerPort: util.NewIntOrStringFromInt(93),
}, {
Name: "q",
Port: 222,
Protocol: current.ProtocolTCP,
ContainerPort: util.NewIntOrStringFromInt(76),
}},
},
expected: newer.Service{
Spec: newer.ServiceSpec{Ports: []newer.ServicePort{{
Name: "p",
Port: 111,
Protocol: newer.ProtocolUDP,
TargetPort: util.NewIntOrStringFromInt(93),
}, {
Name: "q",
Port: 222,
Protocol: newer.ProtocolTCP,
TargetPort: util.NewIntOrStringFromInt(76),
}}},
},
roundtrip: current.Service{
Ports: []current.ServicePort{{
Name: "p",
Port: 111,
Protocol: current.ProtocolUDP,
ContainerPort: util.NewIntOrStringFromInt(93),
}, {
Name: "q",
Port: 222,
Protocol: current.ProtocolTCP,
ContainerPort: util.NewIntOrStringFromInt(76),
}},
},
},
}
for i, tc := range testCases {
// Convert versioned -> internal.
got := newer.Service{}
if err := newer.Scheme.Convert(&tc.given, &got); err != nil {
t.Errorf("[Case: %d] Unexpected error: %v", i, err)
continue
}
if !reflect.DeepEqual(got.Spec.Ports, tc.expected.Spec.Ports) {
t.Errorf("[Case: %d] Expected %v, got %v", i, tc.expected.Spec.Ports, got.Spec.Ports)
}
// Convert internal -> versioned.
got2 := current.Service{}
if err := newer.Scheme.Convert(&got, &got2); err != nil {
t.Errorf("[Case: %d] Unexpected error: %v", i, err)
continue
}
if !reflect.DeepEqual(got2.Ports, tc.roundtrip.Ports) {
t.Errorf("[Case: %d] Expected %v, got %v", i, tc.roundtrip.Ports, got2.Ports)
}
}
}
func TestNodeConversion(t *testing.T) { func TestNodeConversion(t *testing.T) {
version, kind, err := newer.Scheme.ObjectVersionAndKind(&current.Minion{}) version, kind, err := newer.Scheme.ObjectVersionAndKind(&current.Minion{})
if err != nil { if err != nil {
......
...@@ -69,6 +69,14 @@ func init() { ...@@ -69,6 +69,14 @@ func init() {
obj.SessionAffinity = AffinityTypeNone obj.SessionAffinity = AffinityTypeNone
} }
}, },
func(obj *ServicePort) {
if obj.Protocol == "" {
obj.Protocol = ProtocolTCP
}
if obj.ContainerPort == util.NewIntOrStringFromInt(0) || obj.ContainerPort == util.NewIntOrStringFromString("") {
obj.ContainerPort = util.NewIntOrStringFromInt(obj.Port)
}
},
func(obj *PodSpec) { func(obj *PodSpec) {
if obj.DNSPolicy == "" { if obj.DNSPolicy == "" {
obj.DNSPolicy = DNSClusterFirst obj.DNSPolicy = DNSClusterFirst
......
...@@ -22,6 +22,7 @@ import ( ...@@ -22,6 +22,7 @@ import (
current "github.com/GoogleCloudPlatform/kubernetes/pkg/api/v1beta2" current "github.com/GoogleCloudPlatform/kubernetes/pkg/api/v1beta2"
"github.com/GoogleCloudPlatform/kubernetes/pkg/runtime" "github.com/GoogleCloudPlatform/kubernetes/pkg/runtime"
"github.com/GoogleCloudPlatform/kubernetes/pkg/util"
) )
func roundTrip(t *testing.T, obj runtime.Object) runtime.Object { func roundTrip(t *testing.T, obj runtime.Object) runtime.Object {
...@@ -147,3 +148,35 @@ func TestSetDefaultContainerManifestHostNetwork(t *testing.T) { ...@@ -147,3 +148,35 @@ func TestSetDefaultContainerManifestHostNetwork(t *testing.T) {
t.Errorf("Expected container port to be defaulted, was made %d instead of %d", hostPortNum, portNum) t.Errorf("Expected container port to be defaulted, was made %d instead of %d", hostPortNum, portNum)
} }
} }
func TestSetDefaultServicePort(t *testing.T) {
// Unchanged if set.
in := &current.Service{Ports: []current.ServicePort{{Protocol: "UDP", Port: 9376, ContainerPort: util.NewIntOrStringFromInt(118)}}}
out := roundTrip(t, runtime.Object(in)).(*current.Service)
if out.Ports[0].Protocol != current.ProtocolUDP {
t.Errorf("Expected protocol %s, got %s", current.ProtocolUDP, out.Ports[0].Protocol)
}
if out.Ports[0].ContainerPort != in.Ports[0].ContainerPort {
t.Errorf("Expected port %d, got %d", in.Ports[0].ContainerPort, out.Ports[0].ContainerPort)
}
// Defaulted.
in = &current.Service{Ports: []current.ServicePort{{Protocol: "", Port: 9376, ContainerPort: util.NewIntOrStringFromInt(0)}}}
out = roundTrip(t, runtime.Object(in)).(*current.Service)
if out.Ports[0].Protocol != current.ProtocolTCP {
t.Errorf("Expected protocol %s, got %s", current.ProtocolTCP, out.Ports[0].Protocol)
}
if out.Ports[0].ContainerPort != util.NewIntOrStringFromInt(in.Ports[0].Port) {
t.Errorf("Expected port %d, got %v", in.Ports[0].Port, out.Ports[0].ContainerPort)
}
// Defaulted.
in = &current.Service{Ports: []current.ServicePort{{Protocol: "", Port: 9376, ContainerPort: util.NewIntOrStringFromString("")}}}
out = roundTrip(t, runtime.Object(in)).(*current.Service)
if out.Ports[0].Protocol != current.ProtocolTCP {
t.Errorf("Expected protocol %s, got %s", current.ProtocolTCP, out.Ports[0].Protocol)
}
if out.Ports[0].ContainerPort != util.NewIntOrStringFromInt(in.Ports[0].Port) {
t.Errorf("Expected port %d, got %v", in.Ports[0].Port, out.Ports[0].ContainerPort)
}
}
...@@ -719,14 +719,21 @@ type Service struct { ...@@ -719,14 +719,21 @@ type Service struct {
// Required. // Required.
Port int `json:"port" description:"port exposed by the service"` Port int `json:"port" description:"port exposed by the service"`
// Optional: The name of the first port.
PortName string `json:"portName,omitempty" description:"the name of the first port; optional"`
// Optional: Defaults to "TCP". // Optional: Defaults to "TCP".
Protocol Protocol `json:"protocol,omitempty" description:"protocol for port; must be UDP or TCP; TCP if unspecified"` Protocol Protocol `json:"protocol,omitempty" description:"protocol for port; must be UDP or TCP; TCP if unspecified"`
// ContainerPort is the name or number of the port on the container to direct traffic to.
// This is useful if the containers the service points to have multiple open ports.
// Optional: If unspecified, the first port on the container will be used.
ContainerPort util.IntOrString `json:"containerPort,omitempty" description:"number or name of the port to access on the containers belonging to pods targeted by the service; defaults to the container's first open port"`
// This service's labels. // This service's labels.
Labels map[string]string `json:"labels,omitempty" description:"map of string keys and values that can be used to organize and categorize services"` Labels map[string]string `json:"labels,omitempty" description:"map of string keys and values that can be used to organize and categorize services"`
// This service will route traffic to pods having labels matching this selector. If null, no endpoints will be automatically created. If empty, all pods will be selected. // This service will route traffic to pods having labels matching this selector. If null, no endpoints will be automatically created. If empty, all pods will be selected.
Selector map[string]string `json:"selector" description:"label keys and values that must match in order to receive traffic for this service; if empty, all pods are selected, if not specified, endpoints must be manually specified"` Selector map[string]string `json:"selector" description:"label keys and values that must match in order to receive traffic for this service; if empty, all pods are selected, if not specified, endpoints must be manually specified"`
// An external load balancer should be set up via the cloud-provider // An external load balancer should be set up via the cloud-provider
CreateExternalLoadBalancer bool `json:"createExternalLoadBalancer,omitempty" description:"set up a cloud-provider-specific load balancer on an external IP"` CreateExternalLoadBalancer bool `json:"createExternalLoadBalancer,omitempty" description:"set up a cloud-provider-specific load balancer on an external IP"`
...@@ -734,11 +741,6 @@ type Service struct { ...@@ -734,11 +741,6 @@ type Service struct {
// users to handle external traffic that arrives at a node. // users to handle external traffic that arrives at a node.
PublicIPs []string `json:"publicIPs,omitempty" description:"externally visible IPs (e.g. load balancers) that should be proxied to this service"` PublicIPs []string `json:"publicIPs,omitempty" description:"externally visible IPs (e.g. load balancers) that should be proxied to this service"`
// ContainerPort is the name or number of the port on the container to direct traffic to.
// This is useful if the containers the service points to have multiple open ports.
// Optional: If unspecified, the first port on the container will be used.
ContainerPort util.IntOrString `json:"containerPort,omitempty" description:"number or name of the port to access on the containers belonging to pods targeted by the service; defaults to the container's first open port"`
// PortalIP is usually assigned by the master. If specified by the user // PortalIP is usually assigned by the master. If specified by the user
// we will try to respect it or else fail the request. This field can // we will try to respect it or else fail the request. This field can
// not be changed by updates. // not be changed by updates.
...@@ -751,6 +753,35 @@ type Service struct { ...@@ -751,6 +753,35 @@ type Service struct {
// Optional: Supports "ClientIP" and "None". Used to maintain session affinity. // Optional: Supports "ClientIP" and "None". Used to maintain session affinity.
SessionAffinity AffinityType `json:"sessionAffinity,omitempty" description:"enable client IP based session affinity; must be ClientIP or None; defaults to None"` SessionAffinity AffinityType `json:"sessionAffinity,omitempty" description:"enable client IP based session affinity; must be ClientIP or None; defaults to None"`
// Optional: Ports to expose on the service. If this field is
// specified, the legacy fields (Port, PortName, Protocol, and
// ContainerPort) will be overwritten by the first member of this
// array. If this field is not specified, it will be populated from
// the legacy fields.
Ports []ServicePort `json:"ports" description:"ports to be exposed on the service"`
}
type ServicePort struct {
// Required: The name of this port within the service. This must be a
// DNS_LABEL. All ports within a ServiceSpec must have unique names.
// This maps to the 'Name' field in EndpointPort objects.
Name string `json:"name" description:"the name of this port; optional if only one port is defined"`
// Optional: The IP protocol for this port. Supports "TCP" and "UDP",
// default is TCP.
Protocol Protocol `json:"protocol" description:"the protocol used by this port; must be UDP or TCP; TCP if unspecified"`
// Required: The port that will be exposed.
Port int `json:"port" description:"the port number that is exposed"`
// Optional: The port number on the target pod to direct traffic to.
// This is useful if the containers the service points to have multiple
// open ports. If this is a string, it will be looked up as a named
// port in the target Pod's container ports. If unspecified, the value
// of Port is used (an identity map) - note this is a different default
// than Service.ContainerPort.
ContainerPort util.IntOrString `json:"containerPort" description:"the port to access on the containers belonging to pods targeted by the service; defaults to the service port"`
} }
// EndpointObjectReference is a reference to an object exposing the endpoint // EndpointObjectReference is a reference to an object exposing the endpoint
......
...@@ -53,13 +53,18 @@ func init() { ...@@ -53,13 +53,18 @@ func init() {
} }
}, },
func(obj *Service) { func(obj *Service) {
if obj.Spec.Protocol == "" {
obj.Spec.Protocol = ProtocolTCP
}
if obj.Spec.SessionAffinity == "" { if obj.Spec.SessionAffinity == "" {
obj.Spec.SessionAffinity = AffinityTypeNone obj.Spec.SessionAffinity = AffinityTypeNone
} }
}, },
func(obj *ServicePort) {
if obj.Protocol == "" {
obj.Protocol = ProtocolTCP
}
if obj.TargetPort == util.NewIntOrStringFromInt(0) || obj.TargetPort == util.NewIntOrStringFromString("") {
obj.TargetPort = util.NewIntOrStringFromInt(obj.Port)
}
},
func(obj *PodSpec) { func(obj *PodSpec) {
if obj.DNSPolicy == "" { if obj.DNSPolicy == "" {
obj.DNSPolicy = DNSClusterFirst obj.DNSPolicy = DNSClusterFirst
...@@ -97,9 +102,11 @@ func init() { ...@@ -97,9 +102,11 @@ func init() {
obj.Path = "/" obj.Path = "/"
} }
}, },
func(obj *ServiceSpec) { func(obj *ServicePort) {
if obj.TargetPort.Kind == util.IntstrInt && obj.TargetPort.IntVal == 0 || if obj.Protocol == "" {
obj.TargetPort.Kind == util.IntstrString && obj.TargetPort.StrVal == "" { obj.Protocol = ProtocolTCP
}
if obj.TargetPort == util.NewIntOrStringFromInt(0) || obj.TargetPort == util.NewIntOrStringFromString("") {
obj.TargetPort = util.NewIntOrStringFromInt(obj.Port) obj.TargetPort = util.NewIntOrStringFromInt(obj.Port)
} }
}, },
......
...@@ -44,9 +44,6 @@ func TestSetDefaultService(t *testing.T) { ...@@ -44,9 +44,6 @@ func TestSetDefaultService(t *testing.T) {
svc := &current.Service{} svc := &current.Service{}
obj2 := roundTrip(t, runtime.Object(svc)) obj2 := roundTrip(t, runtime.Object(svc))
svc2 := obj2.(*current.Service) 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 { if svc2.Spec.SessionAffinity != current.AffinityTypeNone {
t.Errorf("Expected default sesseion affinity type:%s, got: %s", current.AffinityTypeNone, svc2.Spec.SessionAffinity) t.Errorf("Expected default sesseion affinity type:%s, got: %s", current.AffinityTypeNone, svc2.Spec.SessionAffinity)
} }
...@@ -85,18 +82,62 @@ func TestSetDefaulEndpointsProtocol(t *testing.T) { ...@@ -85,18 +82,62 @@ func TestSetDefaulEndpointsProtocol(t *testing.T) {
} }
func TestSetDefaulServiceTargetPort(t *testing.T) { func TestSetDefaulServiceTargetPort(t *testing.T) {
in := &current.Service{Spec: current.ServiceSpec{Port: 1234}} in := &current.Service{Spec: current.ServiceSpec{Ports: []current.ServicePort{{Port: 1234}}}}
obj := roundTrip(t, runtime.Object(in)) obj := roundTrip(t, runtime.Object(in))
out := obj.(*current.Service) out := obj.(*current.Service)
if out.Spec.TargetPort.Kind != util.IntstrInt || out.Spec.TargetPort.IntVal != 1234 { if out.Spec.Ports[0].TargetPort != util.NewIntOrStringFromInt(1234) {
t.Errorf("Expected TargetPort to be defaulted, got %s", out.Spec.TargetPort) t.Errorf("Expected TargetPort to be defaulted, got %s", out.Spec.Ports[0].TargetPort)
} }
in = &current.Service{Spec: current.ServiceSpec{Port: 1234, TargetPort: util.NewIntOrStringFromInt(5678)}} in = &current.Service{Spec: current.ServiceSpec{Ports: []current.ServicePort{{Port: 1234, TargetPort: util.NewIntOrStringFromInt(5678)}}}}
obj = roundTrip(t, runtime.Object(in)) obj = roundTrip(t, runtime.Object(in))
out = obj.(*current.Service) out = obj.(*current.Service)
if out.Spec.TargetPort.Kind != util.IntstrInt || out.Spec.TargetPort.IntVal != 5678 { if out.Spec.Ports[0].TargetPort != util.NewIntOrStringFromInt(5678) {
t.Errorf("Expected TargetPort to be unchanged, got %s", out.Spec.TargetPort) t.Errorf("Expected TargetPort to be unchanged, got %s", out.Spec.Ports[0].TargetPort)
}
}
func TestSetDefaultServicePort(t *testing.T) {
// Unchanged if set.
in := &current.Service{Spec: current.ServiceSpec{
Ports: []current.ServicePort{
{Protocol: "UDP", Port: 9376, TargetPort: util.NewIntOrStringFromString("p")},
{Protocol: "UDP", Port: 8675, TargetPort: util.NewIntOrStringFromInt(309)},
},
}}
out := roundTrip(t, runtime.Object(in)).(*current.Service)
if out.Spec.Ports[0].Protocol != current.ProtocolUDP {
t.Errorf("Expected protocol %s, got %s", current.ProtocolUDP, out.Spec.Ports[0].Protocol)
}
if out.Spec.Ports[0].TargetPort != util.NewIntOrStringFromString("p") {
t.Errorf("Expected port %d, got %s", in.Spec.Ports[0].Port, out.Spec.Ports[0].TargetPort)
}
if out.Spec.Ports[1].Protocol != current.ProtocolUDP {
t.Errorf("Expected protocol %s, got %s", current.ProtocolUDP, out.Spec.Ports[1].Protocol)
}
if out.Spec.Ports[1].TargetPort != util.NewIntOrStringFromInt(309) {
t.Errorf("Expected port %d, got %s", in.Spec.Ports[1].Port, out.Spec.Ports[1].TargetPort)
}
// Defaulted.
in = &current.Service{Spec: current.ServiceSpec{
Ports: []current.ServicePort{
{Protocol: "", Port: 9376, TargetPort: util.NewIntOrStringFromString("")},
{Protocol: "", Port: 8675, TargetPort: util.NewIntOrStringFromInt(0)},
},
}}
out = roundTrip(t, runtime.Object(in)).(*current.Service)
if out.Spec.Ports[0].Protocol != current.ProtocolTCP {
t.Errorf("Expected protocol %s, got %s", current.ProtocolTCP, out.Spec.Ports[0].Protocol)
}
if out.Spec.Ports[0].TargetPort != util.NewIntOrStringFromInt(in.Spec.Ports[0].Port) {
t.Errorf("Expected port %d, got %d", in.Spec.Ports[0].Port, out.Spec.Ports[0].TargetPort)
}
if out.Spec.Ports[1].Protocol != current.ProtocolTCP {
t.Errorf("Expected protocol %s, got %s", current.ProtocolTCP, out.Spec.Ports[1].Protocol)
}
if out.Spec.Ports[1].TargetPort != util.NewIntOrStringFromInt(in.Spec.Ports[1].Port) {
t.Errorf("Expected port %d, got %d", in.Spec.Ports[1].Port, out.Spec.Ports[1].TargetPort)
} }
} }
......
...@@ -853,12 +853,8 @@ type ServiceStatus struct{} ...@@ -853,12 +853,8 @@ type ServiceStatus struct{}
// ServiceSpec describes the attributes that a user creates on a service // ServiceSpec describes the attributes that a user creates on a service
type ServiceSpec struct { type ServiceSpec struct {
// Port is the TCP or UDP port that will be made available to each pod for connecting to the pods // Required: The list of ports that are exposed by this service.
// proxied by this service. Ports []ServicePort `json:"ports" description:"ports exposed by the service"`
Port int `json:"port" description:"port exposed by the service"`
// Optional: Supports "TCP" and "UDP". Defaults to "TCP".
Protocol Protocol `json:"protocol,omitempty" description:"protocol for port; must be UDP or TCP; TCP if unspecified"`
// This service will route traffic to pods having labels matching this selector. If null, no endpoints will be automatically created. If empty, all pods will be selected. // This service will route traffic to pods having labels matching this selector. If null, no endpoints will be automatically created. If empty, all pods will be selected.
Selector map[string]string `json:"selector" description:"label keys and values that must match in order to receive traffic for this service; if empty, all pods are selected, if not specified, endpoints must be manually specified"` Selector map[string]string `json:"selector" description:"label keys and values that must match in order to receive traffic for this service; if empty, all pods are selected, if not specified, endpoints must be manually specified"`
...@@ -877,15 +873,31 @@ type ServiceSpec struct { ...@@ -877,15 +873,31 @@ type ServiceSpec struct {
// users to handle external traffic that arrives at a node. // users to handle external traffic that arrives at a node.
PublicIPs []string `json:"publicIPs,omitempty" description:"externally visible IPs (e.g. load balancers) that should be proxied to this service"` PublicIPs []string `json:"publicIPs,omitempty" description:"externally visible IPs (e.g. load balancers) that should be proxied to this service"`
// TargetPort is the name or number of the port on the container to direct traffic to.
// This is useful if the containers the service points to have multiple open ports.
// Optional: If unspecified, the service port is used (an identity map).
TargetPort util.IntOrString `json:"targetPort,omitempty" description:"number or name of the port to access on the containers belonging to pods targeted by the service; defaults to the container's first open port"`
// Optional: Supports "ClientIP" and "None". Used to maintain session affinity. // Optional: Supports "ClientIP" and "None". Used to maintain session affinity.
SessionAffinity AffinityType `json:"sessionAffinity,omitempty" description:"enable client IP based session affinity; must be ClientIP or None; defaults to None"` SessionAffinity AffinityType `json:"sessionAffinity,omitempty" description:"enable client IP based session affinity; must be ClientIP or None; defaults to None"`
} }
type ServicePort struct {
// Optional if only one ServicePort is defined on this service: The
// name of this port within the service. This must be a DNS_LABEL.
// All ports within a ServiceSpec must have unique names. This maps to
// the 'Name' field in EndpointPort objects.
Name string `json:"name" description:"the name of this port; optional if only one port is defined"`
// Optional: The IP protocol for this port. Supports "TCP" and "UDP",
// default is TCP.
Protocol Protocol `json:"protocol" description:"the protocol used by this port; must be UDP or TCP; TCP if unspecified"`
// Required: The port that will be exposed by this service.
Port int `json:"port" description:"the port number that is exposed"`
// Optional: The target port on pods selected by this service.
// If this is a string, it will be looked up as a named port in the
// target Pod's container ports. If this is not specified, the value
// of Port is used (an identity map).
TargetPort util.IntOrString `json:"targetPort" description:"the port to access on the pods targeted by the service; defaults to the service port"`
}
// Service is a named abstraction of software service (for example, mysql) consisting of local port // Service is a named abstraction of software service (for example, mysql) consisting of local port
// (for example 3306) that the proxy listens on, and the selector that determines which pods // (for example 3306) that the proxy listens on, and the selector that determines which pods
// will answer requests sent through the proxy. // will answer requests sent through the proxy.
......
...@@ -786,18 +786,12 @@ func ValidateService(service *api.Service) errs.ValidationErrorList { ...@@ -786,18 +786,12 @@ func ValidateService(service *api.Service) errs.ValidationErrorList {
allErrs := errs.ValidationErrorList{} allErrs := errs.ValidationErrorList{}
allErrs = append(allErrs, ValidateObjectMeta(&service.ObjectMeta, true, ValidateServiceName).Prefix("metadata")...) allErrs = append(allErrs, ValidateObjectMeta(&service.ObjectMeta, true, ValidateServiceName).Prefix("metadata")...)
if !util.IsValidPortNum(service.Spec.Port) { if len(service.Spec.Ports) == 0 {
allErrs = append(allErrs, errs.NewFieldInvalid("spec.port", service.Spec.Port, portRangeErrorMsg)) allErrs = append(allErrs, errs.NewFieldRequired("spec.ports"))
} }
if len(service.Spec.Protocol) == 0 { allPortNames := util.StringSet{}
allErrs = append(allErrs, errs.NewFieldRequired("spec.protocol")) for i := range service.Spec.Ports {
} else if !supportedPortProtocols.Has(strings.ToUpper(string(service.Spec.Protocol))) { allErrs = append(allErrs, validateServicePort(&service.Spec.Ports[i], i, &allPortNames).PrefixIndex(i).Prefix("spec.ports")...)
allErrs = append(allErrs, errs.NewFieldNotSupported("spec.protocol", service.Spec.Protocol))
}
if service.Spec.TargetPort.Kind == util.IntstrInt && service.Spec.TargetPort.IntVal != 0 && !util.IsValidPortNum(service.Spec.TargetPort.IntVal) {
allErrs = append(allErrs, errs.NewFieldInvalid("spec.containerPort", service.Spec.Port, portRangeErrorMsg))
} else if service.Spec.TargetPort.Kind == util.IntstrString && len(service.Spec.TargetPort.StrVal) == 0 {
allErrs = append(allErrs, errs.NewFieldRequired("spec.containerPort"))
} }
if service.Spec.Selector != nil { if service.Spec.Selector != nil {
...@@ -827,6 +821,39 @@ func ValidateService(service *api.Service) errs.ValidationErrorList { ...@@ -827,6 +821,39 @@ func ValidateService(service *api.Service) errs.ValidationErrorList {
return allErrs return allErrs
} }
func validateServicePort(sp *api.ServicePort, index int, allNames *util.StringSet) errs.ValidationErrorList {
allErrs := errs.ValidationErrorList{}
if len(sp.Name) == 0 {
// Allow empty names if they are the first port (mostly for compat).
if index != 0 {
allErrs = append(allErrs, errs.NewFieldRequired("name"))
}
} else if !util.IsDNS1123Label(sp.Name) {
allErrs = append(allErrs, errs.NewFieldInvalid("name", sp.Name, dns1123LabelErrorMsg))
} else if allNames.Has(sp.Name) {
allErrs = append(allErrs, errs.NewFieldDuplicate("name", sp.Name))
}
if !util.IsValidPortNum(sp.Port) {
allErrs = append(allErrs, errs.NewFieldInvalid("port", sp.Port, portRangeErrorMsg))
}
if len(sp.Protocol) == 0 {
allErrs = append(allErrs, errs.NewFieldRequired("protocol"))
} else if !supportedPortProtocols.Has(strings.ToUpper(string(sp.Protocol))) {
allErrs = append(allErrs, errs.NewFieldNotSupported("protocol", sp.Protocol))
}
if sp.TargetPort != util.NewIntOrStringFromInt(0) && sp.TargetPort != util.NewIntOrStringFromString("") {
if sp.TargetPort.Kind == util.IntstrInt && !util.IsValidPortNum(sp.TargetPort.IntVal) {
allErrs = append(allErrs, errs.NewFieldInvalid("targetPort", sp.TargetPort, portRangeErrorMsg))
}
}
return allErrs
}
// ValidateServiceUpdate tests if required fields in the service are set during an update // ValidateServiceUpdate tests if required fields in the service are set during an update
func ValidateServiceUpdate(oldService, service *api.Service) errs.ValidationErrorList { func ValidateServiceUpdate(oldService, service *api.Service) errs.ValidationErrorList {
allErrs := errs.ValidationErrorList{} allErrs := errs.ValidationErrorList{}
......
...@@ -1293,6 +1293,13 @@ func TestValidateService(t *testing.T) { ...@@ -1293,6 +1293,13 @@ func TestValidateService(t *testing.T) {
numErrs: 1, numErrs: 1,
}, },
{ {
name: "nil selector",
makeSvc: func(s *api.Service) {
s.Spec.Selector = nil
},
numErrs: 0,
},
{
name: "invalid selector", name: "invalid selector",
makeSvc: func(s *api.Service) { makeSvc: func(s *api.Service) {
s.Spec.Selector["NoSpecialCharsLike=Equals"] = "bar" s.Spec.Selector["NoSpecialCharsLike=Equals"] = "bar"
...@@ -1307,16 +1314,44 @@ func TestValidateService(t *testing.T) { ...@@ -1307,16 +1314,44 @@ func TestValidateService(t *testing.T) {
numErrs: 1, numErrs: 1,
}, },
{ {
name: "missing ports",
makeSvc: func(s *api.Service) {
s.Spec.Ports = nil
},
numErrs: 1,
},
{
name: "empty port[0] name",
makeSvc: func(s *api.Service) {
s.Spec.Ports[0].Name = ""
},
numErrs: 0,
},
{
name: "empty port[1] name",
makeSvc: func(s *api.Service) {
s.Spec.Ports = append(s.Spec.Ports, api.ServicePort{Name: "", Protocol: "TCP", Port: 12345})
},
numErrs: 1,
},
{
name: "invalid port name",
makeSvc: func(s *api.Service) {
s.Spec.Ports[0].Name = "INVALID"
},
numErrs: 1,
},
{
name: "missing protocol", name: "missing protocol",
makeSvc: func(s *api.Service) { makeSvc: func(s *api.Service) {
s.Spec.Protocol = "" s.Spec.Ports[0].Protocol = ""
}, },
numErrs: 1, numErrs: 1,
}, },
{ {
name: "invalid protocol", name: "invalid protocol",
makeSvc: func(s *api.Service) { makeSvc: func(s *api.Service) {
s.Spec.Protocol = "INVALID" s.Spec.Ports[0].Protocol = "INVALID"
}, },
numErrs: 1, numErrs: 1,
}, },
...@@ -1330,28 +1365,21 @@ func TestValidateService(t *testing.T) { ...@@ -1330,28 +1365,21 @@ func TestValidateService(t *testing.T) {
{ {
name: "missing port", name: "missing port",
makeSvc: func(s *api.Service) { makeSvc: func(s *api.Service) {
s.Spec.Port = 0 s.Spec.Ports[0].Port = 0
}, },
numErrs: 1, numErrs: 1,
}, },
{ {
name: "invalid port", name: "invalid port",
makeSvc: func(s *api.Service) { makeSvc: func(s *api.Service) {
s.Spec.Port = 65536 s.Spec.Ports[0].Port = 65536
}, },
numErrs: 1, numErrs: 1,
}, },
{ {
name: "missing targetPort string", name: "invalid TargetPort int",
makeSvc: func(s *api.Service) { makeSvc: func(s *api.Service) {
s.Spec.TargetPort = util.NewIntOrStringFromString("") s.Spec.Ports[0].TargetPort = util.NewIntOrStringFromInt(65536)
},
numErrs: 1,
},
{
name: "invalid targetPort int",
makeSvc: func(s *api.Service) {
s.Spec.TargetPort = util.NewIntOrStringFromInt(65536)
}, },
numErrs: 1, numErrs: 1,
}, },
...@@ -1377,11 +1405,12 @@ func TestValidateService(t *testing.T) { ...@@ -1377,11 +1405,12 @@ func TestValidateService(t *testing.T) {
numErrs: 0, numErrs: 0,
}, },
{ {
name: "nil selector", name: "dup port name",
makeSvc: func(s *api.Service) { makeSvc: func(s *api.Service) {
s.Spec.Selector = nil s.Spec.Ports[0].Name = "p"
s.Spec.Ports = append(s.Spec.Ports, api.ServicePort{Name: "p", Port: 12345})
}, },
numErrs: 0, numErrs: 1,
}, },
{ {
name: "valid 1", name: "valid 1",
...@@ -1393,15 +1422,15 @@ func TestValidateService(t *testing.T) { ...@@ -1393,15 +1422,15 @@ func TestValidateService(t *testing.T) {
{ {
name: "valid 2", name: "valid 2",
makeSvc: func(s *api.Service) { makeSvc: func(s *api.Service) {
s.Spec.Protocol = "UDP" s.Spec.Ports[0].Protocol = "UDP"
s.Spec.TargetPort = util.NewIntOrStringFromInt(12345) s.Spec.Ports[0].TargetPort = util.NewIntOrStringFromInt(12345)
}, },
numErrs: 0, numErrs: 0,
}, },
{ {
name: "valid 3", name: "valid 3",
makeSvc: func(s *api.Service) { makeSvc: func(s *api.Service) {
s.Spec.TargetPort = util.NewIntOrStringFromString("http") s.Spec.Ports[0].TargetPort = util.NewIntOrStringFromString("http")
}, },
numErrs: 0, numErrs: 0,
}, },
...@@ -1416,6 +1445,7 @@ func TestValidateService(t *testing.T) { ...@@ -1416,6 +1445,7 @@ func TestValidateService(t *testing.T) {
name: "valid portal ip - empty", name: "valid portal ip - empty",
makeSvc: func(s *api.Service) { makeSvc: func(s *api.Service) {
s.Spec.PortalIP = "" s.Spec.PortalIP = ""
s.Spec.Ports[0].TargetPort = util.NewIntOrStringFromString("http")
}, },
numErrs: 0, numErrs: 0,
}, },
...@@ -1432,8 +1462,7 @@ func TestValidateService(t *testing.T) { ...@@ -1432,8 +1462,7 @@ func TestValidateService(t *testing.T) {
Spec: api.ServiceSpec{ Spec: api.ServiceSpec{
Selector: map[string]string{"key": "val"}, Selector: map[string]string{"key": "val"},
SessionAffinity: "None", SessionAffinity: "None",
Port: 8675, Ports: []api.ServicePort{{Name: "p", Protocol: "TCP", Port: 8675}},
Protocol: "TCP",
}, },
} }
tc.makeSvc(&svc) tc.makeSvc(&svc)
......
...@@ -694,7 +694,7 @@ func TestRequestDo(t *testing.T) { ...@@ -694,7 +694,7 @@ func TestRequestDo(t *testing.T) {
func TestDoRequestNewWay(t *testing.T) { func TestDoRequestNewWay(t *testing.T) {
reqBody := "request body" reqBody := "request body"
expectedObj := &api.Service{Spec: api.ServiceSpec{Port: 12345}} expectedObj := &api.Service{Spec: api.ServiceSpec{Ports: []api.ServicePort{{Port: 12345}}}}
expectedBody, _ := v1beta2.Codec.Encode(expectedObj) expectedBody, _ := v1beta2.Codec.Encode(expectedObj)
fakeHandler := util.FakeHandler{ fakeHandler := util.FakeHandler{
StatusCode: 200, StatusCode: 200,
...@@ -728,7 +728,7 @@ func TestDoRequestNewWay(t *testing.T) { ...@@ -728,7 +728,7 @@ func TestDoRequestNewWay(t *testing.T) {
func TestDoRequestNewWayReader(t *testing.T) { func TestDoRequestNewWayReader(t *testing.T) {
reqObj := &api.Pod{ObjectMeta: api.ObjectMeta{Name: "foo"}} reqObj := &api.Pod{ObjectMeta: api.ObjectMeta{Name: "foo"}}
reqBodyExpected, _ := v1beta1.Codec.Encode(reqObj) reqBodyExpected, _ := v1beta1.Codec.Encode(reqObj)
expectedObj := &api.Service{Spec: api.ServiceSpec{Port: 12345}} expectedObj := &api.Service{Spec: api.ServiceSpec{Ports: []api.ServicePort{{Port: 12345}}}}
expectedBody, _ := v1beta1.Codec.Encode(expectedObj) expectedBody, _ := v1beta1.Codec.Encode(expectedObj)
fakeHandler := util.FakeHandler{ fakeHandler := util.FakeHandler{
StatusCode: 200, StatusCode: 200,
...@@ -764,7 +764,7 @@ func TestDoRequestNewWayReader(t *testing.T) { ...@@ -764,7 +764,7 @@ func TestDoRequestNewWayReader(t *testing.T) {
func TestDoRequestNewWayObj(t *testing.T) { func TestDoRequestNewWayObj(t *testing.T) {
reqObj := &api.Pod{ObjectMeta: api.ObjectMeta{Name: "foo"}} reqObj := &api.Pod{ObjectMeta: api.ObjectMeta{Name: "foo"}}
reqBodyExpected, _ := v1beta2.Codec.Encode(reqObj) reqBodyExpected, _ := v1beta2.Codec.Encode(reqObj)
expectedObj := &api.Service{Spec: api.ServiceSpec{Port: 12345}} expectedObj := &api.Service{Spec: api.ServiceSpec{Ports: []api.ServicePort{{Port: 12345}}}}
expectedBody, _ := v1beta2.Codec.Encode(expectedObj) expectedBody, _ := v1beta2.Codec.Encode(expectedObj)
fakeHandler := util.FakeHandler{ fakeHandler := util.FakeHandler{
StatusCode: 200, StatusCode: 200,
...@@ -814,7 +814,7 @@ func TestDoRequestNewWayFile(t *testing.T) { ...@@ -814,7 +814,7 @@ func TestDoRequestNewWayFile(t *testing.T) {
t.Errorf("unexpected error: %v", err) t.Errorf("unexpected error: %v", err)
} }
expectedObj := &api.Service{Spec: api.ServiceSpec{Port: 12345}} expectedObj := &api.Service{Spec: api.ServiceSpec{Ports: []api.ServicePort{{Port: 12345}}}}
expectedBody, _ := v1beta1.Codec.Encode(expectedObj) expectedBody, _ := v1beta1.Codec.Encode(expectedObj)
fakeHandler := util.FakeHandler{ fakeHandler := util.FakeHandler{
StatusCode: 200, StatusCode: 200,
...@@ -855,7 +855,7 @@ func TestWasCreated(t *testing.T) { ...@@ -855,7 +855,7 @@ func TestWasCreated(t *testing.T) {
t.Errorf("unexpected error: %v", err) t.Errorf("unexpected error: %v", err)
} }
expectedObj := &api.Service{Spec: api.ServiceSpec{Port: 12345}} expectedObj := &api.Service{Spec: api.ServiceSpec{Ports: []api.ServicePort{{Port: 12345}}}}
expectedBody, _ := v1beta1.Codec.Encode(expectedObj) expectedBody, _ := v1beta1.Codec.Encode(expectedObj)
fakeHandler := util.FakeHandler{ fakeHandler := util.FakeHandler{
StatusCode: 201, StatusCode: 201,
......
...@@ -48,7 +48,7 @@ type TCPLoadBalancer interface { ...@@ -48,7 +48,7 @@ type TCPLoadBalancer interface {
// TODO: Break this up into different interfaces (LB, etc) when we have more than one type of service // TODO: Break this up into different interfaces (LB, etc) when we have more than one type of service
TCPLoadBalancerExists(name, region string) (bool, error) TCPLoadBalancerExists(name, region string) (bool, error)
// CreateTCPLoadBalancer creates a new tcp load balancer. Returns the IP address or hostname of the balancer // CreateTCPLoadBalancer creates a new tcp load balancer. Returns the IP address or hostname of the balancer
CreateTCPLoadBalancer(name, region string, externalIP net.IP, port int, hosts []string, affinityType api.AffinityType) (string, error) CreateTCPLoadBalancer(name, region string, externalIP net.IP, ports []int, hosts []string, affinityType api.AffinityType) (string, error)
// UpdateTCPLoadBalancer updates hosts under the specified load balancer. // UpdateTCPLoadBalancer updates hosts under the specified load balancer.
UpdateTCPLoadBalancer(name, region string, hosts []string) error UpdateTCPLoadBalancer(name, region string, hosts []string) error
// DeleteTCPLoadBalancer deletes a specified load balancer. // DeleteTCPLoadBalancer deletes a specified load balancer.
......
...@@ -29,7 +29,7 @@ type FakeBalancer struct { ...@@ -29,7 +29,7 @@ type FakeBalancer struct {
Name string Name string
Region string Region string
ExternalIP net.IP ExternalIP net.IP
Port int Ports []int
Hosts []string Hosts []string
} }
...@@ -95,9 +95,9 @@ func (f *FakeCloud) TCPLoadBalancerExists(name, region string) (bool, error) { ...@@ -95,9 +95,9 @@ func (f *FakeCloud) TCPLoadBalancerExists(name, region string) (bool, error) {
// CreateTCPLoadBalancer is a test-spy implementation of TCPLoadBalancer.CreateTCPLoadBalancer. // CreateTCPLoadBalancer is a test-spy implementation of TCPLoadBalancer.CreateTCPLoadBalancer.
// It adds an entry "create" into the internal method call record. // It adds an entry "create" into the internal method call record.
func (f *FakeCloud) CreateTCPLoadBalancer(name, region string, externalIP net.IP, port int, hosts []string, affinityType api.AffinityType) (string, error) { func (f *FakeCloud) CreateTCPLoadBalancer(name, region string, externalIP net.IP, ports []int, hosts []string, affinityType api.AffinityType) (string, error) {
f.addCall("create") f.addCall("create")
f.Balancers = append(f.Balancers, FakeBalancer{name, region, externalIP, port, hosts}) f.Balancers = append(f.Balancers, FakeBalancer{name, region, externalIP, ports, hosts})
return f.ExternalIP.String(), f.Err return f.ExternalIP.String(), f.Err
} }
......
...@@ -228,15 +228,29 @@ func translateAffinityType(affinityType api.AffinityType) GCEAffinityType { ...@@ -228,15 +228,29 @@ func translateAffinityType(affinityType api.AffinityType) GCEAffinityType {
} }
// CreateTCPLoadBalancer is an implementation of TCPLoadBalancer.CreateTCPLoadBalancer. // CreateTCPLoadBalancer is an implementation of TCPLoadBalancer.CreateTCPLoadBalancer.
func (gce *GCECloud) CreateTCPLoadBalancer(name, region string, externalIP net.IP, port int, hosts []string, affinityType api.AffinityType) (string, error) { func (gce *GCECloud) CreateTCPLoadBalancer(name, region string, externalIP net.IP, ports []int, hosts []string, affinityType api.AffinityType) (string, error) {
pool, err := gce.makeTargetPool(name, region, hosts, translateAffinityType(affinityType)) pool, err := gce.makeTargetPool(name, region, hosts, translateAffinityType(affinityType))
if err != nil { if err != nil {
return "", err return "", err
} }
if len(ports) == 0 {
return "", fmt.Errorf("no ports specified for GCE load balancer")
}
minPort := 65536
maxPort := 0
for i := range ports {
if ports[i] < minPort {
minPort = ports[i]
}
if ports[i] > maxPort {
maxPort = ports[i]
}
}
req := &compute.ForwardingRule{ req := &compute.ForwardingRule{
Name: name, Name: name,
IPProtocol: "TCP", IPProtocol: "TCP",
PortRange: strconv.Itoa(port), PortRange: fmt.Sprintf("%d-%d", minPort, maxPort),
Target: pool, Target: pool,
} }
if len(externalIP) > 0 { if len(externalIP) > 0 {
......
...@@ -485,8 +485,8 @@ func (lb *LoadBalancer) TCPLoadBalancerExists(name, region string) (bool, error) ...@@ -485,8 +485,8 @@ func (lb *LoadBalancer) TCPLoadBalancerExists(name, region string) (bool, error)
// a list of regions (from config) and query/create loadbalancers in // a list of regions (from config) and query/create loadbalancers in
// each region. // each region.
func (lb *LoadBalancer) CreateTCPLoadBalancer(name, region string, externalIP net.IP, port int, hosts []string, affinity api.AffinityType) (string, error) { func (lb *LoadBalancer) CreateTCPLoadBalancer(name, region string, externalIP net.IP, ports []int, hosts []string, affinity api.AffinityType) (string, error) {
glog.V(4).Infof("CreateTCPLoadBalancer(%v, %v, %v, %v, %v, %v)", name, region, externalIP, port, hosts, affinity) glog.V(4).Infof("CreateTCPLoadBalancer(%v, %v, %v, %v, %v, %v)", name, region, externalIP, ports, hosts, affinity)
var persistence *vips.SessionPersistence var persistence *vips.SessionPersistence
switch affinity { switch affinity {
...@@ -515,7 +515,7 @@ func (lb *LoadBalancer) CreateTCPLoadBalancer(name, region string, externalIP ne ...@@ -515,7 +515,7 @@ func (lb *LoadBalancer) CreateTCPLoadBalancer(name, region string, externalIP ne
_, err = members.Create(lb.network, members.CreateOpts{ _, err = members.Create(lb.network, members.CreateOpts{
PoolID: pool.ID, PoolID: pool.ID,
ProtocolPort: port, ProtocolPort: ports[0], //FIXME: need to handle multi-port
Address: addr, Address: addr,
}).Extract() }).Extract()
if err != nil { if err != nil {
...@@ -550,7 +550,7 @@ func (lb *LoadBalancer) CreateTCPLoadBalancer(name, region string, externalIP ne ...@@ -550,7 +550,7 @@ func (lb *LoadBalancer) CreateTCPLoadBalancer(name, region string, externalIP ne
Description: fmt.Sprintf("Kubernetes external service %s", name), Description: fmt.Sprintf("Kubernetes external service %s", name),
Address: externalIP.String(), Address: externalIP.String(),
Protocol: "TCP", Protocol: "TCP",
ProtocolPort: port, ProtocolPort: ports[0], //FIXME: need to handle multi-port
PoolID: pool.ID, PoolID: pool.ID,
Persistence: persistence, Persistence: persistence,
}).Extract() }).Extract()
......
...@@ -65,7 +65,6 @@ func testData() (*api.PodList, *api.ServiceList, *api.ReplicationControllerList) ...@@ -65,7 +65,6 @@ func testData() (*api.PodList, *api.ServiceList, *api.ReplicationControllerList)
{ {
ObjectMeta: api.ObjectMeta{Name: "baz", Namespace: "test", ResourceVersion: "12"}, ObjectMeta: api.ObjectMeta{Name: "baz", Namespace: "test", ResourceVersion: "12"},
Spec: api.ServiceSpec{ Spec: api.ServiceSpec{
Protocol: "TCP",
SessionAffinity: "None", SessionAffinity: "None",
}, },
}, },
......
...@@ -135,15 +135,11 @@ func TestMerge(t *testing.T) { ...@@ -135,15 +135,11 @@ func TestMerge(t *testing.T) {
{ {
kind: "Service", kind: "Service",
obj: &api.Service{ obj: &api.Service{
Spec: api.ServiceSpec{ Spec: api.ServiceSpec{},
Port: 10,
},
}, },
fragment: `{ "apiVersion": "v1beta1", "port": 0 }`, fragment: `{ "apiVersion": "v1beta1", "port": 0 }`,
expected: &api.Service{ expected: &api.Service{
Spec: api.ServiceSpec{ Spec: api.ServiceSpec{
Port: 0,
Protocol: "TCP",
SessionAffinity: "None", SessionAffinity: "None",
}, },
}, },
...@@ -160,7 +156,6 @@ func TestMerge(t *testing.T) { ...@@ -160,7 +156,6 @@ func TestMerge(t *testing.T) {
fragment: `{ "apiVersion": "v1beta1", "selector": { "version": "v2" } }`, fragment: `{ "apiVersion": "v1beta1", "selector": { "version": "v2" } }`,
expected: &api.Service{ expected: &api.Service{
Spec: api.ServiceSpec{ Spec: api.ServiceSpec{
Protocol: "TCP",
SessionAffinity: "None", SessionAffinity: "None",
Selector: map[string]string{ Selector: map[string]string{
"version": "v2", "version": "v2",
......
...@@ -333,7 +333,15 @@ func describeService(service *api.Service, endpoints *api.Endpoints, events *api ...@@ -333,7 +333,15 @@ func describeService(service *api.Service, endpoints *api.Endpoints, events *api
list := strings.Join(service.Spec.PublicIPs, ", ") list := strings.Join(service.Spec.PublicIPs, ", ")
fmt.Fprintf(out, "Public IPs:\t%s\n", list) fmt.Fprintf(out, "Public IPs:\t%s\n", list)
} }
fmt.Fprintf(out, "Port:\t%d\n", service.Spec.Port) for i := range service.Spec.Ports {
sp := &service.Spec.Ports[i]
name := sp.Name
if name == "" {
name = "<unnamed>"
}
fmt.Fprintf(out, "Port:\t%s\t%d/%s\n", sp.Name, sp.Port, sp.Protocol)
}
fmt.Fprintf(out, "Endpoints:\t%s\n", formatEndpoints(endpoints)) fmt.Fprintf(out, "Endpoints:\t%s\n", formatEndpoints(endpoints))
fmt.Fprintf(out, "Session Affinity:\t%s\n", service.Spec.SessionAffinity) fmt.Fprintf(out, "Session Affinity:\t%s\n", service.Spec.SessionAffinity)
if events != nil { if events != nil {
......
...@@ -228,7 +228,7 @@ func (h *HumanReadablePrinter) validatePrintHandlerFunc(printFunc reflect.Value) ...@@ -228,7 +228,7 @@ func (h *HumanReadablePrinter) validatePrintHandlerFunc(printFunc reflect.Value)
var podColumns = []string{"POD", "IP", "CONTAINER(S)", "IMAGE(S)", "HOST", "LABELS", "STATUS", "CREATED"} var podColumns = []string{"POD", "IP", "CONTAINER(S)", "IMAGE(S)", "HOST", "LABELS", "STATUS", "CREATED"}
var replicationControllerColumns = []string{"CONTROLLER", "CONTAINER(S)", "IMAGE(S)", "SELECTOR", "REPLICAS"} var replicationControllerColumns = []string{"CONTROLLER", "CONTAINER(S)", "IMAGE(S)", "SELECTOR", "REPLICAS"}
var serviceColumns = []string{"NAME", "LABELS", "SELECTOR", "IP", "PORT"} var serviceColumns = []string{"NAME", "LABELS", "SELECTOR", "IP", "PORT(S)"}
var endpointColumns = []string{"NAME", "ENDPOINTS"} var endpointColumns = []string{"NAME", "ENDPOINTS"}
var nodeColumns = []string{"NAME", "LABELS", "STATUS"} var nodeColumns = []string{"NAME", "LABELS", "STATUS"}
var statusColumns = []string{"STATUS"} var statusColumns = []string{"STATUS"}
...@@ -390,9 +390,18 @@ func printReplicationControllerList(list *api.ReplicationControllerList, w io.Wr ...@@ -390,9 +390,18 @@ func printReplicationControllerList(list *api.ReplicationControllerList, w io.Wr
} }
func printService(svc *api.Service, w io.Writer) error { func printService(svc *api.Service, w io.Writer) error {
_, err := fmt.Fprintf(w, "%s\t%s\t%s\t%s\t%d\n", svc.Name, formatLabels(svc.Labels), if _, err := fmt.Fprintf(w, "%s\t%s\t%s\t%s\t%d/%s\n", svc.Name, formatLabels(svc.Labels),
formatLabels(svc.Spec.Selector), svc.Spec.PortalIP, svc.Spec.Port) formatLabels(svc.Spec.Selector), svc.Spec.PortalIP, svc.Spec.Ports[0].Port, svc.Spec.Ports[0].Protocol); err != nil {
return err return err
}
for i := 1; i < len(svc.Spec.Ports); i++ {
// Lay out additional ports.
if _, err := fmt.Fprintf(w, "%s\t%s\t%s\t%s\t%d/%s\n", "", "", "", "", svc.Spec.Ports[i].Port, svc.Spec.Ports[i].Protocol); err != nil {
return err
}
}
return nil
} }
func printServiceList(list *api.ServiceList, w io.Writer) error { func printServiceList(list *api.ServiceList, w io.Writer) error {
......
...@@ -71,9 +71,14 @@ func (ServiceGenerator) Generate(params map[string]string) (runtime.Object, erro ...@@ -71,9 +71,14 @@ func (ServiceGenerator) Generate(params map[string]string) (runtime.Object, erro
Labels: labels, Labels: labels,
}, },
Spec: api.ServiceSpec{ Spec: api.ServiceSpec{
Port: port,
Protocol: api.Protocol(params["protocol"]),
Selector: selector, Selector: selector,
Ports: []api.ServicePort{
{
Name: "default",
Port: port,
Protocol: api.Protocol(params["protocol"]),
},
},
}, },
} }
targetPort, found := params["target-port"] targetPort, found := params["target-port"]
...@@ -82,12 +87,12 @@ func (ServiceGenerator) Generate(params map[string]string) (runtime.Object, erro ...@@ -82,12 +87,12 @@ func (ServiceGenerator) Generate(params map[string]string) (runtime.Object, erro
} }
if found && len(targetPort) > 0 { if found && len(targetPort) > 0 {
if portNum, err := strconv.Atoi(targetPort); err != nil { if portNum, err := strconv.Atoi(targetPort); err != nil {
service.Spec.TargetPort = util.NewIntOrStringFromString(targetPort) service.Spec.Ports[0].TargetPort = util.NewIntOrStringFromString(targetPort)
} else { } else {
service.Spec.TargetPort = util.NewIntOrStringFromInt(portNum) service.Spec.Ports[0].TargetPort = util.NewIntOrStringFromInt(portNum)
} }
} else { } else {
service.Spec.TargetPort = util.NewIntOrStringFromInt(port) service.Spec.Ports[0].TargetPort = util.NewIntOrStringFromInt(port)
} }
if params["create-external-load-balancer"] == "true" { if params["create-external-load-balancer"] == "true" {
service.Spec.CreateExternalLoadBalancer = true service.Spec.CreateExternalLoadBalancer = true
......
...@@ -46,9 +46,14 @@ func TestGenerateService(t *testing.T) { ...@@ -46,9 +46,14 @@ func TestGenerateService(t *testing.T) {
"foo": "bar", "foo": "bar",
"baz": "blah", "baz": "blah",
}, },
Port: 80, Ports: []api.ServicePort{
Protocol: "TCP", {
TargetPort: util.NewIntOrStringFromInt(1234), Name: "default",
Port: 80,
Protocol: "TCP",
TargetPort: util.NewIntOrStringFromInt(1234),
},
},
}, },
}, },
}, },
...@@ -69,9 +74,14 @@ func TestGenerateService(t *testing.T) { ...@@ -69,9 +74,14 @@ func TestGenerateService(t *testing.T) {
"foo": "bar", "foo": "bar",
"baz": "blah", "baz": "blah",
}, },
Port: 80, Ports: []api.ServicePort{
Protocol: "UDP", {
TargetPort: util.NewIntOrStringFromString("foobar"), Name: "default",
Port: 80,
Protocol: "UDP",
TargetPort: util.NewIntOrStringFromString("foobar"),
},
},
}, },
}, },
}, },
...@@ -97,9 +107,14 @@ func TestGenerateService(t *testing.T) { ...@@ -97,9 +107,14 @@ func TestGenerateService(t *testing.T) {
"foo": "bar", "foo": "bar",
"baz": "blah", "baz": "blah",
}, },
Port: 80, Ports: []api.ServicePort{
Protocol: "TCP", {
TargetPort: util.NewIntOrStringFromInt(1234), Name: "default",
Port: 80,
Protocol: "TCP",
TargetPort: util.NewIntOrStringFromInt(1234),
},
},
}, },
}, },
}, },
...@@ -121,10 +136,15 @@ func TestGenerateService(t *testing.T) { ...@@ -121,10 +136,15 @@ func TestGenerateService(t *testing.T) {
"foo": "bar", "foo": "bar",
"baz": "blah", "baz": "blah",
}, },
Port: 80, Ports: []api.ServicePort{
Protocol: "UDP", {
PublicIPs: []string{"1.2.3.4"}, Name: "default",
TargetPort: util.NewIntOrStringFromString("foobar"), Port: 80,
Protocol: "UDP",
TargetPort: util.NewIntOrStringFromString("foobar"),
},
},
PublicIPs: []string{"1.2.3.4"},
}, },
}, },
}, },
...@@ -147,10 +167,15 @@ func TestGenerateService(t *testing.T) { ...@@ -147,10 +167,15 @@ func TestGenerateService(t *testing.T) {
"foo": "bar", "foo": "bar",
"baz": "blah", "baz": "blah",
}, },
Port: 80, Ports: []api.ServicePort{
Protocol: "UDP", {
Name: "default",
Port: 80,
Protocol: "UDP",
TargetPort: util.NewIntOrStringFromString("foobar"),
},
},
PublicIPs: []string{"1.2.3.4"}, PublicIPs: []string{"1.2.3.4"},
TargetPort: util.NewIntOrStringFromString("foobar"),
CreateExternalLoadBalancer: true, CreateExternalLoadBalancer: true,
}, },
}, },
......
...@@ -29,19 +29,30 @@ import ( ...@@ -29,19 +29,30 @@ import (
// provided as an argument. // provided as an argument.
func FromServices(services *api.ServiceList) []api.EnvVar { func FromServices(services *api.ServiceList) []api.EnvVar {
var result []api.EnvVar var result []api.EnvVar
for _, service := range services.Items { for i := range services.Items {
service := &services.Items[i]
// ignore services where PortalIP is "None" or empty // ignore services where PortalIP is "None" or empty
// the services passed to this method should be pre-filtered // the services passed to this method should be pre-filtered
// only services that have the portal IP set should be included here // only services that have the portal IP set should be included here
if !api.IsServiceIPSet(&service) { if !api.IsServiceIPSet(service) {
continue continue
} }
// Host // Host
name := makeEnvVariableName(service.Name) + "_SERVICE_HOST" name := makeEnvVariableName(service.Name) + "_SERVICE_HOST"
result = append(result, api.EnvVar{Name: name, Value: service.Spec.PortalIP}) result = append(result, api.EnvVar{Name: name, Value: service.Spec.PortalIP})
// Port // First port - give it the backwards-compatible name
name = makeEnvVariableName(service.Name) + "_SERVICE_PORT" name = makeEnvVariableName(service.Name) + "_SERVICE_PORT"
result = append(result, api.EnvVar{Name: name, Value: strconv.Itoa(service.Spec.Port)}) result = append(result, api.EnvVar{Name: name, Value: strconv.Itoa(service.Spec.Ports[0].Port)})
// All named ports (only the first may be unnamed, checked in validation)
for i := range service.Spec.Ports {
sp := &service.Spec.Ports[i]
if sp.Name != "" {
pn := name + "_" + makeEnvVariableName(sp.Name)
result = append(result, api.EnvVar{Name: pn, Value: strconv.Itoa(sp.Port)})
}
}
// Docker-compatible vars. // Docker-compatible vars.
result = append(result, makeLinkVariables(service)...) result = append(result, makeLinkVariables(service)...)
} }
...@@ -56,33 +67,42 @@ func makeEnvVariableName(str string) string { ...@@ -56,33 +67,42 @@ func makeEnvVariableName(str string) string {
return strings.ToUpper(strings.Replace(str, "-", "_", -1)) return strings.ToUpper(strings.Replace(str, "-", "_", -1))
} }
func makeLinkVariables(service api.Service) []api.EnvVar { func makeLinkVariables(service *api.Service) []api.EnvVar {
prefix := makeEnvVariableName(service.Name) prefix := makeEnvVariableName(service.Name)
protocol := string(api.ProtocolTCP) all := []api.EnvVar{}
if service.Spec.Protocol != "" { for i := range service.Spec.Ports {
protocol = string(service.Spec.Protocol) sp := &service.Spec.Ports[i]
}
portPrefix := fmt.Sprintf("%s_PORT_%d_%s", prefix, service.Spec.Port, strings.ToUpper(protocol)) protocol := string(api.ProtocolTCP)
return []api.EnvVar{ if sp.Protocol != "" {
{ protocol = string(sp.Protocol)
Name: prefix + "_PORT", }
Value: fmt.Sprintf("%s://%s:%d", strings.ToLower(protocol), service.Spec.PortalIP, service.Spec.Port), if i == 0 {
}, // Docker special-cases the first port.
{ all = append(all, api.EnvVar{
Name: portPrefix, Name: prefix + "_PORT",
Value: fmt.Sprintf("%s://%s:%d", strings.ToLower(protocol), service.Spec.PortalIP, service.Spec.Port), Value: fmt.Sprintf("%s://%s:%d", strings.ToLower(protocol), service.Spec.PortalIP, sp.Port),
}, })
{ }
Name: portPrefix + "_PROTO", portPrefix := fmt.Sprintf("%s_PORT_%d_%s", prefix, sp.Port, strings.ToUpper(protocol))
Value: strings.ToLower(protocol), all = append(all, []api.EnvVar{
}, {
{ Name: portPrefix,
Name: portPrefix + "_PORT", Value: fmt.Sprintf("%s://%s:%d", strings.ToLower(protocol), service.Spec.PortalIP, sp.Port),
Value: strconv.Itoa(service.Spec.Port), },
}, {
{ Name: portPrefix + "_PROTO",
Name: portPrefix + "_ADDR", Value: strings.ToLower(protocol),
Value: service.Spec.PortalIP, },
}, {
Name: portPrefix + "_PORT",
Value: strconv.Itoa(sp.Port),
},
{
Name: portPrefix + "_ADDR",
Value: service.Spec.PortalIP,
},
}...)
} }
return all
} }
...@@ -30,46 +30,53 @@ func TestFromServices(t *testing.T) { ...@@ -30,46 +30,53 @@ func TestFromServices(t *testing.T) {
{ {
ObjectMeta: api.ObjectMeta{Name: "foo-bar"}, ObjectMeta: api.ObjectMeta{Name: "foo-bar"},
Spec: api.ServiceSpec{ Spec: api.ServiceSpec{
Port: 8080,
Selector: map[string]string{"bar": "baz"}, Selector: map[string]string{"bar": "baz"},
Protocol: "TCP",
PortalIP: "1.2.3.4", PortalIP: "1.2.3.4",
Ports: []api.ServicePort{
{Port: 8080, Protocol: "TCP"},
},
}, },
}, },
{ {
ObjectMeta: api.ObjectMeta{Name: "abc-123"}, ObjectMeta: api.ObjectMeta{Name: "abc-123"},
Spec: api.ServiceSpec{ Spec: api.ServiceSpec{
Port: 8081,
Selector: map[string]string{"bar": "baz"}, Selector: map[string]string{"bar": "baz"},
Protocol: "UDP",
PortalIP: "5.6.7.8", PortalIP: "5.6.7.8",
Ports: []api.ServicePort{
{Name: "u-d-p", Port: 8081, Protocol: "UDP"},
{Name: "t-c-p", Port: 8081, Protocol: "TCP"},
},
}, },
}, },
{ {
ObjectMeta: api.ObjectMeta{Name: "q-u-u-x"}, ObjectMeta: api.ObjectMeta{Name: "q-u-u-x"},
Spec: api.ServiceSpec{ Spec: api.ServiceSpec{
Port: 8082,
Selector: map[string]string{"bar": "baz"}, Selector: map[string]string{"bar": "baz"},
Protocol: "TCP",
PortalIP: "9.8.7.6", PortalIP: "9.8.7.6",
Ports: []api.ServicePort{
{Port: 8082, Protocol: "TCP"},
{Name: "8083", Port: 8083, Protocol: "TCP"},
},
}, },
}, },
{ {
ObjectMeta: api.ObjectMeta{Name: "svrc-portalip-none"}, ObjectMeta: api.ObjectMeta{Name: "svrc-portalip-none"},
Spec: api.ServiceSpec{ Spec: api.ServiceSpec{
Port: 8082,
Selector: map[string]string{"bar": "baz"}, Selector: map[string]string{"bar": "baz"},
Protocol: "TCP",
PortalIP: "None", PortalIP: "None",
Ports: []api.ServicePort{
{Port: 8082, Protocol: "TCP"},
},
}, },
}, },
{ {
ObjectMeta: api.ObjectMeta{Name: "svrc-portalip-empty"}, ObjectMeta: api.ObjectMeta{Name: "svrc-portalip-empty"},
Spec: api.ServiceSpec{ Spec: api.ServiceSpec{
Port: 8082,
Selector: map[string]string{"bar": "baz"}, Selector: map[string]string{"bar": "baz"},
Protocol: "TCP",
PortalIP: "", PortalIP: "",
Ports: []api.ServicePort{
{Port: 8082, Protocol: "TCP"},
},
}, },
}, },
}, },
...@@ -85,18 +92,29 @@ func TestFromServices(t *testing.T) { ...@@ -85,18 +92,29 @@ func TestFromServices(t *testing.T) {
{Name: "FOO_BAR_PORT_8080_TCP_ADDR", Value: "1.2.3.4"}, {Name: "FOO_BAR_PORT_8080_TCP_ADDR", Value: "1.2.3.4"},
{Name: "ABC_123_SERVICE_HOST", Value: "5.6.7.8"}, {Name: "ABC_123_SERVICE_HOST", Value: "5.6.7.8"},
{Name: "ABC_123_SERVICE_PORT", Value: "8081"}, {Name: "ABC_123_SERVICE_PORT", Value: "8081"},
{Name: "ABC_123_SERVICE_PORT_U_D_P", Value: "8081"},
{Name: "ABC_123_SERVICE_PORT_T_C_P", Value: "8081"},
{Name: "ABC_123_PORT", Value: "udp://5.6.7.8:8081"}, {Name: "ABC_123_PORT", Value: "udp://5.6.7.8:8081"},
{Name: "ABC_123_PORT_8081_UDP", Value: "udp://5.6.7.8:8081"}, {Name: "ABC_123_PORT_8081_UDP", Value: "udp://5.6.7.8:8081"},
{Name: "ABC_123_PORT_8081_UDP_PROTO", Value: "udp"}, {Name: "ABC_123_PORT_8081_UDP_PROTO", Value: "udp"},
{Name: "ABC_123_PORT_8081_UDP_PORT", Value: "8081"}, {Name: "ABC_123_PORT_8081_UDP_PORT", Value: "8081"},
{Name: "ABC_123_PORT_8081_UDP_ADDR", Value: "5.6.7.8"}, {Name: "ABC_123_PORT_8081_UDP_ADDR", Value: "5.6.7.8"},
{Name: "ABC_123_PORT_8081_TCP", Value: "tcp://5.6.7.8:8081"},
{Name: "ABC_123_PORT_8081_TCP_PROTO", Value: "tcp"},
{Name: "ABC_123_PORT_8081_TCP_PORT", Value: "8081"},
{Name: "ABC_123_PORT_8081_TCP_ADDR", Value: "5.6.7.8"},
{Name: "Q_U_U_X_SERVICE_HOST", Value: "9.8.7.6"}, {Name: "Q_U_U_X_SERVICE_HOST", Value: "9.8.7.6"},
{Name: "Q_U_U_X_SERVICE_PORT", Value: "8082"}, {Name: "Q_U_U_X_SERVICE_PORT", Value: "8082"},
{Name: "Q_U_U_X_SERVICE_PORT_8083", Value: "8083"},
{Name: "Q_U_U_X_PORT", Value: "tcp://9.8.7.6:8082"}, {Name: "Q_U_U_X_PORT", Value: "tcp://9.8.7.6:8082"},
{Name: "Q_U_U_X_PORT_8082_TCP", Value: "tcp://9.8.7.6:8082"}, {Name: "Q_U_U_X_PORT_8082_TCP", Value: "tcp://9.8.7.6:8082"},
{Name: "Q_U_U_X_PORT_8082_TCP_PROTO", Value: "tcp"}, {Name: "Q_U_U_X_PORT_8082_TCP_PROTO", Value: "tcp"},
{Name: "Q_U_U_X_PORT_8082_TCP_PORT", Value: "8082"}, {Name: "Q_U_U_X_PORT_8082_TCP_PORT", Value: "8082"},
{Name: "Q_U_U_X_PORT_8082_TCP_ADDR", Value: "9.8.7.6"}, {Name: "Q_U_U_X_PORT_8082_TCP_ADDR", Value: "9.8.7.6"},
{Name: "Q_U_U_X_PORT_8083_TCP", Value: "tcp://9.8.7.6:8083"},
{Name: "Q_U_U_X_PORT_8083_TCP_PROTO", Value: "tcp"},
{Name: "Q_U_U_X_PORT_8083_TCP_PORT", Value: "8083"},
{Name: "Q_U_U_X_PORT_8083_TCP_ADDR", Value: "9.8.7.6"},
} }
if len(vars) != len(expected) { if len(vars) != len(expected) {
t.Errorf("Expected %d env vars, got: %+v", len(expected), vars) t.Errorf("Expected %d env vars, got: %+v", len(expected), vars)
......
...@@ -1787,97 +1787,139 @@ func TestMakeEnvironmentVariables(t *testing.T) { ...@@ -1787,97 +1787,139 @@ func TestMakeEnvironmentVariables(t *testing.T) {
{ {
ObjectMeta: api.ObjectMeta{Name: "kubernetes", Namespace: api.NamespaceDefault}, ObjectMeta: api.ObjectMeta{Name: "kubernetes", Namespace: api.NamespaceDefault},
Spec: api.ServiceSpec{ Spec: api.ServiceSpec{
Port: 8081, Ports: []api.ServicePort{{
Protocol: "TCP",
Port: 8081,
}},
PortalIP: "1.2.3.1", PortalIP: "1.2.3.1",
}, },
}, },
{ {
ObjectMeta: api.ObjectMeta{Name: "kubernetes-ro", Namespace: api.NamespaceDefault}, ObjectMeta: api.ObjectMeta{Name: "kubernetes-ro", Namespace: api.NamespaceDefault},
Spec: api.ServiceSpec{ Spec: api.ServiceSpec{
Port: 8082, Ports: []api.ServicePort{{
Protocol: "TCP",
Port: 8082,
}},
PortalIP: "1.2.3.2", PortalIP: "1.2.3.2",
}, },
}, },
{ {
ObjectMeta: api.ObjectMeta{Name: "kubernetes-ro", Namespace: api.NamespaceDefault}, ObjectMeta: api.ObjectMeta{Name: "kubernetes-ro", Namespace: api.NamespaceDefault},
Spec: api.ServiceSpec{ Spec: api.ServiceSpec{
Port: 8082, Ports: []api.ServicePort{{
Protocol: "TCP",
Port: 8082,
}},
PortalIP: "None", PortalIP: "None",
}, },
}, },
{ {
ObjectMeta: api.ObjectMeta{Name: "kubernetes-ro", Namespace: api.NamespaceDefault}, ObjectMeta: api.ObjectMeta{Name: "kubernetes-ro", Namespace: api.NamespaceDefault},
Spec: api.ServiceSpec{ Spec: api.ServiceSpec{
Port: 8082, Ports: []api.ServicePort{{
Protocol: "TCP",
Port: 8082,
}},
PortalIP: "", PortalIP: "",
}, },
}, },
{ {
ObjectMeta: api.ObjectMeta{Name: "test", Namespace: "test1"}, ObjectMeta: api.ObjectMeta{Name: "test", Namespace: "test1"},
Spec: api.ServiceSpec{ Spec: api.ServiceSpec{
Port: 8083, Ports: []api.ServicePort{{
Protocol: "TCP",
Port: 8083,
}},
PortalIP: "1.2.3.3", PortalIP: "1.2.3.3",
}, },
}, },
{ {
ObjectMeta: api.ObjectMeta{Name: "kubernetes", Namespace: "test2"}, ObjectMeta: api.ObjectMeta{Name: "kubernetes", Namespace: "test2"},
Spec: api.ServiceSpec{ Spec: api.ServiceSpec{
Port: 8084, Ports: []api.ServicePort{{
Protocol: "TCP",
Port: 8084,
}},
PortalIP: "1.2.3.4", PortalIP: "1.2.3.4",
}, },
}, },
{ {
ObjectMeta: api.ObjectMeta{Name: "test", Namespace: "test2"}, ObjectMeta: api.ObjectMeta{Name: "test", Namespace: "test2"},
Spec: api.ServiceSpec{ Spec: api.ServiceSpec{
Port: 8085, Ports: []api.ServicePort{{
Protocol: "TCP",
Port: 8085,
}},
PortalIP: "1.2.3.5", PortalIP: "1.2.3.5",
}, },
}, },
{ {
ObjectMeta: api.ObjectMeta{Name: "test", Namespace: "test2"}, ObjectMeta: api.ObjectMeta{Name: "test", Namespace: "test2"},
Spec: api.ServiceSpec{ Spec: api.ServiceSpec{
Port: 8085, Ports: []api.ServicePort{{
Protocol: "TCP",
Port: 8085,
}},
PortalIP: "None", PortalIP: "None",
}, },
}, },
{ {
ObjectMeta: api.ObjectMeta{Name: "test", Namespace: "test2"}, ObjectMeta: api.ObjectMeta{Name: "test", Namespace: "test2"},
Spec: api.ServiceSpec{ Spec: api.ServiceSpec{
Port: 8085, Ports: []api.ServicePort{{
Protocol: "TCP",
Port: 8085,
}},
}, },
}, },
{ {
ObjectMeta: api.ObjectMeta{Name: "kubernetes", Namespace: "kubernetes"}, ObjectMeta: api.ObjectMeta{Name: "kubernetes", Namespace: "kubernetes"},
Spec: api.ServiceSpec{ Spec: api.ServiceSpec{
Port: 8086, Ports: []api.ServicePort{{
Protocol: "TCP",
Port: 8086,
}},
PortalIP: "1.2.3.6", PortalIP: "1.2.3.6",
}, },
}, },
{ {
ObjectMeta: api.ObjectMeta{Name: "kubernetes-ro", Namespace: "kubernetes"}, ObjectMeta: api.ObjectMeta{Name: "kubernetes-ro", Namespace: "kubernetes"},
Spec: api.ServiceSpec{ Spec: api.ServiceSpec{
Port: 8087, Ports: []api.ServicePort{{
Protocol: "TCP",
Port: 8087,
}},
PortalIP: "1.2.3.7", PortalIP: "1.2.3.7",
}, },
}, },
{ {
ObjectMeta: api.ObjectMeta{Name: "not-special", Namespace: "kubernetes"}, ObjectMeta: api.ObjectMeta{Name: "not-special", Namespace: "kubernetes"},
Spec: api.ServiceSpec{ Spec: api.ServiceSpec{
Port: 8088, Ports: []api.ServicePort{{
Protocol: "TCP",
Port: 8088,
}},
PortalIP: "1.2.3.8", PortalIP: "1.2.3.8",
}, },
}, },
{ {
ObjectMeta: api.ObjectMeta{Name: "not-special", Namespace: "kubernetes"}, ObjectMeta: api.ObjectMeta{Name: "not-special", Namespace: "kubernetes"},
Spec: api.ServiceSpec{ Spec: api.ServiceSpec{
Port: 8088, Ports: []api.ServicePort{{
Protocol: "TCP",
Port: 8088,
}},
PortalIP: "None", PortalIP: "None",
}, },
}, },
{ {
ObjectMeta: api.ObjectMeta{Name: "not-special", Namespace: "kubernetes"}, ObjectMeta: api.ObjectMeta{Name: "not-special", Namespace: "kubernetes"},
Spec: api.ServiceSpec{ Spec: api.ServiceSpec{
Port: 8088, Ports: []api.ServicePort{{
Protocol: "TCP",
Port: 8088,
}},
PortalIP: "", PortalIP: "",
}, },
}, },
......
...@@ -114,11 +114,10 @@ func (m *Master) createMasterServiceIfNeeded(serviceName string, serviceIP net.I ...@@ -114,11 +114,10 @@ func (m *Master) createMasterServiceIfNeeded(serviceName string, serviceIP net.I
Labels: map[string]string{"provider": "kubernetes", "component": "apiserver"}, Labels: map[string]string{"provider": "kubernetes", "component": "apiserver"},
}, },
Spec: api.ServiceSpec{ Spec: api.ServiceSpec{
Port: servicePort, Ports: []api.ServicePort{{Port: servicePort, Protocol: api.ProtocolTCP}},
// maintained by this code, not by the pod selector // maintained by this code, not by the pod selector
Selector: nil, Selector: nil,
PortalIP: serviceIP.String(), PortalIP: serviceIP.String(),
Protocol: api.ProtocolTCP,
SessionAffinity: api.AffinityTypeNone, SessionAffinity: api.AffinityTypeNone,
}, },
} }
......
...@@ -136,7 +136,10 @@ func TestNewServiceAddedAndNotified(t *testing.T) { ...@@ -136,7 +136,10 @@ func TestNewServiceAddedAndNotified(t *testing.T) {
handler := NewServiceHandlerMock() handler := NewServiceHandlerMock()
handler.Wait(1) handler.Wait(1)
config.RegisterHandler(handler) config.RegisterHandler(handler)
serviceUpdate := CreateServiceUpdate(ADD, api.Service{ObjectMeta: api.ObjectMeta{Namespace: "testnamespace", Name: "foo"}, Spec: api.ServiceSpec{Port: 10}}) serviceUpdate := CreateServiceUpdate(ADD, api.Service{
ObjectMeta: api.ObjectMeta{Namespace: "testnamespace", Name: "foo"},
Spec: api.ServiceSpec{Ports: []api.ServicePort{{Protocol: "TCP", Port: 10}}},
})
channel <- serviceUpdate channel <- serviceUpdate
handler.ValidateServices(t, serviceUpdate.Services) handler.ValidateServices(t, serviceUpdate.Services)
...@@ -147,24 +150,35 @@ func TestServiceAddedRemovedSetAndNotified(t *testing.T) { ...@@ -147,24 +150,35 @@ func TestServiceAddedRemovedSetAndNotified(t *testing.T) {
channel := config.Channel("one") channel := config.Channel("one")
handler := NewServiceHandlerMock() handler := NewServiceHandlerMock()
config.RegisterHandler(handler) config.RegisterHandler(handler)
serviceUpdate := CreateServiceUpdate(ADD, api.Service{ObjectMeta: api.ObjectMeta{Namespace: "testnamespace", Name: "foo"}, Spec: api.ServiceSpec{Port: 10}}) serviceUpdate := CreateServiceUpdate(ADD, api.Service{
ObjectMeta: api.ObjectMeta{Namespace: "testnamespace", Name: "foo"},
Spec: api.ServiceSpec{Ports: []api.ServicePort{{Protocol: "TCP", Port: 10}}},
})
handler.Wait(1) handler.Wait(1)
channel <- serviceUpdate channel <- serviceUpdate
handler.ValidateServices(t, serviceUpdate.Services) handler.ValidateServices(t, serviceUpdate.Services)
serviceUpdate2 := CreateServiceUpdate(ADD, api.Service{ObjectMeta: api.ObjectMeta{Namespace: "testnamespace", Name: "bar"}, Spec: api.ServiceSpec{Port: 20}}) serviceUpdate2 := CreateServiceUpdate(ADD, api.Service{
ObjectMeta: api.ObjectMeta{Namespace: "testnamespace", Name: "bar"},
Spec: api.ServiceSpec{Ports: []api.ServicePort{{Protocol: "TCP", Port: 20}}},
})
handler.Wait(1) handler.Wait(1)
channel <- serviceUpdate2 channel <- serviceUpdate2
services := []api.Service{serviceUpdate2.Services[0], serviceUpdate.Services[0]} services := []api.Service{serviceUpdate2.Services[0], serviceUpdate.Services[0]}
handler.ValidateServices(t, services) handler.ValidateServices(t, services)
serviceUpdate3 := CreateServiceUpdate(REMOVE, api.Service{ObjectMeta: api.ObjectMeta{Namespace: "testnamespace", Name: "foo"}}) serviceUpdate3 := CreateServiceUpdate(REMOVE, api.Service{
ObjectMeta: api.ObjectMeta{Namespace: "testnamespace", Name: "foo"},
})
handler.Wait(1) handler.Wait(1)
channel <- serviceUpdate3 channel <- serviceUpdate3
services = []api.Service{serviceUpdate2.Services[0]} services = []api.Service{serviceUpdate2.Services[0]}
handler.ValidateServices(t, services) handler.ValidateServices(t, services)
serviceUpdate4 := CreateServiceUpdate(SET, api.Service{ObjectMeta: api.ObjectMeta{Namespace: "testnamespace", Name: "foobar"}, Spec: api.ServiceSpec{Port: 99}}) serviceUpdate4 := CreateServiceUpdate(SET, api.Service{
ObjectMeta: api.ObjectMeta{Namespace: "testnamespace", Name: "foobar"},
Spec: api.ServiceSpec{Ports: []api.ServicePort{{Protocol: "TCP", Port: 99}}},
})
handler.Wait(1) handler.Wait(1)
channel <- serviceUpdate4 channel <- serviceUpdate4
services = []api.Service{serviceUpdate4.Services[0]} services = []api.Service{serviceUpdate4.Services[0]}
...@@ -180,8 +194,14 @@ func TestNewMultipleSourcesServicesAddedAndNotified(t *testing.T) { ...@@ -180,8 +194,14 @@ func TestNewMultipleSourcesServicesAddedAndNotified(t *testing.T) {
} }
handler := NewServiceHandlerMock() handler := NewServiceHandlerMock()
config.RegisterHandler(handler) config.RegisterHandler(handler)
serviceUpdate1 := CreateServiceUpdate(ADD, api.Service{ObjectMeta: api.ObjectMeta{Namespace: "testnamespace", Name: "foo"}, Spec: api.ServiceSpec{Port: 10}}) serviceUpdate1 := CreateServiceUpdate(ADD, api.Service{
serviceUpdate2 := CreateServiceUpdate(ADD, api.Service{ObjectMeta: api.ObjectMeta{Namespace: "testnamespace", Name: "bar"}, Spec: api.ServiceSpec{Port: 20}}) ObjectMeta: api.ObjectMeta{Namespace: "testnamespace", Name: "foo"},
Spec: api.ServiceSpec{Ports: []api.ServicePort{{Protocol: "TCP", Port: 10}}},
})
serviceUpdate2 := CreateServiceUpdate(ADD, api.Service{
ObjectMeta: api.ObjectMeta{Namespace: "testnamespace", Name: "bar"},
Spec: api.ServiceSpec{Ports: []api.ServicePort{{Protocol: "TCP", Port: 20}}},
})
handler.Wait(2) handler.Wait(2)
channelOne <- serviceUpdate1 channelOne <- serviceUpdate1
channelTwo <- serviceUpdate2 channelTwo <- serviceUpdate2
...@@ -197,8 +217,14 @@ func TestNewMultipleSourcesServicesMultipleHandlersAddedAndNotified(t *testing.T ...@@ -197,8 +217,14 @@ func TestNewMultipleSourcesServicesMultipleHandlersAddedAndNotified(t *testing.T
handler2 := NewServiceHandlerMock() handler2 := NewServiceHandlerMock()
config.RegisterHandler(handler) config.RegisterHandler(handler)
config.RegisterHandler(handler2) config.RegisterHandler(handler2)
serviceUpdate1 := CreateServiceUpdate(ADD, api.Service{ObjectMeta: api.ObjectMeta{Namespace: "testnamespace", Name: "foo"}, Spec: api.ServiceSpec{Port: 10}}) serviceUpdate1 := CreateServiceUpdate(ADD, api.Service{
serviceUpdate2 := CreateServiceUpdate(ADD, api.Service{ObjectMeta: api.ObjectMeta{Namespace: "testnamespace", Name: "bar"}, Spec: api.ServiceSpec{Port: 20}}) ObjectMeta: api.ObjectMeta{Namespace: "testnamespace", Name: "foo"},
Spec: api.ServiceSpec{Ports: []api.ServicePort{{Protocol: "TCP", Port: 10}}},
})
serviceUpdate2 := CreateServiceUpdate(ADD, api.Service{
ObjectMeta: api.ObjectMeta{Namespace: "testnamespace", Name: "bar"},
Spec: api.ServiceSpec{Ports: []api.ServicePort{{Protocol: "TCP", Port: 20}}},
})
handler.Wait(2) handler.Wait(2)
handler2.Wait(2) handler2.Wait(2)
channelOne <- serviceUpdate1 channelOne <- serviceUpdate1
......
...@@ -17,6 +17,7 @@ limitations under the License. ...@@ -17,6 +17,7 @@ limitations under the License.
package proxy package proxy
import ( import (
"fmt"
"net" "net"
"github.com/GoogleCloudPlatform/kubernetes/pkg/api" "github.com/GoogleCloudPlatform/kubernetes/pkg/api"
...@@ -27,7 +28,18 @@ import ( ...@@ -27,7 +28,18 @@ import (
type LoadBalancer interface { type LoadBalancer interface {
// NextEndpoint returns the endpoint to handle a request for the given // NextEndpoint returns the endpoint to handle a request for the given
// service-port and source address. // service-port and source address.
NextEndpoint(service types.NamespacedName, port string, srcAddr net.Addr) (string, error) NextEndpoint(service ServicePortName, srcAddr net.Addr) (string, error)
NewService(service types.NamespacedName, port string, sessionAffinityType api.AffinityType, stickyMaxAgeMinutes int) error NewService(service ServicePortName, sessionAffinityType api.AffinityType, stickyMaxAgeMinutes int) error
CleanupStaleStickySessions(service types.NamespacedName, port string) CleanupStaleStickySessions(service ServicePortName)
}
// ServicePortName carries a namespace + name + portname. This is the unique
// identfier for a load-balanced service.
type ServicePortName struct {
types.NamespacedName
Port string
}
func (spn ServicePortName) String() string {
return fmt.Sprintf("%s:%s", spn.NamespacedName.String(), spn.Port)
} }
...@@ -50,20 +50,10 @@ type affinityPolicy struct { ...@@ -50,20 +50,10 @@ type affinityPolicy struct {
ttlMinutes int ttlMinutes int
} }
// servicePort is the type that the balancer uses to key stored state.
type servicePort struct {
types.NamespacedName
port string
}
func (sp servicePort) String() string {
return fmt.Sprintf("%s:%s", sp.NamespacedName, sp.port)
}
// LoadBalancerRR is a round-robin load balancer. // LoadBalancerRR is a round-robin load balancer.
type LoadBalancerRR struct { type LoadBalancerRR struct {
lock sync.RWMutex lock sync.RWMutex
services map[servicePort]*balancerState services map[ServicePortName]*balancerState
} }
// Ensure this implements LoadBalancer. // Ensure this implements LoadBalancer.
...@@ -86,13 +76,11 @@ func newAffinityPolicy(affinityType api.AffinityType, ttlMinutes int) *affinityP ...@@ -86,13 +76,11 @@ func newAffinityPolicy(affinityType api.AffinityType, ttlMinutes int) *affinityP
// NewLoadBalancerRR returns a new LoadBalancerRR. // NewLoadBalancerRR returns a new LoadBalancerRR.
func NewLoadBalancerRR() *LoadBalancerRR { func NewLoadBalancerRR() *LoadBalancerRR {
return &LoadBalancerRR{ return &LoadBalancerRR{
services: map[servicePort]*balancerState{}, services: map[ServicePortName]*balancerState{},
} }
} }
func (lb *LoadBalancerRR) NewService(service types.NamespacedName, port string, affinityType api.AffinityType, ttlMinutes int) error { func (lb *LoadBalancerRR) NewService(svcPort ServicePortName, affinityType api.AffinityType, ttlMinutes int) error {
svcPort := servicePort{service, port}
lb.lock.Lock() lb.lock.Lock()
defer lb.lock.Unlock() defer lb.lock.Unlock()
lb.newServiceInternal(svcPort, affinityType, ttlMinutes) lb.newServiceInternal(svcPort, affinityType, ttlMinutes)
...@@ -100,7 +88,7 @@ func (lb *LoadBalancerRR) NewService(service types.NamespacedName, port string, ...@@ -100,7 +88,7 @@ func (lb *LoadBalancerRR) NewService(service types.NamespacedName, port string,
} }
// This assumes that lb.lock is already held. // This assumes that lb.lock is already held.
func (lb *LoadBalancerRR) newServiceInternal(svcPort servicePort, affinityType api.AffinityType, ttlMinutes int) *balancerState { func (lb *LoadBalancerRR) newServiceInternal(svcPort ServicePortName, affinityType api.AffinityType, ttlMinutes int) *balancerState {
if ttlMinutes == 0 { if ttlMinutes == 0 {
ttlMinutes = 180 //default to 3 hours if not specified. Should 0 be unlimeted instead???? ttlMinutes = 180 //default to 3 hours if not specified. Should 0 be unlimeted instead????
} }
...@@ -123,9 +111,7 @@ func isSessionAffinity(affinity *affinityPolicy) bool { ...@@ -123,9 +111,7 @@ func isSessionAffinity(affinity *affinityPolicy) bool {
// NextEndpoint returns a service endpoint. // NextEndpoint returns a service endpoint.
// The service endpoint is chosen using the round-robin algorithm. // The service endpoint is chosen using the round-robin algorithm.
func (lb *LoadBalancerRR) NextEndpoint(service types.NamespacedName, port string, srcAddr net.Addr) (string, error) { func (lb *LoadBalancerRR) NextEndpoint(svcPort ServicePortName, srcAddr net.Addr) (string, error) {
svcPort := servicePort{service, port}
// Coarse locking is simple. We can get more fine-grained if/when we // Coarse locking is simple. We can get more fine-grained if/when we
// can prove it matters. // can prove it matters.
lb.lock.Lock() lb.lock.Lock()
...@@ -202,7 +188,7 @@ func flattenValidEndpoints(endpoints []hostPortPair) []string { ...@@ -202,7 +188,7 @@ func flattenValidEndpoints(endpoints []hostPortPair) []string {
} }
// Remove any session affinity records associated to a particular endpoint (for example when a pod goes down). // Remove any session affinity records associated to a particular endpoint (for example when a pod goes down).
func removeSessionAffinityByEndpoint(state *balancerState, svcPort servicePort, endpoint string) { func removeSessionAffinityByEndpoint(state *balancerState, svcPort ServicePortName, endpoint string) {
for _, affinity := range state.affinity.affinityMap { for _, affinity := range state.affinity.affinityMap {
if affinity.endpoint == endpoint { if affinity.endpoint == endpoint {
glog.V(4).Infof("Removing client: %s from affinityMap for service %q", affinity.endpoint, svcPort) glog.V(4).Infof("Removing client: %s from affinityMap for service %q", affinity.endpoint, svcPort)
...@@ -214,7 +200,7 @@ func removeSessionAffinityByEndpoint(state *balancerState, svcPort servicePort, ...@@ -214,7 +200,7 @@ func removeSessionAffinityByEndpoint(state *balancerState, svcPort servicePort,
// Loop through the valid endpoints and then the endpoints associated with the Load Balancer. // Loop through the valid endpoints and then the endpoints associated with the Load Balancer.
// Then remove any session affinity records that are not in both lists. // Then remove any session affinity records that are not in both lists.
// This assumes the lb.lock is held. // This assumes the lb.lock is held.
func (lb *LoadBalancerRR) updateAffinityMap(svcPort servicePort, newEndpoints []string) { func (lb *LoadBalancerRR) updateAffinityMap(svcPort ServicePortName, newEndpoints []string) {
allEndpoints := map[string]int{} allEndpoints := map[string]int{}
for _, newEndpoint := range newEndpoints { for _, newEndpoint := range newEndpoints {
allEndpoints[newEndpoint] = 1 allEndpoints[newEndpoint] = 1
...@@ -238,7 +224,7 @@ func (lb *LoadBalancerRR) updateAffinityMap(svcPort servicePort, newEndpoints [] ...@@ -238,7 +224,7 @@ func (lb *LoadBalancerRR) updateAffinityMap(svcPort servicePort, newEndpoints []
// Registered endpoints are updated if found in the update set or // Registered endpoints are updated if found in the update set or
// unregistered if missing from the update set. // unregistered if missing from the update set.
func (lb *LoadBalancerRR) OnUpdate(allEndpoints []api.Endpoints) { func (lb *LoadBalancerRR) OnUpdate(allEndpoints []api.Endpoints) {
registeredEndpoints := make(map[servicePort]bool) registeredEndpoints := make(map[ServicePortName]bool)
lb.lock.Lock() lb.lock.Lock()
defer lb.lock.Unlock() defer lb.lock.Unlock()
...@@ -262,7 +248,7 @@ func (lb *LoadBalancerRR) OnUpdate(allEndpoints []api.Endpoints) { ...@@ -262,7 +248,7 @@ func (lb *LoadBalancerRR) OnUpdate(allEndpoints []api.Endpoints) {
} }
for portname := range portsToEndpoints { for portname := range portsToEndpoints {
svcPort := servicePort{types.NamespacedName{svcEndpoints.Namespace, svcEndpoints.Name}, portname} svcPort := ServicePortName{types.NamespacedName{svcEndpoints.Namespace, svcEndpoints.Name}, portname}
state, exists := lb.services[svcPort] state, exists := lb.services[svcPort]
curEndpoints := []string{} curEndpoints := []string{}
if state != nil { if state != nil {
...@@ -305,15 +291,12 @@ func slicesEquiv(lhs, rhs []string) bool { ...@@ -305,15 +291,12 @@ func slicesEquiv(lhs, rhs []string) bool {
return false return false
} }
func (lb *LoadBalancerRR) CleanupStaleStickySessions(service types.NamespacedName, port string) { func (lb *LoadBalancerRR) CleanupStaleStickySessions(svcPort ServicePortName) {
svcPort := servicePort{service, port}
lb.lock.Lock() lb.lock.Lock()
defer lb.lock.Unlock() defer lb.lock.Unlock()
state, exists := lb.services[svcPort] state, exists := lb.services[svcPort]
if !exists { if !exists {
glog.Warning("CleanupStaleStickySessions called for non-existent balancer key %q", svcPort)
return return
} }
for ip, affinity := range state.affinity.affinityMap { for ip, affinity := range state.affinity.affinityMap {
......
...@@ -576,7 +576,6 @@ func TestEtcdUpdateService(t *testing.T) { ...@@ -576,7 +576,6 @@ func TestEtcdUpdateService(t *testing.T) {
Selector: map[string]string{ Selector: map[string]string{
"baz": "bar", "baz": "bar",
}, },
Protocol: "TCP",
SessionAffinity: "None", SessionAffinity: "None",
}, },
} }
......
...@@ -281,10 +281,12 @@ func (rs *REST) createExternalLoadBalancer(ctx api.Context, service *api.Service ...@@ -281,10 +281,12 @@ func (rs *REST) createExternalLoadBalancer(ctx api.Context, service *api.Service
if rs.cloud == nil { if rs.cloud == nil {
return fmt.Errorf("requested an external service, but no cloud provider supplied.") return fmt.Errorf("requested an external service, but no cloud provider supplied.")
} }
if service.Spec.Protocol != api.ProtocolTCP {
// TODO: Support UDP here too. ports, err := getTCPPorts(service)
return fmt.Errorf("external load balancers for non TCP services are not currently supported.") if err != nil {
return err
} }
balancer, ok := rs.cloud.TCPLoadBalancer() balancer, ok := rs.cloud.TCPLoadBalancer()
if !ok { if !ok {
return fmt.Errorf("the cloud provider does not support external TCP load balancers.") return fmt.Errorf("the cloud provider does not support external TCP load balancers.")
...@@ -302,18 +304,17 @@ func (rs *REST) createExternalLoadBalancer(ctx api.Context, service *api.Service ...@@ -302,18 +304,17 @@ func (rs *REST) createExternalLoadBalancer(ctx api.Context, service *api.Service
return err return err
} }
name := rs.getLoadbalancerName(ctx, service) name := rs.getLoadbalancerName(ctx, service)
// TODO: We should be able to rely on valid input, and not do defaulting here.
var affinityType api.AffinityType = service.Spec.SessionAffinity var affinityType api.AffinityType = service.Spec.SessionAffinity
if len(service.Spec.PublicIPs) > 0 { if len(service.Spec.PublicIPs) > 0 {
for _, publicIP := range service.Spec.PublicIPs { for _, publicIP := range service.Spec.PublicIPs {
_, err = balancer.CreateTCPLoadBalancer(name, zone.Region, net.ParseIP(publicIP), service.Spec.Port, hostsFromMinionList(hosts), affinityType) _, err = balancer.CreateTCPLoadBalancer(name, zone.Region, net.ParseIP(publicIP), ports, hostsFromMinionList(hosts), affinityType)
if err != nil { if err != nil {
// TODO: have to roll-back any successful calls. // TODO: have to roll-back any successful calls.
return err return err
} }
} }
} else { } else {
endpoint, err := balancer.CreateTCPLoadBalancer(name, zone.Region, nil, service.Spec.Port, hostsFromMinionList(hosts), affinityType) endpoint, err := balancer.CreateTCPLoadBalancer(name, zone.Region, nil, ports, hostsFromMinionList(hosts), affinityType)
if err != nil { if err != nil {
return err return err
} }
...@@ -322,6 +323,39 @@ func (rs *REST) createExternalLoadBalancer(ctx api.Context, service *api.Service ...@@ -322,6 +323,39 @@ func (rs *REST) createExternalLoadBalancer(ctx api.Context, service *api.Service
return nil return nil
} }
func getTCPPorts(service *api.Service) ([]int, error) {
ports := []int{}
for i := range service.Spec.Ports {
sp := &service.Spec.Ports[i]
if sp.Protocol != api.ProtocolTCP {
// TODO: Support UDP here too.
return nil, fmt.Errorf("external load balancers for non TCP services are not currently supported.")
}
ports = append(ports, sp.Port)
}
return ports, nil
}
func portsEqual(x, y *api.Service) bool {
xPorts, err := getTCPPorts(x)
if err != nil {
return false
}
yPorts, err := getTCPPorts(y)
if err != nil {
return false
}
if len(xPorts) != len(yPorts) {
return false
}
for i := range xPorts {
if xPorts[i] != yPorts[i] {
return false
}
}
return true
}
func (rs *REST) deleteExternalLoadBalancer(ctx api.Context, service *api.Service) error { func (rs *REST) deleteExternalLoadBalancer(ctx api.Context, service *api.Service) error {
if rs.cloud == nil { if rs.cloud == nil {
return fmt.Errorf("requested an external service, but no cloud provider supplied.") return fmt.Errorf("requested an external service, but no cloud provider supplied.")
...@@ -348,21 +382,21 @@ func (rs *REST) deleteExternalLoadBalancer(ctx api.Context, service *api.Service ...@@ -348,21 +382,21 @@ func (rs *REST) deleteExternalLoadBalancer(ctx api.Context, service *api.Service
return nil return nil
} }
func externalLoadBalancerNeedsUpdate(old, new *api.Service) bool { func externalLoadBalancerNeedsUpdate(oldService, newService *api.Service) bool {
if !old.Spec.CreateExternalLoadBalancer && !new.Spec.CreateExternalLoadBalancer { if !oldService.Spec.CreateExternalLoadBalancer && !newService.Spec.CreateExternalLoadBalancer {
return false return false
} }
if old.Spec.CreateExternalLoadBalancer != new.Spec.CreateExternalLoadBalancer || if oldService.Spec.CreateExternalLoadBalancer != newService.Spec.CreateExternalLoadBalancer {
old.Spec.Port != new.Spec.Port || return true
old.Spec.SessionAffinity != new.Spec.SessionAffinity || }
old.Spec.Protocol != new.Spec.Protocol { if !portsEqual(oldService, newService) || oldService.Spec.SessionAffinity != newService.Spec.SessionAffinity {
return true return true
} }
if len(old.Spec.PublicIPs) != len(new.Spec.PublicIPs) { if len(oldService.Spec.PublicIPs) != len(newService.Spec.PublicIPs) {
return true return true
} }
for i := range old.Spec.PublicIPs { for i := range oldService.Spec.PublicIPs {
if old.Spec.PublicIPs[i] != new.Spec.PublicIPs[i] { if oldService.Spec.PublicIPs[i] != newService.Spec.PublicIPs[i] {
return true return true
} }
} }
......
...@@ -73,44 +73,48 @@ func (e *EndpointController) SyncServiceEndpoints() error { ...@@ -73,44 +73,48 @@ func (e *EndpointController) SyncServiceEndpoints() error {
for i := range pods.Items { for i := range pods.Items {
pod := &pods.Items[i] pod := &pods.Items[i]
// TODO: Once v1beta1 and v1beta2 are EOL'ed, this can for i := range service.Spec.Ports {
// assume that service.Spec.TargetPort is populated. servicePort := &service.Spec.Ports[i]
_ = v1beta1.Dependency
_ = v1beta2.Dependency // TODO: Once v1beta1 and v1beta2 are EOL'ed, this can
// TODO: Add multiple-ports to Service and expose them here. // assume that service.Spec.TargetPort is populated.
portName := "" _ = v1beta1.Dependency
portProto := service.Spec.Protocol _ = v1beta2.Dependency
portNum, err := findPort(pod, service)
if err != nil { portName := servicePort.Name
glog.Errorf("Failed to find port for service %s/%s: %v", service.Namespace, service.Name, err) portProto := servicePort.Protocol
continue portNum, err := findPort(pod, servicePort)
} if err != nil {
if len(pod.Status.PodIP) == 0 { glog.Errorf("Failed to find port for service %s/%s: %v", service.Namespace, service.Name, err)
glog.Errorf("Failed to find an IP for pod %s/%s", pod.Namespace, pod.Name) continue
continue }
} if len(pod.Status.PodIP) == 0 {
glog.Errorf("Failed to find an IP for pod %s/%s", pod.Namespace, pod.Name)
continue
}
inService := false inService := false
for _, c := range pod.Status.Conditions { for _, c := range pod.Status.Conditions {
if c.Type == api.PodReady && c.Status == api.ConditionTrue { if c.Type == api.PodReady && c.Status == api.ConditionTrue {
inService = true inService = true
break break
}
}
if !inService {
glog.V(5).Infof("Pod is out of service: %v/%v", pod.Namespace, pod.Name)
continue
} }
}
if !inService {
glog.V(5).Infof("Pod is out of service: %v/%v", pod.Namespace, pod.Name)
continue
}
epp := api.EndpointPort{Name: portName, Port: portNum, Protocol: portProto} epp := api.EndpointPort{Name: portName, Port: portNum, Protocol: portProto}
epa := api.EndpointAddress{IP: pod.Status.PodIP, TargetRef: &api.ObjectReference{ epa := api.EndpointAddress{IP: pod.Status.PodIP, TargetRef: &api.ObjectReference{
Kind: "Pod", Kind: "Pod",
Namespace: pod.ObjectMeta.Namespace, Namespace: pod.ObjectMeta.Namespace,
Name: pod.ObjectMeta.Name, Name: pod.ObjectMeta.Name,
UID: pod.ObjectMeta.UID, UID: pod.ObjectMeta.UID,
ResourceVersion: pod.ObjectMeta.ResourceVersion, ResourceVersion: pod.ObjectMeta.ResourceVersion,
}} }}
subsets = append(subsets, api.EndpointSubset{Addresses: []api.EndpointAddress{epa}, Ports: []api.EndpointPort{epp}}) subsets = append(subsets, api.EndpointSubset{Addresses: []api.EndpointAddress{epa}, Ports: []api.EndpointPort{epp}})
}
} }
subsets = endpoints.RepackSubsets(subsets) subsets = endpoints.RepackSubsets(subsets)
...@@ -169,24 +173,24 @@ func findDefaultPort(pod *api.Pod, servicePort int, proto api.Protocol) int { ...@@ -169,24 +173,24 @@ func findDefaultPort(pod *api.Pod, servicePort int, proto api.Protocol) int {
// the service's port. If the targetPort is a non-empty string, look that // the service's port. If the targetPort is a non-empty string, look that
// string up in all named ports in all containers in the target pod. If no // string up in all named ports in all containers in the target pod. If no
// match is found, fail. // match is found, fail.
func findPort(pod *api.Pod, service *api.Service) (int, error) { func findPort(pod *api.Pod, svcPort *api.ServicePort) (int, error) {
portName := service.Spec.TargetPort portName := svcPort.TargetPort
switch portName.Kind { switch portName.Kind {
case util.IntstrString: case util.IntstrString:
if len(portName.StrVal) == 0 { if len(portName.StrVal) == 0 {
return findDefaultPort(pod, service.Spec.Port, service.Spec.Protocol), nil return findDefaultPort(pod, svcPort.Port, svcPort.Protocol), nil
} }
name := portName.StrVal name := portName.StrVal
for _, container := range pod.Spec.Containers { for _, container := range pod.Spec.Containers {
for _, port := range container.Ports { for _, port := range container.Ports {
if port.Name == name && port.Protocol == service.Spec.Protocol { if port.Name == name && port.Protocol == svcPort.Protocol {
return port.ContainerPort, nil return port.ContainerPort, nil
} }
} }
} }
case util.IntstrInt: case util.IntstrInt:
if portName.IntVal == 0 { if portName.IntVal == 0 {
return findDefaultPort(pod, service.Spec.Port, service.Spec.Protocol), nil return findDefaultPort(pod, svcPort.Port, svcPort.Protocol), nil
} }
return portName.IntVal, nil return portName.IntVal, nil
} }
......
...@@ -51,7 +51,8 @@ func newPodList(nPods int, nPorts int) *api.PodList { ...@@ -51,7 +51,8 @@ func newPodList(nPods int, nPorts int) *api.PodList {
}, },
} }
for j := 0; j < nPorts; j++ { for j := 0; j < nPorts; j++ {
p.Spec.Containers[0].Ports = append(p.Spec.Containers[0].Ports, api.ContainerPort{ContainerPort: 8080 + j}) p.Spec.Containers[0].Ports = append(p.Spec.Containers[0].Ports,
api.ContainerPort{Name: fmt.Sprintf("port%d", i), ContainerPort: 8080 + j})
} }
pods = append(pods, p) pods = append(pods, p)
} }
...@@ -203,7 +204,7 @@ func TestFindPort(t *testing.T) { ...@@ -203,7 +204,7 @@ func TestFindPort(t *testing.T) {
for _, tc := range testCases { for _, tc := range testCases {
port, err := findPort(&api.Pod{Spec: api.PodSpec{Containers: tc.containers}}, port, err := findPort(&api.Pod{Spec: api.PodSpec{Containers: tc.containers}},
&api.Service{Spec: api.ServiceSpec{Protocol: "TCP", Port: servicePort, TargetPort: tc.port}}) &api.ServicePort{Protocol: "TCP", Port: servicePort, TargetPort: tc.port})
if err != nil && tc.pass { if err != nil && tc.pass {
t.Errorf("unexpected error for %s: %v", tc.name, err) t.Errorf("unexpected error for %s: %v", tc.name, err)
} }
...@@ -277,7 +278,7 @@ func TestSyncEndpointsItemsPreserveNoSelector(t *testing.T) { ...@@ -277,7 +278,7 @@ func TestSyncEndpointsItemsPreserveNoSelector(t *testing.T) {
Items: []api.Service{ Items: []api.Service{
{ {
ObjectMeta: api.ObjectMeta{Name: "foo"}, ObjectMeta: api.ObjectMeta{Name: "foo"},
Spec: api.ServiceSpec{}, Spec: api.ServiceSpec{Ports: []api.ServicePort{{Port: 80}}},
}, },
}, },
} }
...@@ -310,7 +311,7 @@ func TestSyncEndpointsProtocolTCP(t *testing.T) { ...@@ -310,7 +311,7 @@ func TestSyncEndpointsProtocolTCP(t *testing.T) {
ObjectMeta: api.ObjectMeta{Name: "foo", Namespace: "other"}, ObjectMeta: api.ObjectMeta{Name: "foo", Namespace: "other"},
Spec: api.ServiceSpec{ Spec: api.ServiceSpec{
Selector: map[string]string{}, Selector: map[string]string{},
Protocol: api.ProtocolTCP, Ports: []api.ServicePort{{Port: 80}},
}, },
}, },
}, },
...@@ -344,7 +345,7 @@ func TestSyncEndpointsProtocolUDP(t *testing.T) { ...@@ -344,7 +345,7 @@ func TestSyncEndpointsProtocolUDP(t *testing.T) {
ObjectMeta: api.ObjectMeta{Name: "foo", Namespace: "other"}, ObjectMeta: api.ObjectMeta{Name: "foo", Namespace: "other"},
Spec: api.ServiceSpec{ Spec: api.ServiceSpec{
Selector: map[string]string{}, Selector: map[string]string{},
Protocol: api.ProtocolUDP, Ports: []api.ServicePort{{Port: 80}},
}, },
}, },
}, },
...@@ -378,6 +379,7 @@ func TestSyncEndpointsItemsEmptySelectorSelectsAll(t *testing.T) { ...@@ -378,6 +379,7 @@ func TestSyncEndpointsItemsEmptySelectorSelectsAll(t *testing.T) {
ObjectMeta: api.ObjectMeta{Name: "foo", Namespace: "other"}, ObjectMeta: api.ObjectMeta{Name: "foo", Namespace: "other"},
Spec: api.ServiceSpec{ Spec: api.ServiceSpec{
Selector: map[string]string{}, Selector: map[string]string{},
Ports: []api.ServicePort{{Port: 80, Protocol: "TCP", TargetPort: util.NewIntOrStringFromInt(8080)}},
}, },
}, },
}, },
...@@ -417,9 +419,8 @@ func TestSyncEndpointsItemsPreexisting(t *testing.T) { ...@@ -417,9 +419,8 @@ func TestSyncEndpointsItemsPreexisting(t *testing.T) {
{ {
ObjectMeta: api.ObjectMeta{Name: "foo", Namespace: "bar"}, ObjectMeta: api.ObjectMeta{Name: "foo", Namespace: "bar"},
Spec: api.ServiceSpec{ Spec: api.ServiceSpec{
Selector: map[string]string{ Selector: map[string]string{"foo": "bar"},
"foo": "bar", Ports: []api.ServicePort{{Port: 80, Protocol: "TCP", TargetPort: util.NewIntOrStringFromInt(8080)}},
},
}, },
}, },
}, },
...@@ -462,9 +463,8 @@ func TestSyncEndpointsItemsPreexistingIdentical(t *testing.T) { ...@@ -462,9 +463,8 @@ func TestSyncEndpointsItemsPreexistingIdentical(t *testing.T) {
{ {
ObjectMeta: api.ObjectMeta{Name: "foo", Namespace: api.NamespaceDefault}, ObjectMeta: api.ObjectMeta{Name: "foo", Namespace: api.NamespaceDefault},
Spec: api.ServiceSpec{ Spec: api.ServiceSpec{
Selector: map[string]string{ Selector: map[string]string{"foo": "bar"},
"foo": "bar", Ports: []api.ServicePort{{Port: 80, Protocol: "TCP", TargetPort: util.NewIntOrStringFromInt(8080)}},
},
}, },
}, },
}, },
...@@ -496,8 +496,10 @@ func TestSyncEndpointsItems(t *testing.T) { ...@@ -496,8 +496,10 @@ func TestSyncEndpointsItems(t *testing.T) {
{ {
ObjectMeta: api.ObjectMeta{Name: "foo", Namespace: "other"}, ObjectMeta: api.ObjectMeta{Name: "foo", Namespace: "other"},
Spec: api.ServiceSpec{ Spec: api.ServiceSpec{
Selector: map[string]string{ Selector: map[string]string{"foo": "bar"},
"foo": "bar", Ports: []api.ServicePort{
{Name: "port0", Port: 80, Protocol: "TCP", TargetPort: util.NewIntOrStringFromInt(8080)},
{Name: "port1", Port: 88, Protocol: "TCP", TargetPort: util.NewIntOrStringFromInt(8088)},
}, },
}, },
}, },
...@@ -520,7 +522,8 @@ func TestSyncEndpointsItems(t *testing.T) { ...@@ -520,7 +522,8 @@ func TestSyncEndpointsItems(t *testing.T) {
{IP: "1.2.3.6", TargetRef: &api.ObjectReference{Kind: "Pod", Name: "pod2"}}, {IP: "1.2.3.6", TargetRef: &api.ObjectReference{Kind: "Pod", Name: "pod2"}},
}, },
Ports: []api.EndpointPort{ Ports: []api.EndpointPort{
{Port: 8080, Protocol: "TCP"}, {Name: "port0", Port: 8080, Protocol: "TCP"},
{Name: "port1", Port: 8088, Protocol: "TCP"},
}, },
}} }}
data := runtime.EncodeOrDie(testapi.Codec(), &api.Endpoints{ data := runtime.EncodeOrDie(testapi.Codec(), &api.Endpoints{
...@@ -540,9 +543,8 @@ func TestSyncEndpointsPodError(t *testing.T) { ...@@ -540,9 +543,8 @@ func TestSyncEndpointsPodError(t *testing.T) {
{ {
ObjectMeta: api.ObjectMeta{Name: "foo", Namespace: api.NamespaceDefault}, ObjectMeta: api.ObjectMeta{Name: "foo", Namespace: api.NamespaceDefault},
Spec: api.ServiceSpec{ Spec: api.ServiceSpec{
Selector: map[string]string{ Selector: map[string]string{"foo": "bar"},
"foo": "bar", Ports: []api.ServicePort{{Port: 80, Protocol: "TCP", TargetPort: util.NewIntOrStringFromInt(8080)}},
},
}, },
}, },
}, },
......
/*
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 types
import "fmt"
func NewNamespacedNameOrDie(namespace, name string) (ret NamespacedName) {
if len(namespace) == 0 || len(name) == 0 {
panic(fmt.Sprintf("invalid call to NewNamespacedNameOrDie(%q, %q)", namespace, name))
}
return NamespacedName{namespace, name}
}
...@@ -17,10 +17,106 @@ limitations under the License. ...@@ -17,10 +17,106 @@ limitations under the License.
package util package util
import ( import (
"fmt"
"hash/adler32" "hash/adler32"
"testing" "testing"
"github.com/davecgh/go-spew/spew"
) )
type A struct {
x int
y string
}
type B struct {
x []int
y map[string]bool
}
type C struct {
x int
y string
}
func (c C) String() string {
return fmt.Sprintf("%d:%s", c.x, c.y)
}
func TestDeepHashObject(t *testing.T) {
// These types are known to fail object hashing because of how spew implements map keys.
// http://github.com/davecgh/go-spew/issues/30
failureCases := []func() interface{}{
func() interface{} { return map[A]bool{A{8675309, "Jenny"}: true, A{9765683, "!Jenny"}: false} },
func() interface{} { return map[C]bool{C{8675309, "Jenny"}: true, C{9765683, "!Jenny"}: false} },
func() interface{} { return map[*A]bool{&A{8675309, "Jenny"}: true, &A{9765683, "!Jenny"}: false} },
func() interface{} { return map[*C]bool{&C{8675309, "Jenny"}: true, &C{9765683, "!Jenny"}: false} },
}
for _, tc := range failureCases {
hasher := adler32.New()
DeepHashObject(hasher, tc())
first := hasher.Sum32()
alwaysSame := false
for i := 0; i < 100; i++ {
DeepHashObject(hasher, tc())
if hasher.Sum32() != first {
alwaysSame = false
break
}
}
if alwaysSame {
t.Errorf("expected failure for %q", toString(tc()))
}
}
successCases := []func() interface{}{
func() interface{} { return 8675309 },
func() interface{} { return "Jenny, I got your number" },
func() interface{} { return []string{"eight", "six", "seven"} },
func() interface{} { return [...]int{5, 3, 0, 9} },
func() interface{} { return map[int]string{8: "8", 6: "6", 7: "7"} },
func() interface{} { return map[string]int{"5": 5, "3": 3, "0": 0, "9": 9} },
func() interface{} { return A{867, "5309"} },
func() interface{} { return &A{867, "5309"} },
func() interface{} {
return B{[]int{8, 6, 7}, map[string]bool{"5": true, "3": true, "0": true, "9": true}}
},
}
for _, tc := range successCases {
hasher1 := adler32.New()
DeepHashObject(hasher1, tc())
hash1 := hasher1.Sum32()
DeepHashObject(hasher1, tc())
hash2 := hasher1.Sum32()
if hash1 != hash2 {
t.Fatalf("hash of the same object (%q) produced different results: %d vs %d", toString(tc()), hash1, hash2)
}
for i := 0; i < 100; i++ {
hasher2 := adler32.New()
DeepHashObject(hasher1, tc())
hash1a := hasher1.Sum32()
DeepHashObject(hasher2, tc())
hash2a := hasher2.Sum32()
if hash1a != hash1 {
t.Errorf("repeated hash of the same object (%q) produced different results: %d vs %d", toString(tc()), hash1, hash1a)
}
if hash2a != hash2 {
t.Errorf("repeated hash of the same object (%q) produced different results: %d vs %d", toString(tc()), hash2, hash2a)
}
if hash1a != hash2a {
t.Errorf("hash of the same object produced (%q) different results: %d vs %d", toString(tc()), hash1a, hash2a)
}
}
}
}
func toString(obj interface{}) string {
return spew.Sprintf("%#v", obj)
}
type wheel struct { type wheel struct {
radius uint32 radius uint32
} }
......
...@@ -77,8 +77,11 @@ var _ = Describe("Networking", func() { ...@@ -77,8 +77,11 @@ var _ = Describe("Networking", func() {
}, },
}, },
Spec: api.ServiceSpec{ Spec: api.ServiceSpec{
Port: 8080, Ports: []api.ServicePort{{
TargetPort: util.NewIntOrStringFromInt(8080), Protocol: "TCP",
Port: 8080,
TargetPort: util.NewIntOrStringFromInt(8080),
}},
Selector: map[string]string{ Selector: map[string]string{
"name": name, "name": name,
}, },
......
...@@ -307,8 +307,10 @@ var _ = Describe("Pods", func() { ...@@ -307,8 +307,10 @@ var _ = Describe("Pods", func() {
}, },
}, },
Spec: api.ServiceSpec{ Spec: api.ServiceSpec{
Port: 8765, Ports: []api.ServicePort{{
TargetPort: util.NewIntOrStringFromInt(8080), Port: 8765,
TargetPort: util.NewIntOrStringFromInt(8080),
}},
Selector: map[string]string{ Selector: map[string]string{
"name": serverName, "name": serverName,
}, },
......
...@@ -204,9 +204,11 @@ var _ = Describe("Services", func() { ...@@ -204,9 +204,11 @@ var _ = Describe("Services", func() {
Name: serviceName, Name: serviceName,
}, },
Spec: api.ServiceSpec{ Spec: api.ServiceSpec{
Port: 80, Selector: labels,
Selector: labels, Ports: []api.ServicePort{{
TargetPort: util.NewIntOrStringFromInt(80), Port: 80,
TargetPort: util.NewIntOrStringFromInt(80),
}},
}, },
} }
_, err := c.Services(ns).Create(service) _, err := c.Services(ns).Create(service)
...@@ -264,9 +266,11 @@ var _ = Describe("Services", func() { ...@@ -264,9 +266,11 @@ var _ = Describe("Services", func() {
Name: serviceName, Name: serviceName,
}, },
Spec: api.ServiceSpec{ Spec: api.ServiceSpec{
Port: 80, Selector: labels,
Selector: labels, Ports: []api.ServicePort{{
TargetPort: util.NewIntOrStringFromInt(80), Port: 80,
TargetPort: util.NewIntOrStringFromInt(80),
}},
CreateExternalLoadBalancer: true, CreateExternalLoadBalancer: true,
}, },
} }
...@@ -287,7 +291,7 @@ var _ = Describe("Services", func() { ...@@ -287,7 +291,7 @@ var _ = Describe("Services", func() {
Failf("got unexpected number (%d) of public IPs for externally load balanced service: %v", result.Spec.PublicIPs, result) Failf("got unexpected number (%d) of public IPs for externally load balanced service: %v", result.Spec.PublicIPs, result)
} }
ip := result.Spec.PublicIPs[0] ip := result.Spec.PublicIPs[0]
port := result.Spec.Port port := result.Spec.Ports[0].Port
pod := &api.Pod{ pod := &api.Pod{
TypeMeta: api.TypeMeta{ TypeMeta: api.TypeMeta{
...@@ -351,9 +355,11 @@ var _ = Describe("Services", func() { ...@@ -351,9 +355,11 @@ var _ = Describe("Services", func() {
service := &api.Service{ service := &api.Service{
ObjectMeta: api.ObjectMeta{}, ObjectMeta: api.ObjectMeta{},
Spec: api.ServiceSpec{ Spec: api.ServiceSpec{
Port: 80, Selector: labels,
Selector: labels, Ports: []api.ServicePort{{
TargetPort: util.NewIntOrStringFromInt(80), Port: 80,
TargetPort: util.NewIntOrStringFromInt(80),
}},
CreateExternalLoadBalancer: true, CreateExternalLoadBalancer: true,
}, },
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment