Skip to content
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

[installer]: Allow replica counts to be specified for all components #9495

Merged
merged 8 commits into from
Apr 26, 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
14 changes: 14 additions & 0 deletions install/installer/pkg/common/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -337,6 +337,20 @@ func NodeAffinity(orLabels ...string) *corev1.Affinity {
}
}

func Replicas(ctx *RenderContext, component string) *int32 {
geropl marked this conversation as resolved.
Show resolved Hide resolved
easyCZ marked this conversation as resolved.
Show resolved Hide resolved
replicas := int32(1)

_ = ctx.WithExperimental(func(cfg *experimental.Config) error {
if cfg.Common != nil && cfg.Common.PodConfig[component] != nil {
if cfg.Common.PodConfig[component].Replicas != nil {
replicas = *cfg.Common.PodConfig[component].Replicas
}
}
return nil
})
return &replicas
}

// ObjectHash marshals the objects to YAML and produces a sha256 hash of the output.
// This function is useful for restarting pods when the config changes.
// Takes an error as argument to make calling it more conventient. If that error is not nil,
Expand Down
61 changes: 56 additions & 5 deletions install/installer/pkg/common/render_test.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
// 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 common
package common_test

import (
"testing"

"github.com/gitpod-io/gitpod/installer/pkg/common"
content_service "github.com/gitpod-io/gitpod/installer/pkg/components/content-service"
"github.com/gitpod-io/gitpod/installer/pkg/components/dashboard"
"github.com/gitpod-io/gitpod/installer/pkg/components/server"
"github.com/gitpod-io/gitpod/installer/pkg/config/v1"
"github.com/gitpod-io/gitpod/installer/pkg/config/v1/experimental"
"github.com/gitpod-io/gitpod/installer/pkg/config/versions"
"github.com/google/go-cmp/cmp"
"github.com/stretchr/testify/require"
Expand All @@ -15,19 +20,65 @@ import (
)

func TestCompositeRenderFunc_NilObjectsNilError(t *testing.T) {
f := CompositeRenderFunc(
func(cfg *RenderContext) ([]runtime.Object, error) {
f := common.CompositeRenderFunc(
func(cfg *common.RenderContext) ([]runtime.Object, error) {
return nil, nil
})

ctx, err := NewRenderContext(config.Config{}, versions.Manifest{}, "test_namespace")
ctx, err := common.NewRenderContext(config.Config{}, versions.Manifest{}, "test_namespace")
require.NoError(t, err)

objects, err := f(ctx)
require.NoError(t, err)
require.Len(t, objects, 0)
}

func TestReplicas(t *testing.T) {
testCases := []struct {
Component string
Name string
ExpectedReplicas int32
}{
{
Component: server.Component,
Name: "server takes replica count from common config",
ExpectedReplicas: 123,
},
{
Component: dashboard.Component,
Name: "dashboard takes replica count from common config",
ExpectedReplicas: 456,
},
{
Component: content_service.Component,
Name: "content_service takes default replica count",
ExpectedReplicas: 1,
},
}
ctx, err := common.NewRenderContext(config.Config{
Experimental: &experimental.Config{
Common: &experimental.CommonConfig{
PodConfig: map[string]*experimental.PodConfig{
"server": {Replicas: pointer.Int32(123)},
"dashboard": {Replicas: pointer.Int32(456)},
},
},
},
}, versions.Manifest{}, "test_namespace")
require.NoError(t, err)

for _, testCase := range testCases {
t.Run(testCase.Name, func(t *testing.T) {
actualReplicas := common.Replicas(ctx, testCase.Component)

if *actualReplicas != testCase.ExpectedReplicas {
t.Errorf("expected %d replicas for %q component, but got %d",
testCase.ExpectedReplicas, testCase.Component, *actualReplicas)
}
})
}
}

func TestRepoName(t *testing.T) {
type Expectation struct {
Result string
Expand Down Expand Up @@ -126,7 +177,7 @@ func TestRepoName(t *testing.T) {
}
}()

ctx, err := NewRenderContext(config.Config{
ctx, err := common.NewRenderContext(config.Config{
DropImageRepo: test.DropImageRepo,
Repository: test.CfgRepo,
}, versions.Manifest{}, "test_namespace")
Expand Down
2 changes: 1 addition & 1 deletion install/installer/pkg/components/blobserve/deployment.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ func deployment(ctx *common.RenderContext) ([]runtime.Object, error) {
},
Spec: appsv1.DeploymentSpec{
Selector: &metav1.LabelSelector{MatchLabels: labels},
Replicas: pointer.Int32(1),
Replicas: common.Replicas(ctx, Component),
Strategy: common.DeploymentStrategy,
Template: corev1.PodTemplateSpec{
ObjectMeta: metav1.ObjectMeta{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ func deployment(ctx *common.RenderContext) ([]runtime.Object, error) {
},
Spec: v1.DeploymentSpec{
Selector: &metav1.LabelSelector{MatchLabels: labels},
Replicas: pointer.Int32(1),
Replicas: common.Replicas(ctx, Component),
Strategy: common.DeploymentStrategy,
Template: corev1.PodTemplateSpec{
ObjectMeta: metav1.ObjectMeta{
Expand Down
3 changes: 1 addition & 2 deletions install/installer/pkg/components/dashboard/deployment.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,7 @@ func deployment(ctx *common.RenderContext) ([]runtime.Object, error) {
},
Spec: appsv1.DeploymentSpec{
Selector: &metav1.LabelSelector{MatchLabels: labels},
// todo(sje): receive config value
Replicas: pointer.Int32(1),
Replicas: common.Replicas(ctx, Component),
Strategy: common.DeploymentStrategy,
Template: corev1.PodTemplateSpec{
ObjectMeta: metav1.ObjectMeta{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,7 @@ func deployment(ctx *common.RenderContext) ([]runtime.Object, error) {
},
},
Selector: &metav1.LabelSelector{MatchLabels: labels},
// todo(sje): receive config value
Replicas: pointer.Int32(1),
Replicas: common.Replicas(ctx, Component),
Template: corev1.PodTemplateSpec{
ObjectMeta: metav1.ObjectMeta{
Name: Component,
Expand Down
3 changes: 1 addition & 2 deletions install/installer/pkg/components/ide-proxy/deployment.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,7 @@ func deployment(ctx *common.RenderContext) ([]runtime.Object, error) {
},
Spec: appsv1.DeploymentSpec{
Selector: &metav1.LabelSelector{MatchLabels: labels},
// todo(sje): receive config value
Replicas: pointer.Int32(1),
Replicas: common.Replicas(ctx, Component),
Strategy: common.DeploymentStrategy,
Template: corev1.PodTemplateSpec{
ObjectMeta: metav1.ObjectMeta{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ func deployment(ctx *common.RenderContext) ([]runtime.Object, error) {
},
Spec: appsv1.DeploymentSpec{
Selector: &metav1.LabelSelector{MatchLabels: labels},
Replicas: pointer.Int32(1),
Replicas: common.Replicas(ctx, Component),
Strategy: common.DeploymentStrategy,
Template: corev1.PodTemplateSpec{
ObjectMeta: metav1.ObjectMeta{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ func statefulset(ctx *common.RenderContext) ([]runtime.Object, error) {
},
ServiceName: Component,
// todo(sje): receive config value
Replicas: pointer.Int32(1),
Replicas: common.Replicas(ctx, Component),
Template: v1.PodTemplateSpec{
ObjectMeta: metav1.ObjectMeta{
Name: Component,
Expand Down
2 changes: 1 addition & 1 deletion install/installer/pkg/components/proxy/deployment.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ func deployment(ctx *common.RenderContext) ([]runtime.Object, error) {
},
Spec: appsv1.DeploymentSpec{
Selector: &metav1.LabelSelector{MatchLabels: labels},
Replicas: pointer.Int32(1),
Replicas: common.Replicas(ctx, Component),
Strategy: common.DeploymentStrategy,
Template: corev1.PodTemplateSpec{
ObjectMeta: metav1.ObjectMeta{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ func deployment(ctx *common.RenderContext) ([]runtime.Object, error) {
},
Spec: appsv1.DeploymentSpec{
Selector: &metav1.LabelSelector{MatchLabels: labels},
Replicas: pointer.Int32(1),
Replicas: common.Replicas(ctx, Component),
Strategy: common.DeploymentStrategy,
Template: corev1.PodTemplateSpec{
ObjectMeta: metav1.ObjectMeta{
Expand Down
3 changes: 1 addition & 2 deletions install/installer/pkg/components/server/deployment.go
Original file line number Diff line number Diff line change
Expand Up @@ -173,8 +173,7 @@ func deployment(ctx *common.RenderContext) ([]runtime.Object, error) {
},
Spec: appsv1.DeploymentSpec{
Selector: &metav1.LabelSelector{MatchLabels: labels},
// todo(sje): receive config value
Replicas: pointer.Int32(1),
Replicas: common.Replicas(ctx, Component),
Strategy: common.DeploymentStrategy,
Template: corev1.PodTemplateSpec{
ObjectMeta: metav1.ObjectMeta{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ func deployment(ctx *common.RenderContext) ([]runtime.Object, error) {
},
Spec: appsv1.DeploymentSpec{
Selector: &metav1.LabelSelector{MatchLabels: labels},
Replicas: pointer.Int32(1),
Replicas: common.Replicas(ctx, Component),
Strategy: common.DeploymentStrategy,
Template: corev1.PodTemplateSpec{
ObjectMeta: metav1.ObjectMeta{
Expand Down
2 changes: 1 addition & 1 deletion install/installer/pkg/components/ws-manager/deployment.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ func deployment(ctx *common.RenderContext) ([]runtime.Object, error) {
},
Spec: appsv1.DeploymentSpec{
Selector: &metav1.LabelSelector{MatchLabels: labels},
Replicas: pointer.Int32(1),
Replicas: common.Replicas(ctx, Component),
Strategy: common.DeploymentStrategy,
Template: corev1.PodTemplateSpec{
ObjectMeta: metav1.ObjectMeta{
Expand Down
3 changes: 1 addition & 2 deletions install/installer/pkg/components/ws-proxy/deployment.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,7 @@ func deployment(ctx *common.RenderContext) ([]runtime.Object, error) {
},
Spec: appsv1.DeploymentSpec{
Selector: &metav1.LabelSelector{MatchLabels: labels},
// todo(sje): receive config value
Replicas: pointer.Int32(1),
Replicas: common.Replicas(ctx, Component),
Strategy: common.DeploymentStrategy,
Template: corev1.PodTemplateSpec{
ObjectMeta: metav1.ObjectMeta{
Expand Down
9 changes: 9 additions & 0 deletions install/installer/pkg/config/v1/experimental/experimental.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,15 @@ type Config struct {
Workspace *WorkspaceConfig `json:"workspace,omitempty"`
WebApp *WebAppConfig `json:"webapp,omitempty"`
IDE *IDEConfig `json:"ide,omitempty"`
Common *CommonConfig `json:"common,omitempty"`
}

type CommonConfig struct {
PodConfig map[string]*PodConfig `json:"podConfig,omitempty"`
}

type PodConfig struct {
Replicas *int32 `json:"replicas,omitempty"`
}

type WorkspaceConfig struct {
Expand Down