From f74977e279ac40253e5251e0bc8463a7b26069ae Mon Sep 17 00:00:00 2001 From: Adam Janikowski <12255597+ajanikow@users.noreply.github.com> Date: Wed, 1 Jul 2020 12:52:02 +0200 Subject: [PATCH] [Bugfix] Checksum compatibility (#591) --- pkg/apis/deployment/v1/deployment_spec.go | 2 +- .../deployment/v1/immutable_checksum_test.go | 65 +++++++++++++++++++ .../deployment/v1/server_id_group_spec.go | 8 +++ .../deployment/v1/zz_generated.deepcopy.go | 6 +- pkg/deployment/images.go | 14 ++-- 5 files changed, 86 insertions(+), 9 deletions(-) create mode 100644 pkg/apis/deployment/v1/immutable_checksum_test.go diff --git a/pkg/apis/deployment/v1/deployment_spec.go b/pkg/apis/deployment/v1/deployment_spec.go index a0acc00dc..774476355 100644 --- a/pkg/apis/deployment/v1/deployment_spec.go +++ b/pkg/apis/deployment/v1/deployment_spec.go @@ -83,7 +83,7 @@ type DeploymentSpec struct { Metrics MetricsSpec `json:"metrics"` Lifecycle LifecycleSpec `json:"lifecycle,omitempty"` - ID ServerIDGroupSpec `json:"id"` + ID *ServerIDGroupSpec `json:"id,omitempty"` Single ServerGroupSpec `json:"single"` Agents ServerGroupSpec `json:"agents"` diff --git a/pkg/apis/deployment/v1/immutable_checksum_test.go b/pkg/apis/deployment/v1/immutable_checksum_test.go new file mode 100644 index 000000000..0713a24d7 --- /dev/null +++ b/pkg/apis/deployment/v1/immutable_checksum_test.go @@ -0,0 +1,65 @@ +// +// DISCLAIMER +// +// Copyright 2020 ArangoDB GmbH, Cologne, Germany +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// +// Copyright holder is ArangoDB GmbH, Cologne, Germany +// +// Author Adam Janikowski +// + +package v1 + +import ( + "testing" + + "github.com/stretchr/testify/require" +) + +type checksumCompareCases []checksumCompareCase + +type checksumCompareCase struct { + name string + + spec DeploymentSpec + checksum string +} + +func runChecksumCompareCases(t *testing.T, cases checksumCompareCases) { + for _, c := range cases { + t.Run(c.name, func(t *testing.T) { + runChecksumCompareCase(t, c) + }) + } +} + +func runChecksumCompareCase(t *testing.T, c checksumCompareCase) { + s, err := c.spec.Checksum() + require.NoError(t, err) + + require.Equalf(t, c.checksum, s, "Checksum od ArangoDeployment mismatch") +} + +func TestImmutableSpec(t *testing.T) { + cases := checksumCompareCases{ + { + name: "Default case - from 1.0.3", + spec: DeploymentSpec{}, + checksum: "a164088b280d72c177c2eafdab7a346fb296264b70c06329b776c506925bb54e", + }, + } + + runChecksumCompareCases(t, cases) +} diff --git a/pkg/apis/deployment/v1/server_id_group_spec.go b/pkg/apis/deployment/v1/server_id_group_spec.go index c8e2f5151..1d66dacf1 100644 --- a/pkg/apis/deployment/v1/server_id_group_spec.go +++ b/pkg/apis/deployment/v1/server_id_group_spec.go @@ -39,3 +39,11 @@ type ServerIDGroupSpec struct { // NodeAffinity specified additional nodeAffinity settings in ArangoDB Pod definitions NodeAffinity *core.NodeAffinity `json:"nodeAffinity,omitempty"` } + +func (s *ServerIDGroupSpec) Get() ServerIDGroupSpec { + if s != nil { + return *s + } + + return ServerIDGroupSpec{} +} diff --git a/pkg/apis/deployment/v1/zz_generated.deepcopy.go b/pkg/apis/deployment/v1/zz_generated.deepcopy.go index dbb9eed1d..91d6f273f 100644 --- a/pkg/apis/deployment/v1/zz_generated.deepcopy.go +++ b/pkg/apis/deployment/v1/zz_generated.deepcopy.go @@ -343,7 +343,11 @@ func (in *DeploymentSpec) DeepCopyInto(out *DeploymentSpec) { in.License.DeepCopyInto(&out.License) in.Metrics.DeepCopyInto(&out.Metrics) in.Lifecycle.DeepCopyInto(&out.Lifecycle) - in.ID.DeepCopyInto(&out.ID) + if in.ID != nil { + in, out := &in.ID, &out.ID + *out = new(ServerIDGroupSpec) + (*in).DeepCopyInto(*out) + } in.Single.DeepCopyInto(&out.Single) in.Agents.DeepCopyInto(&out.Agents) in.DBServers.DeepCopyInto(&out.DBServers) diff --git a/pkg/deployment/images.go b/pkg/deployment/images.go index 11ddd5326..5b084db93 100644 --- a/pkg/deployment/images.go +++ b/pkg/deployment/images.go @@ -271,7 +271,7 @@ func (i *ImageUpdatePod) GetRole() string { func (i *ImageUpdatePod) Init(pod *core.Pod) { terminationGracePeriodSeconds := int64((time.Second * 30).Seconds()) pod.Spec.TerminationGracePeriodSeconds = &terminationGracePeriodSeconds - pod.Spec.PriorityClassName = i.spec.ID.PriorityClassName + pod.Spec.PriorityClassName = i.spec.ID.Get().PriorityClassName } func (i *ImageUpdatePod) GetImagePullSecrets() []string { @@ -316,9 +316,9 @@ func (i *ImageUpdatePod) GetTolerations() []core.Toleration { TimeSpan: time.Second * 5, } - tolerations := make([]core.Toleration, 0, 3+len(i.spec.ID.Tolerations)) + tolerations := make([]core.Toleration, 0, 3+len(i.spec.ID.Get().Tolerations)) - if idTolerations := i.spec.ID.Tolerations; len(idTolerations) > 0 { + if idTolerations := i.spec.ID.Get().Tolerations; len(idTolerations) > 0 { for _, toleration := range idTolerations { tolerations = k8sutil.AddTolerationIfNotFound(tolerations, toleration) } @@ -339,7 +339,7 @@ func (i *ImageUpdatePod) IsDeploymentMode() bool { } func (i *ImageUpdatePod) GetNodeSelector() map[string]string { - return i.spec.ID.NodeSelector + return i.spec.ID.Get().NodeSelector } func (i *ImageUpdatePod) GetServiceAccountName() string { @@ -367,7 +367,7 @@ func (i *ImageUpdatePod) GetPodAntiAffinity() *core.PodAntiAffinity { pod.AppendPodAntiAffinityDefault(i, &a) - pod.MergePodAntiAffinity(&a, i.spec.ID.AntiAffinity) + pod.MergePodAntiAffinity(&a, i.spec.ID.Get().AntiAffinity) return pod.ReturnPodAntiAffinityOrNil(a) } @@ -375,7 +375,7 @@ func (i *ImageUpdatePod) GetPodAntiAffinity() *core.PodAntiAffinity { func (i *ImageUpdatePod) GetPodAffinity() *core.PodAffinity { a := core.PodAffinity{} - pod.MergePodAffinity(&a, i.spec.ID.Affinity) + pod.MergePodAffinity(&a, i.spec.ID.Get().Affinity) return pod.ReturnPodAffinityOrNil(a) } @@ -385,7 +385,7 @@ func (i *ImageUpdatePod) GetNodeAffinity() *core.NodeAffinity { pod.AppendNodeSelector(&a) - pod.MergeNodeAffinity(&a, i.spec.ID.NodeAffinity) + pod.MergeNodeAffinity(&a, i.spec.ID.Get().NodeAffinity) return pod.ReturnNodeAffinityOrNil(a) }