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

Commit 68cee1a

Browse files
committed
Getting env
1 parent c4c4a77 commit 68cee1a

File tree

4 files changed

+65
-51
lines changed

4 files changed

+65
-51
lines changed

main.go

Lines changed: 42 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package main
33
import (
44
"context"
55
"encoding/json"
6+
"errors"
67
"flag"
78
"fmt"
89
"log"
@@ -20,30 +21,31 @@ import (
2021
)
2122

2223
var (
23-
logLevel string
24-
logTimeEncoder string
25-
logDev bool
26-
27-
configDir string
28-
stateDir string
29-
24+
logLevel string
25+
logTimeEncoder string
26+
logDev bool
27+
configDir string
28+
stateDir string
3029
mariadbName string
3130
mariadbNamespace string
3231
)
3332

33+
type Env struct {
34+
podName string
35+
podNamespace string
36+
mariadbRootPassword string
37+
}
38+
3439
func main() {
3540
flag.StringVar(&logLevel, "log-level", "info", "Log level to use, one of: "+
3641
"debug, info, warn, error, dpanic, panic, fatal.")
3742
flag.StringVar(&logTimeEncoder, "log-time-encoder", "epoch", "Log time encoder to use, one of: "+
3843
"epoch, millis, nano, iso8601, rfc3339 or rfc3339nano")
3944
flag.BoolVar(&logDev, "log-dev", false, "Enable development logs")
40-
4145
flag.StringVar(&configDir, "config-dir", "/etc/mysql/mariadb.conf.d", "The directory that contains MariaDB configuration files")
4246
flag.StringVar(&stateDir, "state-dir", "/var/lib/mysql", "The directory that contains MariaDB state files")
43-
4447
flag.StringVar(&mariadbName, "mariadb-name", "", "The name of the MariaDB to be initialized")
4548
flag.StringVar(&mariadbNamespace, "mariadb-namespace", "", "The namespace of the MariaDB to be initialized")
46-
4749
flag.Parse()
4850

4951
ctx, cancel := signal.NotifyContext(context.Background(), []os.Signal{
@@ -61,10 +63,16 @@ func main() {
6163
logger.WithDevelopment(logDev),
6264
)
6365
if err != nil {
64-
log.Fatalf("error creating logger: %v", err)
66+
log.Fatalf("Error creating logger: %v", err)
6567
}
6668
logger.Info("Staring init")
6769

70+
env, err := env()
71+
if err != nil {
72+
logger.Error(err, "Missing environment variables")
73+
os.Exit(1)
74+
}
75+
6876
restConfig, err := restConfig()
6977
if err != nil {
7078
logger.Error(err, "Error getting Kubernetes config")
@@ -86,14 +94,14 @@ func main() {
8694
logger.Error(err, "Error creating file manager")
8795
os.Exit(1)
8896
}
89-
configBytes, err := config.NewConfigFile(mdb).Marshal()
97+
configBytes, err := config.NewConfigFile(mdb).Marshal(env.podName, env.mariadbRootPassword)
9098
if err != nil {
91-
logger.Error(err, "Error getting galera config")
99+
logger.Error(err, "Error getting Galera config")
92100
os.Exit(1)
93101
}
94102
logger.Info("Configuring Galera")
95103
if err := fileManager.WriteConfigFile(config.ConfigFileName, configBytes); err != nil {
96-
logger.Error(err, "Error writing galera config")
104+
logger.Error(err, "Error writing Galera config")
97105
os.Exit(1)
98106
}
99107
logger.Info("Configuring bootstrap")
@@ -104,6 +112,26 @@ func main() {
104112
logger.Info("Init done")
105113
}
106114

115+
func env() (*Env, error) {
116+
podName := os.Getenv("POD_NAME")
117+
if podName == "" {
118+
return nil, errors.New("environment variable 'POD_NAME' is required")
119+
}
120+
podNamespace := os.Getenv("POD_NAMESPACE")
121+
if podNamespace == "" {
122+
return nil, errors.New("environment variable 'POD_NAMESPACE' is required")
123+
}
124+
mariadbRootPassword := os.Getenv("MARIADB_ROOT_PASSWORD")
125+
if mariadbRootPassword == "" {
126+
return nil, errors.New("environment variable 'MARIADB_ROOT_PASSWORD' is required")
127+
}
128+
return &Env{
129+
podName: podName,
130+
podNamespace: podNamespace,
131+
mariadbRootPassword: mariadbRootPassword,
132+
}, nil
133+
}
134+
107135
func restConfig() (*rest.Config, error) {
108136
if kubeconfig := os.Getenv("KUBECONFIG"); kubeconfig != "" {
109137
return clientcmd.BuildConfigFromFlags("", kubeconfig)

make/dev.mk

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@ dir: ## Create config and state directories for local development.
2727
mkdir -p mariadb/state
2828

2929
export KUBECONFIG ?= $(HOME)/.kube/config
30-
export HOSTNAME ?= mariadb-galera-0
30+
export POD_NAME ?= mariadb-galera-0
31+
export POD_NAMESPACE ?= default
3132
export MARIADB_ROOT_PASSWORD ?= mariadb
3233
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
3334
.PHONY: run

pkg/config/config.go

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import (
44
"bytes"
55
"errors"
66
"fmt"
7-
"os"
87
"strings"
98
"text/template"
109

@@ -32,7 +31,7 @@ func NewConfigFile(mariadb *mariadbv1alpha1.MariaDB) *ConfigFile {
3231
}
3332
}
3433

35-
func (c *ConfigFile) Marshal() ([]byte, error) {
34+
func (c *ConfigFile) Marshal(podName, mariadbRootPassword string) ([]byte, error) {
3635
tpl := createTpl("galera", `[mysqld]
3736
bind-address=0.0.0.0
3837
default_storage_engine=InnoDB
@@ -63,14 +62,6 @@ wsrep_sst_auth="root:{{ .RootPassword }}"
6362
if err != nil {
6463
return nil, fmt.Errorf("error getting SST: %v", err)
6564
}
66-
hostname := os.Getenv("HOSTNAME")
67-
if hostname == "" {
68-
return nil, errors.New("HOSTNAME environment variable not found")
69-
}
70-
rootPassword := os.Getenv("MARIADB_ROOT_PASSWORD")
71-
if rootPassword == "" {
72-
return nil, errors.New("MARIADB_ROOT_PASSWORD environment variable not found")
73-
}
7465

7566
err = tpl.Execute(buf, struct {
7667
ClusterAddress string
@@ -83,14 +74,14 @@ wsrep_sst_auth="root:{{ .RootPassword }}"
8374
}{
8475
ClusterAddress: clusterAddr,
8576
Threads: c.mariadb.Spec.Galera.ReplicaThreads,
86-
Pod: hostname,
77+
Pod: podName,
8778
Service: statefulset.ServiceFQDNWithService(
8879
c.mariadb.ObjectMeta,
8980
ctrlresources.InternalServiceKey(c.mariadb).Name,
9081
),
9182
SST: sst,
9283
SSTAuth: c.mariadb.Spec.Galera.SST == mariadbv1alpha1.SSTMariaBackup || c.mariadb.Spec.Galera.SST == mariadbv1alpha1.SSTMysqldump,
93-
RootPassword: rootPassword,
84+
RootPassword: mariadbRootPassword,
9485
})
9586
if err != nil {
9687
return nil, err

pkg/config/config_test.go

Lines changed: 18 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,15 @@ import (
99

1010
func TestConfigMarshal(t *testing.T) {
1111
tests := []struct {
12-
name string
13-
mariadb *mariadbv1alpha1.MariaDB
14-
env map[string]string
15-
wantConfig string
16-
wantErr bool
12+
name string
13+
mariadb *mariadbv1alpha1.MariaDB
14+
podName string
15+
mariadbRootPassword string
16+
wantConfig string
17+
wantErr bool
1718
}{
1819
{
19-
name: "no env",
20+
name: "no replicas",
2021
mariadb: &mariadbv1alpha1.MariaDB{
2122
ObjectMeta: v1.ObjectMeta{
2223
Name: "mariadb-galera",
@@ -27,12 +28,13 @@ func TestConfigMarshal(t *testing.T) {
2728
SST: mariadbv1alpha1.SSTRsync,
2829
ReplicaThreads: 1,
2930
},
30-
Replicas: 3,
31+
Replicas: 0,
3132
},
3233
},
33-
env: map[string]string{},
34-
wantConfig: "",
35-
wantErr: true,
34+
podName: "mariadb-galera-0",
35+
mariadbRootPassword: "mariadb",
36+
wantConfig: "",
37+
wantErr: true,
3638
},
3739
{
3840
name: "rsync",
@@ -49,10 +51,8 @@ func TestConfigMarshal(t *testing.T) {
4951
Replicas: 3,
5052
},
5153
},
52-
env: map[string]string{
53-
"HOSTNAME": "mariadb-galera-0",
54-
"MARIADB_ROOT_PASSWORD": "foo",
55-
},
54+
podName: "mariadb-galera-0",
55+
mariadbRootPassword: "mariadb",
5656
//nolint:lll
5757
wantConfig: `[mysqld]
5858
bind-address=0.0.0.0
@@ -89,10 +89,8 @@ wsrep_sst_method="rsync"
8989
Replicas: 3,
9090
},
9191
},
92-
env: map[string]string{
93-
"HOSTNAME": "mariadb-galera-1",
94-
"MARIADB_ROOT_PASSWORD": "foo",
95-
},
92+
podName: "mariadb-galera-1",
93+
mariadbRootPassword: "mariadb",
9694
//nolint:lll
9795
wantConfig: `[mysqld]
9896
bind-address=0.0.0.0
@@ -111,19 +109,15 @@ wsrep_slave_threads=2
111109
wsrep_node_address="mariadb-galera-1.mariadb-galera-internal.default.svc.cluster.local"
112110
wsrep_node_name="mariadb-galera-1"
113111
wsrep_sst_method="mariabackup"
114-
wsrep_sst_auth="root:foo"
112+
wsrep_sst_auth="root:mariadb"
115113
`,
116114
wantErr: false,
117115
},
118116
}
119117
for _, tt := range tests {
120118
t.Run(tt.name, func(t *testing.T) {
121119
config := NewConfigFile(tt.mariadb)
122-
for k, v := range tt.env {
123-
t.Setenv(k, v)
124-
}
125-
126-
bytes, err := config.Marshal()
120+
bytes, err := config.Marshal(tt.podName, tt.mariadbRootPassword)
127121
if tt.wantErr && err == nil {
128122
t.Fatal("error expected, got nil")
129123
}

0 commit comments

Comments
 (0)