Skip to content

[usage] Configure DB credentials and connect #10295

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
May 30, 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
13 changes: 13 additions & 0 deletions components/usage/cmd/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@ package cmd
import (
"github.com/gitpod-io/gitpod/common-go/baseserver"
"github.com/gitpod-io/gitpod/common-go/log"
"github.com/gitpod-io/gitpod/usage/pkg/db"
"github.com/spf13/cobra"
"net"
"os"
)

func init() {
Expand All @@ -28,6 +31,16 @@ func run() *cobra.Command {

log.Info("Hello world usage server")

_, err := db.Connect(db.ConnectionParams{
User: os.Getenv("DB_USERNAME"),
Password: os.Getenv("DB_PASSWORD"),
Host: net.JoinHostPort(os.Getenv("DB_HOST"), os.Getenv("DB_PORT")),
Database: "gitpod",
})
if err != nil {
log.WithError(err).Fatal("Failed to establish database connection.")
}

srv, err := baseserver.New("usage")
if err != nil {
log.WithError(err).Fatal("Failed to initialize server.")
Expand Down
2 changes: 2 additions & 0 deletions install/installer/pkg/components/usage/deployment.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (

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

return []runtime.Object{
&appsv1.Deployment{
TypeMeta: common.TypeMetaDeployment,
Expand Down Expand Up @@ -64,6 +65,7 @@ func deployment(ctx *common.RenderContext) ([]runtime.Object, error) {
},
Env: common.MergeEnv(
common.DefaultEnv(&ctx.Config),
common.DatabaseEnv(&ctx.Config),
),
LivenessProbe: &corev1.Probe{
ProbeHandler: corev1.ProbeHandler{
Expand Down
50 changes: 50 additions & 0 deletions install/installer/pkg/components/usage/deployment_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
// Copyright (c) 2022 Gitpod GmbH. All rights reserved.
// Licensed under the MIT License. See License-MIT.txt in the project root for license information.

package usage

import (
"github.com/stretchr/testify/require"
appsv1 "k8s.io/api/apps/v1"
corev1 "k8s.io/api/core/v1"
"testing"
)

func TestDeployment_ContainsDBEnvVars(t *testing.T) {
ctx := renderContextWithUsageEnabled(t)

objs, err := deployment(ctx)
require.NoError(t, err)

dpl, ok := objs[0].(*appsv1.Deployment)
require.True(t, ok)

containers := dpl.Spec.Template.Spec.Containers
require.Len(t, containers, 2)

usageContainer := containers[0]
secretRef := corev1.LocalObjectReference{Name: ctx.Config.Database.CloudSQL.ServiceAccount.Name}

require.Contains(t, usageContainer.Env, corev1.EnvVar{
Name: "DB_HOST",
Value: "cloudsqlproxy",
})
require.Contains(t, usageContainer.Env, corev1.EnvVar{
Name: "DB_PORT",
Value: "3306",
})
require.Contains(t, usageContainer.Env, corev1.EnvVar{
Name: "DB_PASSWORD",
ValueFrom: &corev1.EnvVarSource{SecretKeyRef: &corev1.SecretKeySelector{
LocalObjectReference: secretRef,
Key: "password",
}},
})
require.Contains(t, usageContainer.Env, corev1.EnvVar{
Name: "DB_USERNAME",
ValueFrom: &corev1.EnvVarSource{SecretKeyRef: &corev1.SecretKeySelector{
LocalObjectReference: secretRef,
Key: "username",
}},
})
}
7 changes: 7 additions & 0 deletions install/installer/pkg/components/usage/objects_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,13 @@ func renderContextWithUsageEnabled(t *testing.T) *common.RenderContext {
Usage: &experimental.UsageConfig{Enabled: true},
},
},
Database: config.Database{
CloudSQL: &config.DatabaseCloudSQL{
ServiceAccount: config.ObjectRef{
Name: "gcp-db-creds-service-account-name",
},
},
},
}, versions.Manifest{
Components: versions.Components{
Usage: versions.Versioned{
Expand Down