k3s/pkg/kubectl/run.go

137 lines
3.0 KiB
Go
Raw Normal View History

2015-01-12 23:30:52 +00:00
/*
Copyright 2014 The Kubernetes Authors All rights reserved.
2015-01-12 23:30:52 +00:00
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"
2015-01-12 23:30:52 +00:00
"strconv"
2015-08-05 22:03:47 +00:00
"k8s.io/kubernetes/pkg/api"
"k8s.io/kubernetes/pkg/runtime"
2015-01-12 23:30:52 +00:00
)
type BasicReplicationController struct{}
func (BasicReplicationController) ParamNames() []GeneratorParam {
return []GeneratorParam{
{"labels", false},
{"default-name", false},
{"name", true},
2015-01-12 23:30:52 +00:00
{"replicas", true},
{"image", true},
{"port", false},
{"hostport", false},
{"stdin", false},
{"tty", false},
2015-01-12 23:30:52 +00:00
}
}
func (BasicReplicationController) Generate(params map[string]string) (runtime.Object, error) {
name, found := params["name"]
if !found || len(name) == 0 {
name, found = params["default-name"]
if !found || len(name) == 0 {
return nil, fmt.Errorf("'name' is a required parameter.")
}
}
2015-01-12 23:30:52 +00:00
// TODO: extract this flag to a central location.
labelString, found := params["labels"]
var labels map[string]string
2015-04-10 09:36:49 +00:00
var err error
2015-01-12 23:30:52 +00:00
if found && len(labelString) > 0 {
2015-04-10 09:36:49 +00:00
labels, err = ParseLabels(labelString)
if err != nil {
return nil, err
}
2015-01-12 23:30:52 +00:00
} else {
labels = map[string]string{
"run": name,
2015-01-12 23:30:52 +00:00
}
}
count, err := strconv.Atoi(params["replicas"])
if err != nil {
return nil, err
}
stdin, err := GetBool(params, "stdin", false)
if err != nil {
return nil, err
}
tty, err := GetBool(params, "tty", false)
if err != nil {
return nil, err
}
2015-01-12 23:30:52 +00:00
controller := api.ReplicationController{
ObjectMeta: api.ObjectMeta{
Name: name,
2015-01-12 23:30:52 +00:00
Labels: labels,
},
Spec: api.ReplicationControllerSpec{
Replicas: count,
Selector: labels,
Template: &api.PodTemplateSpec{
ObjectMeta: api.ObjectMeta{
Labels: labels,
},
Spec: api.PodSpec{
Containers: []api.Container{
{
Name: name,
2015-01-12 23:30:52 +00:00
Image: params["image"],
Stdin: stdin,
TTY: tty,
2015-01-12 23:30:52 +00:00
},
},
},
},
},
}
2015-01-31 04:51:40 +00:00
port := -1
hostPort := -1
2015-01-31 04:51:40 +00:00
if len(params["port"]) > 0 {
port, err = strconv.Atoi(params["port"])
2015-01-31 04:51:40 +00:00
if err != nil {
return nil, err
}
}
if len(params["hostport"]) > 0 {
hostPort, err = strconv.Atoi(params["hostport"])
if err != nil {
return nil, err
}
if hostPort > 0 && port < 0 {
return nil, fmt.Errorf("--hostport requires --port to be specified")
}
}
// Don't include the port if it was not specified.
if port > 0 {
controller.Spec.Template.Spec.Containers[0].Ports = []api.ContainerPort{
{
ContainerPort: port,
},
}
if hostPort > 0 {
controller.Spec.Template.Spec.Containers[0].Ports[0].HostPort = hostPort
2015-01-12 23:30:52 +00:00
}
}
return &controller, nil
2015-01-12 23:30:52 +00:00
}