Generic service type handler for kompose

Moved label handling code from Transformer to loader,
to make it generic to handle creating service types.

Added new attribute to ServiceConfig which gets populated
in loader.

Fixes #273
This commit is contained in:
Suraj Deshmukh
2016-11-16 22:01:01 +05:30
parent 83c7a824dc
commit bb9e4fba61
5 changed files with 72 additions and 77 deletions
+1 -15
View File
@@ -244,21 +244,7 @@ func (k *Kubernetes) CreateService(name string, service kobject.ServiceConfig, o
servicePorts := k.ConfigServicePorts(name, service)
svc.Spec.Ports = servicePorts
// Configure service types
for key, value := range service.Annotations {
if key == "kompose.service.type" {
if strings.ToLower(value) == "nodeport" {
svc.Spec.Type = "NodePort"
} else if strings.ToLower(value) == "clusterip" {
svc.Spec.Type = "ClusterIP"
} else if strings.ToLower(value) == "loadbalancer" {
svc.Spec.Type = "LoadBalancer"
} else {
logrus.Fatalf("Unknown value '%s', supported values are 'NodePort, ClusterIP and LoadBalancer' " , value)
}
}
}
svc.Spec.Type = api.ServiceType(service.ServiceType)
// Configure annotations
annotations := transformer.ConfigAnnotations(service)
svc.ObjectMeta.Annotations = annotations
+3 -62
View File
@@ -1,5 +1,5 @@
/*
Copyright 2016 Skippbox, Ltd All rights reserved.
Copyright 2016 The Kubernetes Authors All rights reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
@@ -17,9 +17,10 @@ limitations under the License.
package kubernetes
import (
"testing"
"github.com/kubernetes-incubator/kompose/pkg/kobject"
"k8s.io/kubernetes/pkg/api"
"testing"
)
/*
@@ -64,63 +65,3 @@ func TestCreateService(t *testing.T) {
t.Errorf("Expected port 123 upon conversion, actual %d", svc.Spec.Ports[0].Port)
}
}
/*
Test the creation of a service with a specified annotation (kompose.service.type) as "nodeport".
The expected result is that Kompose will convert the spec type to "NodePort" upon generation.
*/
func TestCreateServiceWithServiceTypeNodePort(t *testing.T) {
// An example service
service := kobject.ServiceConfig{
ContainerName: "name",
Image: "image",
Environment: []kobject.EnvVar{kobject.EnvVar{Name: "env", Value: "value"}},
Port: []kobject.Ports{kobject.Ports{HostPort: 123, ContainerPort: 456, Protocol: api.ProtocolTCP}},
Command: []string{"cmd"},
WorkingDir: "dir",
Args: []string{"arg1", "arg2"},
Volumes: []string{"/tmp/volume"},
Network: []string{"network1", "network2"}, // not supported
Labels: nil,
Annotations: map[string]string{"kompose.service.type": "nodeport"},
CPUSet: "cpu_set", // not supported
CPUShares: 1, // not supported
CPUQuota: 1, // not supported
CapAdd: []string{"cap_add"}, // not supported
CapDrop: []string{"cap_drop"}, // not supported
Expose: []string{"expose"}, // not supported
Privileged: true,
Restart: "always",
User: "user", // not supported
}
// An example object generated via k8s runtime.Objects()
kompose_object := kobject.KomposeObject{
ServiceConfigs: map[string]kobject.ServiceConfig{"app": service},
}
k := Kubernetes{}
tests := []struct {
labelValue string
serviceType string
}{
{"NodePort", "NodePort"},
{"nodeport", "NodePort"},
{"LoadBalancer", "LoadBalancer"},
{"loadbalancer", "LoadBalancer"},
{"ClusterIP", "ClusterIP"},
{"clusterip", "ClusterIP"},
}
for _, tt := range tests {
kompose_object.ServiceConfigs["app"].Annotations["kompose.service.type"] = tt.labelValue
objects := k.Transform(kompose_object, kobject.ConvertOptions{CreateD: true, Replicas: 3})
// Test the creation of the service with modified annotations (kompose.service.type)
svc := k.CreateService("foo", service, objects)
if svc.Spec.Type != api.ServiceType(tt.serviceType) {
t.Errorf("Expected NodePort, actual %d", svc.Spec.Type)
}
}
}