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

Add Replicas field to DeploymentParams struct #129

Merged
merged 6 commits into from
Feb 1, 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
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
sigs.k8s.io/controller-runtime v0.9.5
sigs.k8s.io/yaml v1.2.0
)
3 changes: 2 additions & 1 deletion 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 @@ -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{})
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
2 changes: 2 additions & 0 deletions pkg/devfile/generator/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -245,6 +246,7 @@ func getDeploymentSpec(deploySpecParams deploymentSpecParams) *appsv1.Deployment
MatchLabels: deploySpecParams.PodSelectorLabels,
},
Template: deploySpecParams.PodTemplateSpec,
Replicas: deploySpecParams.Replicas,
}

return deploymentSpec
Expand Down