Skip to content

Commit 35cdfb7

Browse files
authored
METAL-2746 check and skip if gsql instance is not ready for a new update (#74)
* METAL-2746 check and skip if gsql instance is not ready for a new update * METAL-2746 fix unit test, cleanup makefile * METAL-2746 replace egrep (deprecated) with grep -E * METAL-2746 fix comments on mr * METAL-2746 fix comments on mr
1 parent c426432 commit 35cdfb7

7 files changed

Lines changed: 60 additions & 43 deletions

File tree

Makefile

Lines changed: 30 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -2,53 +2,47 @@
22
CRD_OPTIONS ?= "crd:trivialVersions=true,preserveUnknownFields=false"
33

44
.PHONY: all deploy build helm
5-
all: help
6-
7-
help:
8-
@echo "make miniup: start minikube"
9-
@echo "make minidown: stop minikube"
10-
@echo "make minidelete: delete minikube"
11-
@echo "make minidashboard: open minikube dashboard"
12-
@echo "make build: build db-operator docker image"
13-
@echo "make helm: install helm if not exist and install local chart using helm upgrade --install command"
14-
@echo "make setup: build db-operator image, install helm"
15-
@echo "make update: build db-operator image again and delete running pod"
16-
@echo "make addexamples: kubectl create -f examples/"
17-
@echo "make test: spin up mysql, postgres containers and run go unit test"
18-
@echo "make microsetup: install microk8s locally and deploy db-operator (only for linux and mac)"
19-
@echo "make manifests: generate custom resource definitions"
20-
@echo "make generate: generate supporting code for custom resource types"
21-
22-
build:
5+
.ONESHELL: test
6+
7+
help: ## show this help
8+
@echo 'usage: make [target] ...'
9+
@echo ''
10+
@echo 'targets:'
11+
@grep -E '^(.+)\:\ .*##\ (.+)' ${MAKEFILE_LIST} | sort | sed 's/:.*##/#/' | column -t -c 2 -s '#'
12+
13+
build: ## build db-operator docker image
2314
@docker build -t my-db-operator:local .
2415
@docker save my-db-operator > my-image.tar
2516
@docker build -t mock-googleapi:local mock/
2617
@docker save mock-googleapi > mock-google-api.tar
2718

28-
helm:
19+
helm: ## install helm if not exist and install local chart using helm upgrade --install command
2920
@helm upgrade --install --create-namespace --namespace operator my-dboperator helm/db-operator -f helm/db-operator/values.yaml -f helm/db-operator/values-local.yaml
3021

31-
helm-lint:
22+
helm-lint: ## lint helm manifests
3223
@helm lint -f helm/db-operator/values.yaml -f helm/db-operator/ci/ci-1.yaml --strict ./helm/db-operator
3324
@helm lint -f helm/db-instances/values.yaml --strict ./helm/db-instances
3425

35-
addexamples:
26+
addexamples: ## add examples via kubectl create -f examples/
3627
cd ./examples/; ls | while read line; do kubectl apply -f $$line; done
3728

38-
setup: build helm
29+
setup: build helm ## build db-operator image, install helm
3930

4031
deploy:
4132
@kubectl delete pod -l app=db-operator -n operator &
4233
watch -n0.2 -c 'kubectl logs -l app=db-operator --all-containers=true -n operator'
4334

44-
update: build deploy
35+
update: build deploy ## build db-operator image again and delete running pod
4536

46-
test:
47-
@docker-compose up -d
48-
@docker-compose restart sqladmin
49-
@sleep 2
50-
@go test -count=1 -tags tests ./... -v -cover
51-
@docker-compose down
37+
test: ## spin up mysql, postgres containers and run go unit test
38+
tearDown() {
39+
docker-compose down
40+
}
41+
trap tearDown EXIT
42+
docker-compose up -d
43+
docker-compose restart sqladmin
44+
sleep 10
45+
go test -count=1 -tags tests ./... -v -cover
5246

5347
lint:
5448
@golint ./...
@@ -58,23 +52,23 @@ vet:
5852

5953
minisetup: miniup miniimage helm
6054

61-
miniup:
55+
miniup: ## start minikube
6256
@minikube start --kubernetes-version=v1.19.12 --cpus 2 --memory 4096
6357

64-
minidown:
58+
minidown: ## stop minikube
6559
@minikube stop
6660

67-
minidelete:
61+
minidelete: ## delete minikube
6862
@minikube delete
6963

70-
minidashboard:
64+
minidashboard: ## open minikube dashboard
7165
@minikube dashboard
7266

7367
miniimage: build
7468
@minikube image load my-image.tar
7569
@minikube image load mock-google-api.tar
7670

77-
k3d_setup: k3d_install k3d_image helm
71+
k3d_setup: k3d_install k3d_image helm ## install microk8s locally and deploy db-operator (only for linux and mac)
7872

7973
k3d_install:
8074
@wget -q -O - https://raw.githubusercontent.com/rancher/k3d/main/install.sh | bash
@@ -86,11 +80,11 @@ k3d_image: build
8680
@k3d image import mock-google-api.tar -c myk3s
8781

8882
## Generate WebhookConfiguration, ClusterRole and CustomResourceDefinition objects.
89-
manifests: controller-gen
83+
manifests: controller-gen ## generate custom resource definitions
9084
$(CONTROLLER_GEN) $(CRD_OPTIONS) rbac:roleName=manager-role webhook paths="./..." output:crd:artifacts:config=config/crd/bases
9185

9286
## Generate code containing DeepCopy, DeepCopyInto, and DeepCopyObject method implementations.
93-
generate: controller-gen
87+
generate: controller-gen ## generate supporting code for custom resource types
9488
$(CONTROLLER_GEN) object:headerFile="hack/boilerplate.go.txt" paths="./..."
9589

9690
CONTROLLER_GEN = $(shell pwd)/bin/controller-gen

pkg/utils/dbinstance/errors.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,6 @@ var (
2323
ErrAlreadyExists = errors.New("instance already exists")
2424
// ErrNotExists is thrown when db instance does not exists
2525
ErrNotExists = errors.New("instance does not exists")
26+
// ErrInstanceNotReady is thrown when gsql instance is still not marked as Ready
27+
ErrInstanceNotReady = errors.New("instance is not ready")
2628
)

pkg/utils/dbinstance/generic.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,11 @@ func makeInterface(in *Generic) (kcidb.Database, error) {
6666
}
6767
}
6868

69+
func (ins *Generic) state() (string, error) {
70+
logrus.Debug("generic db instance not support a state check")
71+
return "NOT_SUPPORTED", nil
72+
}
73+
6974
func (ins *Generic) exist() error {
7075
db, err := makeInterface(ins)
7176
if err != nil {

pkg/utils/dbinstance/gsql.go

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -202,13 +202,11 @@ func (ins *Gsql) waitUntilRunnable() error {
202202
time.Sleep(delay * time.Second)
203203

204204
err := kci.Retry(10, 60*time.Second, func() error {
205-
instance, err := ins.getInstance()
205+
state, err := ins.state()
206206
if err != nil {
207207
return err
208208
}
209-
logrus.Debugf("waiting gsql instance %s state: %s", ins.Name, instance.State)
210-
211-
if instance.State != "RUNNABLE" {
209+
if state != "RUNNABLE" {
212210
return errors.New("gsql instance not ready yet")
213211
}
214212

@@ -226,6 +224,15 @@ func (ins *Gsql) waitUntilRunnable() error {
226224
return nil
227225
}
228226

227+
func (ins *Gsql) state() (string, error) {
228+
instance, err := ins.getInstance()
229+
if err != nil {
230+
return "", err
231+
}
232+
logrus.Debugf("check gsql instance %s state: %s", ins.Name, instance.State)
233+
return instance.State, nil
234+
}
235+
229236
func (ins *Gsql) create() error {
230237
err := ins.createInstance()
231238
if err != nil {

pkg/utils/dbinstance/gsql_test.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,11 @@ func (ins *Gsql) mockWaitUntilRunnable() error {
1515

1616
time.Sleep(10 * time.Second)
1717

18-
instance, err := ins.getInstance()
18+
state, err := ins.state()
1919
if err != nil {
2020
return err
2121
}
22-
23-
if instance.State != "RUNNABLE" {
22+
if state != "RUNNABLE" {
2423
return errors.New("gsql instance not ready yet")
2524
}
2625

pkg/utils/dbinstance/service.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,15 @@ func Update(ins DbInstance) (map[string]string, error) {
4949
return nil, ErrNotExists
5050
}
5151

52+
state, err := ins.state()
53+
if err != nil {
54+
return nil, err
55+
}
56+
57+
if state != "RUNNABLE" && state != "NOT_SUPPORTED" {
58+
return nil, ErrInstanceNotReady
59+
}
60+
5261
err = ins.update()
5362
if err != nil {
5463
logrus.Debug("update failed")

pkg/utils/dbinstance/types.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,5 +21,6 @@ type DbInstance interface {
2121
exist() error
2222
create() error
2323
update() error
24+
state() (string, error)
2425
getInfoMap() (map[string]string, error)
2526
}

0 commit comments

Comments
 (0)