diff --git a/go.mod b/go.mod index 827ce877..df40e094 100644 --- a/go.mod +++ b/go.mod @@ -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 sigs.k8s.io/controller-runtime v0.9.5 sigs.k8s.io/yaml v1.2.0 ) diff --git a/pkg/devfile/generator/generators.go b/pkg/devfile/generator/generators.go index 6a2c5a18..7ceafc19 100644 --- a/pkg/devfile/generator/generators.go +++ b/pkg/devfile/generator/generators.go @@ -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" @@ -158,6 +157,7 @@ type DeploymentParams struct { Containers []corev1.Container Volumes []corev1.Volume PodSelectorLabels map[string]string + Replicas *int32 } // GetDeployment gets a deployment object @@ -173,6 +173,7 @@ func GetDeployment(devfileObj parser.DevfileObj, deployParams DeploymentParams) deploySpecParams := deploymentSpecParams{ PodTemplateSpec: *getPodTemplateSpec(podTemplateSpecParams), PodSelectorLabels: deployParams.PodSelectorLabels, + Replicas: deployParams.Replicas, } containerAnnotations, err := getContainerAnnotations(devfileObj, common.DevfileOptions{}) diff --git a/pkg/devfile/generator/generators_test.go b/pkg/devfile/generator/generators_test.go index 733064e7..49b1b31a 100644 --- a/pkg/devfile/generator/generators_test.go +++ b/pkg/devfile/generator/generators_test.go @@ -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" @@ -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{ @@ -1027,6 +1020,7 @@ func TestGetDeployment(t *testing.T) { tests := []struct { name string containerComponents []v1.Component + deploymentParams DeploymentParams expected appsv1.Deployment }{ { @@ -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{ @@ -1065,6 +1068,7 @@ func TestGetDeployment(t *testing.T) { Containers: containers, }, }, + Replicas: pointer.Int32Ptr(1), }, }, }, @@ -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{ @@ -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) diff --git a/pkg/devfile/generator/utils.go b/pkg/devfile/generator/utils.go index 0b1567ae..35af3243 100644 --- a/pkg/devfile/generator/utils.go +++ b/pkg/devfile/generator/utils.go @@ -233,6 +233,7 @@ func getPodTemplateSpec(podTemplateSpecParams podTemplateSpecParams) *corev1.Pod type deploymentSpecParams struct { PodTemplateSpec corev1.PodTemplateSpec PodSelectorLabels map[string]string + Replicas *int32 } // getDeploymentSpec gets a deployment spec @@ -245,6 +246,7 @@ func getDeploymentSpec(deploySpecParams deploymentSpecParams) *appsv1.Deployment MatchLabels: deploySpecParams.PodSelectorLabels, }, Template: deploySpecParams.PodTemplateSpec, + Replicas: deploySpecParams.Replicas, } return deploymentSpec