Skip to content

Commit

Permalink
[installer]: trigger a migration job when DB is ready
Browse files Browse the repository at this point in the history
  • Loading branch information
Simon Emms committed Oct 26, 2021
1 parent 9eb84b6 commit 6c8b206
Show file tree
Hide file tree
Showing 6 changed files with 118 additions and 0 deletions.
4 changes: 4 additions & 0 deletions installer/pkg/common/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -484,6 +484,10 @@ var (
APIVersion: "v1",
Kind: "ResourceQuota",
}
TypeMetaBatchJob = metav1.TypeMeta{
APIVersion: "batch/v1",
Kind: "Job",
}
)

type TLS struct {
Expand Down
2 changes: 2 additions & 0 deletions installer/pkg/components/components.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"github.com/gitpod-io/gitpod/installer/pkg/components/gitpod"
imagebuildermk3 "github.com/gitpod-io/gitpod/installer/pkg/components/image-builder-mk3"
jaegeroperator "github.com/gitpod-io/gitpod/installer/pkg/components/jaeger-operator"
"github.com/gitpod-io/gitpod/installer/pkg/components/migrations"
"github.com/gitpod-io/gitpod/installer/pkg/components/minio"
"github.com/gitpod-io/gitpod/installer/pkg/components/mysql"
openvsxproxy "github.com/gitpod-io/gitpod/installer/pkg/components/openvsx-proxy"
Expand All @@ -34,6 +35,7 @@ var MetaObjects = common.CompositeRenderFunc(
proxy.Objects,
dashboard.Objects,
imagebuildermk3.Objects,
migrations.Objects,
mysql.Objects,
openvsxproxy.Objects,
rabbitmq.Objects,
Expand Down
9 changes: 9 additions & 0 deletions installer/pkg/components/migrations/constants.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// 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 migrations

const (
Component = "migrations"
)
53 changes: 53 additions & 0 deletions installer/pkg/components/migrations/job.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
// 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 migrations

import (
"github.com/gitpod-io/gitpod/installer/pkg/common"
batchv1 "k8s.io/api/batch/v1"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/utils/pointer"
)

func job(ctx *common.RenderContext) ([]runtime.Object, error) {
objectMeta := metav1.ObjectMeta{
Name: Component,
Namespace: ctx.Namespace,
Labels: common.DefaultLabels(Component),
}

return []runtime.Object{&batchv1.Job{
TypeMeta: common.TypeMetaBatchJob,
ObjectMeta: objectMeta,
Spec: batchv1.JobSpec{
Template: corev1.PodTemplateSpec{
ObjectMeta: objectMeta,
Spec: corev1.PodSpec{
Affinity: &corev1.Affinity{},
RestartPolicy: corev1.RestartPolicyNever,
ServiceAccountName: Component,
EnableServiceLinks: pointer.Bool(false),
// The init container is designed to emulate Helm hooks
InitContainers: []corev1.Container{*common.DatabaseWaiterContainer(ctx)},
Containers: []corev1.Container{{
Name: Component,
Image: common.ImageName(ctx.Config.Repository, "db-migrations", ctx.VersionManifest.Components.DBMigrations.Version),
ImagePullPolicy: corev1.PullIfNotPresent,
Env: common.MergeEnv(
common.DatabaseEnv(&ctx.Config),
),
Command: []string{
"sh",
"-c",
"cd /app/node_modules/@gitpod/gitpod-db && yarn run wait-for-db && yarn run typeorm migrations:run",
},
}},
},
},
},
}}, nil
}
15 changes: 15 additions & 0 deletions installer/pkg/components/migrations/objects.go
Original file line number Diff line number Diff line change
@@ -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 migrations

import (
"github.com/gitpod-io/gitpod/installer/pkg/common"
)

var Objects = common.CompositeRenderFunc(
job,
rolebinding,
common.DefaultServiceAccount(Component),
)
35 changes: 35 additions & 0 deletions installer/pkg/components/migrations/rolebinding.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// 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 migrations

import (
"fmt"
"github.com/gitpod-io/gitpod/installer/pkg/common"
rbacv1 "k8s.io/api/rbac/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
)

func rolebinding(ctx *common.RenderContext) ([]runtime.Object, error) {
labels := common.DefaultLabels(Component)

return []runtime.Object{&rbacv1.RoleBinding{
TypeMeta: common.TypeMetaRoleBinding,
ObjectMeta: metav1.ObjectMeta{
Name: Component,
Namespace: ctx.Namespace,
Labels: labels,
},
RoleRef: rbacv1.RoleRef{
Kind: "ClusterRole",
Name: fmt.Sprintf("%s-ns-psp:restricted-root-user", ctx.Namespace),
APIGroup: "rbac.authorization.k8s.io",
},
Subjects: []rbacv1.Subject{{
Kind: "ServiceAccount",
Name: Component,
}},
}}, nil
}

0 comments on commit 6c8b206

Please sign in to comment.