Skip to content

[usage] Add configmap for usage component to control run frequency #10729

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 3 commits into from
Jun 21, 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
38 changes: 38 additions & 0 deletions install/installer/pkg/components/usage/configmap.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// 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 (
"fmt"

"github.com/gitpod-io/gitpod/installer/pkg/common"
"github.com/gitpod-io/gitpod/installer/pkg/config/v1/experimental"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
)

func configmap(ctx *common.RenderContext) ([]runtime.Object, error) {
schedule := "1h"
_ = ctx.WithExperimental(func(ucfg *experimental.Config) error {
if ucfg.WebApp != nil && ucfg.WebApp.Usage != nil && ucfg.WebApp.Usage.Schedule != "" {
schedule = ucfg.WebApp.Usage.Schedule
}
return nil
})

return []runtime.Object{
&corev1.ConfigMap{
TypeMeta: common.TypeMetaConfigmap,
ObjectMeta: metav1.ObjectMeta{
Name: fmt.Sprintf("%s-config", Component),
Namespace: ctx.Namespace,
Labels: common.DefaultLabels(Component),
},
Data: map[string]string{
"schedule": schedule,
},
},
}, nil
}
24 changes: 24 additions & 0 deletions install/installer/pkg/components/usage/configmap_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// 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 (
"testing"

"github.com/gitpod-io/gitpod/installer/pkg/config/v1/experimental"
"github.com/stretchr/testify/require"
corev1 "k8s.io/api/core/v1"
)

func TestConfigMap_ContainsSchedule(t *testing.T) {
ctx := renderContextWithUsageConfig(t, &experimental.UsageConfig{Enabled: true, Schedule: "2m"})

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

cfgmap, ok := objs[0].(*corev1.ConfigMap)
require.True(t, ok)

require.Equal(t, cfgmap.Data["schedule"], "2m")
}
13 changes: 13 additions & 0 deletions install/installer/pkg/components/usage/deployment.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
package usage

import (
"fmt"

"github.com/gitpod-io/gitpod/common-go/baseserver"
"github.com/gitpod-io/gitpod/installer/pkg/cluster"
"github.com/gitpod-io/gitpod/installer/pkg/common"
Expand Down Expand Up @@ -77,6 +79,8 @@ func deployment(ctx *common.RenderContext) ([]runtime.Object, error) {
Image: ctx.ImageName(ctx.Config.Repository, Component, ctx.VersionManifest.Components.Usage.Version),
Args: []string{
"run",
"--schedule",
"$(RECONCILER_SCHEDULE)",
},
ImagePullPolicy: corev1.PullIfNotPresent,
Resources: common.ResourceRequirements(ctx, Component, Component, corev1.ResourceRequirements{
Expand All @@ -92,6 +96,15 @@ func deployment(ctx *common.RenderContext) ([]runtime.Object, error) {
Env: common.MergeEnv(
common.DefaultEnv(&ctx.Config),
common.DatabaseEnv(&ctx.Config),
[]corev1.EnvVar{{
Name: "RECONCILER_SCHEDULE",
ValueFrom: &corev1.EnvVarSource{
ConfigMapKeyRef: &corev1.ConfigMapKeySelector{
LocalObjectReference: corev1.LocalObjectReference{Name: fmt.Sprintf("%s-config", Component)},
Key: "schedule",
},
},
}},
),
VolumeMounts: volumeMounts,
LivenessProbe: &corev1.Probe{
Expand Down
1 change: 1 addition & 0 deletions install/installer/pkg/components/usage/objects.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ func Objects(ctx *common.RenderContext) ([]runtime.Object, error) {
return common.CompositeRenderFunc(
deployment,
rolebinding,
configmap,
common.DefaultServiceAccount(Component),
)(ctx)
}
Expand Down
12 changes: 7 additions & 5 deletions install/installer/pkg/components/usage/objects_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,16 +27,14 @@ func TestObjects_RenderedWhenExperimentalConfigSet(t *testing.T) {
objects, err := Objects(ctx)
require.NoError(t, err)
require.NotEmpty(t, objects, "must render objects because experimental config is specified")
require.Len(t, objects, 4, "should render expected k8s objects")
require.Len(t, objects, 5, "should render expected k8s objects")
}

func renderContextWithUsageEnabled(t *testing.T) *common.RenderContext {
func renderContextWithUsageConfig(t *testing.T, usage *experimental.UsageConfig) *common.RenderContext {
ctx, err := common.NewRenderContext(config.Config{
Domain: "test.domain.everything.awesome.is",
Experimental: &experimental.Config{
WebApp: &experimental.WebAppConfig{
Usage: &experimental.UsageConfig{Enabled: true},
},
WebApp: &experimental.WebAppConfig{Usage: usage},
},
Database: config.Database{
CloudSQL: &config.DatabaseCloudSQL{
Expand All @@ -59,3 +57,7 @@ func renderContextWithUsageEnabled(t *testing.T) *common.RenderContext {

return ctx
}

func renderContextWithUsageEnabled(t *testing.T) *common.RenderContext {
return renderContextWithUsageConfig(t, &experimental.UsageConfig{Enabled: true})
}
3 changes: 2 additions & 1 deletion install/installer/pkg/config/v1/experimental/experimental.go
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,8 @@ type PublicAPIConfig struct {
}

type UsageConfig struct {
Enabled bool `json:"enabled"`
Enabled bool `json:"enabled"`
Schedule string `json:"schedule"`
}

type IDEConfig struct {
Expand Down