@@ -3,7 +3,6 @@ package main
33import (
44 "context"
55 "encoding/json"
6- "errors"
76 "flag"
87 "fmt"
98 "log"
@@ -14,16 +13,16 @@ import (
1413
1514 "github.com/go-logr/logr"
1615 "github.com/mariadb-operator/agent/pkg/filemanager"
16+ "github.com/mariadb-operator/agent/pkg/kubeclientset"
1717 "github.com/mariadb-operator/agent/pkg/logger"
1818 "github.com/mariadb-operator/init/pkg/config"
19+ "github.com/mariadb-operator/init/pkg/environment"
1920 mariadbv1alpha1 "github.com/mariadb-operator/mariadb-operator/api/v1alpha1"
2021 mariadbpod "github.com/mariadb-operator/mariadb-operator/pkg/pod"
2122 "github.com/mariadb-operator/mariadb-operator/pkg/statefulset"
2223 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2324 "k8s.io/apimachinery/pkg/util/wait"
2425 "k8s.io/client-go/kubernetes"
25- "k8s.io/client-go/rest"
26- "k8s.io/client-go/tools/clientcmd"
2726)
2827
2928var (
3635 mariadbNamespace string
3736)
3837
39- type environment struct {
40- podName string
41- mariadbRootPassword string
42- }
43-
4438func main () {
4539 flag .StringVar (& logLevel , "log-level" , "info" , "Log level to use, one of: debug, info, warn, error, dpanic, panic, fatal." )
4640 flag .StringVar (& logTimeEncoder , "log-time-encoder" , "epoch" , "Log time encoder to use, one of: " +
@@ -71,18 +65,13 @@ func main() {
7165 }
7266 logger .Info ("Statring init" )
7367
74- env , err := env ( )
68+ env , err := environment . GetEnvironment ( ctx )
7569 if err != nil {
76- logger .Error (err , "Missing environment variables" )
70+ logger .Error (err , "Error getting environment variables" )
7771 os .Exit (1 )
7872 }
7973
80- restConfig , err := restConfig ()
81- if err != nil {
82- logger .Error (err , "Error getting Kubernetes config" )
83- os .Exit (1 )
84- }
85- clientset , err := kubernetes .NewForConfig (restConfig )
74+ clientset , err := kubeclientset .NewKubeclientSet ()
8675 if err != nil {
8776 logger .Error (err , "Error creating Kubernetes clientset" )
8877 os .Exit (1 )
@@ -98,7 +87,7 @@ func main() {
9887 logger .Error (err , "Error creating file manager" )
9988 os .Exit (1 )
10089 }
101- configBytes , err := config .NewConfigFile (mdb ).Marshal (env .podName , env .mariadbRootPassword )
90+ configBytes , err := config .NewConfigFile (mdb ).Marshal (env .PodName , env .MariadbRootPassword )
10291 if err != nil {
10392 logger .Error (err , "Error getting Galera config" )
10493 os .Exit (1 )
@@ -119,9 +108,9 @@ func main() {
119108 os .Exit (0 )
120109 }
121110
122- idx , err := statefulset .PodIndex (env .podName )
111+ idx , err := statefulset .PodIndex (env .PodName )
123112 if err != nil {
124- logger .Error (err , "error getting index from Pod" , "pod" , env .podName )
113+ logger .Error (err , "error getting index from Pod" , "pod" , env .PodName )
125114 os .Exit (1 )
126115 }
127116 if * idx == 0 {
@@ -147,28 +136,6 @@ func main() {
147136 logger .Info ("Init done" )
148137}
149138
150- func env () (* environment , error ) {
151- podName := os .Getenv ("POD_NAME" )
152- if podName == "" {
153- return nil , errors .New ("environment variable 'POD_NAME' is required" )
154- }
155- mariadbRootPassword := os .Getenv ("MARIADB_ROOT_PASSWORD" )
156- if mariadbRootPassword == "" {
157- return nil , errors .New ("environment variable 'MARIADB_ROOT_PASSWORD' is required" )
158- }
159- return & environment {
160- podName : podName ,
161- mariadbRootPassword : mariadbRootPassword ,
162- }, nil
163- }
164-
165- func restConfig () (* rest.Config , error ) {
166- if kubeconfig := os .Getenv ("KUBECONFIG" ); kubeconfig != "" {
167- return clientcmd .BuildConfigFromFlags ("" , kubeconfig )
168- }
169- return rest .InClusterConfig ()
170- }
171-
172139func mariadb (ctx context.Context , name , namespace string , clientset * kubernetes.Clientset ) (* mariadbv1alpha1.MariaDB , error ) {
173140 path := fmt .Sprintf ("/apis/mariadb.mmontes.io/v1alpha1/namespaces/%s/mariadbs/%s" , namespace , name )
174141 bytes , err := clientset .RESTClient ().Get ().AbsPath (path ).DoRaw (ctx )
0 commit comments