remove dependency from service generator

This commit is contained in:
zhengjiajin 2017-10-31 15:35:36 +08:00
parent d118e44320
commit d5e5cabafc
2 changed files with 90 additions and 90 deletions

View File

@ -21,10 +21,10 @@ import (
"strconv" "strconv"
"strings" "strings"
"k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime" "k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/util/intstr" "k8s.io/apimachinery/pkg/util/intstr"
"k8s.io/kubernetes/pkg/api"
) )
// The only difference between ServiceGeneratorV1 and V2 is that the service port is named "default" in V1, while it is left unnamed in V2. // The only difference between ServiceGeneratorV1 and V2 is that the service port is named "default" in V1, while it is left unnamed in V2.
@ -113,7 +113,7 @@ func generate(genericParams map[string]interface{}) (runtime.Object, error) {
isHeadlessService := params["cluster-ip"] == "None" isHeadlessService := params["cluster-ip"] == "None"
ports := []api.ServicePort{} ports := []v1.ServicePort{}
servicePortName, found := params["port-name"] servicePortName, found := params["port-name"]
if !found { if !found {
// Leave the port unnamed. // Leave the port unnamed.
@ -168,20 +168,20 @@ func generate(genericParams map[string]interface{}) (runtime.Object, error) {
protocol = exposeProtocol protocol = exposeProtocol
} }
} }
ports = append(ports, api.ServicePort{ ports = append(ports, v1.ServicePort{
Name: name, Name: name,
Port: int32(port), Port: int32(port),
Protocol: api.Protocol(protocol), Protocol: v1.Protocol(protocol),
}) })
} }
} }
service := api.Service{ service := v1.Service{
ObjectMeta: metav1.ObjectMeta{ ObjectMeta: metav1.ObjectMeta{
Name: name, Name: name,
Labels: labels, Labels: labels,
}, },
Spec: api.ServiceSpec{ Spec: v1.ServiceSpec{
Selector: selector, Selector: selector,
Ports: ports, Ports: ports,
}, },
@ -213,24 +213,24 @@ func generate(genericParams map[string]interface{}) (runtime.Object, error) {
service.Spec.ExternalIPs = []string{params["external-ip"]} service.Spec.ExternalIPs = []string{params["external-ip"]}
} }
if len(params["type"]) != 0 { if len(params["type"]) != 0 {
service.Spec.Type = api.ServiceType(params["type"]) service.Spec.Type = v1.ServiceType(params["type"])
} }
if service.Spec.Type == api.ServiceTypeLoadBalancer { if service.Spec.Type == v1.ServiceTypeLoadBalancer {
service.Spec.LoadBalancerIP = params["load-balancer-ip"] service.Spec.LoadBalancerIP = params["load-balancer-ip"]
} }
if len(params["session-affinity"]) != 0 { if len(params["session-affinity"]) != 0 {
switch api.ServiceAffinity(params["session-affinity"]) { switch v1.ServiceAffinity(params["session-affinity"]) {
case api.ServiceAffinityNone: case v1.ServiceAffinityNone:
service.Spec.SessionAffinity = api.ServiceAffinityNone service.Spec.SessionAffinity = v1.ServiceAffinityNone
case api.ServiceAffinityClientIP: case v1.ServiceAffinityClientIP:
service.Spec.SessionAffinity = api.ServiceAffinityClientIP service.Spec.SessionAffinity = v1.ServiceAffinityClientIP
default: default:
return nil, fmt.Errorf("unknown session affinity: %s", params["session-affinity"]) return nil, fmt.Errorf("unknown session affinity: %s", params["session-affinity"])
} }
} }
if len(params["cluster-ip"]) != 0 { if len(params["cluster-ip"]) != 0 {
if params["cluster-ip"] == "None" { if params["cluster-ip"] == "None" {
service.Spec.ClusterIP = api.ClusterIPNone service.Spec.ClusterIP = v1.ClusterIPNone
} else { } else {
service.Spec.ClusterIP = params["cluster-ip"] service.Spec.ClusterIP = params["cluster-ip"]
} }

View File

@ -20,16 +20,16 @@ import (
"reflect" "reflect"
"testing" "testing"
"k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/intstr" "k8s.io/apimachinery/pkg/util/intstr"
"k8s.io/kubernetes/pkg/api"
) )
func TestGenerateService(t *testing.T) { func TestGenerateService(t *testing.T) {
tests := []struct { tests := []struct {
generator Generator generator Generator
params map[string]interface{} params map[string]interface{}
expected api.Service expected v1.Service
}{ }{
{ {
generator: ServiceGeneratorV2{}, generator: ServiceGeneratorV2{},
@ -40,16 +40,16 @@ func TestGenerateService(t *testing.T) {
"protocol": "TCP", "protocol": "TCP",
"container-port": "1234", "container-port": "1234",
}, },
expected: api.Service{ expected: v1.Service{
ObjectMeta: metav1.ObjectMeta{ ObjectMeta: metav1.ObjectMeta{
Name: "test", Name: "test",
}, },
Spec: api.ServiceSpec{ Spec: v1.ServiceSpec{
Selector: map[string]string{ Selector: map[string]string{
"foo": "bar", "foo": "bar",
"baz": "blah", "baz": "blah",
}, },
Ports: []api.ServicePort{ Ports: []v1.ServicePort{
{ {
Port: 80, Port: 80,
Protocol: "TCP", Protocol: "TCP",
@ -69,16 +69,16 @@ func TestGenerateService(t *testing.T) {
"protocol": "UDP", "protocol": "UDP",
"container-port": "foobar", "container-port": "foobar",
}, },
expected: api.Service{ expected: v1.Service{
ObjectMeta: metav1.ObjectMeta{ ObjectMeta: metav1.ObjectMeta{
Name: "test", Name: "test",
}, },
Spec: api.ServiceSpec{ Spec: v1.ServiceSpec{
Selector: map[string]string{ Selector: map[string]string{
"foo": "bar", "foo": "bar",
"baz": "blah", "baz": "blah",
}, },
Ports: []api.ServicePort{ Ports: []v1.ServicePort{
{ {
Port: 80, Port: 80,
Protocol: "UDP", Protocol: "UDP",
@ -98,7 +98,7 @@ func TestGenerateService(t *testing.T) {
"protocol": "TCP", "protocol": "TCP",
"container-port": "1234", "container-port": "1234",
}, },
expected: api.Service{ expected: v1.Service{
ObjectMeta: metav1.ObjectMeta{ ObjectMeta: metav1.ObjectMeta{
Name: "test", Name: "test",
Labels: map[string]string{ Labels: map[string]string{
@ -106,12 +106,12 @@ func TestGenerateService(t *testing.T) {
"key2": "value2", "key2": "value2",
}, },
}, },
Spec: api.ServiceSpec{ Spec: v1.ServiceSpec{
Selector: map[string]string{ Selector: map[string]string{
"foo": "bar", "foo": "bar",
"baz": "blah", "baz": "blah",
}, },
Ports: []api.ServicePort{ Ports: []v1.ServicePort{
{ {
Port: 80, Port: 80,
Protocol: "TCP", Protocol: "TCP",
@ -131,16 +131,16 @@ func TestGenerateService(t *testing.T) {
"container-port": "foobar", "container-port": "foobar",
"external-ip": "1.2.3.4", "external-ip": "1.2.3.4",
}, },
expected: api.Service{ expected: v1.Service{
ObjectMeta: metav1.ObjectMeta{ ObjectMeta: metav1.ObjectMeta{
Name: "test", Name: "test",
}, },
Spec: api.ServiceSpec{ Spec: v1.ServiceSpec{
Selector: map[string]string{ Selector: map[string]string{
"foo": "bar", "foo": "bar",
"baz": "blah", "baz": "blah",
}, },
Ports: []api.ServicePort{ Ports: []v1.ServicePort{
{ {
Port: 80, Port: 80,
Protocol: "UDP", Protocol: "UDP",
@ -162,23 +162,23 @@ func TestGenerateService(t *testing.T) {
"external-ip": "1.2.3.4", "external-ip": "1.2.3.4",
"type": "LoadBalancer", "type": "LoadBalancer",
}, },
expected: api.Service{ expected: v1.Service{
ObjectMeta: metav1.ObjectMeta{ ObjectMeta: metav1.ObjectMeta{
Name: "test", Name: "test",
}, },
Spec: api.ServiceSpec{ Spec: v1.ServiceSpec{
Selector: map[string]string{ Selector: map[string]string{
"foo": "bar", "foo": "bar",
"baz": "blah", "baz": "blah",
}, },
Ports: []api.ServicePort{ Ports: []v1.ServicePort{
{ {
Port: 80, Port: 80,
Protocol: "UDP", Protocol: "UDP",
TargetPort: intstr.FromString("foobar"), TargetPort: intstr.FromString("foobar"),
}, },
}, },
Type: api.ServiceTypeLoadBalancer, Type: v1.ServiceTypeLoadBalancer,
ExternalIPs: []string{"1.2.3.4"}, ExternalIPs: []string{"1.2.3.4"},
}, },
}, },
@ -191,25 +191,25 @@ func TestGenerateService(t *testing.T) {
"port": "80", "port": "80",
"protocol": "UDP", "protocol": "UDP",
"container-port": "foobar", "container-port": "foobar",
"type": string(api.ServiceTypeNodePort), "type": string(v1.ServiceTypeNodePort),
}, },
expected: api.Service{ expected: v1.Service{
ObjectMeta: metav1.ObjectMeta{ ObjectMeta: metav1.ObjectMeta{
Name: "test", Name: "test",
}, },
Spec: api.ServiceSpec{ Spec: v1.ServiceSpec{
Selector: map[string]string{ Selector: map[string]string{
"foo": "bar", "foo": "bar",
"baz": "blah", "baz": "blah",
}, },
Ports: []api.ServicePort{ Ports: []v1.ServicePort{
{ {
Port: 80, Port: 80,
Protocol: "UDP", Protocol: "UDP",
TargetPort: intstr.FromString("foobar"), TargetPort: intstr.FromString("foobar"),
}, },
}, },
Type: api.ServiceTypeNodePort, Type: v1.ServiceTypeNodePort,
}, },
}, },
}, },
@ -222,25 +222,25 @@ func TestGenerateService(t *testing.T) {
"protocol": "UDP", "protocol": "UDP",
"container-port": "foobar", "container-port": "foobar",
"create-external-load-balancer": "true", // ignored when type is present "create-external-load-balancer": "true", // ignored when type is present
"type": string(api.ServiceTypeNodePort), "type": string(v1.ServiceTypeNodePort),
}, },
expected: api.Service{ expected: v1.Service{
ObjectMeta: metav1.ObjectMeta{ ObjectMeta: metav1.ObjectMeta{
Name: "test", Name: "test",
}, },
Spec: api.ServiceSpec{ Spec: v1.ServiceSpec{
Selector: map[string]string{ Selector: map[string]string{
"foo": "bar", "foo": "bar",
"baz": "blah", "baz": "blah",
}, },
Ports: []api.ServicePort{ Ports: []v1.ServicePort{
{ {
Port: 80, Port: 80,
Protocol: "UDP", Protocol: "UDP",
TargetPort: intstr.FromString("foobar"), TargetPort: intstr.FromString("foobar"),
}, },
}, },
Type: api.ServiceTypeNodePort, Type: v1.ServiceTypeNodePort,
}, },
}, },
}, },
@ -253,16 +253,16 @@ func TestGenerateService(t *testing.T) {
"protocol": "TCP", "protocol": "TCP",
"container-port": "1234", "container-port": "1234",
}, },
expected: api.Service{ expected: v1.Service{
ObjectMeta: metav1.ObjectMeta{ ObjectMeta: metav1.ObjectMeta{
Name: "test", Name: "test",
}, },
Spec: api.ServiceSpec{ Spec: v1.ServiceSpec{
Selector: map[string]string{ Selector: map[string]string{
"foo": "bar", "foo": "bar",
"baz": "blah", "baz": "blah",
}, },
Ports: []api.ServicePort{ Ports: []v1.ServicePort{
{ {
Name: "default", Name: "default",
Port: 80, Port: 80,
@ -283,16 +283,16 @@ func TestGenerateService(t *testing.T) {
"container-port": "1234", "container-port": "1234",
"session-affinity": "ClientIP", "session-affinity": "ClientIP",
}, },
expected: api.Service{ expected: v1.Service{
ObjectMeta: metav1.ObjectMeta{ ObjectMeta: metav1.ObjectMeta{
Name: "test", Name: "test",
}, },
Spec: api.ServiceSpec{ Spec: v1.ServiceSpec{
Selector: map[string]string{ Selector: map[string]string{
"foo": "bar", "foo": "bar",
"baz": "blah", "baz": "blah",
}, },
Ports: []api.ServicePort{ Ports: []v1.ServicePort{
{ {
Name: "default", Name: "default",
Port: 80, Port: 80,
@ -300,7 +300,7 @@ func TestGenerateService(t *testing.T) {
TargetPort: intstr.FromInt(1234), TargetPort: intstr.FromInt(1234),
}, },
}, },
SessionAffinity: api.ServiceAffinityClientIP, SessionAffinity: v1.ServiceAffinityClientIP,
}, },
}, },
}, },
@ -314,16 +314,16 @@ func TestGenerateService(t *testing.T) {
"container-port": "1234", "container-port": "1234",
"cluster-ip": "10.10.10.10", "cluster-ip": "10.10.10.10",
}, },
expected: api.Service{ expected: v1.Service{
ObjectMeta: metav1.ObjectMeta{ ObjectMeta: metav1.ObjectMeta{
Name: "test", Name: "test",
}, },
Spec: api.ServiceSpec{ Spec: v1.ServiceSpec{
Selector: map[string]string{ Selector: map[string]string{
"foo": "bar", "foo": "bar",
"baz": "blah", "baz": "blah",
}, },
Ports: []api.ServicePort{ Ports: []v1.ServicePort{
{ {
Port: 80, Port: 80,
Protocol: "TCP", Protocol: "TCP",
@ -344,23 +344,23 @@ func TestGenerateService(t *testing.T) {
"container-port": "1234", "container-port": "1234",
"cluster-ip": "None", "cluster-ip": "None",
}, },
expected: api.Service{ expected: v1.Service{
ObjectMeta: metav1.ObjectMeta{ ObjectMeta: metav1.ObjectMeta{
Name: "test", Name: "test",
}, },
Spec: api.ServiceSpec{ Spec: v1.ServiceSpec{
Selector: map[string]string{ Selector: map[string]string{
"foo": "bar", "foo": "bar",
"baz": "blah", "baz": "blah",
}, },
Ports: []api.ServicePort{ Ports: []v1.ServicePort{
{ {
Port: 80, Port: 80,
Protocol: "TCP", Protocol: "TCP",
TargetPort: intstr.FromInt(1234), TargetPort: intstr.FromInt(1234),
}, },
}, },
ClusterIP: api.ClusterIPNone, ClusterIP: v1.ClusterIPNone,
}, },
}, },
}, },
@ -373,25 +373,25 @@ func TestGenerateService(t *testing.T) {
"protocol": "TCP", "protocol": "TCP",
"container-port": "foobar", "container-port": "foobar",
}, },
expected: api.Service{ expected: v1.Service{
ObjectMeta: metav1.ObjectMeta{ ObjectMeta: metav1.ObjectMeta{
Name: "test", Name: "test",
}, },
Spec: api.ServiceSpec{ Spec: v1.ServiceSpec{
Selector: map[string]string{ Selector: map[string]string{
"foo": "bar", "foo": "bar",
}, },
Ports: []api.ServicePort{ Ports: []v1.ServicePort{
{ {
Name: "port-1", Name: "port-1",
Port: 80, Port: 80,
Protocol: api.ProtocolTCP, Protocol: v1.ProtocolTCP,
TargetPort: intstr.FromString("foobar"), TargetPort: intstr.FromString("foobar"),
}, },
{ {
Name: "port-2", Name: "port-2",
Port: 443, Port: 443,
Protocol: api.ProtocolTCP, Protocol: v1.ProtocolTCP,
TargetPort: intstr.FromString("foobar"), TargetPort: intstr.FromString("foobar"),
}, },
}, },
@ -407,25 +407,25 @@ func TestGenerateService(t *testing.T) {
"protocol": "UDP", "protocol": "UDP",
"target-port": "1234", "target-port": "1234",
}, },
expected: api.Service{ expected: v1.Service{
ObjectMeta: metav1.ObjectMeta{ ObjectMeta: metav1.ObjectMeta{
Name: "test", Name: "test",
}, },
Spec: api.ServiceSpec{ Spec: v1.ServiceSpec{
Selector: map[string]string{ Selector: map[string]string{
"foo": "bar", "foo": "bar",
}, },
Ports: []api.ServicePort{ Ports: []v1.ServicePort{
{ {
Name: "port-1", Name: "port-1",
Port: 80, Port: 80,
Protocol: api.ProtocolUDP, Protocol: v1.ProtocolUDP,
TargetPort: intstr.FromInt(1234), TargetPort: intstr.FromInt(1234),
}, },
{ {
Name: "port-2", Name: "port-2",
Port: 443, Port: 443,
Protocol: api.ProtocolUDP, Protocol: v1.ProtocolUDP,
TargetPort: intstr.FromInt(1234), TargetPort: intstr.FromInt(1234),
}, },
}, },
@ -440,25 +440,25 @@ func TestGenerateService(t *testing.T) {
"ports": "80,443", "ports": "80,443",
"protocol": "TCP", "protocol": "TCP",
}, },
expected: api.Service{ expected: v1.Service{
ObjectMeta: metav1.ObjectMeta{ ObjectMeta: metav1.ObjectMeta{
Name: "test", Name: "test",
}, },
Spec: api.ServiceSpec{ Spec: v1.ServiceSpec{
Selector: map[string]string{ Selector: map[string]string{
"foo": "bar", "foo": "bar",
}, },
Ports: []api.ServicePort{ Ports: []v1.ServicePort{
{ {
Name: "port-1", Name: "port-1",
Port: 80, Port: 80,
Protocol: api.ProtocolTCP, Protocol: v1.ProtocolTCP,
TargetPort: intstr.FromInt(80), TargetPort: intstr.FromInt(80),
}, },
{ {
Name: "port-2", Name: "port-2",
Port: 443, Port: 443,
Protocol: api.ProtocolTCP, Protocol: v1.ProtocolTCP,
TargetPort: intstr.FromInt(443), TargetPort: intstr.FromInt(443),
}, },
}, },
@ -473,25 +473,25 @@ func TestGenerateService(t *testing.T) {
"ports": "80,8080", "ports": "80,8080",
"protocols": "8080/UDP", "protocols": "8080/UDP",
}, },
expected: api.Service{ expected: v1.Service{
ObjectMeta: metav1.ObjectMeta{ ObjectMeta: metav1.ObjectMeta{
Name: "test", Name: "test",
}, },
Spec: api.ServiceSpec{ Spec: v1.ServiceSpec{
Selector: map[string]string{ Selector: map[string]string{
"foo": "bar", "foo": "bar",
}, },
Ports: []api.ServicePort{ Ports: []v1.ServicePort{
{ {
Name: "port-1", Name: "port-1",
Port: 80, Port: 80,
Protocol: api.ProtocolTCP, Protocol: v1.ProtocolTCP,
TargetPort: intstr.FromInt(80), TargetPort: intstr.FromInt(80),
}, },
{ {
Name: "port-2", Name: "port-2",
Port: 8080, Port: 8080,
Protocol: api.ProtocolUDP, Protocol: v1.ProtocolUDP,
TargetPort: intstr.FromInt(8080), TargetPort: intstr.FromInt(8080),
}, },
}, },
@ -506,31 +506,31 @@ func TestGenerateService(t *testing.T) {
"ports": "80,8080,8081", "ports": "80,8080,8081",
"protocols": "8080/UDP,8081/TCP", "protocols": "8080/UDP,8081/TCP",
}, },
expected: api.Service{ expected: v1.Service{
ObjectMeta: metav1.ObjectMeta{ ObjectMeta: metav1.ObjectMeta{
Name: "test", Name: "test",
}, },
Spec: api.ServiceSpec{ Spec: v1.ServiceSpec{
Selector: map[string]string{ Selector: map[string]string{
"foo": "bar", "foo": "bar",
}, },
Ports: []api.ServicePort{ Ports: []v1.ServicePort{
{ {
Name: "port-1", Name: "port-1",
Port: 80, Port: 80,
Protocol: api.ProtocolTCP, Protocol: v1.ProtocolTCP,
TargetPort: intstr.FromInt(80), TargetPort: intstr.FromInt(80),
}, },
{ {
Name: "port-2", Name: "port-2",
Port: 8080, Port: 8080,
Protocol: api.ProtocolUDP, Protocol: v1.ProtocolUDP,
TargetPort: intstr.FromInt(8080), TargetPort: intstr.FromInt(8080),
}, },
{ {
Name: "port-3", Name: "port-3",
Port: 8081, Port: 8081,
Protocol: api.ProtocolTCP, Protocol: v1.ProtocolTCP,
TargetPort: intstr.FromInt(8081), TargetPort: intstr.FromInt(8081),
}, },
}, },
@ -546,17 +546,17 @@ func TestGenerateService(t *testing.T) {
"container-port": "1234", "container-port": "1234",
"cluster-ip": "None", "cluster-ip": "None",
}, },
expected: api.Service{ expected: v1.Service{
ObjectMeta: metav1.ObjectMeta{ ObjectMeta: metav1.ObjectMeta{
Name: "test", Name: "test",
}, },
Spec: api.ServiceSpec{ Spec: v1.ServiceSpec{
Selector: map[string]string{ Selector: map[string]string{
"foo": "bar", "foo": "bar",
"baz": "blah", "baz": "blah",
}, },
Ports: []api.ServicePort{}, Ports: []v1.ServicePort{},
ClusterIP: api.ClusterIPNone, ClusterIP: v1.ClusterIPNone,
}, },
}, },
}, },
@ -567,16 +567,16 @@ func TestGenerateService(t *testing.T) {
"name": "test", "name": "test",
"cluster-ip": "None", "cluster-ip": "None",
}, },
expected: api.Service{ expected: v1.Service{
ObjectMeta: metav1.ObjectMeta{ ObjectMeta: metav1.ObjectMeta{
Name: "test", Name: "test",
}, },
Spec: api.ServiceSpec{ Spec: v1.ServiceSpec{
Selector: map[string]string{ Selector: map[string]string{
"foo": "bar", "foo": "bar",
}, },
Ports: []api.ServicePort{}, Ports: []v1.ServicePort{},
ClusterIP: api.ClusterIPNone, ClusterIP: v1.ClusterIPNone,
}, },
}, },
}, },