forked from knative/client
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added monitoring database connection test endpoint to the app (knativ…
…e#778) * added monitoring database connection test endpoint to the skeleton app * connect to the cloudsql database without using cloudsql proxy * updated dep to include cloudsql, mysql driver * read the secrets directly and pass database instance information in flags
- Loading branch information
1 parent
b1676d9
commit 4783931
Showing
145 changed files
with
6,201 additions
and
9,564 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
/* | ||
Copyright 2019 The Knative Authors | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package mysql | ||
|
||
import ( | ||
"database/sql" | ||
"database/sql/driver" | ||
"fmt" | ||
|
||
_ "github.com/go-sql-driver/mysql" | ||
) | ||
|
||
const driverName = "mysql" | ||
|
||
// DBConfig is the configuration used to connection to database | ||
type DBConfig struct { | ||
Username string | ||
Password string | ||
Instance string | ||
DatabaseName string | ||
} | ||
|
||
func (c DBConfig) TestConn() error { | ||
conn, err := c.getConn() | ||
if err != nil { | ||
return err | ||
} | ||
defer conn.Close() | ||
|
||
return nil | ||
} | ||
|
||
func (c DBConfig) getConn() (*sql.DB, error) { | ||
conn, err := sql.Open(driverName, c.dataStoreName(c.DatabaseName)) | ||
if err != nil { | ||
return nil, fmt.Errorf("could not get a connection: %v", err) | ||
} | ||
|
||
if conn.Ping() == driver.ErrBadConn { | ||
return nil, fmt.Errorf("could not connect to the datastore. " + | ||
"could be bad address, or this address is inaccessible from your host.\n") | ||
} | ||
|
||
return conn, nil | ||
} | ||
|
||
func (c DBConfig) dataStoreName(dbName string) string { | ||
var cred string | ||
// [username[:password]@] | ||
if len(c.Username) > 0 { | ||
cred = c.Username | ||
if len(c.Password) > 0 { | ||
cred = cred + ":" + c.Password | ||
} | ||
cred = cred + "@" | ||
} | ||
|
||
return fmt.Sprintf("%sunix(%s)/%s", cred, "/cloudsql/"+c.Instance, dbName) | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.