-
Notifications
You must be signed in to change notification settings - Fork 772
/
Copy pathutils.go
484 lines (417 loc) · 13.5 KB
/
utils.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
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
/*
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 transformer
import (
"fmt"
"os"
"os/exec"
"path"
"path/filepath"
"regexp"
"strings"
dockerlib "github.com/fsouza/go-dockerclient"
"github.com/kubernetes/kompose/pkg/kobject"
"github.com/kubernetes/kompose/pkg/utils/docker"
"github.com/kubernetes/kompose/pkg/version"
"github.com/pkg/errors"
log "github.com/sirupsen/logrus"
api "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
)
// Selector used as labels and selector
const Selector = "io.kompose.service"
// Exists returns true if a file path exists.
// Otherwise, returns false.
func Exists(p string) bool {
_, err := os.Stat(p)
return err == nil
}
// CreateOutFile creates the file to write to if --out is specified
func CreateOutFile(out string) (*os.File, error) {
if len(out) == 0 {
return nil, nil
}
// Creates directories if "out" contains nonexistent directories.
if dir := filepath.Dir(out); !Exists(dir) {
if err := os.MkdirAll(dir, os.ModePerm); err != nil {
return nil, errors.Wrap(err, "failed to create directories")
}
}
f, err := os.Create(out)
if err != nil {
return nil, errors.Wrap(err, "failed to create file, os.Create failed")
}
return f, nil
}
// ParseVolume parses a given volume, which might be [name:][host:]container[:access_mode]
func ParseVolume(volume string) (name, host, container, mode string, err error) {
if containWindowsPath(volume) {
return parseWindowsVolume(volume)
}
return parseVolume(volume)
}
func parseVolume(volume string) (name, host, container, mode string, err error) {
separator := ":"
// Parse based on ":"
volumeStrings := strings.Split(volume, separator)
if len(volumeStrings) == 0 {
return
}
// Set name if existed
if !isPath(volumeStrings[0]) {
name = volumeStrings[0]
volumeStrings = volumeStrings[1:]
}
// Check if *anything* has been passed
if len(volumeStrings) == 0 {
err = fmt.Errorf("invalid volume format: %s", volume)
return
}
// Get the last ":" passed which is presumingly the "access mode"
possibleAccessMode := volumeStrings[len(volumeStrings)-1]
// Check to see if :Z or :z exists. We do not support SELinux relabeling at the moment.
// See https://github.com/kubernetes/kompose/issues/176
// Otherwise, check to see if "rw" or "ro" has been passed
if possibleAccessMode == "z" || possibleAccessMode == "Z" {
log.Warnf("Volume mount \"%s\" will be mounted without labeling support. :z or :Z not supported", volume)
mode = ""
volumeStrings = volumeStrings[:len(volumeStrings)-1]
} else if possibleAccessMode == "rw" || possibleAccessMode == "ro" {
mode = possibleAccessMode
volumeStrings = volumeStrings[:len(volumeStrings)-1]
}
// Check the volume format as well as host
container = volumeStrings[len(volumeStrings)-1]
volumeStrings = volumeStrings[:len(volumeStrings)-1]
if len(volumeStrings) == 1 {
host = volumeStrings[0]
}
if !isPath(container) || (len(host) > 0 && !isPath(host)) || len(volumeStrings) > 1 {
err = fmt.Errorf("invalid volume format: %s", volume)
return
}
return
}
// parseVolume parses window volume.
// example: windows host mount to windows container
// volume = dataVolumeName:C:\Users\Data:D:\config:rw
// it can be parsed:
// name=dataVolumeName, host=C:\Users\Data, container=D:\config, mode=rw
// example: windows host mount to linux container
// volume = dataVolumeName:C:\Users\Data:/etc/config:rw
// it can be parsed:
// name=dataVolumeName, host=C:\Users\Data, container=/etc/config, mode=rw
func parseWindowsVolume(volume string) (name, host, container, mode string, err error) {
var (
buffer, volumePaths []string
volumeStrings = strings.Split(volume, ":")
)
// extract path and leave order
for _, fragment := range volumeStrings {
switch {
case containWindowsPath(fragment):
if len(buffer) == 0 {
err = fmt.Errorf("invalid windows volume %s", volume)
return
}
driveLetter := buffer[len(buffer)-1]
if len(driveLetter) != 1 {
err = fmt.Errorf("invalid windows volume %s", volume)
return
}
volumePaths = append(volumePaths, driveLetter+":"+fragment)
buffer = buffer[:len(buffer)-1]
case isPath(fragment):
volumePaths = append(volumePaths, fragment)
default:
buffer = append(buffer, fragment)
}
}
// set name and mode if exist
if len(buffer) == 1 {
if volumeStrings[0] == buffer[0] {
name = buffer[0]
} else if volumeStrings[len(volumeStrings)-1] == buffer[0] {
mode = buffer[0]
}
} else if len(buffer) == 2 {
name = buffer[0]
mode = buffer[1]
} else if len(buffer) > 2 {
err = fmt.Errorf("invalid windows volume %s", volume)
return
}
// Support in pass time
// Check to see if :Z or :z exists. We do not support SELinux relabeling at the moment.
// See https://github.com/kubernetes/kompose/issues/176
// Otherwise, check to see if "rw" or "ro" has been passed
if mode == "z" || mode == "Z" {
log.Warnf("Volume mount \"%s\" will be mounted without labeling support. :z or :Z not supported", volume)
mode = ""
}
// Set host and container if exist
if len(volumePaths) == 1 {
container = volumePaths[0]
} else if len(volumePaths) == 2 {
host = volumePaths[0]
container = volumePaths[1]
} else {
err = fmt.Errorf("invalid windows volume %s", volume)
return
}
return
}
// containWindowsPath check whether it contains windows path.
// windows path's separator is "\"
func containWindowsPath(substring string) bool {
return strings.Contains(substring, "\\")
}
// ParseIngressPath parse path for ingress.
// eg. example.com/org -> example.com org
func ParseIngressPath(url string) (string, string) {
if strings.Contains(url, "/") {
splits := strings.Split(url, "/")
return splits[0], "/" + strings.Join(splits[1:], "/")
}
return url, ""
}
func isPath(substring string) bool {
return strings.Contains(substring, "/") || substring == "."
}
// ConfigLabels configures label name alone
func ConfigLabels(name string) map[string]string {
return map[string]string{Selector: name}
}
// ConfigLabelsWithNetwork configures label and add Network Information in labels
func ConfigLabelsWithNetwork(name string, net []string) map[string]string {
labels := map[string]string{}
labels[Selector] = name
for _, n := range net {
labels["io.kompose.network/"+n] = "true"
}
return labels
//return map[string]string{Selector: name, "Network": net}
}
// ConfigAllLabels creates labels with service nam and deploy labels
func ConfigAllLabels(name string, service *kobject.ServiceConfig) map[string]string {
base := ConfigLabels(name)
if service.DeployLabels != nil {
for k, v := range service.DeployLabels {
base[k] = v
}
}
return base
}
// ConfigAnnotations configures annotations
func ConfigAnnotations(service kobject.ServiceConfig) map[string]string {
annotations := map[string]string{}
if !service.WithKomposeAnnotation {
return annotations
}
for key, value := range service.Annotations {
annotations[key] = value
}
annotations["kompose.cmd"] = strings.Join(os.Args, " ")
versionCmd := exec.Command("kompose", "version")
out, err := versionCmd.Output()
if err != nil {
errors.Wrap(err, "Failed to get kompose version")
}
annotations["kompose.version"] = strings.Trim(string(out), " \n")
// If the version is blank (couldn't retrieve the kompose version for whatever reason)
if annotations["kompose.version"] == "" {
annotations["kompose.version"] = version.VERSION + " (" + version.GITCOMMIT + ")"
}
return annotations
}
// Print either prints to stdout or to file/s
func Print(name, path string, trailing string, data []byte, toStdout, generateJSON bool, f *os.File, provider string) (string, error) {
file := ""
// TODO: we should refactor / change this hack in the future once we have a better solution
re := regexp.MustCompile(`(?s)status:.*`)
data = re.ReplaceAll(data, nil)
if generateJSON {
file = fmt.Sprintf("%s-%s.json", name, trailing)
} else {
file = fmt.Sprintf("%s-%s.yaml", name, trailing)
}
if toStdout {
fmt.Fprintf(os.Stdout, "%s\n", string(data))
return "", nil
} else if f != nil {
// Write all content to a single file f
if _, err := f.WriteString(fmt.Sprintf("%s\n", string(data))); err != nil {
return "", errors.Wrap(err, "f.WriteString failed, Failed to write %s to file: "+trailing)
}
f.Sync()
} else {
// Write content separately to each file
file = filepath.Join(path, file)
if err := os.WriteFile(file, data, 0644); err != nil {
return "", errors.Wrap(err, "Failed to write %s: "+trailing)
}
log.Printf("%s file %q created", formatProviderName(provider), file)
}
return file, nil
}
// If Openshift, change to OpenShift!
func formatProviderName(provider string) string {
if strings.EqualFold(provider, "openshift") {
return "OpenShift"
} else if strings.EqualFold(provider, "kubernetes") {
return "Kubernetes"
}
return provider
}
// EnvSort struct
type EnvSort []api.EnvVar
// Len returns the number of elements in the collection.
func (env EnvSort) Len() int {
return len(env)
}
// Less returns whether the element with index i should sort before
// the element with index j.
func (env EnvSort) Less(i, j int) bool {
return env[i].Name < env[j].Name
}
// Swap swaps the elements with indexes i and j.
func (env EnvSort) Swap(i, j int) {
env[i], env[j] = env[j], env[i]
}
// GetComposeFileDir returns compose file directory
func GetComposeFileDir(inputFiles []string) (string, error) {
// Check if input files are specified
if len(inputFiles) <= 0 {
return "", errors.New("No input files specified")
}
// Lets assume all the docker-compose files are in the same directory
inputFile, err := filepath.Abs(inputFiles[0])
if err != nil {
return "", err
}
log.Debugf("Compose file dir: %s", filepath.Dir(inputFile))
return filepath.Dir(inputFile), nil
}
// BuildDockerImage builds docker image
func BuildDockerImage(service kobject.ServiceConfig, name string) error {
wd, err := os.Getwd()
if err != nil {
return err
}
log.Debug("Build image working dir is: ", wd)
log.Debug("Build image service build is: ", service.Build)
// Get the appropriate image source and name
imagePath := service.Build
if !path.IsAbs(service.Build) {
imagePath = path.Join(wd, service.Build)
}
log.Debugf("Build image context is: %s", imagePath)
if _, err := os.Stat(imagePath); err != nil {
return errors.Wrapf(err, "%s is not a valid path for building image %s. Check if this dir exists.", service.Build, name)
}
imageName := name
if service.Image != "" {
imageName = service.Image
}
buildargs := []dockerlib.BuildArg{}
for envName, envValue := range service.BuildArgs {
var value string
if envValue == nil {
value = os.Getenv(envName)
} else {
value = *envValue
}
buildargs = append(buildargs, dockerlib.BuildArg{Name: envName, Value: value})
}
// Connect to the Docker client
client, err := docker.Client()
if err != nil {
return err
}
// Use the build struct function to build the image
// Build the image!
build := docker.Build{Client: *client}
err = build.BuildImage(imagePath, imageName, service.Dockerfile, buildargs)
if err != nil {
return err
}
return nil
}
// PushDockerImageWithOpt pushes docker image
func PushDockerImageWithOpt(service kobject.ServiceConfig, serviceName string, opt kobject.ConvertOptions) error {
if !opt.PushImage {
// Don't do anything if registry is specified but push is disabled, just WARN about it
if opt.PushImageRegistry != "" {
log.Warnf("Push image registry '%s' is specified but push image is disabled, skipping pushing to repository", opt.PushImageRegistry)
}
return nil
}
log.Infof("Push image is enabled. Attempting to push image '%s'", service.Image)
// Don't do anything if service.Image is blank, but at least WARN about it
// else, let's push the image
if service.Image == "" {
log.Warnf("No image name has been passed for service %s, skipping pushing to repository", serviceName)
return nil
}
image, err := docker.ParseImage(service.Image, opt.PushImageRegistry)
if err != nil {
return err
}
client, err := docker.Client()
if err != nil {
return err
}
if opt.PushImageRegistry != "" {
log.Info("Push image registry is specified. Tag the image into registry firstly.")
tag := docker.Tag{Client: *client}
err = tag.TagImage(image)
if err != nil {
return err
}
}
push := docker.Push{Client: *client}
err = push.PushImage(image)
if err != nil {
return err
}
return nil
}
// CreateNamespace creates a Kubernetes namespace, which can be used in both:
// Openshift and Kubernetes
func CreateNamespace(namespace string) *api.Namespace {
return &api.Namespace{
TypeMeta: metav1.TypeMeta{
Kind: "Namespace",
APIVersion: "v1",
},
ObjectMeta: metav1.ObjectMeta{
Name: namespace,
},
}
}
// AssignNamespaceToObjects will add the namespace metadata to each object
func AssignNamespaceToObjects(objs *[]runtime.Object, namespace string) {
ns := "default"
if namespace != "" {
ns = namespace
}
var result []runtime.Object
for _, obj := range *objs {
if us, ok := obj.(metav1.Object); ok {
us.SetNamespace(ns)
}
result = append(result, obj)
}
*objs = result
}