-
Notifications
You must be signed in to change notification settings - Fork 1
/
config.go
204 lines (178 loc) · 7.48 KB
/
config.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
// SPDX-License-Identifier: MIT
// SPDX-FileCopyrightText: 2023 Steadybit GmbH
package config
import (
"errors"
"flag"
"github.com/jessevdk/go-flags"
"github.com/rs/zerolog/log"
"k8s.io/apimachinery/pkg/util/yaml"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/rest"
"k8s.io/client-go/tools/clientcmd"
"k8s.io/client-go/util/homedir"
"os"
"path/filepath"
"time"
)
type Config struct {
OutputPath string `yaml:"outputPath" short:"o" long:"output" description:"Path to output directory that will contain the debugging information"`
NoCleanup bool `yaml:"noCleanup" long:"no-cleanup" description:"Skip output directory deletion on command completion?"`
Kubernetes KubernetesConfig `yaml:"kubernetes"`
Platform PlatformConfig `yaml:"platform"`
PlatformPortSplitter PlatformportSplitterConfig `yaml:"platform-port-splitter"`
Outpost OutpostConfig `yaml:"outpost"`
Agent AgentConfig `yaml:"agent"`
Tls Tls `yaml:"tls"`
}
type PlatformConfig struct {
Deployment string `yaml:"deployment" long:"platform-deployment" description:"Kubernetes deployment name of the Steadybit platform"`
Namespace string `yaml:"namespace" long:"platform-namespace" description:"Kubernetes namespace name of the Steadybit platform"`
ExportDatabase bool `yaml:"exportDatabase" long:"export-database" description:"Export database?"`
}
type PlatformportSplitterConfig struct {
Deployment string `yaml:"deployment" long:"platform-splitter-deployment" description:"Kubernetes deployment name of the Steadybit platform splitter"`
Namespace string `yaml:"namespace" long:"platform-splitter-namespace" description:"Kubernetes namespace name of the Steadybit platform splitter"`
}
type OutpostConfig struct {
StatefulSet string `yaml:"statefulSet" long:"outpost-stateful-set" description:"Kubernetes stateful set name of the Steadybit outpost"`
Namespace string `yaml:"namespace" long:"outpost-namespace" description:"Kubernetes namespace name of the Steadybit outpost"`
CurlImage string `yaml:"curlImage" long:"outpost-curl-image" description:"Image to use for connection testing with curl installed"`
WebsocatImage string `yaml:"websocatImage" long:"outpost-websocat-image" description:"Image to use for connection testing with websocat installed"`
TracerouteImage string `yaml:"tracerouteImage" long:"outpost-traceroute-image" description:"Image to use for connection testing with traceroute installed"`
}
type AgentConfig struct {
StatefulSet string `yaml:"statefulSet" long:"agent-stateful-set" description:"Kubernetes stateful set name of the Steadybit agent"`
Namespace string `yaml:"namespace" long:"agent-namespace" description:"Kubernetes namespace name of the Steadybit agent"`
CurlImage string `yaml:"curlImage" long:"agent-curl-image" description:"Image to use for connection testing with curl installed"`
WebsocatImage string `yaml:"websocatImage" long:"agent-websocat-image" description:"Image to use for connection testing with websocat installed"`
TracerouteImage string `yaml:"tracerouteImage" long:"agent-traceroute-image" description:"Image to use for connection testing with traceroute installed"`
}
type Tls struct {
CertChainFile string `yaml:"certChainFile" long:"cert-chain-file" description:"Path to the certificate chain file"`
CertKeyFile string `yaml:"certKeyFile" long:"cert-key-file" description:"Path to the certificate key file"`
}
type KubernetesConfig struct {
KubeConfigPath string `yaml:"kubeConfigPath" long:"kube-config" description:"Path to Kubernetes config"`
}
func (c KubernetesConfig) Client() (*kubernetes.Clientset, error) {
config, err := rest.InClusterConfig()
if err == nil {
log.Debug().Msgf("Steadybit-Debug is running inside a cluster, config found")
} else if errors.Is(err, rest.ErrNotInCluster) {
log.Debug().Msgf("Steadybit-Debug is not running inside a cluster, try local .kube config")
var kubeconfig *string
// use the current context in kubeconfig
if home := homedir.HomeDir(); home != "" {
config, err = clientcmd.BuildConfigFromFlags("", c.KubeConfigPath)
} else {
kubeconfig = flag.String("kubeconfig", "", "absolute path to the kubeconfig file")
flag.Parse()
config, err = clientcmd.BuildConfigFromFlags("", *kubeconfig)
}
}
if err != nil {
log.Debug().Err(err).Msgf("Could not find kubernetes config")
return nil, err
}
config.UserAgent = "steadybit-debug"
config.Timeout = time.Second * 10
clientset, err := kubernetes.NewForConfig(config)
if err != nil {
log.Debug().Err(err).Msgf("Could not create kubernetes client")
return nil, err
}
info, err := clientset.ServerVersion()
if err != nil {
log.Debug().Err(err).Msgf("Could not fetch server version.")
return nil, err
}
log.Debug().Msgf("Cluster connected! Kubernetes Server Version %+v", info)
return clientset, nil
}
func newConfig() Config {
var kubeConfigPath string
if home := homedir.HomeDir(); home != "" {
kubeConfigPath = filepath.Join(home, ".kube", "config")
}
outputPath := os.TempDir()
workingDirectory, err := os.Getwd()
if err == nil {
outputPath = workingDirectory
}
return Config{
OutputPath: outputPath,
NoCleanup: false,
Kubernetes: KubernetesConfig{
KubeConfigPath: kubeConfigPath,
},
Platform: PlatformConfig{
Namespace: "steadybit-platform",
Deployment: "steadybit-platform",
ExportDatabase: false,
},
PlatformPortSplitter: PlatformportSplitterConfig{
Namespace: "steadybit-platform",
Deployment: "platform-port-splitter",
},
Agent: AgentConfig{
Namespace: "steadybit-agent",
StatefulSet: "steadybit-agent",
CurlImage: "curlimages/curl",
WebsocatImage: "mtilson/websocat",
TracerouteImage: "alpine",
},
Tls: Tls{
CertChainFile: "",
CertKeyFile: "",
},
}
}
func loadConfig() Config {
config := newConfig()
path := "steadybit-debug.yml"
fileContent, err := os.ReadFile(path)
if err != nil {
if os.IsNotExist(err) {
log.Info().Msgf("No steadybit-debug configuration file found at path '%s'. Will continue with default configuration.", path)
return config
} else {
log.Err(err).Msgf("Failed to load steadybit-debug configuration file from path '%s'", path)
os.Exit(1)
}
}
err = yaml.Unmarshal(fileContent, &config)
if err != nil {
log.Err(err).Msgf("Failed to parse steadybit-debug configuration from path '%s' as YAML", path)
os.Exit(1)
}
return config
}
func GetConfig() Config {
config := loadConfig()
_, err := flags.Parse(&config)
if err != nil {
os.Exit(1)
}
if config.Outpost.CurlImage != "" {
log.Error().Msg("'outpost-curl-image' config param has been removed. Please use 'agent-curl-image' instead.")
os.Exit(1)
}
if config.Outpost.Namespace != "" {
log.Error().Msg("'outpost-namespace' config param has been removed. Please use 'agent-namespace' instead.")
os.Exit(1)
}
if config.Outpost.StatefulSet != "" {
log.Error().Msg("'outpost-stateful-set' config param has been removed. Please use 'agent-outpost-stateful-set' instead.")
os.Exit(1)
}
if config.Outpost.WebsocatImage != "" {
log.Error().Msg("'outpost-websocat-image' config param has been removed. Please use 'agent-websocat-image' instead.")
os.Exit(1)
}
if config.Outpost.TracerouteImage != "" {
log.Error().Msg("'outpost-traceroute-image' config param has been removed. Please use 'agent-traceroute-image' instead.")
os.Exit(1)
}
return config
}