This repository has been archived by the owner on Apr 18, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 29
/
common.go
184 lines (161 loc) · 4.4 KB
/
common.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
package cmd
import (
"fmt"
"io/ioutil"
"path/filepath"
"strconv"
"strings"
"github.com/flanksource/commons/console"
"github.com/flanksource/commons/is"
"github.com/flanksource/commons/logger"
"github.com/flanksource/commons/lookup"
"github.com/flanksource/commons/text"
"github.com/flanksource/karina/pkg/phases/harbor"
"github.com/flanksource/karina/pkg/platform"
"github.com/flanksource/karina/pkg/types"
"github.com/imdario/mergo"
"github.com/pkg/errors"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
yaml "gopkg.in/flanksource/yaml.v3"
)
func getPlatform(cmd *cobra.Command) *platform.Platform {
platform := platform.Platform{
PlatformConfig: getConfig(cmd),
}
kubeconfig, _ := cmd.Flags().GetString("kubeconfig")
if kubeconfig != "" {
platform.KubeConfigPath = kubeconfig
}
if err := platform.Init(); err != nil {
log.Fatalf("failed to initialise: %v", err)
}
return &platform
}
func getConfig(cmd *cobra.Command) types.PlatformConfig {
paths, _ := cmd.Flags().GetStringArray("config")
dryRun, _ := cmd.Flags().GetBool("dry-run")
extras, _ := cmd.Flags().GetStringArray("extra")
trace, _ := cmd.Flags().GetBool("trace")
e2e, _ := cmd.Flags().GetBool("e2e")
config := NewConfig(paths, extras)
config.E2E = e2e
config.Trace = trace
config.DryRun = dryRun
return config
}
func NewConfig(paths []string, extras []string) types.PlatformConfig {
splitPaths := []string{}
for _, path := range paths {
splitPaths = append(splitPaths, strings.Split(path, ",")...)
}
if len(paths) == 0 {
log.Fatalf("Must specify at least 1 config")
}
paths = splitPaths
base := types.PlatformConfig{
Source: paths[0],
}
if err := mergeConfigs(&base, paths); err != nil {
log.Fatalf("Failed to merge configs: %v", err)
}
defaultConfig := types.DefaultPlatformConfig()
if err := mergo.Merge(&base, defaultConfig); err != nil {
log.Fatalf("Failed to merge default config, %v", err)
}
ldap := base.Ldap
if ldap.Port == "" {
ldap.Port = "636"
}
if ldap != nil {
base.Ldap = ldap
}
if base.TrustedCA != "" && !is.File(base.TrustedCA) {
base.TrustedCA = text.ToFile(base.TrustedCA, ".pem")
}
for _, extra := range extras {
key := strings.Split(extra, "=")[0]
val := extra[len(key)+1:]
log.Debugf("Looking up %s to set it to: %s", key, val)
value, err := lookup.LookupString(&base, key)
if err != nil {
log.Fatalf("Cannot lookup %s: %v", key, err)
}
log.Infof("Overriding %s %v => %v", key, value, val)
switch value.Interface().(type) {
case string:
value.SetString(val)
case int:
i, err := strconv.ParseInt(val, 10, 64)
if err != nil {
log.Fatalf("Cannot convert %s to int", val)
}
value.SetInt(i)
case bool:
b, err := strconv.ParseBool(val)
if err != nil {
log.Fatalf("Cannot convert %s to a boolean", val)
}
value.SetBool(b)
}
}
harbor.Defaults(&base)
if base.Trace {
data, _ := yaml.Marshal(base)
log.Infof("Using configuration: \n%s\n", console.StripSecrets(string(data)))
}
return base
}
func mergeConfigs(base *types.PlatformConfig, paths []string) error {
for _, path := range paths {
logger.Debugf("Merging %s", path)
cfg := types.PlatformConfig{
Source: path,
}
data, err := ioutil.ReadFile(path)
if err != nil {
return errors.Wrapf(err, "Failed to read config file %s", path)
}
if err := yaml.Unmarshal(data, &cfg); err != nil {
return errors.Wrap(err, "Failed to parse YAML")
}
for node, vm := range cfg.Nodes {
if baseNode, ok := base.Nodes[node]; ok {
if err := mergo.Merge(&baseNode, vm); err != nil {
return errors.Wrapf(err, "Failed to merge nodes %s", node)
}
base.Nodes[node] = baseNode
}
}
if err := mergo.Merge(base, cfg); err != nil {
return errors.Wrapf(err, "Failed to merge in %s", path)
}
for _, config := range cfg.ImportConfigs {
fullPath := filepath.Dir(path) + "/" + config
if err := mergeConfigs(base, []string{fullPath}); err != nil {
return err
}
}
}
return nil
}
func GlobalPreRun(cmd *cobra.Command, args []string) {
level, _ := cmd.Flags().GetCount("loglevel")
switch {
case level > 1:
log.SetLevel(log.TraceLevel)
case level > 0:
log.SetLevel(log.DebugLevel)
default:
log.SetLevel(log.InfoLevel)
}
}
var Render = &cobra.Command{
Use: "render",
Short: "Generate kubeconfig files",
Run: func(cmd *cobra.Command, args []string) {
base := getConfig(cmd)
data, _ := yaml.Marshal(base)
fmt.Println(string(data))
},
}