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

Commit b14c061

Browse files
committed
Waiting from previous Pod
1 parent 68cee1a commit b14c061

File tree

2 files changed

+56
-40
lines changed

2 files changed

+56
-40
lines changed

main.go

Lines changed: 54 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,17 @@ import (
1010
"os"
1111
"os/signal"
1212
"syscall"
13+
"time"
1314

15+
"github.com/go-logr/logr"
1416
"github.com/mariadb-operator/agent/pkg/filemanager"
1517
"github.com/mariadb-operator/agent/pkg/logger"
1618
"github.com/mariadb-operator/init/pkg/config"
1719
mariadbv1alpha1 "github.com/mariadb-operator/mariadb-operator/api/v1alpha1"
20+
"github.com/mariadb-operator/mariadb-operator/pkg/pod"
21+
"github.com/mariadb-operator/mariadb-operator/pkg/statefulset"
22+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
23+
"k8s.io/apimachinery/pkg/util/wait"
1824
"k8s.io/client-go/kubernetes"
1925
"k8s.io/client-go/rest"
2026
"k8s.io/client-go/tools/clientcmd"
@@ -32,13 +38,11 @@ var (
3238

3339
type Env struct {
3440
podName string
35-
podNamespace string
3641
mariadbRootPassword string
3742
}
3843

3944
func main() {
40-
flag.StringVar(&logLevel, "log-level", "info", "Log level to use, one of: "+
41-
"debug, info, warn, error, dpanic, panic, fatal.")
45+
flag.StringVar(&logLevel, "log-level", "info", "Log level to use, one of: debug, info, warn, error, dpanic, panic, fatal.")
4246
flag.StringVar(&logTimeEncoder, "log-time-encoder", "epoch", "Log time encoder to use, one of: "+
4347
"epoch, millis, nano, iso8601, rfc3339 or rfc3339nano")
4448
flag.BoolVar(&logDev, "log-dev", false, "Enable development logs")
@@ -104,11 +108,30 @@ func main() {
104108
logger.Error(err, "Error writing Galera config")
105109
os.Exit(1)
106110
}
107-
logger.Info("Configuring bootstrap")
108-
if err := fileManager.WriteConfigFile(config.BootstrapFileName, config.BootstrapFile); err != nil {
109-
logger.Error(err, "Error writing bootstrap config")
111+
112+
idx, err := statefulset.PodIndex(env.podName)
113+
if err != nil {
114+
logger.Error(err, "error getting index from Pod", "pod", env.podName)
110115
os.Exit(1)
111116
}
117+
118+
if *idx == 0 {
119+
logger.Info("Configuring bootstrap")
120+
if err := fileManager.WriteConfigFile(config.BootstrapFileName, config.BootstrapFile); err != nil {
121+
logger.Error(err, "Error writing bootstrap config")
122+
os.Exit(1)
123+
}
124+
} else {
125+
previousPodName, err := previousPodName(mdb, *idx)
126+
if err != nil {
127+
logger.Error(err, "error getting previous Pod")
128+
os.Exit(1)
129+
}
130+
if err := waitForPreviousPod(ctx, mdb, previousPodName, clientset, logger); err != nil {
131+
logger.Error(err, "error getting previous Pod", "pod", previousPodName)
132+
os.Exit(1)
133+
}
134+
}
112135
logger.Info("Init done")
113136
}
114137

@@ -117,17 +140,12 @@ func env() (*Env, error) {
117140
if podName == "" {
118141
return nil, errors.New("environment variable 'POD_NAME' is required")
119142
}
120-
podNamespace := os.Getenv("POD_NAMESPACE")
121-
if podNamespace == "" {
122-
return nil, errors.New("environment variable 'POD_NAMESPACE' is required")
123-
}
124143
mariadbRootPassword := os.Getenv("MARIADB_ROOT_PASSWORD")
125144
if mariadbRootPassword == "" {
126145
return nil, errors.New("environment variable 'MARIADB_ROOT_PASSWORD' is required")
127146
}
128147
return &Env{
129148
podName: podName,
130-
podNamespace: podNamespace,
131149
mariadbRootPassword: mariadbRootPassword,
132150
}, nil
133151
}
@@ -141,11 +159,7 @@ func restConfig() (*rest.Config, error) {
141159

142160
func mariadb(ctx context.Context, name, namespace string, clientset *kubernetes.Clientset) (*mariadbv1alpha1.MariaDB, error) {
143161
path := fmt.Sprintf("/apis/mariadb.mmontes.io/v1alpha1/namespaces/%s/mariadbs/%s", namespace, name)
144-
bytes, err := clientset.
145-
RESTClient().
146-
Get().
147-
AbsPath(path).
148-
DoRaw(ctx)
162+
bytes, err := clientset.RESTClient().Get().AbsPath(path).DoRaw(ctx)
149163
if err != nil {
150164
return nil, fmt.Errorf("error requesting '%s' MariaDB in namespace '%s': %v", name, namespace, err)
151165
}
@@ -155,3 +169,27 @@ func mariadb(ctx context.Context, name, namespace string, clientset *kubernetes.
155169
}
156170
return &mdb, nil
157171
}
172+
173+
func previousPodName(mariadb *mariadbv1alpha1.MariaDB, podIndex int) (string, error) {
174+
if podIndex == 0 {
175+
return "", fmt.Errorf("Pod '%s' is the first Pod", statefulset.PodName(mariadb.ObjectMeta, podIndex))
176+
}
177+
previousPodIndex := podIndex - 1
178+
return statefulset.PodName(mariadb.ObjectMeta, previousPodIndex), nil
179+
}
180+
181+
func waitForPreviousPod(ctx context.Context, mariadb *mariadbv1alpha1.MariaDB, previousPodName string, clientset *kubernetes.Clientset,
182+
logger logr.Logger) error {
183+
return wait.PollImmediateUntilWithContext(ctx, 1*time.Second, func(context.Context) (bool, error) {
184+
previousPod, err := clientset.CoreV1().Pods(mariadb.Namespace).Get(ctx, previousPodName, metav1.GetOptions{})
185+
if err != nil {
186+
return false, nil
187+
}
188+
if !pod.PodReady(previousPod) {
189+
logger.V(1).Info("Previous Pod not ready", "pod", previousPodName)
190+
return false, nil
191+
}
192+
logger.V(1).Info("Previous Pod ready", "pod", previousPodName)
193+
return true, nil
194+
})
195+
}

make/deploy.mk

Lines changed: 2 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
CLUSTER ?= init
2-
CLUSTER_OPERATOR ?= mdb
1+
CLUSTER ?= mdb
32

43
##@ Docker
54

@@ -30,31 +29,10 @@ docker-inspect: ## Inspect docker image.
3029

3130
.PHONY: docker-load
3231
docker-load: ## Load docker image in KIND.
33-
kind load docker-image --name ${CLUSTER_OPERATOR} ${IMG}
32+
kind load docker-image --name ${CLUSTER} ${IMG}
3433

3534
##@ Cluster
3635

37-
KIND_CONFIG ?= hack/config/kind.yaml
38-
KIND_IMAGE ?= kindest/node:v1.26.0
39-
40-
.PHONY: cluster
41-
cluster: kind ## Create the kind cluster.
42-
$(KIND) create cluster --name $(CLUSTER) --config $(KIND_CONFIG)
43-
44-
.PHONY: cluster-delete
45-
cluster-delete: kind ## Delete the kind cluster.
46-
$(KIND) delete cluster --name $(CLUSTER)
47-
4836
.PHONY: cluster-ctx
4937
cluster-ctx: ## Sets cluster context.
5038
@kubectl config use-context kind-$(CLUSTER)
51-
52-
##@ MariaDB
53-
54-
.PHONY: mariadb
55-
mariadb: ## Create a MariaDB galera in kind.
56-
@./hack/mariadb.sh
57-
58-
.PHONY: mariadb-delete
59-
mariadb-delete: ## Delete the MariaDB galera cluster.
60-
@./hack/mariadb-delete.sh

0 commit comments

Comments
 (0)