Skip to content

Commit 486375b

Browse files
author
marcel
committed
added support for waiting for sql server
1 parent 0263b6b commit 486375b

File tree

3 files changed

+94
-0
lines changed

3 files changed

+94
-0
lines changed

cmd/sql.go

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
/*
2+
Copyright © 2025 NAME HERE <EMAIL ADDRESS>
3+
*/
4+
package cmd
5+
6+
import (
7+
"database/sql"
8+
"fmt"
9+
"log"
10+
"time"
11+
12+
_ "github.com/go-sql-driver/mysql"
13+
14+
"github.com/spf13/cobra"
15+
)
16+
17+
// sqlCmd represents the sql command
18+
var sqlCmd = &cobra.Command{
19+
Use: "sql",
20+
Short: "Wait for SQL connection",
21+
Long: `Wait for SQL connection until available
22+
23+
Simple example:
24+
waitfor sql -u root -p mysecretpassword -s mariadb.mydatabase.cluster.local -d mydb
25+
26+
Example, if you have a non-standard port, set it with -P, default is 3306:
27+
waitfor sql -u root -p mysecretpassword -s mariadb.mydatabase.cluster.local -P 3307 -d mydb
28+
`,
29+
Run: func(cmd *cobra.Command, args []string) {
30+
31+
user, _ := cmd.Flags().GetString("user")
32+
password, _ := cmd.Flags().GetString("password")
33+
server, _ := cmd.Flags().GetString("server")
34+
port, _ := cmd.Flags().GetString("port")
35+
database, _ := cmd.Flags().GetString("database")
36+
37+
// check if required flags are set
38+
if user == "" || password == "" || server == "" || database == "" {
39+
log.Fatalf("Error: user, password, server and database are required")
40+
}
41+
42+
// build dsn
43+
dsn := user + ":" + password + "@tcp(" + server + ":" + port + ")/" + database
44+
45+
// Try to connect with db
46+
db, err := waitForDB(dsn, 10, time.Second*time.Duration(timer))
47+
if err != nil {
48+
log.Fatalf("DB connection failed: %v", err)
49+
}
50+
defer db.Close()
51+
52+
log.Println("Connection established!")
53+
},
54+
}
55+
56+
func init() {
57+
rootCmd.AddCommand(sqlCmd)
58+
sqlCmd.Flags().StringP("user", "u", "", "Database user")
59+
sqlCmd.Flags().StringP("password", "p", "", "Database password")
60+
sqlCmd.Flags().StringP("server", "s", "", "Database server")
61+
sqlCmd.Flags().StringP("port", "P", "3306", "Database port")
62+
sqlCmd.Flags().StringP("database", "d", "", "Database name")
63+
}
64+
65+
func waitForDB(dsn string, retries int, delay time.Duration) (*sql.DB, error) {
66+
var db *sql.DB
67+
var err error
68+
69+
for i := 0; i < retries; i++ {
70+
// Open does not establish a connection immediately, it just prepares the handle
71+
db, err = sql.Open("mysql", dsn)
72+
if err != nil {
73+
log.Printf("Error opening DB handle: %v", err)
74+
} else {
75+
// Ping actually checks if the DB is reachable
76+
err = db.Ping()
77+
if err == nil {
78+
return db, nil // success
79+
}
80+
log.Printf("DB not ready yet: %v", err)
81+
}
82+
83+
// Wait before trying again
84+
time.Sleep(delay)
85+
}
86+
87+
return nil, fmt.Errorf("could not connect after %d retries: %w", retries, err)
88+
}

go.mod

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,15 @@ module waitfor
33
go 1.25.1
44

55
require (
6+
github.com/go-sql-driver/mysql v1.9.3
67
github.com/spf13/cobra v1.10.1
78
k8s.io/api v0.34.1
89
k8s.io/apimachinery v0.34.1
910
k8s.io/client-go v0.34.1
1011
)
1112

1213
require (
14+
filippo.io/edwards25519 v1.1.0 // indirect
1315
github.com/davecgh/go-spew v1.1.1 // indirect
1416
github.com/emicklei/go-restful/v3 v3.12.2 // indirect
1517
github.com/fxamacker/cbor/v2 v2.9.0 // indirect

go.sum

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
filippo.io/edwards25519 v1.1.0 h1:FNf4tywRC1HmFuKW5xopWpigGjJKiJSV0Cqo0cJWDaA=
2+
filippo.io/edwards25519 v1.1.0/go.mod h1:BxyFTGdWcka3PhytdK4V28tE5sGfRvvvRV7EaN4VDT4=
13
github.com/cpuguy83/go-md2man/v2 v2.0.6/go.mod h1:oOW0eioCTA6cOiMLiUPZOpcVxMig6NIQQ7OS05n1F4g=
24
github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=
35
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
@@ -17,6 +19,8 @@ github.com/go-openapi/jsonreference v0.20.2/go.mod h1:Bl1zwGIM8/wsvqjsOQLJ/SH+En
1719
github.com/go-openapi/swag v0.22.3/go.mod h1:UzaqsxGiab7freDnrUUra0MwWfN/q7tE4j+VcZ0yl14=
1820
github.com/go-openapi/swag v0.23.0 h1:vsEVJDUo2hPJ2tu0/Xc+4noaxyEffXNIs3cOULZ+GrE=
1921
github.com/go-openapi/swag v0.23.0/go.mod h1:esZ8ITTYEsH1V2trKHjAN8Ai7xHb8RV+YSZ577vPjgQ=
22+
github.com/go-sql-driver/mysql v1.9.3 h1:U/N249h2WzJ3Ukj8SowVFjdtZKfu9vlLZxjPXV1aweo=
23+
github.com/go-sql-driver/mysql v1.9.3/go.mod h1:qn46aNg1333BRMNU69Lq93t8du/dwxI64Gl8i5p1WMU=
2024
github.com/go-task/slim-sprig/v3 v3.0.0 h1:sUs3vkvUymDpBKi3qH1YSqBQk9+9D/8M2mN1vB6EwHI=
2125
github.com/go-task/slim-sprig/v3 v3.0.0/go.mod h1:W848ghGpv3Qj3dhTPRyJypKRiqCdHZiAzKg9hl15HA8=
2226
github.com/gogo/protobuf v1.3.2 h1:Ov1cvc58UF3b5XjBnZv7+opcTcQFZebYjWzi34vdm4Q=

0 commit comments

Comments
 (0)