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

Applying defaults after immutable fields have been reset #232

Merged
merged 2 commits into from
Aug 7, 2018
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
44 changes: 40 additions & 4 deletions pkg/apis/deployment/v1alpha/deployment_spec_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,22 +45,39 @@ func TestDeploymentSpecSetDefaults(t *testing.T) {

func TestDeploymentSpecResetImmutableFields(t *testing.T) {
tests := []struct {
Original DeploymentSpec
Target DeploymentSpec
Expected DeploymentSpec
Result []string
Original DeploymentSpec
Target DeploymentSpec
Expected DeploymentSpec
ApplyDefaults bool
Result []string
}{
// Valid "changes"
{
DeploymentSpec{Image: util.NewString("foo")},
DeploymentSpec{Image: util.NewString("foo2")},
DeploymentSpec{Image: util.NewString("foo2")},
false,
nil,
},
{
DeploymentSpec{Image: util.NewString("foo")},
DeploymentSpec{Image: util.NewString("foo2")},
DeploymentSpec{Image: util.NewString("foo2")},
true,
nil,
},
{
DeploymentSpec{ImagePullPolicy: util.NewPullPolicy(v1.PullAlways)},
DeploymentSpec{ImagePullPolicy: util.NewPullPolicy(v1.PullNever)},
DeploymentSpec{ImagePullPolicy: util.NewPullPolicy(v1.PullNever)},
false,
nil,
},
{
DeploymentSpec{ImagePullPolicy: util.NewPullPolicy(v1.PullAlways)},
DeploymentSpec{ImagePullPolicy: util.NewPullPolicy(v1.PullNever)},
DeploymentSpec{ImagePullPolicy: util.NewPullPolicy(v1.PullNever)},
true,
nil,
},

Expand All @@ -69,12 +86,31 @@ func TestDeploymentSpecResetImmutableFields(t *testing.T) {
DeploymentSpec{Mode: NewMode(DeploymentModeSingle)},
DeploymentSpec{Mode: NewMode(DeploymentModeCluster)},
DeploymentSpec{Mode: NewMode(DeploymentModeSingle)},
false,
[]string{"mode"},
},
{
DeploymentSpec{Mode: NewMode(DeploymentModeSingle)},
DeploymentSpec{Mode: NewMode(DeploymentModeCluster)},
DeploymentSpec{Mode: NewMode(DeploymentModeSingle)},
true,
[]string{"mode", "agents.count"},
},
}

for _, test := range tests {
if test.ApplyDefaults {
test.Original.SetDefaults("foo")
test.Expected.SetDefaults("foo")
test.Target.SetDefaultsFrom(test.Original)
test.Target.SetDefaults("foo")
}
result := test.Original.ResetImmutableFields(&test.Target)
if test.ApplyDefaults {
if len(result) > 0 {
test.Target.SetDefaults("foo")
}
}
assert.Equal(t, test.Result, result)
assert.Equal(t, test.Expected, test.Target)
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/apis/deployment/v1alpha/server_group_spec.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ func (s *ServerGroupSpec) SetDefaults(group ServerGroup, used bool, mode Deploym
s.Count = util.NewInt(3)
}
} else if s.GetCount() > 0 && !used {
s.Count = util.NewInt(0)
s.Count = nil
}
if _, found := s.Resources.Requests[v1.ResourceStorage]; !found {
switch group {
Expand Down
1 change: 1 addition & 0 deletions pkg/deployment/deployment.go
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,7 @@ func (d *Deployment) handleArangoDeploymentUpdatedEvent() error {
resetFields := specBefore.ResetImmutableFields(&newAPIObject.Spec)
if len(resetFields) > 0 {
log.Debug().Strs("fields", resetFields).Msg("Found modified immutable fields")
newAPIObject.Spec.SetDefaults(d.apiObject.GetName())
}
if err := newAPIObject.Spec.Validate(); err != nil {
d.CreateEvent(k8sutil.NewErrorEvent("Validation failed", err, d.apiObject))
Expand Down