forked from kelseyhightower/compose2kube
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
165 lines (151 loc) · 4.89 KB
/
main.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
161
162
163
164
165
/*
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
https://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 (
"encoding/json"
"flag"
"fmt"
"io/ioutil"
"log"
"os"
"path/filepath"
"strconv"
"strings"
"github.com/docker/libcompose/project"
"k8s.io/kubernetes/pkg/api"
"k8s.io/kubernetes/pkg/api/unversioned"
)
var (
composeFile string
outputDir string
)
func init() {
flag.StringVar(&composeFile, "compose-file", "docker-compose.yml", "Specify an alternate compose `file`")
flag.StringVar(&outputDir, "output-dir", "output", "Kubernetes configs output `directory`")
}
func main() {
flag.Parse()
p := project.NewProject(&project.Context{
ProjectName: "kube",
ComposeFiles: []string{composeFile},
}, nil, nil)
if err := p.Parse(); err != nil {
log.Fatalf("Failed to parse the compose project from %s: %v", composeFile, err)
}
if err := os.MkdirAll(outputDir, 0755); err != nil {
log.Fatalf("Failed to create the output directory %s: %v", outputDir, err)
}
for _, name := range p.ServiceConfigs.Keys() {
service, _ := p.ServiceConfigs.Get(name)
rc := &api.ReplicationController{
TypeMeta: unversioned.TypeMeta{
Kind: "ReplicationController",
APIVersion: "v1",
},
ObjectMeta: api.ObjectMeta{
Name: name,
Labels: map[string]string{"service": name},
},
Spec: api.ReplicationControllerSpec{
Replicas: 1,
Selector: map[string]string{"service": name},
Template: &api.PodTemplateSpec{
ObjectMeta: api.ObjectMeta{
Labels: map[string]string{"service": name},
},
Spec: api.PodSpec{
Containers: []api.Container{
{
Name: name,
Image: service.Image,
Command: service.Command,
},
},
},
},
},
}
// Configure the container ports.
var ports []api.ContainerPort
for _, port := range service.Ports {
// Check if we have to deal with a mapped port
strings.TrimPrefix(port, "\"")
strings.TrimSuffix(port, "\"")
if strings.Contains(port, ":") {
parts := strings.Split(port, ":")
port = parts[1]
}
portNumber, err := strconv.Atoi(port)
if err != nil {
log.Fatalf("Invalid container port %s for service %s", port, name)
}
ports = append(ports, api.ContainerPort{ContainerPort: int32(portNumber)})
}
rc.Spec.Template.Spec.Containers[0].Ports = ports
// 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})
}
}
rc.Spec.Template.Spec.Containers[0].Env = envs
// Configure the volumes
var volumemounts []api.VolumeMount
var volumes []api.Volume
for _, volumestr := range service.Volumes {
parts := strings.Split(volumestr, ":")
porig := parts[0]
pdest := parts[1]
pname := strings.Replace(porig, "/", "", -1)
if len(parts) > 2 {
volumemounts = append(volumemounts, api.VolumeMount{Name: pname, ReadOnly: true, MountPath: porig })
} else {
volumemounts = append(volumemounts, api.VolumeMount{Name: pname, ReadOnly: false, MountPath: porig })
}
source := &api.HostPathVolumeSource{
Path: pdest,
}
vsource := api.VolumeSource{HostPath: source}
volumes = append(volumes, api.Volume{Name: pname, VolumeSource: vsource})
}
rc.Spec.Template.Spec.Containers[0].VolumeMounts = volumemounts
rc.Spec.Template.Spec.Volumes = volumes
// Configure the container restart policy.
switch service.Restart {
case "", "always":
rc.Spec.Template.Spec.RestartPolicy = api.RestartPolicyAlways
case "no":
rc.Spec.Template.Spec.RestartPolicy = api.RestartPolicyNever
case "on-failure":
rc.Spec.Template.Spec.RestartPolicy = api.RestartPolicyOnFailure
default:
log.Fatalf("Unknown restart policy %s for service %s", service.Restart, name)
}
data, err := json.MarshalIndent(rc, "", " ")
if err != nil {
log.Fatalf("Failed to marshal the replication controller: %v", err)
}
// Save the replication controller for the Docker compose service to the
// configs directory.
outputFileName := fmt.Sprintf("%s-rc.yaml", name)
outputFilePath := filepath.Join(outputDir, outputFileName)
if err := ioutil.WriteFile(outputFilePath, data, 0644); err != nil {
log.Fatalf("Failed to write replication controller %s: %v", outputFileName, err)
}
fmt.Println(outputFilePath)
}
}