Skip to content

Commit

Permalink
[kots]: add database to preflight and support checks
Browse files Browse the repository at this point in the history
This checks the connection and the version is correct, based upon the
configuration given.
  • Loading branch information
Simon Emms authored and roboquat committed Jun 1, 2022
1 parent 135a7de commit 1166474
Show file tree
Hide file tree
Showing 6 changed files with 160 additions and 0 deletions.
1 change: 1 addition & 0 deletions components/BUILD.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ packages:
- components/ws-manager:docker
- components/ws-proxy:docker
- components/ide-proxy:docker
- components/kots-config-check/database:docker
- test:docker
- dev/version-manifest:app
config:
Expand Down
18 changes: 18 additions & 0 deletions components/kots-config-check/database/BUILD.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Copyright (c) 2022 Gitpod GmbH. All rights reserved.
# Licensed under the GNU Affero General Public License (AGPL).
# See License-AGPL.txt in the project root for license information.

packages:
- name: docker
type: docker
argdeps:
- imageRepoBase
srcs:
- entrypoint.sh
config:
dockerfile: leeway.Dockerfile
metadata:
helm-component: kots-config-check.database
image:
- ${imageRepoBase}/kots-config-check/database:${version}
- ${imageRepoBase}/kots-config-check/database:commit-${__git_commit}
87 changes: 87 additions & 0 deletions components/kots-config-check/database/entrypoint.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
#!/bin/bash
# Copyright (c) 2022 Gitpod GmbH. All rights reserved.
# Licensed under the GNU Affero General Public License (AGPL).
# See License-AGPL.txt in the project root for license information.


set -euo pipefail

DB_IN_CLUSTER_ENABLED="${1:-""}"
DB_CLOUDSQL_ENABLED="${2:-""}"
DB_USERNAME="${3:-""}"
DB_PASSWORD="${4:-""}"
DB_HOST="${5:-""}"
DB_PORT="${6:-""}"
CSP_INSTANCES="${7:-""}"
CSP_CREDENTIALS="${8:-""}"

connection="false"
version=""

DB_TYPE="incluster"
if [ "${DB_IN_CLUSTER_ENABLED}" == "0" ]; then
if [ "${DB_CLOUDSQL_ENABLED}" == "1" ]; then
DB_TYPE="cloudsqlproxy"
else
DB_TYPE="external"
fi
fi

case "${DB_TYPE}" in
cloudsqlproxy | external)
if [ "${DB_TYPE}" = "cloudsqlproxy" ]; then
echo "Connecting to CloudSQLProxy"

CREDENTIALS_FILE="/tmp/credentials.json"
echo "${CSP_CREDENTIALS}" | base64 -d > "${CREDENTIALS_FILE}"

# Config overrides
DB_HOST="0.0.0.0"
DB_PORT="8080"

# This is a long-running process
cloud_sql_proxy \
--instances="${CSP_INSTANCES}=tcp:${DB_PORT}" \
-credential_file="${CREDENTIALS_FILE}" &

# Give it a chance to connect
sleep 5
else
echo "Using external database"
fi

# Check the database version
version_query=$(mysql \
--connect-timeout=5 \
--database=gitpod \
--user="${DB_USERNAME}" \
--password="${DB_PASSWORD}" \
--host="${DB_HOST}" \
--port="${DB_PORT}" \
--execute="SELECT VERSION();" \
--silent \
--raw \
--skip-column-names || echo "fail")

if [ "${version_query}" != "fail" ]; then
connection="true"
version="${version_query}"
fi
;;
incluster)
echo "Using in-cluster database"
connection="true"
version="5.7"
;;
*)
echo "Unknown database type: '${DB_TYPE}'"
exit 1
;;
esac

if [ "${connection}" = "true" ]; then
echo "connection: ok"
else
echo "connection: error"
fi
echo "version: ${version}"
8 changes: 8 additions & 0 deletions components/kots-config-check/database/leeway.Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# Copyright (c) 2022 Gitpod GmbH. All rights reserved.
# Licensed under the GNU Affero General Public License (AGPL).
# See License-AGPL.txt in the project root for license information.

FROM bitnami/mysql:5.7
COPY --from=gcr.io/cloudsql-docker/gce-proxy /cloud_sql_proxy /usr/local/bin/cloud_sql_proxy
COPY entrypoint.sh /entrypoint.sh
ENTRYPOINT [ "/entrypoint.sh" ]
33 changes: 33 additions & 0 deletions install/kots/manifests/kots-preflight.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,19 @@ metadata:
name: gitpod
spec:
collectors:
- run:
collectorName: database
image: eu.gcr.io/gitpod-core-dev/build/kots-config-check/database:sje-kots-config-check.9
name: database
args:
- '{{repl ConfigOption "db_incluster" }}' # DB_IN_CLUSTER_ENABLED
- '{{repl ConfigOption "db_cloudsql_enabled" }}' # DB_CLOUDSQL_ENABLED
- '{{repl ConfigOption "db_username" }}' # DB_USERNAME
- '{{repl ConfigOption "db_password" }}' # DB_PASSWORD
- '{{repl ConfigOption "db_host" }}' # DB_HOST
- '{{repl ConfigOption "db_port" }}' # DB_PORT
- '{{repl ConfigOption "db_cloudsql_instance" }}' # CloudSQL instances
- '{{repl ConfigOption "db_gcp_credentials" }}' # CloudSQL credentials file
- run:
collectorName: "kernel"
image: alpine/semver
Expand Down Expand Up @@ -151,3 +164,23 @@ spec:
message: No default storage class found
- pass:
message: Default storage class found
- textAnalyze:
checkName: Database connection is valid
fileName: database/database.log
regexGroups: 'connection: (?P<Connection>\w+)'
outcomes:
- pass:
when: "Connection == ok"
message: Database connection is valid
- fail:
message: Database connection is invalid. Please check your settings and that the database is accessible from your cluster
- textAnalyze:
checkName: Database version is valid
fileName: database/database.log
regexGroups: 'version: (?P<Version>\d(\.\d+)?)'
outcomes:
- pass:
when: "Version == 5.7"
message: Database version is valid
- warn:
message: Database version could not be verified. This should be MySQL 5.7
13 changes: 13 additions & 0 deletions install/kots/manifests/kots-support-bundle.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,19 @@ metadata:
name: gitpod
spec:
collectors:
- run:
collectorName: database
image: eu.gcr.io/gitpod-core-dev/build/kots-config-check/database:sje-kots-config-check.9
name: database
args:
- '{{repl ConfigOption "db_incluster" }}' # DB_IN_CLUSTER_ENABLED
- '{{repl ConfigOption "db_cloudsql_enabled" }}' # DB_CLOUDSQL_ENABLED
- '{{repl ConfigOption "db_username" }}' # DB_USERNAME
- '{{repl ConfigOption "db_password" }}' # DB_PASSWORD
- '{{repl ConfigOption "db_host" }}' # DB_HOST
- '{{repl ConfigOption "db_port" }}' # DB_PORT
- '{{repl ConfigOption "db_cloudsql_instance" }}' # CloudSQL instances
- '{{repl ConfigOption "db_gcp_credentials" }}' # CloudSQL credentials file
- clusterInfo: {}
- clusterResources: {}
- logs:
Expand Down

0 comments on commit 1166474

Please sign in to comment.