2015-01-17 01:52:27 +00:00
|
|
|
/*
|
|
|
|
Copyright 2014 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 kubectl
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"strconv"
|
|
|
|
|
|
|
|
"github.com/GoogleCloudPlatform/kubernetes/pkg/api"
|
|
|
|
"github.com/GoogleCloudPlatform/kubernetes/pkg/runtime"
|
|
|
|
"github.com/GoogleCloudPlatform/kubernetes/pkg/util"
|
|
|
|
)
|
|
|
|
|
|
|
|
type ServiceGenerator struct{}
|
|
|
|
|
|
|
|
func (ServiceGenerator) ParamNames() []GeneratorParam {
|
|
|
|
return []GeneratorParam{
|
|
|
|
{"name", true},
|
2015-01-27 18:23:28 +00:00
|
|
|
{"selector", true},
|
2015-01-17 01:52:27 +00:00
|
|
|
{"port", true},
|
|
|
|
{"public-ip", false},
|
|
|
|
{"create-external-load-balancer", false},
|
|
|
|
{"protocol", false},
|
|
|
|
{"container-port", false},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ServiceGenerator) Generate(params map[string]string) (runtime.Object, error) {
|
2015-01-27 18:23:28 +00:00
|
|
|
selectorString, found := params["selector"]
|
|
|
|
if !found || len(selectorString) == 0 {
|
|
|
|
return nil, fmt.Errorf("'selector' is a required parameter.")
|
2015-01-17 01:52:27 +00:00
|
|
|
}
|
2015-01-27 18:23:28 +00:00
|
|
|
selector := ParseLabels(selectorString)
|
2015-01-17 01:52:27 +00:00
|
|
|
name, found := params["name"]
|
|
|
|
if !found {
|
|
|
|
return nil, fmt.Errorf("'name' is a required parameter.")
|
|
|
|
}
|
|
|
|
port, err := strconv.Atoi(params["port"])
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
service := api.Service{
|
|
|
|
ObjectMeta: api.ObjectMeta{
|
2015-01-27 18:23:28 +00:00
|
|
|
Name: name,
|
2015-01-17 01:52:27 +00:00
|
|
|
},
|
|
|
|
Spec: api.ServiceSpec{
|
|
|
|
Port: port,
|
|
|
|
Protocol: api.Protocol(params["protocol"]),
|
2015-01-27 18:23:28 +00:00
|
|
|
Selector: selector,
|
2015-01-17 01:52:27 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
containerPort, found := params["container-port"]
|
|
|
|
if found && len(containerPort) > 0 {
|
|
|
|
cPort, err := strconv.Atoi(containerPort)
|
|
|
|
if err != nil {
|
|
|
|
service.Spec.ContainerPort = util.NewIntOrStringFromInt(cPort)
|
|
|
|
} else {
|
|
|
|
service.Spec.ContainerPort = util.NewIntOrStringFromString(containerPort)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
service.Spec.ContainerPort = util.NewIntOrStringFromInt(port)
|
|
|
|
}
|
|
|
|
if params["create-external-load-balancer"] == "true" {
|
|
|
|
service.Spec.CreateExternalLoadBalancer = true
|
|
|
|
}
|
|
|
|
if len(params["public-ip"]) == 0 {
|
|
|
|
service.Spec.PublicIPs = []string{params["public-ip"]}
|
|
|
|
}
|
|
|
|
return &service, nil
|
|
|
|
}
|