Skip to content

Commit a61e7b0

Browse files
author
lamai93
committed
Added test for resource rotation.
1 parent d1b2879 commit a61e7b0

File tree

4 files changed

+160
-1
lines changed

4 files changed

+160
-1
lines changed

pkg/apis/deployment/v1alpha/deployment_spec.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,25 @@ func (s DeploymentSpec) GetServerGroupSpec(group ServerGroup) ServerGroupSpec {
162162
}
163163
}
164164

165+
// UpdateServerGroupSpec returns the server group spec (from this
166+
// deployment spec) for the given group.
167+
func (s *DeploymentSpec) UpdateServerGroupSpec(group ServerGroup, gspec ServerGroupSpec) {
168+
switch group {
169+
case ServerGroupSingle:
170+
s.Single = gspec
171+
case ServerGroupAgents:
172+
s.Agents = gspec
173+
case ServerGroupDBServers:
174+
s.DBServers = gspec
175+
case ServerGroupCoordinators:
176+
s.Coordinators = gspec
177+
case ServerGroupSyncMasters:
178+
s.SyncMasters = gspec
179+
case ServerGroupSyncWorkers:
180+
s.SyncWorkers = gspec
181+
}
182+
}
183+
165184
// SetDefaults fills in default values when a field is not specified.
166185
func (s *DeploymentSpec) SetDefaults(deploymentName string) {
167186
if s.GetMode() == "" {

pkg/deployment/reconcile/plan_builder.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,11 @@ import (
2929
upgraderules "github.com/arangodb/go-upgrade-rules"
3030
"github.com/rs/zerolog"
3131
"github.com/rs/zerolog/log"
32-
"k8s.io/api/core/v1"
3332
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
3433

3534
api "github.com/arangodb/kube-arangodb/pkg/apis/deployment/v1alpha"
3635
"github.com/arangodb/kube-arangodb/pkg/util/k8sutil"
36+
v1 "k8s.io/api/core/v1"
3737
)
3838

3939
// upgradeDecision is the result of an upgrade check.

tests/predicates.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,9 @@ import (
2626
"fmt"
2727

2828
api "github.com/arangodb/kube-arangodb/pkg/apis/deployment/v1alpha"
29+
"github.com/arangodb/kube-arangodb/pkg/util/k8sutil"
30+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
31+
"k8s.io/client-go/kubernetes"
2932
)
3033

3134
// deploymentIsReady creates a predicate that returns nil when the deployment is in
@@ -41,3 +44,28 @@ func deploymentIsReady() func(*api.ArangoDeployment) error {
4144
return fmt.Errorf("Expected Ready condition to be set, it is not")
4245
}
4346
}
47+
48+
func resourcesAsRequested(kubecli kubernetes.Interface, ns string) func(obj *api.ArangoDeployment) error {
49+
return func(obj *api.ArangoDeployment) error {
50+
return obj.ForeachServerGroup(func(group api.ServerGroup, spec api.ServerGroupSpec, status *api.MemberStatusList) error {
51+
52+
for _, m := range *status {
53+
pod, err := kubecli.CoreV1().Pods(ns).Get(m.PodName, metav1.GetOptions{})
54+
if err != nil {
55+
return err
56+
}
57+
58+
c, found := k8sutil.GetContainerByName(pod, k8sutil.ServerContainerName)
59+
if !found {
60+
return fmt.Errorf("Container not found: %s", m.PodName)
61+
}
62+
63+
if resourcesRequireRotation(spec.Resources, c.Resources) {
64+
return fmt.Errorf("Container of Pod %s need rotation", m.PodName)
65+
}
66+
}
67+
68+
return nil
69+
}, nil)
70+
}
71+
}

tests/resources_test.go

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
//
2+
// DISCLAIMER
3+
//
4+
// Copyright 2018 ArangoDB GmbH, Cologne, Germany
5+
//
6+
// Licensed under the Apache License, Version 2.0 (the "License");
7+
// you may not use this file except in compliance with the License.
8+
// You may obtain a copy of the License at
9+
//
10+
// http://www.apache.org/licenses/LICENSE-2.0
11+
//
12+
// Unless required by applicable law or agreed to in writing, software
13+
// distributed under the License is distributed on an "AS IS" BASIS,
14+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
// See the License for the specific language governing permissions and
16+
// limitations under the License.
17+
//
18+
// Copyright holder is ArangoDB GmbH, Cologne, Germany
19+
//
20+
// Author Lars Maier
21+
//
22+
23+
package tests
24+
25+
import (
26+
"fmt"
27+
"testing"
28+
29+
api "github.com/arangodb/kube-arangodb/pkg/apis/deployment/v1alpha"
30+
"github.com/arangodb/kube-arangodb/pkg/client"
31+
kubeArangoClient "github.com/arangodb/kube-arangodb/pkg/client"
32+
"github.com/arangodb/kube-arangodb/pkg/util"
33+
"github.com/dchest/uniuri"
34+
"github.com/stretchr/testify/assert"
35+
v1 "k8s.io/api/core/v1"
36+
"k8s.io/apimachinery/pkg/api/resource"
37+
)
38+
39+
func resourcesRequireRotation(wanted, given v1.ResourceRequirements) bool {
40+
checkList := func(wanted, given v1.ResourceList) bool {
41+
for k, v := range wanted {
42+
if gv, ok := given[k]; !ok {
43+
return true
44+
} else if v.Cmp(gv) != 0 {
45+
return true
46+
}
47+
}
48+
49+
return false
50+
}
51+
52+
return checkList(wanted.Limits, given.Limits) || checkList(wanted.Requests, given.Requests)
53+
}
54+
55+
func TestResourcesChangeLimitsCluster(t *testing.T) {
56+
longOrSkip(t)
57+
c := client.MustNewInCluster()
58+
kubecli := mustNewKubeClient(t)
59+
deploymentClient := kubeArangoClient.MustNewInCluster()
60+
ns := getNamespace(t)
61+
62+
size500mCPU, _ := resource.ParseQuantity("50m")
63+
size1CPU, _ := resource.ParseQuantity("1")
64+
65+
// Prepare deployment config
66+
depl := newDeployment("test-chng-limits-" + uniuri.NewLen(4))
67+
depl.Spec.Mode = api.NewMode(api.DeploymentModeCluster)
68+
depl.Spec.DBServers.Count = util.NewInt(2)
69+
depl.Spec.Coordinators.Count = util.NewInt(2)
70+
depl.Spec.SetDefaults(depl.GetName()) // this must be last
71+
defer deferedCleanupDeployment(c, depl.GetName(), ns)
72+
73+
// Create deployment
74+
_, err := deploymentClient.DatabaseV1alpha().ArangoDeployments(ns).Create(depl)
75+
defer removeDeployment(deploymentClient, depl.GetName(), ns)
76+
assert.NoError(t, err, "failed to create deplyment: %s", err)
77+
78+
testGroups := []api.ServerGroup{api.ServerGroupCoordinators, api.ServerGroupAgents, api.ServerGroupDBServers}
79+
80+
for _, testgroup := range testGroups {
81+
t.Run(testgroup.AsRole(), func(t *testing.T) {
82+
83+
_, err = waitUntilDeployment(deploymentClient, depl.GetName(), ns, deploymentIsReady())
84+
assert.NoError(t, err, fmt.Sprintf("Deployment not running in time: %s", err))
85+
86+
depl, err = updateDeployment(c, depl.GetName(), ns, func(spec *api.DeploymentSpec) {
87+
gspec := spec.GetServerGroupSpec(testgroup)
88+
gspec.Resources.Limits = v1.ResourceList{
89+
v1.ResourceCPU: size1CPU,
90+
}
91+
spec.UpdateServerGroupSpec(testgroup, gspec)
92+
})
93+
assert.NoError(t, err, fmt.Sprintf("Failed to update deployment: %s", err))
94+
95+
_, err = waitUntilDeployment(deploymentClient, depl.GetName(), ns, resourcesAsRequested(kubecli, ns))
96+
assert.NoError(t, err, fmt.Sprintf("Deployment not rotated in time: %s", err))
97+
98+
depl, err = updateDeployment(c, depl.GetName(), ns, func(spec *api.DeploymentSpec) {
99+
gspec := spec.GetServerGroupSpec(testgroup)
100+
gspec.Resources.Requests = v1.ResourceList{
101+
v1.ResourceCPU: size500mCPU,
102+
}
103+
spec.UpdateServerGroupSpec(testgroup, gspec)
104+
})
105+
assert.NoError(t, err, fmt.Sprintf("Failed to update deployment: %s", err))
106+
107+
_, err = waitUntilDeployment(deploymentClient, depl.GetName(), ns, resourcesAsRequested(kubecli, ns))
108+
assert.NoError(t, err, fmt.Sprintf("Deployment not rotated in time: %s", err))
109+
})
110+
}
111+
112+
}

0 commit comments

Comments
 (0)