diff --git a/Makefile b/Makefile index 9aae841c9..5307237c5 100644 --- a/Makefile +++ b/Makefile @@ -572,3 +572,10 @@ release: .PHONY: clean clean: rm -rf .go bin + +# make and load docker image to kind cluster +.PHONY: push-to-kind +push-to-kind: container + @echo "Loading docker image into kind cluster...." + @kind load docker-image $(REGISTRY)/stash-mysql:$(TAG) + @echo "Image has been pushed successfully into kind cluster." diff --git a/pkg/utils.go b/pkg/utils.go index cb9c35bcb..abd91885e 100644 --- a/pkg/utils.go +++ b/pkg/utils.go @@ -18,6 +18,7 @@ package pkg import ( "fmt" + "time" stash "stash.appscode.dev/apimachinery/client/clientset/versioned" "stash.appscode.dev/apimachinery/pkg/restic" @@ -25,6 +26,7 @@ import ( "github.com/codeskyblue/go-sh" "gomodules.xyz/x/log" core "k8s.io/api/core/v1" + "k8s.io/apimachinery/pkg/util/wait" "k8s.io/client-go/kubernetes" "kmodules.xyz/custom-resources/apis/appcatalog/v1alpha1" appcatalog_cs "kmodules.xyz/custom-resources/client/clientset/versioned" @@ -61,13 +63,24 @@ func waitForDBReady(appBinding *v1alpha1.AppBinding, secret *core.Secret, waitTi shell := sh.NewSession() shell.SetEnv(EnvMySqlPassword, string(secret.Data[MySqlPassword])) args := []interface{}{ - "ping", "--host", appBinding.Spec.ClientConfig.Service.Name, - "--user=root", - fmt.Sprintf("--wait=%d", waitTimeout), + "-u", string(secret.Data[MySqlUser]), } if appBinding.Spec.ClientConfig.Service.Port != 0 { args = append(args, fmt.Sprintf("--port=%d", appBinding.Spec.ClientConfig.Service.Port)) } - return shell.Command("mysqladmin", args...).Run() + + // Execute "SELECT 1" query to the database. It should return an error when mysqld is not ready. + args = append(args, "-e", "SELECT 1;") + + // don't show the output of the query + shell.Stdout = nil + return wait.PollImmediate(5*time.Second, time.Duration(waitTimeout)*time.Second, func() (done bool, err error) { + if err := shell.Command("mysql", args...).Run(); err == nil { + log.Infoln("Database is accepting connection....") + return true, nil + } + log.Infoln("Retrying after 5 seconds....") + return false, nil + }) }