Skip to content

[kots]: add database to preflight checks #9759

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 1, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Small optimization suggestion:

For deploying a preview env, we don't need to build this image. We need this only when we deploy a KOTS release. Building this only when the publish-to-kots werft flag is set would help us to not extend the time to wait for a preview env for every dev.

Note: We can handle this in a follow up PR and create an issue and merge the PR as is.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, that would be a good optimisation. Is there an example I can use to follow as I didn't know this was possible?

- 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 \
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Random thought: Do we still get enough log data with the silent flag for the support bundle or should we remove this flag?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes. The --silent flag basically removes all the connection information. At this point, we're only interested in the version so we can assign it to the version_query variable. It is therefore correct that we have the --silent flag.

Also, there's actually no way of debugging a preflight check so we are limited to just putting the data in a known format so we can perform regex queries on it.

--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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think, in .werft/jobs/build/build-and-publish.ts we need to set the version tag in this file, right?

We could get the version with something like this:

const kotsConfigCheckDatabaseTag = exec(`docker run --rm eu.gcr.io/gitpod-core-dev/build/versions:${jobConfig.version} cat /versions.yaml | yq r - 'components.kots-config-check.database.version'`, { silent: true })
            .stdout.trim();

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think so. My understanding is that this will only be built by Werft if it detects a change - as this is very unlikely to change, this won't be built on each push. I think this is more akin to our .gitpod.yml image

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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In .werft/jobs/build/build-and-publish.ts we need to set the version tag in this file. (same as above)

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