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
+24
View File
@@ -207,6 +207,16 @@ func (c *Compose) LoadFile(file string) kobject.KomposeObject {
}
}
// canonical "Custom Labels" handler
// Labels used to influence conversion of kompose will be handled
// from here for docker-compose. Each loader will have such handler.
for key, value := range composeServiceConfig.Labels {
switch key {
case "kompose.service.type":
serviceConfig.ServiceType = handleServiceType(value)
}
}
// convert compose labels to annotations
serviceConfig.Annotations = map[string]string(composeServiceConfig.Labels)
@@ -227,3 +237,17 @@ func (c *Compose) LoadFile(file string) kobject.KomposeObject {
return komposeObject
}
func handleServiceType(ServiceType string) string {
switch strings.ToLower(ServiceType) {
case "", "clusterip":
return string(api.ServiceTypeClusterIP)
case "nodeport":
return string(api.ServiceTypeNodePort)
case "loadbalancer":
return string(api.ServiceTypeLoadBalancer)
default:
logrus.Fatalf("Unknown value '%s', supported values are 'NodePort, ClusterIP or LoadBalancer'", ServiceType)
return ""
}
}
+43
View File
@@ -0,0 +1,43 @@
/*
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.
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 compose
import "testing"
// Test if service types are parsed properly on user input
// give a service type and expect correct input
func TestHandleServiceType(t *testing.T) {
tests := []struct {
labelValue string
serviceType string
}{
{"NodePort", "NodePort"},
{"nodeport", "NodePort"},
{"LoadBalancer", "LoadBalancer"},
{"loadbalancer", "LoadBalancer"},
{"ClusterIP", "ClusterIP"},
{"clusterip", "ClusterIP"},
{"", "ClusterIP"},
}
for _, tt := range tests {
result := handleServiceType(tt.labelValue)
if result != tt.serviceType {
t.Errorf("Expected %q, got %q", tt.serviceType, result)
}
}
}