Skip to content
This repository has been archived by the owner on Feb 28, 2024. It is now read-only.

Commit

Permalink
Using kubeclientset and go-envconfig
Browse files Browse the repository at this point in the history
  • Loading branch information
mmontes11 committed Jul 5, 2023
1 parent 473e42f commit 308be60
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 45 deletions.
3 changes: 2 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
Expand Down
6 changes: 4 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down Expand Up @@ -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=
Expand Down
49 changes: 8 additions & 41 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package main
import (
"context"
"encoding/json"
"errors"
"flag"
"fmt"
"log"
Expand All @@ -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 (
Expand All @@ -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: "+
Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand All @@ -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 {
Expand All @@ -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)
Expand Down
1 change: 0 additions & 1 deletion make/dev.mk
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
20 changes: 20 additions & 0 deletions pkg/environment/environment.go
Original file line number Diff line number Diff line change
@@ -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
}

0 comments on commit 308be60

Please sign in to comment.