Add a service generator and a command to easily expose services.

This commit is contained in:
Brendan Burns
2015-01-16 17:52:27 -08:00
parent c65f83f424
commit 09166f5252
8 changed files with 291 additions and 28 deletions

85
pkg/kubectl/service.go Normal file
View File

@@ -0,0 +1,85 @@
/*
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},
{"labels", true},
{"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) {
labelString, found := params["labels"]
if !found || len(labelString) == 0 {
return nil, fmt.Errorf("'labels' is a required parameter.")
}
labels := ParseLabels(labelString)
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{
Name: name,
Labels: labels,
},
Spec: api.ServiceSpec{
Port: port,
Protocol: api.Protocol(params["protocol"]),
Selector: labels,
},
}
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
}