/
get.go
253 lines (202 loc) · 6.67 KB
/
get.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
package configutil
import (
"os"
"sync"
"github.com/juju/errors"
"github.com/covexo/devspace/pkg/util/kubeconfig"
"github.com/covexo/devspace/pkg/util/log"
"k8s.io/client-go/tools/clientcmd"
"github.com/covexo/devspace/pkg/devspace/config/v1"
)
//ConfigInterface defines the pattern of every config
type ConfigInterface interface{}
const configGitignore = `logs/
overwrite.yaml
generated.yaml
`
// DefaultConfigPath is the default config path to use
const DefaultConfigPath = ".devspace/config.yaml"
// DefaultOverwriteConfigPath is the default overwrite config path to use
const DefaultOverwriteConfigPath = ".devspace/overwrite.yaml"
// ConfigPath is the path for the main config
var ConfigPath = DefaultConfigPath
// OverwriteConfigPath specifies where the override.yaml lies
var OverwriteConfigPath = DefaultOverwriteConfigPath
// DefaultDevspaceServiceName is the name of the initial default service
const DefaultDevspaceServiceName = "default"
// DefaultDevspaceDeploymentName is the name of the initial default deployment
const DefaultDevspaceDeploymentName = "devspace-default"
// CurrentConfigVersion has the value of the current config version
const CurrentConfigVersion = "v1alpha1"
// Global config vars
var config *v1.Config // merged config
var configRaw *v1.Config // config from .devspace/config.yaml
var overwriteConfig *v1.Config // overwrite config from .devspace/config.yaml
var defaultConfig *v1.Config // default config values
// Thread-safety helper
var getConfigOnce sync.Once
var setDefaultsOnce sync.Once
// ConfigExists checks whether the yaml file for the config exists
func ConfigExists() (bool, error) {
_, err := os.Stat(ConfigPath)
if os.IsNotExist(err) {
return false, nil
} else if err != nil {
return false, err
}
return true, nil
}
// InitConfig initializes the config objects
func InitConfig() *v1.Config {
getConfigOnce.Do(func() {
config = makeConfig()
overwriteConfig = makeConfig()
configRaw = makeConfig()
overwriteConfig = makeConfig()
defaultConfig = makeConfig()
})
return config
}
// GetConfig returns the config merged from .devspace/config.yaml and .devspace/overwrite.yaml
func GetConfig() *v1.Config {
GetConfigWithoutDefaults()
SetDefaultsOnce()
return config
}
// GetConfigWithoutDefaults returns the config without setting the default values
func GetConfigWithoutDefaults() *v1.Config {
getConfigOnce.Do(func() {
config = makeConfig()
overwriteConfig = makeConfig()
configRaw = makeConfig()
overwriteConfig = makeConfig()
defaultConfig = makeConfig()
err := loadConfig(configRaw, ConfigPath)
if err != nil {
log.Fatalf("Loading config: %v", err)
}
if configRaw.Version == nil || *configRaw.Version != CurrentConfigVersion {
log.Fatal("Your config is out of date. Please run `devspace init -r` to update your config")
}
//ignore error as overwrite.yaml is optional
loadConfig(overwriteConfig, OverwriteConfigPath)
Merge(&config, configRaw, false)
Merge(&config, overwriteConfig, true)
})
return config
}
// GetOverwriteConfig returns the config retrieved from .devspace/overwrite.yaml
func GetOverwriteConfig() *v1.Config {
GetConfig()
return overwriteConfig
}
// SetDefaultsOnce ensures that specific values are set in the config
func SetDefaultsOnce() {
setDefaultsOnce.Do(func() {
defaultNamespace, err := GetDefaultNamespace(config)
if err != nil {
log.Fatalf("Error retrieving default namespace: %v", err)
}
// Initialize Namespaces
if config.DevSpace != nil {
needTiller := config.InternalRegistry != nil
if config.DevSpace.Deployments != nil {
for index, deployConfig := range *config.DevSpace.Deployments {
if deployConfig.Name == nil {
log.Fatalf("Error in config: Unnamed deployment at index %d", index)
}
if deployConfig.Namespace == nil {
deployConfig.Namespace = String("")
}
if deployConfig.Helm != nil {
needTiller = true
}
}
}
if config.DevSpace.Services != nil {
for index, serviceConfig := range *config.DevSpace.Services {
if serviceConfig.Name == nil {
log.Fatalf("Error in config: Unnamed service at index %d", index)
}
if serviceConfig.Namespace == nil {
serviceConfig.Namespace = String("")
}
}
}
if config.DevSpace.Sync != nil {
for _, syncPath := range *config.DevSpace.Sync {
if syncPath.Namespace == nil {
syncPath.Namespace = String("")
}
}
}
if config.DevSpace.Ports != nil {
for _, portForwarding := range *config.DevSpace.Ports {
if portForwarding.Namespace == nil {
portForwarding.Namespace = String("")
}
}
}
if needTiller && config.Tiller == nil {
defaultConfig.Tiller = &v1.TillerConfig{
Namespace: &defaultNamespace,
}
config.Tiller = defaultConfig.Tiller
}
}
if config.Images != nil {
for _, buildConfig := range *config.Images {
if buildConfig.Build != nil && buildConfig.Build.Kaniko != nil {
if buildConfig.Build.Kaniko.Namespace == nil {
buildConfig.Build.Kaniko.Namespace = String("")
}
}
}
}
if config.InternalRegistry != nil {
defaultConfig.InternalRegistry = &v1.InternalRegistryConfig{
Namespace: &defaultNamespace,
}
config.InternalRegistry.Namespace = defaultConfig.InternalRegistry.Namespace
}
})
}
// GetDefaultNamespace retrieves the default namespace where to operate in, either from devspace config or kube config
func GetDefaultNamespace(config *v1.Config) (string, error) {
if config.Cluster != nil && config.Cluster.Namespace != nil {
return *config.Cluster.Namespace, nil
}
if config.Cluster == nil || config.Cluster.APIServer == nil {
kubeConfig, err := kubeconfig.ReadKubeConfig(clientcmd.RecommendedHomeFile)
if err != nil {
return "", err
}
activeContext := kubeConfig.CurrentContext
if config.Cluster.KubeContext != nil {
activeContext = *config.Cluster.KubeContext
}
if kubeConfig.Contexts[activeContext] != nil && kubeConfig.Contexts[activeContext].Namespace != "" {
return kubeConfig.Contexts[activeContext].Namespace, nil
}
}
return "default", nil
}
// GetService returns the service referenced by serviceName
func GetService(serviceName string) (*v1.ServiceConfig, error) {
if config.DevSpace.Services != nil {
for _, service := range *config.DevSpace.Services {
if *service.Name == serviceName {
return service, nil
}
}
}
return nil, errors.New("Unable to find service: " + serviceName)
}
// AddService adds a service to the config
func AddService(service *v1.ServiceConfig) error {
if config.DevSpace.Services == nil {
config.DevSpace.Services = &[]*v1.ServiceConfig{}
}
*config.DevSpace.Services = append(*config.DevSpace.Services, service)
return nil
}