@@ -18,13 +18,15 @@ package pkg
18
18
19
19
import (
20
20
"fmt"
21
+ "time"
21
22
22
23
stash "stash.appscode.dev/apimachinery/client/clientset/versioned"
23
24
"stash.appscode.dev/apimachinery/pkg/restic"
24
25
25
26
"github.com/codeskyblue/go-sh"
26
27
"gomodules.xyz/x/log"
27
28
core "k8s.io/api/core/v1"
29
+ "k8s.io/apimachinery/pkg/util/wait"
28
30
"k8s.io/client-go/kubernetes"
29
31
"kmodules.xyz/custom-resources/apis/appcatalog/v1alpha1"
30
32
appcatalog_cs "kmodules.xyz/custom-resources/client/clientset/versioned"
@@ -61,13 +63,24 @@ func waitForDBReady(appBinding *v1alpha1.AppBinding, secret *core.Secret, waitTi
61
63
shell := sh .NewSession ()
62
64
shell .SetEnv (EnvMySqlPassword , string (secret .Data [MySqlPassword ]))
63
65
args := []interface {}{
64
- "ping" ,
65
66
"--host" , appBinding .Spec .ClientConfig .Service .Name ,
66
- "--user=root" ,
67
- fmt .Sprintf ("--wait=%d" , waitTimeout ),
67
+ "-u" , string (secret .Data [MySqlUser ]),
68
68
}
69
69
if appBinding .Spec .ClientConfig .Service .Port != 0 {
70
70
args = append (args , fmt .Sprintf ("--port=%d" , appBinding .Spec .ClientConfig .Service .Port ))
71
71
}
72
- return shell .Command ("mysqladmin" , args ... ).Run ()
72
+
73
+ // Execute "SELECT 1" query to the database. It should return an error when mysqld is not ready.
74
+ args = append (args , "-e" , "SELECT 1;" )
75
+
76
+ // don't show the output of the query
77
+ shell .Stdout = nil
78
+ return wait .PollImmediate (5 * time .Second , time .Duration (waitTimeout )* time .Second , func () (done bool , err error ) {
79
+ if err := shell .Command ("mysql" , args ... ).Run (); err == nil {
80
+ log .Infoln ("Database is accepting connection...." )
81
+ return true , nil
82
+ }
83
+ log .Infoln ("Retrying after 5 seconds...." )
84
+ return false , nil
85
+ })
73
86
}
0 commit comments