Skip to content

Commit aaaebe5

Browse files
author
Simon Emms
committed
[kots]: add database to preflight and support checks
This checks the connection and the version is correct, based upon the configuration given.
1 parent b517587 commit aaaebe5

File tree

6 files changed

+158
-0
lines changed

6 files changed

+158
-0
lines changed

components/BUILD.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ packages:
7070
- components/ws-manager:docker
7171
- components/ws-proxy:docker
7272
- components/ide-proxy:docker
73+
- components/kots-config-check/database:docker
7374
- test:docker
7475
- dev/version-manifest:app
7576
config:
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# Copyright (c) 2022 Gitpod GmbH. All rights reserved.
2+
# Licensed under the GNU Affero General Public License (AGPL).
3+
# See License-AGPL.txt in the project root for license information.
4+
5+
packages:
6+
- name: docker
7+
type: docker
8+
argdeps:
9+
- imageRepoBase
10+
srcs:
11+
- entrypoint.sh
12+
config:
13+
dockerfile: leeway.Dockerfile
14+
metadata:
15+
helm-component: kots-config-check.database
16+
image:
17+
- ${imageRepoBase}/kots-config-check/database:${version}
18+
- ${imageRepoBase}/kots-config-check/database:commit-${__git_commit}
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
#!/bin/bash
2+
3+
set -euo pipefail
4+
5+
DB_IN_CLUSTER_ENABLED="${1:-""}"
6+
DB_CLOUDSQL_ENABLED="${2:-""}"
7+
DB_USERNAME="${3:-""}"
8+
DB_PASSWORD="${4:-""}"
9+
DB_HOST="${5:-""}"
10+
DB_PORT="${6:-""}"
11+
CSP_INSTANCES="${7:-""}"
12+
CSP_CREDENTIALS="${8:-""}"
13+
14+
connection="false"
15+
version=""
16+
17+
DB_TYPE="incluster"
18+
if [ "${DB_IN_CLUSTER_ENABLED}" == "0" ]; then
19+
if [ "${DB_CLOUDSQL_ENABLED}" == "1" ]; then
20+
DB_TYPE="cloudsqlproxy"
21+
else
22+
DB_TYPE="external"
23+
fi
24+
fi
25+
26+
case "${DB_TYPE}" in
27+
cloudsqlproxy | external)
28+
if [ "${DB_TYPE}" = "cloudsqlproxy" ]; then
29+
echo "Connecting to CloudSQLProxy"
30+
31+
CREDENTIALS_FILE="/tmp/credentials.json"
32+
echo "${CSP_CREDENTIALS}" | base64 -d > "${CREDENTIALS_FILE}"
33+
34+
# Config overrides
35+
DB_HOST="0.0.0.0"
36+
DB_PORT="8080"
37+
38+
# This is a long-running process
39+
cloud_sql_proxy \
40+
--instances="${CSP_INSTANCES}=tcp:${DB_PORT}" \
41+
-credential_file="${CREDENTIALS_FILE}" &
42+
43+
# Give it a chance to connect
44+
sleep 5
45+
else
46+
echo "Using external database"
47+
fi
48+
49+
# Check the database version
50+
version_query=$(mysql \
51+
--connect-timeout=5 \
52+
--database=gitpod \
53+
--user="${DB_USERNAME}" \
54+
--password="${DB_PASSWORD}" \
55+
--host="${DB_HOST}" \
56+
--port="${DB_PORT}" \
57+
--execute="SELECT VERSION();" \
58+
--silent \
59+
--raw \
60+
--skip-column-names || echo "fail")
61+
62+
if [ "${version_query}" != "fail" ]; then
63+
connection="true"
64+
version="${version_query}"
65+
fi
66+
;;
67+
incluster)
68+
echo "Using in-cluster database"
69+
connection="true"
70+
version="5.7"
71+
;;
72+
*)
73+
echo "Unknown database type: '${DB_TYPE}'"
74+
exit 1
75+
;;
76+
esac
77+
78+
if [ "${connection}" = "true" ]; then
79+
echo "connection: ok"
80+
else
81+
echo "connection: error"
82+
fi
83+
echo "version: ${version}"
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# Copyright (c) 2022 Gitpod GmbH. All rights reserved.
2+
# Licensed under the GNU Affero General Public License (AGPL).
3+
# See License-AGPL.txt in the project root for license information.
4+
5+
FROM bitnami/mysql:5.7
6+
COPY --from=gcr.io/cloudsql-docker/gce-proxy /cloud_sql_proxy /usr/local/bin/cloud_sql_proxy
7+
COPY entrypoint.sh /entrypoint.sh
8+
ENTRYPOINT [ "/entrypoint.sh" ]

install/kots/manifests/kots-preflight.yaml

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,20 @@ metadata:
77
name: gitpod
88
spec:
99
collectors:
10+
- run:
11+
collectorName: database
12+
image: eu.gcr.io/gitpod-core-dev/build/kots-config-check/database:sje-kots-config-check.8
13+
imagePullPolicy: Always
14+
name: database
15+
args:
16+
- '{{repl ConfigOption "db_incluster" }}' # DB_IN_CLUSTER_ENABLED
17+
- '{{repl ConfigOption "db_cloudsql_enabled" }}' # DB_CLOUDSQL_ENABLED
18+
- '{{repl ConfigOption "db_username" }}' # DB_USERNAME
19+
- '{{repl ConfigOption "db_password" }}' # DB_PASSWORD
20+
- '{{repl ConfigOption "db_host" }}' # DB_HOST
21+
- '{{repl ConfigOption "db_port" }}' # DB_PORT
22+
- '{{repl ConfigOption "db_cloudsql_instance" }}' # CloudSQL instances
23+
- '{{repl ConfigOption "db_gcp_credentials" }}' # CloudSQL credentials file
1024
- run:
1125
collectorName: "kernel"
1226
image: alpine/semver
@@ -151,3 +165,23 @@ spec:
151165
message: No default storage class found
152166
- pass:
153167
message: Default storage class found
168+
- textAnalyze:
169+
checkName: Database connection is valid
170+
fileName: database/database.log
171+
regexGroups: 'connection: (?P<Connection>\w+)'
172+
outcomes:
173+
- pass:
174+
when: "Connection == ok"
175+
message: Database connection is valid
176+
- fail:
177+
message: Database connection is invalid. Please check your settings and that the database is accessible from your cluster
178+
- textAnalyze:
179+
checkName: Database version is valid
180+
fileName: database/database.log
181+
regexGroups: 'version: (?P<Version>\d(\.\d+)?)'
182+
outcomes:
183+
- pass:
184+
when: "Version == 5.7"
185+
message: Database version is valid
186+
- warn:
187+
message: Database version could not be verified. This should be MySQL 5.7

install/kots/manifests/kots-support-bundle.yaml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,20 @@ metadata:
77
name: gitpod
88
spec:
99
collectors:
10+
- run:
11+
collectorName: database
12+
image: eu.gcr.io/gitpod-core-dev/build/kots-config-check/database:sje-kots-config-check.8
13+
imagePullPolicy: Always
14+
name: database
15+
args:
16+
- '{{repl ConfigOption "db_incluster" }}' # DB_IN_CLUSTER_ENABLED
17+
- '{{repl ConfigOption "db_cloudsql_enabled" }}' # DB_CLOUDSQL_ENABLED
18+
- '{{repl ConfigOption "db_username" }}' # DB_USERNAME
19+
- '{{repl ConfigOption "db_password" }}' # DB_PASSWORD
20+
- '{{repl ConfigOption "db_host" }}' # DB_HOST
21+
- '{{repl ConfigOption "db_port" }}' # DB_PORT
22+
- '{{repl ConfigOption "db_cloudsql_instance" }}' # CloudSQL instances
23+
- '{{repl ConfigOption "db_gcp_credentials" }}' # CloudSQL credentials file
1024
- clusterInfo: {}
1125
- clusterResources: {}
1226
- logs:

0 commit comments

Comments
 (0)