/
main.go
228 lines (203 loc) · 7.11 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
package init
import (
"context"
"fmt"
"os"
"os/signal"
"syscall"
"time"
mariadbv1alpha1 "github.com/mariadb-operator/mariadb-operator/api/v1alpha1"
"github.com/mariadb-operator/mariadb-operator/pkg/environment"
"github.com/mariadb-operator/mariadb-operator/pkg/galera/config"
"github.com/mariadb-operator/mariadb-operator/pkg/galera/filemanager"
"github.com/mariadb-operator/mariadb-operator/pkg/galera/state"
"github.com/mariadb-operator/mariadb-operator/pkg/log"
mariadbpod "github.com/mariadb-operator/mariadb-operator/pkg/pod"
"github.com/mariadb-operator/mariadb-operator/pkg/statefulset"
"github.com/spf13/cobra"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/types"
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
"k8s.io/apimachinery/pkg/util/wait"
clientgoscheme "k8s.io/client-go/kubernetes/scheme"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
)
var (
scheme = runtime.NewScheme()
logger = ctrl.Log
configDir string
stateDir string
)
func init() {
utilruntime.Must(clientgoscheme.AddToScheme(scheme))
utilruntime.Must(mariadbv1alpha1.AddToScheme(scheme))
RootCmd.Flags().StringVar(&configDir, "config-dir", "/etc/mysql/mariadb.conf.d",
"The directory that contains MariaDB configuration files")
RootCmd.Flags().StringVar(&stateDir, "state-dir", "/var/lib/mysql", "The directory that contains MariaDB state files")
}
var RootCmd = &cobra.Command{
Use: "init",
Short: "Init.",
Long: `Init container for Galera and co-operates with mariadb-operator.`,
Args: cobra.NoArgs,
Run: func(cmd *cobra.Command, args []string) {
if err := log.SetupLoggerWithCommand(cmd); err != nil {
fmt.Printf("error setting up logger: %v\n", err)
os.Exit(1)
}
logger.Info("Starting init")
ctx, cancel := newContext()
defer cancel()
env, err := environment.GetPodEnv(ctx)
if err != nil {
logger.Error(err, "Error getting environment variables")
os.Exit(1)
}
fileManager, err := filemanager.NewFileManager(configDir, stateDir)
if err != nil {
logger.Error(err, "Error creating file manager")
os.Exit(1)
}
state := state.NewState(stateDir)
k8sClient, err := getK8sClient()
if err != nil {
logger.Error(err, "Error getting Kubernetes client")
os.Exit(1)
}
isGaleraInit, err := state.IsGaleraInit()
if err != nil {
logger.Error(err, "Error checking Galera init state")
os.Exit(1)
}
podIndex, err := statefulset.PodIndex(env.PodName)
if err != nil {
logger.Error(err, "error getting index from Pod", "pod", env.PodName)
os.Exit(1)
}
key := types.NamespacedName{
Name: env.MariadbName,
Namespace: env.PodNamespace,
}
var mdb mariadbv1alpha1.MariaDB
if err := k8sClient.Get(ctx, key, &mdb); err != nil {
logger.Error(err, "Error getting MariaDB")
if err := updateNodeAddress(fileManager, env); err != nil {
logger.Error(err, "Error upddating node address")
os.Exit(1)
}
logger.Info("Updated node address")
os.Exit(0)
}
if err := configureGalera(fileManager, env, &mdb); err != nil {
logger.Error(err, "error configuring Galera")
os.Exit(1)
}
if err := configureGaleraBootstrap(fileManager, *podIndex, isGaleraInit); err != nil {
logger.Error(err, "error configuring Galera bootstrap")
}
if err := waitForPreviousPod(ctx, k8sClient, env, &mdb, *podIndex, isGaleraInit); err != nil {
logger.Error(err, "error waiting for previous Pod")
os.Exit(1)
}
logger.Info("Init done")
},
}
func newContext() (context.Context, context.CancelFunc) {
return signal.NotifyContext(context.Background(), []os.Signal{
syscall.SIGINT,
syscall.SIGTERM,
syscall.SIGKILL,
syscall.SIGHUP,
syscall.SIGQUIT}...,
)
}
func getK8sClient() (client.Client, error) {
restConfig, err := ctrl.GetConfig()
if err != nil {
return nil, fmt.Errorf("error getting REST config: %v", err)
}
k8sClient, err := client.New(restConfig, client.Options{Scheme: scheme})
if err != nil {
return nil, fmt.Errorf("error creating Kubernetes client: %v", err)
}
return k8sClient, nil
}
func configureGalera(fm *filemanager.FileManager, env *environment.PodEnvironment, mdb *mariadbv1alpha1.MariaDB) error {
logger.Info("Configuring Galera")
configBytes, err := config.NewConfigFile(mdb).Marshal(env)
if err != nil {
return fmt.Errorf("error getting Galera config: %v", err)
}
if err := fm.WriteConfigFile(config.ConfigFileName, configBytes); err != nil {
return fmt.Errorf("error writing Galera config: %v", err)
}
return nil
}
func updateNodeAddress(fm *filemanager.FileManager, env *environment.PodEnvironment) error {
logger.Info("Attempting to update Galera node address in existing config")
configBytes, err := fm.ReadConfigFile(config.ConfigFileName)
if err != nil {
return fmt.Errorf("error getting existing Galera config: %v", err)
}
updatedBytes, err := config.UpdateConfigFile(configBytes, config.WsrepNodeAddressKey, env.PodIP)
if err != nil {
return fmt.Errorf("error updating existing Galera config: %v", err)
}
if err := fm.WriteConfigFile(config.ConfigFileName, updatedBytes); err != nil {
return fmt.Errorf("error writing existing Galera config: %v", err)
}
return nil
}
func configureGaleraBootstrap(fm *filemanager.FileManager, podIndex int, isGaleraInit bool) error {
if podIndex == 0 && !isGaleraInit {
logger.Info("Configuring Galera bootstrap")
if err := fm.WriteConfigFile(config.BootstrapFileName, config.BootstrapFile); err != nil {
return fmt.Errorf("error configuring Galera bootstrap: %v", err)
}
}
return nil
}
func waitForPreviousPod(ctx context.Context, k8sClient client.Client, env *environment.PodEnvironment,
mdb *mariadbv1alpha1.MariaDB, podIndex int, isGaleraInit bool) error {
if podIndex == 0 || isGaleraInit {
return nil
}
previousPodName, err := getPreviousPodName(mdb, podIndex)
if err != nil {
return fmt.Errorf("error getting previous Pod: %v", err)
}
logger.Info("Waiting for previous Pod to be ready", "pod", previousPodName)
previousKey := types.NamespacedName{
Name: previousPodName,
Namespace: env.PodNamespace,
}
if err := waitForPodReady(ctx, previousKey, k8sClient); err != nil {
logger.Info("Waiting for previous Pod to be ready", "pod", previousPodName)
return fmt.Errorf("error waiting for previous Pod '%s' to be ready: %v", previousPodName, err)
}
return nil
}
func getPreviousPodName(mdb *mariadbv1alpha1.MariaDB, podIndex int) (string, error) {
if podIndex == 0 {
return "", fmt.Errorf("Pod '%s' is the first Pod", statefulset.PodName(mdb.ObjectMeta, podIndex))
}
previousPodIndex := podIndex - 1
return statefulset.PodName(mdb.ObjectMeta, previousPodIndex), nil
}
func waitForPodReady(ctx context.Context, key types.NamespacedName, client client.Client) error {
return wait.PollUntilContextCancel(ctx, 1*time.Second, true, func(context.Context) (bool, error) {
var pod corev1.Pod
if err := client.Get(ctx, key, &pod); err != nil {
logger.V(1).Info("Error getting Pod", "err", err)
return false, nil
}
if !mariadbpod.PodReady(&pod) {
logger.V(1).Info("Pod not ready", "pod", pod.Name)
return false, nil
}
logger.V(1).Info("Pod ready", "pod", pod.Name)
return true, nil
})
}