-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
129 lines (112 loc) · 3.71 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
package main
import (
"fmt"
"os"
"time"
composeLoader "github.com/compose-spec/compose-go/loader"
composeTypes "github.com/compose-spec/compose-go/types"
"github.com/joho/godotenv"
"github.com/sirupsen/logrus"
"github.com/spf13/pflag"
"github.com/vshn/k8ify/internal"
"github.com/vshn/k8ify/pkg/converter"
"github.com/vshn/k8ify/pkg/ir"
"github.com/vshn/k8ify/pkg/provider"
"github.com/vshn/k8ify/pkg/util"
)
var (
defaultConfig = internal.Config{
OutputDir: "manifests",
Env: "dev",
Ref: "",
}
modifiedImages internal.ModifiedImagesFlag
shellEnvFiles internal.ShellEnvFilesFlag
pflagInitialized = false
)
func InitPflag() {
// Main() may be called multiple times from tests, hence this kludge
if !pflagInitialized {
pflag.Var(&modifiedImages, "modified-image", "Image that has been modified during the build. Can be repeated.")
pflag.Var(&shellEnvFiles, "shell-env-file", "Shell environment file ('key=value' format) to be used in addition to the current shell environment. Can be repeated.")
pflagInitialized = true
}
}
func main() {
code := Main(os.Args)
os.Exit(code)
}
func Main(args []string) int {
InitPflag()
err := pflag.CommandLine.Parse(args[1:])
if err != nil {
logrus.Error(err)
return 1
}
plainArgs := pflag.Args()
config := defaultConfig // this code may run multiple times during testing, thus we can't modify the defaults and must create a copy
if len(plainArgs) > 0 {
config.Env = plainArgs[0]
}
if len(plainArgs) > 1 {
config.Ref = plainArgs[1]
}
if len(config.ConfigFiles) == 0 {
config.ConfigFiles = []string{"compose.yml", "docker-compose.yml", "compose-" + config.Env + ".yml", "docker-compose-" + config.Env + ".yml"}
}
// Load the additional shell environment files. This will merge everything into the existing shell environment and
// all values can later be retrieved using os.Environ()
for _, shellEnvFile := range shellEnvFiles.Values {
err := godotenv.Load(shellEnvFile)
if err != nil {
logrus.Error(err)
return 1
}
}
composeConfigFiles := []composeTypes.ConfigFile{}
for _, configFile := range config.ConfigFiles {
if _, err := os.Stat(configFile); err == nil {
composeConfigFiles = append(composeConfigFiles, composeTypes.ConfigFile{
Filename: configFile,
})
}
}
env := util.GetEnv()
for _, key := range []string{"_ref_", "_secretRef_", "_fieldRef_"} {
if _, ok := env[key]; ok {
logrus.Errorf("The environment variable '%s' must not be defined as it is needed for internal purposes", key)
return 1
}
}
env["_ref_"] = converter.SecretRefMagic
env["_secretRef_"] = converter.SecretRefMagic
env["_fieldRef_"] = converter.FieldRefMagic
configDetails := composeTypes.ConfigDetails{
ConfigFiles: composeConfigFiles,
Environment: env,
}
project, err := composeLoader.Load(configDetails)
if err != nil {
logrus.Error(err)
return 1
}
inputs := ir.FromCompose(project)
internal.ComposeServicePrecheck(inputs)
internal.VolumesPrecheck(inputs)
internal.DomainLengthPrecheck(inputs)
objects := converter.Objects{}
for _, service := range inputs.Services {
objects = objects.Append(converter.ComposeServiceToK8s(config.Ref, service, inputs.Volumes, inputs.TargetCfg))
}
forceRestartAnnotation := make(map[string]string)
forceRestartAnnotation["k8ify.restart-trigger"] = fmt.Sprintf("%d", time.Now().Unix())
converter.PatchDeployments(objects.Deployments, modifiedImages.Values, forceRestartAnnotation)
converter.PatchStatefulSets(objects.StatefulSets, modifiedImages.Values, forceRestartAnnotation)
objects = provider.PatchEncryptedVolumeSchemeAppuioCloudscale(inputs.TargetCfg, config, objects)
err = internal.WriteManifests(config.OutputDir, objects)
if err != nil {
logrus.Error(err)
return 1
}
return 0
}