forked from kubernetes/kompose
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.go
323 lines (268 loc) · 9.02 KB
/
app.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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
/*
Copyright 2017 The Kubernetes Authors 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 app
import (
"strings"
log "github.com/Sirupsen/logrus"
"github.com/spf13/cobra"
// install kubernetes api
_ "k8s.io/kubernetes/pkg/api/install"
_ "k8s.io/kubernetes/pkg/apis/extensions/install"
// install OpenShift api
_ "github.com/openshift/origin/pkg/build/api/install"
_ "github.com/openshift/origin/pkg/deploy/api/install"
_ "github.com/openshift/origin/pkg/image/api/install"
_ "github.com/openshift/origin/pkg/route/api/install"
"os"
"github.com/kubernetes/kompose/pkg/kobject"
"github.com/kubernetes/kompose/pkg/loader"
"github.com/kubernetes/kompose/pkg/transformer"
"github.com/kubernetes/kompose/pkg/transformer/kubernetes"
"github.com/kubernetes/kompose/pkg/transformer/openshift"
)
const (
// DefaultComposeFile name of the file that kompose will use if no file is explicitly set
DefaultComposeFile = "docker-compose.yml"
// DefaultProvider - provider that will be used if there is no provider was explicitly set
DefaultProvider = "kubernetes"
)
var inputFormat = "compose"
// ValidateFlags validates all command line flags
func ValidateFlags(bundle string, args []string, cmd *cobra.Command, opt *kobject.ConvertOptions) {
// Check to see if the "file" has changed from the default flag value
isFileSet := cmd.Flags().Lookup("file").Changed
if opt.OutFile == "-" {
opt.ToStdout = true
opt.OutFile = ""
}
// Get the provider
provider := cmd.Flags().Lookup("provider").Value.String()
log.Debug("Checking validation of provider %s", provider)
// OpenShift specific flags
deploymentConfig := cmd.Flags().Lookup("deployment-config").Changed
buildRepo := cmd.Flags().Lookup("build-repo").Changed
buildBranch := cmd.Flags().Lookup("build-branch").Changed
// Kubernetes specific flags
chart := cmd.Flags().Lookup("chart").Changed
daemonSet := cmd.Flags().Lookup("daemon-set").Changed
replicationController := cmd.Flags().Lookup("replication-controller").Changed
deployment := cmd.Flags().Lookup("deployment").Changed
// Get the controller
controller := opt.Controller
log.Debug("Checking validation of controller %s", controller)
// Check validations against provider flags
switch {
case provider == "openshift":
if chart {
log.Fatalf("--chart, -c is a Kubernetes only flag")
}
if daemonSet {
log.Fatalf("--daemon-set is a Kubernetes only flag")
}
if replicationController {
log.Fatalf("--replication-controller is a Kubernetes only flag")
}
if deployment {
log.Fatalf("--deployment, -d is a Kubernetes only flag")
}
if controller == "daemonset" || controller == "replicationcontroller" || controller == "deployment" {
log.Fatalf("--controller= daemonset, replicationcontroller or deployment is a Kubernetes only flag")
}
case provider == "kubernetes":
if deploymentConfig {
log.Fatalf("--deployment-config is an OpenShift only flag")
}
if buildRepo {
log.Fatalf("--build-repo is an Openshift only flag")
}
if buildBranch {
log.Fatalf("--build-branch is an Openshift only flag")
}
if controller == "deploymentconfig" {
log.Fatalf("--controller=deploymentConfig is an OpenShift only flag")
}
}
// Standard checks regardless of provider
if len(opt.OutFile) != 0 && opt.ToStdout {
log.Fatalf("Error: --out and --stdout can't be set at the same time")
}
if opt.CreateChart && opt.ToStdout {
log.Fatalf("Error: chart cannot be generated when --stdout is specified")
}
if opt.Replicas < 0 {
log.Fatalf("Error: --replicas cannot be negative")
}
if len(bundle) > 0 {
inputFormat = "bundle"
log.Fatalf("DAB / bundle (--bundle | -b) is no longer supported. See issue: https://github.com/kubernetes/kompose/issues/390")
opt.InputFiles = []string{bundle}
}
if len(bundle) > 0 && isFileSet {
log.Fatalf("Error: 'compose' file and 'dab' file cannot be specified at the same time")
}
if len(args) != 0 {
log.Fatal("Unknown Argument(s): ", strings.Join(args, ","))
}
if opt.GenerateJSON && opt.GenerateYaml {
log.Fatalf("YAML and JSON format cannot be provided at the same time")
}
if opt.Volumes != "persistentVolumeClaim" && opt.Volumes != "emptyDir" {
log.Fatal("Unknown Volume type: ", opt.Volumes, ", possible values are: persistentVolumeClaim and emptyDir")
}
}
// ValidateComposeFile validated the compose file provided for conversion
func ValidateComposeFile(opt *kobject.ConvertOptions) {
if len(opt.InputFiles) == 0 {
// Here docker-compose is the input
opt.InputFiles = []string{"docker-compose.yml"}
_, err := os.Stat("docker-compose.yml")
if err != nil {
log.Debugf("'docker-compose.yml' not found: %v", err)
opt.InputFiles = []string{"docker-compose.yaml"}
_, err = os.Stat("docker-compose.yaml")
if err != nil {
log.Fatalf("No 'docker-compose' file found: %v", err)
}
}
}
}
func validateControllers(opt *kobject.ConvertOptions) {
singleOutput := len(opt.OutFile) != 0 || opt.OutFile == "-" || opt.ToStdout
if opt.Provider == "kubernetes" {
// create deployment by default if no controller has been set
if !opt.CreateD && !opt.CreateDS && !opt.CreateRC && opt.Controller == "" {
opt.CreateD = true
}
if singleOutput {
count := 0
if opt.CreateD {
count++
}
if opt.CreateDS {
count++
}
if opt.CreateRC {
count++
}
if count > 1 {
log.Fatalf("Error: only one kind of Kubernetes resource can be generated when --out or --stdout is specified")
}
}
} else if opt.Provider == "openshift" {
// create deploymentconfig by default if no controller has been set
if !opt.CreateDeploymentConfig {
opt.CreateDeploymentConfig = true
}
if singleOutput {
count := 0
if opt.CreateDeploymentConfig {
count++
}
// Add more controllers here once they are available in OpenShift
// if opt.foo {count++}
if count > 1 {
log.Fatalf("Error: only one kind of OpenShift resource can be generated when --out or --stdout is specified")
}
}
}
}
// Convert transforms docker compose or dab file to k8s objects
func Convert(opt kobject.ConvertOptions) {
validateControllers(&opt)
// loader parses input from file into komposeObject.
l, err := loader.GetLoader(inputFormat)
if err != nil {
log.Fatal(err)
}
komposeObject := kobject.KomposeObject{
ServiceConfigs: make(map[string]kobject.ServiceConfig),
}
komposeObject, err = l.LoadFile(opt.InputFiles)
if err != nil {
log.Fatalf(err.Error())
}
// Get a transformer that maps komposeObject to provider's primitives
t := getTransformer(opt)
// Do the transformation
objects, err := t.Transform(komposeObject, opt)
if err != nil {
log.Fatalf(err.Error())
}
// Print output
err = kubernetes.PrintList(objects, opt)
if err != nil {
log.Fatalf(err.Error())
}
}
// Up brings up deployment, svc.
func Up(opt kobject.ConvertOptions) {
validateControllers(&opt)
// loader parses input from file into komposeObject.
l, err := loader.GetLoader(inputFormat)
if err != nil {
log.Fatal(err)
}
komposeObject := kobject.KomposeObject{
ServiceConfigs: make(map[string]kobject.ServiceConfig),
}
komposeObject, err = l.LoadFile(opt.InputFiles)
if err != nil {
log.Fatalf(err.Error())
}
// Get the transformer
t := getTransformer(opt)
//Submit objects to provider
errDeploy := t.Deploy(komposeObject, opt)
if errDeploy != nil {
log.Fatalf("Error while deploying application: %s", errDeploy)
}
}
// Down deletes all deployment, svc.
func Down(opt kobject.ConvertOptions) {
validateControllers(&opt)
// loader parses input from file into komposeObject.
l, err := loader.GetLoader(inputFormat)
if err != nil {
log.Fatal(err)
}
komposeObject := kobject.KomposeObject{
ServiceConfigs: make(map[string]kobject.ServiceConfig),
}
komposeObject, err = l.LoadFile(opt.InputFiles)
if err != nil {
log.Fatalf(err.Error())
}
// Get the transformer
t := getTransformer(opt)
//Remove deployed application
errUndeploy := t.Undeploy(komposeObject, opt)
if errUndeploy != nil {
for _, err = range errUndeploy {
log.Fatalf("Error while deleting application: %s", err)
}
}
}
// Convenience method to return the appropriate Transformer based on
// what provider we are using.
func getTransformer(opt kobject.ConvertOptions) transformer.Transformer {
var t transformer.Transformer
if opt.Provider == "kubernetes" {
// Create/Init new Kubernetes object with CLI opts
t = &kubernetes.Kubernetes{Opt: opt}
} else {
// Create/Init new OpenShift object that is initialized with a newly
// created Kubernetes object. Openshift inherits from Kubernetes
t = &openshift.OpenShift{Kubernetes: kubernetes.Kubernetes{Opt: opt}}
}
return t
}