Skip to content

Add Replicas field to DeploymentParams struct #129

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 6 commits into from
Feb 1, 2022
Merged
Show file tree
Hide file tree
Changes from 4 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
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ require (
k8s.io/apimachinery v0.21.3
k8s.io/client-go v0.21.3
k8s.io/klog v1.0.0
k8s.io/utils v0.0.0-20210722164352-7f3ee0f31471 // indirect
sigs.k8s.io/controller-runtime v0.9.5
sigs.k8s.io/yaml v1.2.0
)
4 changes: 2 additions & 2 deletions pkg/devfile/generator/generators.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package generator

import (
"fmt"

v1 "github.com/devfile/api/v2/pkg/apis/workspaces/v1alpha2"
"github.com/devfile/library/pkg/devfile/parser"
"github.com/devfile/library/pkg/devfile/parser/data/v2/common"
Expand Down Expand Up @@ -158,6 +157,7 @@ type DeploymentParams struct {
Containers []corev1.Container
Volumes []corev1.Volume
PodSelectorLabels map[string]string
Replicas *int32
}

// GetDeployment gets a deployment object
Expand All @@ -184,7 +184,7 @@ func GetDeployment(devfileObj parser.DevfileObj, deployParams DeploymentParams)
deployment := &appsv1.Deployment{
TypeMeta: deployParams.TypeMeta,
ObjectMeta: deployParams.ObjectMeta,
Spec: *getDeploymentSpec(deploySpecParams),
Spec: *getDeploymentSpec(deploySpecParams, deployParams),
}

return deployment, nil
Expand Down
30 changes: 21 additions & 9 deletions pkg/devfile/generator/generators_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
appsv1 "k8s.io/api/apps/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/intstr"
"k8s.io/utils/pointer"
"reflect"
"strings"
"testing"
Expand Down Expand Up @@ -1000,14 +1001,6 @@ func TestGetDeployment(t *testing.T) {
Name: "container2",
},
}
deploymentParams := DeploymentParams{
ObjectMeta: metav1.ObjectMeta{
Annotations: map[string]string{
"preserved-key": "preserved-value",
},
},
Containers: containers,
}

objectMeta := metav1.ObjectMeta{
Annotations: map[string]string{
Expand All @@ -1027,6 +1020,7 @@ func TestGetDeployment(t *testing.T) {
tests := []struct {
name string
containerComponents []v1.Component
deploymentParams DeploymentParams
expected appsv1.Deployment
}{
{
Expand All @@ -1050,6 +1044,15 @@ func TestGetDeployment(t *testing.T) {
},
}, &trueBool),
},
deploymentParams: DeploymentParams{
ObjectMeta: metav1.ObjectMeta{
Annotations: map[string]string{
"preserved-key": "preserved-value",
},
},
Containers: containers,
Replicas: pointer.Int32Ptr(1),
},
expected: appsv1.Deployment{
ObjectMeta: objectMetaDedicatedPod,
Spec: appsv1.DeploymentSpec{
Expand All @@ -1065,6 +1068,7 @@ func TestGetDeployment(t *testing.T) {
Containers: containers,
},
},
Replicas: pointer.Int32Ptr(1),
},
},
},
Expand All @@ -1087,6 +1091,14 @@ func TestGetDeployment(t *testing.T) {
},
}, nil),
},
deploymentParams: DeploymentParams{
ObjectMeta: metav1.ObjectMeta{
Annotations: map[string]string{
"preserved-key": "preserved-value",
},
},
Containers: containers,
},
expected: appsv1.Deployment{
ObjectMeta: objectMeta,
Spec: appsv1.DeploymentSpec{
Expand Down Expand Up @@ -1125,7 +1137,7 @@ func TestGetDeployment(t *testing.T) {
devObj := parser.DevfileObj{
Data: mockDevfileData,
}
deploy, err := GetDeployment(devObj, deploymentParams)
deploy, err := GetDeployment(devObj, tt.deploymentParams)
// Checks for unexpected error cases
if err != nil {
t.Errorf("TestGetDeployment(): unexpected error %v", err)
Expand Down
6 changes: 5 additions & 1 deletion pkg/devfile/generator/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,7 @@ type deploymentSpecParams struct {
}

// getDeploymentSpec gets a deployment spec
func getDeploymentSpec(deploySpecParams deploymentSpecParams) *appsv1.DeploymentSpec {
func getDeploymentSpec(deploySpecParams deploymentSpecParams, deployParams DeploymentParams) *appsv1.DeploymentSpec {
deploymentSpec := &appsv1.DeploymentSpec{
Strategy: appsv1.DeploymentStrategy{
Type: appsv1.RecreateDeploymentStrategyType,
Expand All @@ -247,6 +247,10 @@ func getDeploymentSpec(deploySpecParams deploymentSpecParams) *appsv1.Deployment
Template: deploySpecParams.PodTemplateSpec,
}

if deployParams.Replicas != nil {
deploymentSpec.Replicas = deployParams.Replicas
}

return deploymentSpec
}

Expand Down