-
Notifications
You must be signed in to change notification settings - Fork 60
/
Copy pathcreateReplicationController.go
160 lines (150 loc) · 4.89 KB
/
createReplicationController.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
/*
Copyright 2015 Kelsey Hightower 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 main
import (
"log"
"strconv"
"strings"
"github.com/docker/libcompose/config"
"k8s.io/kubernetes/pkg/api"
"k8s.io/kubernetes/pkg/api/unversioned"
)
func createReplicationController(name string, shortName string, service *config.ServiceConfig, rancherCompose map[interface{}]interface{}) *api.ReplicationController {
rc := &api.ReplicationController{
TypeMeta: unversioned.TypeMeta{
Kind: "ReplicationController",
APIVersion: "v1",
},
ObjectMeta: api.ObjectMeta{
Name: shortName,
Namespace: "${NAMESPACE}",
Labels: configureLabels(shortName, service),
},
Spec: api.ReplicationControllerSpec{
Replicas: configureScale(name, rancherCompose),
Selector: map[string]string{"service": shortName},
Template: &api.PodTemplateSpec{
ObjectMeta: api.ObjectMeta{
Labels: map[string]string{"service": shortName},
},
Spec: api.PodSpec{
Containers: []api.Container{
{
Name: shortName,
Image: service.Image,
Command: service.Command,
Ports: configurePorts(name, service),
Env: configureVariables(service),
ReadinessProbe: configureHealthCheck(name, rancherCompose),
},
},
RestartPolicy: configureRestartPolicy(name, service),
},
},
},
}
rc.Spec.Template.Spec.Containers[0].VolumeMounts, rc.Spec.Template.Spec.Volumes = configureVolumes(service)
return rc
}
func configurePorts(name string, service *config.ServiceConfig) []api.ContainerPort {
var ports []api.ContainerPort
for _, port := range service.Ports {
// Check if we have to deal with a mapped port
port = strings.Trim(port, "\"")
port = strings.TrimSpace(port)
if strings.Contains(port, ":") {
parts := strings.Split(port, ":")
port = parts[1]
}
portNumber, err := strconv.ParseInt(port, 10, 32)
if err != nil {
log.Fatalf("Invalid container port %s for service %s", port, name)
}
ports = append(ports, api.ContainerPort{ContainerPort: int32(portNumber)})
}
return ports
}
func configureVariables(service *config.ServiceConfig) []api.EnvVar {
// Configure the container ENV variables
var envs []api.EnvVar
for _, env := range service.Environment {
if strings.Contains(env, "=") {
parts := strings.Split(env, "=")
ename := parts[0]
evalue := parts[1]
envs = append(envs, api.EnvVar{Name: ename, Value: evalue})
}
}
return envs
}
func configureLabels(shortName string, service *config.ServiceConfig) map[string]string {
labels := make(map[string]string, len(service.Labels)+1)
labels["service"] = shortName
for index, label := range service.Labels {
labels[index] = label
}
return labels
}
func configureVolumes(service *config.ServiceConfig) ([]api.VolumeMount, []api.Volume) {
var volumemounts []api.VolumeMount
var volumes []api.Volume
if service.Volumes != nil {
for _, volumestr := range service.Volumes.Volumes {
parts := strings.Split(volumestr.String(), ":")
if len(parts) < 2 {
log.Fatalf("Volumes without host path are not supported: %s", parts)
}
partHostDir := parts[0]
partContainerDir := parts[1]
partReadOnly := false
if len(parts) > 2 {
for _, partOpt := range parts[2:] {
switch partOpt {
case "ro":
partReadOnly = true
break
case "rw":
partReadOnly = false
break
}
}
}
partName := strings.Replace(partHostDir, "/", "", -1)
if len(parts) > 2 {
volumemounts = append(volumemounts, api.VolumeMount{Name: partName, ReadOnly: partReadOnly, MountPath: partContainerDir})
} else {
volumemounts = append(volumemounts, api.VolumeMount{Name: partName, ReadOnly: partReadOnly, MountPath: partContainerDir})
}
source := &api.HostPathVolumeSource{
Path: partHostDir,
}
vsource := api.VolumeSource{HostPath: source}
volumes = append(volumes, api.Volume{Name: partName, VolumeSource: vsource})
}
}
return volumemounts, volumes
}
func configureRestartPolicy(name string, service *config.ServiceConfig) api.RestartPolicy {
restartPolicy := api.RestartPolicyAlways
switch service.Restart {
case "", "always":
restartPolicy = api.RestartPolicyAlways
case "no":
restartPolicy = api.RestartPolicyNever
case "on-failure":
restartPolicy = api.RestartPolicyOnFailure
default:
log.Fatalf("Unknown restart policy %s for service %s", service.Restart, name)
}
return restartPolicy
}