forked from kelseyhightower/compose2kube
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
122 lines (108 loc) · 3.35 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
/*
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 (
"encoding/json"
"flag"
"fmt"
"io/ioutil"
"log"
"os"
"path/filepath"
"strconv"
"github.com/docker/libcompose/project"
"k8s.io/kubernetes/pkg/api"
)
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",
ComposeFile: composeFile,
})
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, service := range p.Configs {
rc := &api.ReplicationController{
TypeMeta: api.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,
},
},
},
},
},
}
// Configure the container ports.
var ports []api.ContainerPort
for _, port := range service.Ports {
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: portNumber})
}
rc.Spec.Template.Spec.Containers[0].Ports = ports
// 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)
}
}