From 308be6051ec661693e9ee02aa925b23c80f86c77 Mon Sep 17 00:00:00 2001 From: Martin Montes Date: Thu, 6 Jul 2023 00:46:21 +0200 Subject: [PATCH] Using kubeclientset and go-envconfig --- go.mod | 3 ++- go.sum | 6 +++-- main.go | 49 ++++++---------------------------- make/dev.mk | 1 - pkg/environment/environment.go | 20 ++++++++++++++ 5 files changed, 34 insertions(+), 45 deletions(-) create mode 100644 pkg/environment/environment.go diff --git a/go.mod b/go.mod index b10ceba..f2962ba 100644 --- a/go.mod +++ b/go.mod @@ -4,8 +4,9 @@ go 1.20 require ( github.com/go-logr/logr v1.2.4 - github.com/mariadb-operator/agent v0.0.1 + github.com/mariadb-operator/agent v0.0.2-0.20230705212819-67aac2bf05b9 github.com/mariadb-operator/mariadb-operator v0.0.16-0.20230702105210-e7121043bc33 + github.com/sethvargo/go-envconfig v0.9.0 k8s.io/apimachinery v0.24.2 k8s.io/client-go v0.24.2 ) diff --git a/go.sum b/go.sum index ea73378..170c8b5 100644 --- a/go.sum +++ b/go.sum @@ -318,8 +318,8 @@ github.com/mailru/easyjson v0.0.0-20190614124828-94de47d64c63/go.mod h1:C1wdFJiN github.com/mailru/easyjson v0.0.0-20190626092158-b2ccc519800e/go.mod h1:C1wdFJiN94OJF2b5HbByQZoLdCWB1Yqtg26g4irojpc= github.com/mailru/easyjson v0.7.6 h1:8yTIVnZgCoiM1TgqoeTl+LfU5Jg6/xL3QhGQnimLYnA= github.com/mailru/easyjson v0.7.6/go.mod h1:xzfreul335JAWq5oZzymOObrkdz5UnU4kGfJJLY9Nlc= -github.com/mariadb-operator/agent v0.0.1 h1:YxME2EAueNJVirGWnQ7qFmUWpEWdZ4OFNzDdRmx8nXQ= -github.com/mariadb-operator/agent v0.0.1/go.mod h1:TGDSu8zPTK1lAnLwhOF78NdO3Sc6McC5DFFGLqKEq2w= +github.com/mariadb-operator/agent v0.0.2-0.20230705212819-67aac2bf05b9 h1:NOnvXXDUSPNhe9OJfqn8kkTORplQIHxiqgShVwQ/3jk= +github.com/mariadb-operator/agent v0.0.2-0.20230705212819-67aac2bf05b9/go.mod h1:UrpYUTpGuh8s1M5dpWLwRu8BSIMjswCBsXCQJaYuNlk= github.com/mariadb-operator/mariadb-operator v0.0.16-0.20230702105210-e7121043bc33 h1:vsYIJc31saaSP/OiJln3NnhnEHtQWODdJej033f7x3c= github.com/mariadb-operator/mariadb-operator v0.0.16-0.20230702105210-e7121043bc33/go.mod h1:83Yz2Jj63rO/Kq34JIVFYhpEJCNSsySiOesjDuQBDWI= github.com/mattn/go-colorable v0.0.9/go.mod h1:9vuHe8Xs5qXnSaW/c/ABM9alt+Vo+STaOChaDxuIBZU= @@ -416,6 +416,8 @@ github.com/russross/blackfriday/v2 v2.0.1/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQD github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM= github.com/ryanuber/columnize v0.0.0-20160712163229-9b3edd62028f/go.mod h1:sm1tb6uqfes/u+d4ooFouqFdy9/2g9QGwK3SQygK0Ts= github.com/sean-/seed v0.0.0-20170313163322-e2103e2c3529/go.mod h1:DxrIzT+xaE7yg65j358z/aeFdxmN0P9QXhEzd20vsDc= +github.com/sethvargo/go-envconfig v0.9.0 h1:Q6FQ6hVEeTECULvkJZakq3dZMeBQ3JUpcKMfPQbKMDE= +github.com/sethvargo/go-envconfig v0.9.0/go.mod h1:Iz1Gy1Sf3T64TQlJSvee81qDhf7YIlt8GMUX6yyNFs0= github.com/shurcooL/sanitized_anchor_name v1.0.0/go.mod h1:1NzhyTcUVG4SuEtjjoZeVRXNmyL/1OwPU0+IJeTBvfc= github.com/sirupsen/logrus v1.2.0/go.mod h1:LxeOpSwHxABJmUn/MG1IvRgCAasNZTLOkJPxbbu5VWo= github.com/sirupsen/logrus v1.4.2/go.mod h1:tLMulIdttU9McNUspp0xgXVQah82FyeX6MwdIuYE2rE= diff --git a/main.go b/main.go index 8d1e5b7..20d0bf8 100644 --- a/main.go +++ b/main.go @@ -3,7 +3,6 @@ package main import ( "context" "encoding/json" - "errors" "flag" "fmt" "log" @@ -14,16 +13,16 @@ import ( "github.com/go-logr/logr" "github.com/mariadb-operator/agent/pkg/filemanager" + "github.com/mariadb-operator/agent/pkg/kubeclientset" "github.com/mariadb-operator/agent/pkg/logger" "github.com/mariadb-operator/init/pkg/config" + "github.com/mariadb-operator/init/pkg/environment" mariadbv1alpha1 "github.com/mariadb-operator/mariadb-operator/api/v1alpha1" mariadbpod "github.com/mariadb-operator/mariadb-operator/pkg/pod" "github.com/mariadb-operator/mariadb-operator/pkg/statefulset" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/util/wait" "k8s.io/client-go/kubernetes" - "k8s.io/client-go/rest" - "k8s.io/client-go/tools/clientcmd" ) var ( @@ -36,11 +35,6 @@ var ( mariadbNamespace string ) -type environment struct { - podName string - mariadbRootPassword string -} - func main() { flag.StringVar(&logLevel, "log-level", "info", "Log level to use, one of: debug, info, warn, error, dpanic, panic, fatal.") flag.StringVar(&logTimeEncoder, "log-time-encoder", "epoch", "Log time encoder to use, one of: "+ @@ -71,18 +65,13 @@ func main() { } logger.Info("Statring init") - env, err := env() + env, err := environment.GetEnvironment(ctx) if err != nil { - logger.Error(err, "Missing environment variables") + logger.Error(err, "Error getting environment variables") os.Exit(1) } - restConfig, err := restConfig() - if err != nil { - logger.Error(err, "Error getting Kubernetes config") - os.Exit(1) - } - clientset, err := kubernetes.NewForConfig(restConfig) + clientset, err := kubeclientset.NewKubeclientSet() if err != nil { logger.Error(err, "Error creating Kubernetes clientset") os.Exit(1) @@ -98,7 +87,7 @@ func main() { logger.Error(err, "Error creating file manager") os.Exit(1) } - configBytes, err := config.NewConfigFile(mdb).Marshal(env.podName, env.mariadbRootPassword) + configBytes, err := config.NewConfigFile(mdb).Marshal(env.PodName, env.MariadbRootPassword) if err != nil { logger.Error(err, "Error getting Galera config") os.Exit(1) @@ -119,9 +108,9 @@ func main() { os.Exit(0) } - idx, err := statefulset.PodIndex(env.podName) + idx, err := statefulset.PodIndex(env.PodName) if err != nil { - logger.Error(err, "error getting index from Pod", "pod", env.podName) + logger.Error(err, "error getting index from Pod", "pod", env.PodName) os.Exit(1) } if *idx == 0 { @@ -147,28 +136,6 @@ func main() { logger.Info("Init done") } -func env() (*environment, error) { - podName := os.Getenv("POD_NAME") - if podName == "" { - return nil, errors.New("environment variable 'POD_NAME' is required") - } - mariadbRootPassword := os.Getenv("MARIADB_ROOT_PASSWORD") - if mariadbRootPassword == "" { - return nil, errors.New("environment variable 'MARIADB_ROOT_PASSWORD' is required") - } - return &environment{ - podName: podName, - mariadbRootPassword: mariadbRootPassword, - }, nil -} - -func restConfig() (*rest.Config, error) { - if kubeconfig := os.Getenv("KUBECONFIG"); kubeconfig != "" { - return clientcmd.BuildConfigFromFlags("", kubeconfig) - } - return rest.InClusterConfig() -} - func mariadb(ctx context.Context, name, namespace string, clientset *kubernetes.Clientset) (*mariadbv1alpha1.MariaDB, error) { path := fmt.Sprintf("/apis/mariadb.mmontes.io/v1alpha1/namespaces/%s/mariadbs/%s", namespace, name) bytes, err := clientset.RESTClient().Get().AbsPath(path).DoRaw(ctx) diff --git a/make/dev.mk b/make/dev.mk index f053003..bf4800b 100644 --- a/make/dev.mk +++ b/make/dev.mk @@ -28,7 +28,6 @@ dir: ## Create config and state directories for local development. export KUBECONFIG ?= $(HOME)/.kube/config export POD_NAME ?= mariadb-galera-0 -export POD_NAMESPACE ?= default export MARIADB_ROOT_PASSWORD ?= mariadb RUN_FLAGS ?= --log-dev --log-level=debug --log-time-encoder=iso8601 --mariadb-name=mariadb-galera --mariadb-namespace=default --config-dir=mariadb/config --state-dir=mariadb/state .PHONY: run diff --git a/pkg/environment/environment.go b/pkg/environment/environment.go new file mode 100644 index 0000000..955b7a7 --- /dev/null +++ b/pkg/environment/environment.go @@ -0,0 +1,20 @@ +package environment + +import ( + "context" + + "github.com/sethvargo/go-envconfig" +) + +type Environment struct { + PodName string `env:"POD_NAME,required"` + MariadbRootPassword string `env:"MARIADB_ROOT_PASSWORD,required"` +} + +func GetEnvironment(ctx context.Context) (*Environment, error) { + var env Environment + if err := envconfig.Process(ctx, &env); err != nil { + return nil, err + } + return &env, nil +}