Skip to content

Commit

Permalink
[cherry-pick] /cherry-pick (#271) (#283)
Browse files Browse the repository at this point in the history
Signed-off-by: Emruz Hossain <emruz@appscode.com>
  • Loading branch information
1gtm authored Jan 21, 2021
1 parent d63cbc1 commit 1ba0074
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 4 deletions.
7 changes: 7 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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."
21 changes: 17 additions & 4 deletions pkg/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,15 @@ package pkg

import (
"fmt"
"time"

stash "stash.appscode.dev/apimachinery/client/clientset/versioned"
"stash.appscode.dev/apimachinery/pkg/restic"

"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"
Expand Down Expand Up @@ -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
})
}

0 comments on commit 1ba0074

Please sign in to comment.