-
Notifications
You must be signed in to change notification settings - Fork 215
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Pavel Okhlopkov <[email protected]>
- Loading branch information
Pavel Okhlopkov
committed
Dec 5, 2024
1 parent
7cbd786
commit 6e89b47
Showing
10 changed files
with
237 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,226 @@ | ||
package app | ||
|
||
import ( | ||
"fmt" | ||
"time" | ||
|
||
env "github.com/caarlos0/env/v11" | ||
|
||
"github.com/deckhouse/deckhouse/pkg/log" | ||
) | ||
|
||
type appConfig struct { | ||
HooksDir string `env:"HOOKS_DIR" envDefault:"hooks"` | ||
TmpDir string `env:"TMP_DIR" envDefault:"/tmp/shell-operator"` | ||
ListenAddress string `env:"LISTEN_ADDRESS" envDefault:"0.0.0.0"` | ||
ListenPort string `env:"LISTEN_PORT" envDefault:"9115"` | ||
PrometheusMetricsPrefix string `env:"PROMETHEUS_METRICS_PREFIX" envDefault:"shell_operator_"` | ||
// unused? | ||
HooksMetricsListenPort string `env:"HOOK_METRICS_LISTEN_PORT" envDefault:"9115"` | ||
Namespace string `env:"Namespace"` | ||
} | ||
|
||
func newAppConfig() *appConfig { | ||
return &appConfig{} | ||
} | ||
|
||
type debugConfig struct { | ||
HTTPServerAddress string `env:"HTTP_SERVER_ADDR"` | ||
KeepTemporaryFiles string `env:"KEEP_TMP_FILES" envDefault:"no"` | ||
KubernetesAPI bool `env:"KUBERNETES_API" envDefault:"false"` | ||
UnixSocket string `env:"UNIX_SOCKET" envDefault:"/var/run/shell-operator/debug.socket"` | ||
} | ||
|
||
func newDebugConfig() *debugConfig { | ||
return &debugConfig{} | ||
} | ||
|
||
type jqConfig struct { | ||
LibraryPath string `env:"LIBRARY_PATH"` | ||
} | ||
|
||
func newJQConfig() *jqConfig { | ||
return &jqConfig{} | ||
} | ||
|
||
type kubeConfig struct { | ||
// Settings for Kubernetes connection. | ||
ContextName string `env:"CONTEXT"` | ||
ConfigPath string `env:"CONFIG"` | ||
ServerAddress string `env:"SERVER"` | ||
// Rate limit settings for 'main' kube client | ||
ClientQPS float32 `env:"CLIENT_QPS" envDefault:"5"` | ||
ClientBurst int `env:"CLIENT_BURST" envDefault:"10"` | ||
} | ||
|
||
func newKubeConfig() *kubeConfig { | ||
return &kubeConfig{} | ||
} | ||
|
||
type objectPatcherConfig struct { | ||
// Settings for 'object_patcher' kube client | ||
KubeClientQPS float32 `env:"KUBE_CLIENT_QPS" envDefault:"4"` | ||
KubeClisntBurst int `env:"KUBE_CLIENT_BURST" envDefault:"10"` | ||
KubeClientTimeout time.Duration `env:"KUBE_CLIENT_TIMEOUT" envDefault:"10s"` | ||
} | ||
|
||
func newObjectPatcherConfig() *objectPatcherConfig { | ||
return &objectPatcherConfig{} | ||
} | ||
|
||
type validatingWebhookConfig struct { | ||
ConfigurationName string `env:"CONFIGURATION_NAME" envDefault:"shell-operator-hooks"` | ||
ServiceName string `env:"SERVICE_NAME" envDefault:"shell-operator-validating-svc"` | ||
ServerCert string `env:"SERVER_CERT" envDefault:"/validating-certs/tls.crt"` | ||
ServerKey string `env:"SERVER_KEY" envDefault:"/validating-certs/tls.key"` | ||
CA string `env:"CA" envDefault:"/validating-certs/ca.crt"` | ||
// check separator? | ||
ClientCA []string `env:"CLIENT_CA" envSeparator:","` | ||
// enum "Fail" || "Ignore" | ||
FailurePolicy string `env:"FAILURE_POLICY" envDefault:"Fail"` | ||
ListenPort string `env:"LISTEN_PORT" envDefault:"9680"` | ||
ListenAddress string `env:"LISTEN_ADDRESS" envDefault:"0.0.0.0"` | ||
} | ||
|
||
func newValidatingWebhookConfig() *validatingWebhookConfig { | ||
return &validatingWebhookConfig{} | ||
} | ||
|
||
type conversionWebhookConfig struct { | ||
ServiceName string `env:"SERVICE_NAME" envDefault:"shell-operator-conversion-svc"` | ||
ServerCert string `env:"SERVER_CERT" envDefault:"/conversion-certs/tls.crt"` | ||
ServerKey string `env:"SERVER_KEY" envDefault:"/conversion-certs/tls.key"` | ||
CA string `env:"CA" envDefault:"/conversion-certs/ca.crt"` | ||
// check separator? | ||
ClientCA []string `env:"CLIENT_CA" envSeparator:","` | ||
ListenPort string `env:"LISTEN_PORT" envDefault:"9681"` | ||
ListenAddress string `env:"LISTEN_ADDRESS" envDefault:"0.0.0.0"` | ||
} | ||
|
||
func newConversionWebhookConfig() *conversionWebhookConfig { | ||
return &conversionWebhookConfig{} | ||
} | ||
|
||
type logConfig struct { | ||
Level string `env:"LEVEL" envDefault:"info"` | ||
Type string `env:"TYPE" envDefault:"text"` | ||
NoTime bool `env:"NO_TIME" envDefault:"false"` | ||
ProxyHookJson bool `env:"PROXY_HOOK_JSON" envDefault:"false"` | ||
} | ||
|
||
func newLogConfig() *logConfig { | ||
return &logConfig{} | ||
} | ||
|
||
type Config struct { | ||
AppConfig *appConfig `envPrefix:"SHELL_OPERATOR_"` | ||
JQConfig *jqConfig `envPrefix:"JQ_"` | ||
KubeConfig *kubeConfig `envPrefix:"KUBE_"` | ||
ObjectPatcherConfig *objectPatcherConfig `envPrefix:"OBJECT_PATCHER_"` | ||
ValidatingWebhookConfig *validatingWebhookConfig `envPrefix:"VALIDATING_WEBHOOK_"` | ||
ConversionWebhookConfig *conversionWebhookConfig `envPrefix:"CONVERSION_WEBHOOK_"` | ||
|
||
DebugConfig *debugConfig `envPrefix:"DEBUG_"` | ||
|
||
LogConfig *logConfig `envPrefix:"LOG_"` | ||
LogLevel log.Level `env:"-"` | ||
} | ||
|
||
func NewConfig() *Config { | ||
return &Config{ | ||
AppConfig: newAppConfig(), | ||
JQConfig: newJQConfig(), | ||
KubeConfig: newKubeConfig(), | ||
ObjectPatcherConfig: newObjectPatcherConfig(), | ||
ValidatingWebhookConfig: newValidatingWebhookConfig(), | ||
ConversionWebhookConfig: newConversionWebhookConfig(), | ||
DebugConfig: newDebugConfig(), | ||
LogConfig: newLogConfig(), | ||
} | ||
} | ||
|
||
func (cfg *Config) Parse() error { | ||
opts := env.Options{ | ||
Prefix: "", | ||
} | ||
|
||
err := env.ParseWithOptions(cfg, opts) | ||
if err != nil { | ||
return fmt.Errorf("failed to parse config: %w", err) | ||
} | ||
|
||
cfg.LogLevel = log.LogLevelFromStr(cfg.LogConfig.Level) | ||
|
||
return nil | ||
} | ||
|
||
func (cfg *Config) SetupGlobalVars() { | ||
setIfNotEmpty(&HooksDir, cfg.AppConfig.HooksDir) | ||
setIfNotEmpty(&TempDir, cfg.AppConfig.TmpDir) | ||
setIfNotEmpty(&ListenAddress, cfg.AppConfig.ListenAddress) | ||
setIfNotEmpty(&ListenPort, cfg.AppConfig.ListenPort) | ||
setIfNotEmpty(&PrometheusMetricsPrefix, cfg.AppConfig.PrometheusMetricsPrefix) | ||
setIfNotEmpty(&Namespace, cfg.AppConfig.Namespace) | ||
|
||
setIfNotEmpty(&DebugHttpServerAddr, cfg.DebugConfig.HTTPServerAddress) | ||
setIfNotEmpty(&DebugKeepTmpFilesVar, cfg.DebugConfig.KeepTemporaryFiles) | ||
setIfNotEmpty(&DebugKeepTmpFiles, cfg.DebugConfig.KeepTemporaryFiles == "yes") | ||
setIfNotEmpty(&DebugKubernetesAPI, cfg.DebugConfig.KubernetesAPI) | ||
setIfNotEmpty(&DebugUnixSocket, cfg.DebugConfig.UnixSocket) | ||
|
||
fmt.Println("LOL UNIX SOCKET") | ||
fmt.Println(DebugUnixSocket) | ||
fmt.Println("LOL UNIX SOCKET FROM CFG") | ||
fmt.Println(cfg.DebugConfig.UnixSocket) | ||
|
||
setIfNotEmpty(&JqLibraryPath, cfg.JQConfig.LibraryPath) | ||
|
||
setIfNotEmpty(&KubeContext, cfg.KubeConfig.ContextName) | ||
setIfNotEmpty(&KubeConfig, cfg.KubeConfig.ConfigPath) | ||
setIfNotEmpty(&KubeServer, cfg.KubeConfig.ServerAddress) | ||
setIfNotEmpty(&KubeClientQps, cfg.KubeConfig.ClientQPS) | ||
setIfNotEmpty(&KubeClientBurst, cfg.KubeConfig.ClientBurst) | ||
|
||
setIfNotEmpty(&ObjectPatcherKubeClientQps, cfg.ObjectPatcherConfig.KubeClientQPS) | ||
setIfNotEmpty(&ObjectPatcherKubeClientBurst, cfg.ObjectPatcherConfig.KubeClisntBurst) | ||
setIfNotEmpty(&ObjectPatcherKubeClientTimeout, cfg.ObjectPatcherConfig.KubeClientTimeout) | ||
|
||
setIfNotEmpty(&LogLevel, cfg.LogConfig.Level) | ||
setIfNotEmpty(&LogNoTime, cfg.LogConfig.NoTime) | ||
setIfNotEmpty(&LogType, cfg.LogConfig.Type) | ||
setIfNotEmpty(&LogProxyHookJSON, cfg.LogConfig.ProxyHookJson) | ||
|
||
setIfNotEmpty(&ValidatingWebhookSettings.ConfigurationName, cfg.ValidatingWebhookConfig.ConfigurationName) | ||
setIfNotEmpty(&ValidatingWebhookSettings.ServiceName, cfg.ValidatingWebhookConfig.ServiceName) | ||
setIfNotEmpty(&ValidatingWebhookSettings.ServerCertPath, cfg.ValidatingWebhookConfig.ServerCert) | ||
setIfNotEmpty(&ValidatingWebhookSettings.ServerKeyPath, cfg.ValidatingWebhookConfig.ServerKey) | ||
setIfNotEmpty(&ValidatingWebhookSettings.CAPath, cfg.ValidatingWebhookConfig.CA) | ||
setSliceIfNotEmpty(&ValidatingWebhookSettings.ClientCAPaths, cfg.ValidatingWebhookConfig.ClientCA) | ||
setIfNotEmpty(&ValidatingWebhookSettings.DefaultFailurePolicy, cfg.ValidatingWebhookConfig.FailurePolicy) | ||
setIfNotEmpty(&ValidatingWebhookSettings.ListenPort, cfg.ValidatingWebhookConfig.ListenPort) | ||
setIfNotEmpty(&ValidatingWebhookSettings.ListenAddr, cfg.ValidatingWebhookConfig.ListenAddress) | ||
|
||
setIfNotEmpty(&ConversionWebhookSettings.ServiceName, cfg.ValidatingWebhookConfig.ServiceName) | ||
setIfNotEmpty(&ConversionWebhookSettings.ServerCertPath, cfg.ValidatingWebhookConfig.ServerCert) | ||
setIfNotEmpty(&ConversionWebhookSettings.ServerKeyPath, cfg.ValidatingWebhookConfig.ServerKey) | ||
setIfNotEmpty(&ConversionWebhookSettings.CAPath, cfg.ValidatingWebhookConfig.CA) | ||
setSliceIfNotEmpty(&ConversionWebhookSettings.ClientCAPaths, cfg.ValidatingWebhookConfig.ClientCA) | ||
setIfNotEmpty(&ConversionWebhookSettings.ListenPort, cfg.ValidatingWebhookConfig.ListenPort) | ||
setIfNotEmpty(&ConversionWebhookSettings.ListenAddr, cfg.ValidatingWebhookConfig.ListenAddress) | ||
} | ||
|
||
func setIfNotEmpty[T comparable](v *T, env T) { | ||
if !isZero(env) { | ||
*v = env | ||
} | ||
} | ||
|
||
func setSliceIfNotEmpty[T any](v *[]T, env []T) { | ||
if len(env) != 0 { | ||
*v = env | ||
} | ||
} | ||
|
||
func isZero[T comparable](v T) bool { | ||
return v == *new(T) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.