Skip to content
This repository has been archived by the owner on Nov 15, 2022. It is now read-only.

set default replicas as 1 in DeploymentConfig #361

Merged
merged 1 commit into from
Oct 16, 2017
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: 0 additions & 1 deletion docs/examples/deploymentconfig/deploymentconfig.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
controller: deploymentconfig
name: httpd
replicas: 2
containers:
- image: centos/httpd
services:
Expand Down
17 changes: 16 additions & 1 deletion pkg/spec/deploymentconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,11 +86,26 @@ func (deploymentConfig *DeploymentConfigSpecMod) Fix() error {
return errors.Wrap(err, "unable to fix secrets")
}

// Fix DeploymentConfig
deploymentConfig.fixDeploymentConfig()

return nil
}

func (deploymentConfig *DeploymentConfigSpecMod) fixDeploymentConfig() {
deploymentConfig.ControllerFields.ObjectMeta.Labels = addKeyValueToMap(appLabelKey,
deploymentConfig.ControllerFields.Name,
deploymentConfig.ControllerFields.ObjectMeta.Labels)

return nil
// If the replicas are not specified at all, we need to set the value as 1
if deploymentConfig.Replicas == nil {
deploymentConfig.Replicas = getInt32Addr(1)
}

// Since we have unmarshalled replicas in a custom defined field, we need
// to substitute the unmarshalled (and fixed) value in the internal
// DeploymentConfigSpec struct
deploymentConfig.DeploymentConfigSpec.Replicas = *deploymentConfig.Replicas
}

func (deploymentConfig *DeploymentConfigSpecMod) Transform() ([]runtime.Object, []string, error) {
Expand Down
76 changes: 76 additions & 0 deletions pkg/spec/deploymentconfig_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,82 @@ import (
api_v1 "k8s.io/kubernetes/pkg/api/v1"
)

func TestFixDeploymentConfig(t *testing.T) {
tests := []struct {
name string
input *DeploymentConfigSpecMod
expectedOutput *DeploymentConfigSpecMod
}{
{
name: "No replicas passed at input, expected 1",
input: &DeploymentConfigSpecMod{},
expectedOutput: &DeploymentConfigSpecMod{
ControllerFields: ControllerFields{
ObjectMeta: meta_v1.ObjectMeta{
Labels: map[string]string{
appLabelKey: "",
},
},
},
DeploymentConfigSpec: os_deploy_v1.DeploymentConfigSpec{
Replicas: 1,
},
Replicas: getInt32Addr(1),
},
},
{
name: "replicas set to 0 by the end user, expected 0",
input: &DeploymentConfigSpecMod{
Replicas: getInt32Addr(0),
},
expectedOutput: &DeploymentConfigSpecMod{
ControllerFields: ControllerFields{
ObjectMeta: meta_v1.ObjectMeta{
Labels: map[string]string{
appLabelKey: "",
},
},
},
DeploymentConfigSpec: os_deploy_v1.DeploymentConfigSpec{
Replicas: 0,
},
Replicas: getInt32Addr(0),
},
},
{
name: "replicas set to 2 by the end user, expected 2",
input: &DeploymentConfigSpecMod{
Replicas: getInt32Addr(2),
},
expectedOutput: &DeploymentConfigSpecMod{
ControllerFields: ControllerFields{
ObjectMeta: meta_v1.ObjectMeta{
Labels: map[string]string{
appLabelKey: "",
},
},
},
DeploymentConfigSpec: os_deploy_v1.DeploymentConfigSpec{
Replicas: 2,
},
Replicas: getInt32Addr(2),
},
},
}

for _, test := range tests {

t.Run(test.name, func(t *testing.T) {
test.input.fixDeploymentConfig()
if !reflect.DeepEqual(test.input, test.expectedOutput) {
t.Errorf("Expected output to be:\n%v\nBut got:\n%v\n",
prettyPrintObjects(test.expectedOutput),
prettyPrintObjects(test.input))
}
})
}
}

func TestDeploymentConfigSpecMod_CreateOpenShiftController(t *testing.T) {
tests := []struct {
name string
Expand Down
9 changes: 9 additions & 0 deletions pkg/spec/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -201,4 +201,13 @@ type JobSpecMod struct {
type DeploymentConfigSpecMod struct {
ControllerFields `json:",inline"`
os_deploy_v1.DeploymentConfigSpec `json:",inline"`

// Replicas is the number of desired replicas.
// We need to add this field here despite being in v1.DeploymentConfigSpec
// because the one in v1.DeploymentConfigSpec has the type as int32, which
// does not let us check if the set value is 0, is it set by the user or not
// since this field's value with default to 0. We need the default value as
// 1. Hence, we need to check if the user has set it or not.Making the type
// *int32 helps us do it, followed by substitution later on.
Replicas *int32 `json:"replicas,omitempty"`
}