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 2 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
)
6 changes: 6 additions & 0 deletions pkg/devfile/generator/generators.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package generator

import (
"fmt"
"k8s.io/utils/pointer"

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

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would set the Replicas as a pointer, to make it optional (the nil value indicating the user does not want to set it). The 0 value on which you test below is a valid value a user would want to set.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Makes sense. Does 145564e look good?

}

// GetDeployment gets a deployment object
Expand Down Expand Up @@ -187,6 +189,10 @@ func GetDeployment(devfileObj parser.DevfileObj, deployParams DeploymentParams)
Spec: *getDeploymentSpec(deploySpecParams),
}

if deployParams.Replicas != 0 {
deployment.Spec.Replicas = pointer.Int32Ptr(deployParams.Replicas)
}

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: 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