From c547162e52f09fe31ed8bb15750c93c7b6478d75 Mon Sep 17 00:00:00 2001 From: Simon Emms Date: Mon, 15 Nov 2021 18:35:32 +0000 Subject: [PATCH 1/2] [installer]: configure application to use external database --- .../pkg/components/database/cloudsql/constants.go | 14 +++++--------- .../pkg/components/database/cloudsql/objects.go | 8 +++++--- .../pkg/components/database/external/objects.go | 14 ++++++++++++++ .../database/{cloudsql => init}/configmap.go | 6 +++--- .../pkg/components/database/init/constants.go | 13 +++++++++++++ .../files}/00-create-and-init-sessions-db.sql | 0 .../components/database/{cloudsql => init}/job.go | 8 ++++---- installer/pkg/components/database/init/objects.go | 15 +++++++++++++++ installer/pkg/components/database/objects.go | 8 ++++++++ 9 files changed, 67 insertions(+), 19 deletions(-) create mode 100644 installer/pkg/components/database/external/objects.go rename installer/pkg/components/database/{cloudsql => init}/configmap.go (94%) create mode 100644 installer/pkg/components/database/init/constants.go rename installer/pkg/components/database/{cloudsql/init => init/files}/00-create-and-init-sessions-db.sql (100%) rename installer/pkg/components/database/{cloudsql => init}/job.go (92%) create mode 100644 installer/pkg/components/database/init/objects.go diff --git a/installer/pkg/components/database/cloudsql/constants.go b/installer/pkg/components/database/cloudsql/constants.go index d3b97ddf2a332d..c3f5de32670f03 100644 --- a/installer/pkg/components/database/cloudsql/constants.go +++ b/installer/pkg/components/database/cloudsql/constants.go @@ -5,13 +5,9 @@ package cloudsql const ( - Component = "cloudsqlproxy" - dbSessionsImage = "mysql" - dbSessionsTag = "5.7.34" - ImageRepo = "b.gcr.io/cloudsql-docker" - ImageName = "gce-proxy" - ImageVersion = "1.11" - initScriptDir = "init" - Port = 3306 - SQLInitScripts = "db-init-scripts" + Component = "cloudsqlproxy" + ImageRepo = "b.gcr.io/cloudsql-docker" + ImageName = "gce-proxy" + ImageVersion = "1.11" + Port = 3306 ) diff --git a/installer/pkg/components/database/cloudsql/objects.go b/installer/pkg/components/database/cloudsql/objects.go index e17ee6801ea6d9..d984ac91c78846 100644 --- a/installer/pkg/components/database/cloudsql/objects.go +++ b/installer/pkg/components/database/cloudsql/objects.go @@ -4,12 +4,14 @@ package cloudsql -import "github.com/gitpod-io/gitpod/installer/pkg/common" +import ( + "github.com/gitpod-io/gitpod/installer/pkg/common" + dbinit "github.com/gitpod-io/gitpod/installer/pkg/components/database/init" +) var Objects = common.CompositeRenderFunc( - configmap, deployment, - job, + dbinit.Objects, common.DefaultServiceAccount(Component), common.GenerateService(Component, map[string]common.ServicePort{ Component: { diff --git a/installer/pkg/components/database/external/objects.go b/installer/pkg/components/database/external/objects.go new file mode 100644 index 00000000000000..40bc7d57b99dae --- /dev/null +++ b/installer/pkg/components/database/external/objects.go @@ -0,0 +1,14 @@ +// Copyright (c) 2021 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. + +package external + +import ( + "github.com/gitpod-io/gitpod/installer/pkg/common" + dbinit "github.com/gitpod-io/gitpod/installer/pkg/components/database/init" +) + +var Objects = common.CompositeRenderFunc( + dbinit.Objects, +) diff --git a/installer/pkg/components/database/cloudsql/configmap.go b/installer/pkg/components/database/init/configmap.go similarity index 94% rename from installer/pkg/components/database/cloudsql/configmap.go rename to installer/pkg/components/database/init/configmap.go index f2d12ff190607e..5e1679a67b20bb 100644 --- a/installer/pkg/components/database/cloudsql/configmap.go +++ b/installer/pkg/components/database/init/configmap.go @@ -2,7 +2,7 @@ // Licensed under the GNU Affero General Public License (AGPL). // See License-AGPL.txt in the project root for license information. -package cloudsql +package init import ( "embed" @@ -14,7 +14,7 @@ import ( "k8s.io/apimachinery/pkg/runtime" ) -//go:embed init/*.sql +//go:embed files/*.sql var initScriptFiles embed.FS func configmap(ctx *common.RenderContext) ([]runtime.Object, error) { @@ -42,7 +42,7 @@ func configmap(ctx *common.RenderContext) ([]runtime.Object, error) { &corev1.ConfigMap{ TypeMeta: common.TypeMetaConfigmap, ObjectMeta: metav1.ObjectMeta{ - Name: SQLInitScripts, + Name: sqlInitScripts, Namespace: ctx.Namespace, Labels: common.DefaultLabels(Component), }, diff --git a/installer/pkg/components/database/init/constants.go b/installer/pkg/components/database/init/constants.go new file mode 100644 index 00000000000000..d59390331d27a7 --- /dev/null +++ b/installer/pkg/components/database/init/constants.go @@ -0,0 +1,13 @@ +// Copyright (c) 2021 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. + +package init + +const ( + Component = "dbinit" + dbSessionsImage = "mysql" + dbSessionsTag = "5.7.34" + initScriptDir = "files" + sqlInitScripts = "db-init-scripts" +) diff --git a/installer/pkg/components/database/cloudsql/init/00-create-and-init-sessions-db.sql b/installer/pkg/components/database/init/files/00-create-and-init-sessions-db.sql similarity index 100% rename from installer/pkg/components/database/cloudsql/init/00-create-and-init-sessions-db.sql rename to installer/pkg/components/database/init/files/00-create-and-init-sessions-db.sql diff --git a/installer/pkg/components/database/cloudsql/job.go b/installer/pkg/components/database/init/job.go similarity index 92% rename from installer/pkg/components/database/cloudsql/job.go rename to installer/pkg/components/database/init/job.go index e3cb0e9b8ebd40..6c066e350622b4 100644 --- a/installer/pkg/components/database/cloudsql/job.go +++ b/installer/pkg/components/database/init/job.go @@ -4,7 +4,7 @@ // This runs the init scripts in a non-inCluster DB instance -package cloudsql +package init import ( "fmt" @@ -35,9 +35,9 @@ func job(ctx *common.RenderContext) ([]runtime.Object, error) { ServiceAccountName: Component, EnableServiceLinks: pointer.Bool(false), Volumes: []corev1.Volume{{ - Name: SQLInitScripts, + Name: sqlInitScripts, VolumeSource: corev1.VolumeSource{ConfigMap: &corev1.ConfigMapVolumeSource{ - LocalObjectReference: corev1.LocalObjectReference{Name: SQLInitScripts}, + LocalObjectReference: corev1.LocalObjectReference{Name: sqlInitScripts}, }}, }}, // The init container is designed to emulate Helm hooks @@ -55,7 +55,7 @@ func job(ctx *common.RenderContext) ([]runtime.Object, error) { "mysql -h $DB_HOST --port $DB_PORT -u $DB_USERNAME -p$DB_PASSWORD < /db-init-scripts/init.sql", }, VolumeMounts: []corev1.VolumeMount{{ - Name: SQLInitScripts, + Name: sqlInitScripts, MountPath: "/db-init-scripts", ReadOnly: true, }}, diff --git a/installer/pkg/components/database/init/objects.go b/installer/pkg/components/database/init/objects.go new file mode 100644 index 00000000000000..2a17284edff4d0 --- /dev/null +++ b/installer/pkg/components/database/init/objects.go @@ -0,0 +1,15 @@ +// Copyright (c) 2021 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. + +package init + +import ( + "github.com/gitpod-io/gitpod/installer/pkg/common" +) + +var Objects = common.CompositeRenderFunc( + configmap, + job, + common.DefaultServiceAccount(Component), +) diff --git a/installer/pkg/components/database/objects.go b/installer/pkg/components/database/objects.go index f490c9b4390836..3bdd949e1f4f7a 100644 --- a/installer/pkg/components/database/objects.go +++ b/installer/pkg/components/database/objects.go @@ -7,6 +7,7 @@ package database import ( "github.com/gitpod-io/gitpod/installer/pkg/common" "github.com/gitpod-io/gitpod/installer/pkg/components/database/cloudsql" + "github.com/gitpod-io/gitpod/installer/pkg/components/database/external" "github.com/gitpod-io/gitpod/installer/pkg/components/database/incluster" "k8s.io/apimachinery/pkg/runtime" "k8s.io/utils/pointer" @@ -16,6 +17,10 @@ func cloudSqlEnabled(cfg *common.RenderContext) bool { return !pointer.BoolDeref(cfg.Config.Database.InCluster, false) && cfg.Config.Database.CloudSQL != nil } +func externalEnabled(cfg *common.RenderContext) bool { + return !pointer.BoolDeref(cfg.Config.Database.InCluster, false) && cfg.Config.Database.External != nil +} + func inClusterEnabled(cfg *common.RenderContext) bool { return pointer.BoolDeref(cfg.Config.Database.InCluster, false) } @@ -28,6 +33,9 @@ var Objects = common.CompositeRenderFunc( if cloudSqlEnabled(cfg) { return cloudsql.Objects(cfg) } + if externalEnabled(cfg) { + return external.Objects(cfg) + } return nil, nil }), ) From 1e36df49c897da4ac03474c2cd14a520b328aa28 Mon Sep 17 00:00:00 2001 From: Simon Emms Date: Mon, 15 Nov 2021 18:41:02 +0000 Subject: [PATCH 2/2] [installer]: validate the external db secret --- installer/pkg/config/v1/validation.go | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/installer/pkg/config/v1/validation.go b/installer/pkg/config/v1/validation.go index a8057fc855b3b8..62c1f2c16630f1 100644 --- a/installer/pkg/config/v1/validation.go +++ b/installer/pkg/config/v1/validation.go @@ -87,5 +87,10 @@ func (v version) ClusterValidation(rcfg interface{}) cluster.ValidationChecks { res = append(res, cluster.CheckSecret(secretName, cluster.CheckSecretRequiredData("credentials.json", "encryptionKeys", "password", "username"))) } + if cfg.Database.External != nil { + secretName := cfg.Database.External.Certificate.Name + res = append(res, cluster.CheckSecret(secretName, cluster.CheckSecretRequiredData("encryptionKeys", "host", "password", "port", "username"))) + } + return res }