Skip to content

Commit

Permalink
added monitoring database connection test endpoint to the app (knativ…
Browse files Browse the repository at this point in the history
…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
yt3liu authored and knative-prow-robot committed May 22, 2019
1 parent b1676d9 commit 4783931
Show file tree
Hide file tree
Showing 145 changed files with 6,201 additions and 9,564 deletions.
49 changes: 12 additions & 37 deletions Gopkg.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions tools/monitoring/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,6 @@
# limitations under the License.

all:
go get -u github.com/go-sql-driver/mysql
go get -u gopkg.in/yaml.v2
CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build .
13 changes: 12 additions & 1 deletion tools/monitoring/gke_deployment/monitoring_service.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,22 @@ spec:
containers:
- name: monitoring-app
image: gcr.io/knative-tests/test-infra/monitoring:latest
command: ["/monitoring",
"--database-name=monitoring",
"--database-instance='knative-tests:us-central1:knative-monitoring'"]
# This setting makes nodes pull the docker image every time before
# starting the pod. This is useful when debugging, but should be turned
# off in production.
# TODO(yt3liu) Turn this off once monitoring is more stable
imagePullPolicy: Always
ports:
- name: http-server
containerPort: 8080
containerPort: 8080
volumeMounts:
- name: monitoring-db-credentials
mountPath: /secrets/cloudsql/monitoringdb
readOnly: true
volumes:
- name: monitoring-db-credentials
secret:
secretName: monitoring-db-credentials
63 changes: 60 additions & 3 deletions tools/monitoring/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,36 @@ limitations under the License.
package main

import (
"flag"
"fmt"
"io/ioutil"
"log"
"net/http"
"os"

"github.com/knative/test-infra/tools/monitoring/mysql"
)

const yamlURL = "https://raw.githubusercontent.com/knative/test-infra/master/tools/monitoring/sample.yaml"
var dbConfig mysql.DBConfig

const (
yamlURL = "https://raw.githubusercontent.com/knative/test-infra/master/tools/monitoring/sample.yaml"
dbUserSecretFile = "/secrets/cloudsql/monitoringdb/username"
dbPasswordSecretFile = "/secrets/cloudsql/monitoringdb/password"
)

func main() {
var err error

dbName := flag.String("database-name", "", "The monitoring database name")
dbInst := flag.String("database-instance", "", "The monitoring CloudSQL instance connection name")
flag.Parse()

dbConfig, err = configureMonitoringDatabase(*dbName, *dbInst)
if err != nil {
log.Fatal(err)
}

// use PORT environment variable, or default to 8080
port := "8080"
if fromEnv := os.Getenv("PORT"); fromEnv != "" {
Expand All @@ -34,11 +55,12 @@ func main() {

// register hello function to handle all requests
server := http.NewServeMux()
server.HandleFunc("/", hello)
server.HandleFunc("/hello", hello)
server.HandleFunc("/test-conn", testCloudSQLConn)

// start the web server on port and accept requests
log.Printf("Server listening on port %s", port)
err := http.ListenAndServe(":"+port, server)
err = http.ListenAndServe(":"+port, server)
log.Fatal(err)
}

Expand All @@ -57,3 +79,38 @@ func hello(w http.ResponseWriter, r *http.Request) {
errorPatterns := yamlFile.CollectErrorPatterns()
fmt.Fprintf(w, "error patterns collected from yaml:%s", errorPatterns)
}

func testCloudSQLConn(w http.ResponseWriter, r *http.Request) {
log.Printf("Serving request: %s", r.URL.Path)
log.Println("Testing mysql database connection.")

err := dbConfig.TestConn()
if err != nil {
fmt.Fprintf(w, "Failed to ping the database %v", err)
return
}
fmt.Fprintf(w, "Success\n")
}

func configureMonitoringDatabase(dbName string, dbInst string) (mysql.DBConfig, error) {
var config mysql.DBConfig

user, err := ioutil.ReadFile(dbUserSecretFile)
if err != nil {
return config, err
}

pass, err := ioutil.ReadFile(dbPasswordSecretFile)
if err != nil {
return config, err
}

config = mysql.DBConfig{
Username: string(user),
Password: string(pass),
DatabaseName: dbName,
Instance: dbInst,
}

return config, nil
}
73 changes: 73 additions & 0 deletions tools/monitoring/mysql/db_mysql.go
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)
}
89 changes: 89 additions & 0 deletions vendor/github.com/go-sql-driver/mysql/AUTHORS

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 4783931

Please sign in to comment.