Skip to content

Commit 9271a07

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 9271a07

File tree

6 files changed

+160
-0
lines changed

6 files changed

+160
-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: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
#!/bin/bash
2+
# Copyright (c) 2022 Gitpod GmbH. All rights reserved.
3+
# Licensed under the GNU Affero General Public License (AGPL).
4+
# See License-AGPL.txt in the project root for license information.
5+
6+
7+
set -euo pipefail
8+
9+
DB_IN_CLUSTER_ENABLED="${1:-""}"
10+
DB_CLOUDSQL_ENABLED="${2:-""}"
11+
DB_USERNAME="${3:-""}"
12+
DB_PASSWORD="${4:-""}"
13+
DB_HOST="${5:-""}"
14+
DB_PORT="${6:-""}"
15+
CSP_INSTANCES="${7:-""}"
16+
CSP_CREDENTIALS="${8:-""}"
17+
18+
connection="false"
19+
version=""
20+
21+
DB_TYPE="incluster"
22+
if [ "${DB_IN_CLUSTER_ENABLED}" == "0" ]; then
23+
if [ "${DB_CLOUDSQL_ENABLED}" == "1" ]; then
24+
DB_TYPE="cloudsqlproxy"
25+
else
26+
DB_TYPE="external"
27+
fi
28+
fi
29+
30+
case "${DB_TYPE}" in
31+
cloudsqlproxy | external)
32+
if [ "${DB_TYPE}" = "cloudsqlproxy" ]; then
33+
echo "Connecting to CloudSQLProxy"
34+
35+
CREDENTIALS_FILE="/tmp/credentials.json"
36+
echo "${CSP_CREDENTIALS}" | base64 -d > "${CREDENTIALS_FILE}"
37+
38+
# Config overrides
39+
DB_HOST="0.0.0.0"
40+
DB_PORT="8080"
41+
42+
# This is a long-running process
43+
cloud_sql_proxy \
44+
--instances="${CSP_INSTANCES}=tcp:${DB_PORT}" \
45+
-credential_file="${CREDENTIALS_FILE}" &
46+
47+
# Give it a chance to connect
48+
sleep 5
49+
else
50+
echo "Using external database"
51+
fi
52+
53+
# Check the database version
54+
version_query=$(mysql \
55+
--connect-timeout=5 \
56+
--database=gitpod \
57+
--user="${DB_USERNAME}" \
58+
--password="${DB_PASSWORD}" \
59+
--host="${DB_HOST}" \
60+
--port="${DB_PORT}" \
61+
--execute="SELECT VERSION();" \
62+
--silent \
63+
--raw \
64+
--skip-column-names || echo "fail")
65+
66+
if [ "${version_query}" != "fail" ]; then
67+
connection="true"
68+
version="${version_query}"
69+
fi
70+
;;
71+
incluster)
72+
echo "Using in-cluster database"
73+
connection="true"
74+
version="5.7"
75+
;;
76+
*)
77+
echo "Unknown database type: '${DB_TYPE}'"
78+
exit 1
79+
;;
80+
esac
81+
82+
if [ "${connection}" = "true" ]; then
83+
echo "connection: ok"
84+
else
85+
echo "connection: error"
86+
fi
87+
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: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,19 @@ 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.9
13+
name: database
14+
args:
15+
- '{{repl ConfigOption "db_incluster" }}' # DB_IN_CLUSTER_ENABLED
16+
- '{{repl ConfigOption "db_cloudsql_enabled" }}' # DB_CLOUDSQL_ENABLED
17+
- '{{repl ConfigOption "db_username" }}' # DB_USERNAME
18+
- '{{repl ConfigOption "db_password" }}' # DB_PASSWORD
19+
- '{{repl ConfigOption "db_host" }}' # DB_HOST
20+
- '{{repl ConfigOption "db_port" }}' # DB_PORT
21+
- '{{repl ConfigOption "db_cloudsql_instance" }}' # CloudSQL instances
22+
- '{{repl ConfigOption "db_gcp_credentials" }}' # CloudSQL credentials file
1023
- run:
1124
collectorName: "kernel"
1225
image: alpine/semver
@@ -151,3 +164,23 @@ spec:
151164
message: No default storage class found
152165
- pass:
153166
message: Default storage class found
167+
- textAnalyze:
168+
checkName: Database connection is valid
169+
fileName: database/database.log
170+
regexGroups: 'connection: (?P<Connection>\w+)'
171+
outcomes:
172+
- pass:
173+
when: "Connection == ok"
174+
message: Database connection is valid
175+
- fail:
176+
message: Database connection is invalid. Please check your settings and that the database is accessible from your cluster
177+
- textAnalyze:
178+
checkName: Database version is valid
179+
fileName: database/database.log
180+
regexGroups: 'version: (?P<Version>\d(\.\d+)?)'
181+
outcomes:
182+
- pass:
183+
when: "Version == 5.7"
184+
message: Database version is valid
185+
- warn:
186+
message: Database version could not be verified. This should be MySQL 5.7

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,19 @@ 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.9
13+
name: database
14+
args:
15+
- '{{repl ConfigOption "db_incluster" }}' # DB_IN_CLUSTER_ENABLED
16+
- '{{repl ConfigOption "db_cloudsql_enabled" }}' # DB_CLOUDSQL_ENABLED
17+
- '{{repl ConfigOption "db_username" }}' # DB_USERNAME
18+
- '{{repl ConfigOption "db_password" }}' # DB_PASSWORD
19+
- '{{repl ConfigOption "db_host" }}' # DB_HOST
20+
- '{{repl ConfigOption "db_port" }}' # DB_PORT
21+
- '{{repl ConfigOption "db_cloudsql_instance" }}' # CloudSQL instances
22+
- '{{repl ConfigOption "db_gcp_credentials" }}' # CloudSQL credentials file
1023
- clusterInfo: {}
1124
- clusterResources: {}
1225
- logs:

0 commit comments

Comments
 (0)