Add --restart support to kubectl run

This commit is contained in:
Brendan Burns
2015-08-04 12:54:17 -07:00
parent fbb5ce6636
commit e42d6aa255
15 changed files with 461 additions and 42 deletions

View File

@@ -40,6 +40,30 @@ func (BasicReplicationController) ParamNames() []GeneratorParam {
}
}
func makePodSpec(params map[string]string, name string) (*api.PodSpec, error) {
stdin, err := GetBool(params, "stdin", false)
if err != nil {
return nil, err
}
tty, err := GetBool(params, "tty", false)
if err != nil {
return nil, err
}
spec := api.PodSpec{
Containers: []api.Container{
{
Name: name,
Image: params["image"],
Stdin: stdin,
TTY: tty,
},
},
}
return &spec, nil
}
func (BasicReplicationController) Generate(params map[string]string) (runtime.Object, error) {
name, found := params["name"]
if !found || len(name) == 0 {
@@ -66,16 +90,11 @@ func (BasicReplicationController) Generate(params map[string]string) (runtime.Ob
if err != nil {
return nil, err
}
stdin, err := GetBool(params, "stdin", false)
podSpec, err := makePodSpec(params, name)
if err != nil {
return nil, err
}
tty, err := GetBool(params, "tty", false)
if err != nil {
return nil, err
}
controller := api.ReplicationController{
ObjectMeta: api.ObjectMeta{
Name: name,
@@ -88,49 +107,119 @@ func (BasicReplicationController) Generate(params map[string]string) (runtime.Ob
ObjectMeta: api.ObjectMeta{
Labels: labels,
},
Spec: api.PodSpec{
Containers: []api.Container{
{
Name: name,
Image: params["image"],
Stdin: stdin,
TTY: tty,
},
},
},
Spec: *podSpec,
},
},
}
if err := updatePodPorts(params, &controller.Spec.Template.Spec); err != nil {
return nil, err
}
return &controller, nil
}
func updatePodPorts(params map[string]string, podSpec *api.PodSpec) (err error) {
port := -1
hostPort := -1
if len(params["port"]) > 0 {
port, err = strconv.Atoi(params["port"])
if err != nil {
return nil, err
return err
}
}
if len(params["hostport"]) > 0 {
hostPort, err = strconv.Atoi(params["hostport"])
if err != nil {
return nil, err
return err
}
if hostPort > 0 && port < 0 {
return nil, fmt.Errorf("--hostport requires --port to be specified")
return 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{
podSpec.Containers[0].Ports = []api.ContainerPort{
{
ContainerPort: port,
},
}
if hostPort > 0 {
controller.Spec.Template.Spec.Containers[0].Ports[0].HostPort = hostPort
podSpec.Containers[0].Ports[0].HostPort = hostPort
}
}
return &controller, nil
return nil
}
type BasicPod struct{}
func (BasicPod) ParamNames() []GeneratorParam {
return []GeneratorParam{
{"labels", false},
{"default-name", false},
{"name", true},
{"image", true},
{"port", false},
{"hostport", false},
{"stdin", false},
{"tty", false},
{"restart", false},
}
}
func (BasicPod) 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.")
}
}
// TODO: extract this flag to a central location.
labelString, found := params["labels"]
var labels map[string]string
var err error
if found && len(labelString) > 0 {
labels, err = ParseLabels(labelString)
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
}
restartPolicy := api.RestartPolicy(params["restart"])
if len(restartPolicy) == 0 {
restartPolicy = api.RestartPolicyAlways
}
pod := api.Pod{
ObjectMeta: api.ObjectMeta{
Name: name,
Labels: labels,
},
Spec: api.PodSpec{
Containers: []api.Container{
{
Name: name,
Image: params["image"],
ImagePullPolicy: api.PullIfNotPresent,
Stdin: stdin,
TTY: tty,
},
},
DNSPolicy: api.DNSClusterFirst,
RestartPolicy: restartPolicy,
},
}
if err := updatePodPorts(params, &pod.Spec); err != nil {
return nil, err
}
return &pod, nil
}