2020-03-20 13:39:32 +00:00
|
|
|
//
|
|
|
|
// DISCLAIMER
|
|
|
|
//
|
2022-01-10 11:35:49 +00:00
|
|
|
// Copyright 2016-2022 ArangoDB GmbH, Cologne, Germany
|
2020-03-20 13:39:32 +00:00
|
|
|
//
|
|
|
|
// 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
|
|
|
|
//
|
|
|
|
|
|
|
|
package deployment
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/require"
|
2022-07-11 11:49:47 +00:00
|
|
|
core "k8s.io/api/core/v1"
|
2020-03-20 13:39:32 +00:00
|
|
|
|
|
|
|
api "github.com/arangodb/kube-arangodb/pkg/apis/deployment/v1"
|
2022-03-28 13:53:58 +00:00
|
|
|
"github.com/arangodb/kube-arangodb/pkg/apis/shared"
|
2022-07-11 11:49:47 +00:00
|
|
|
"github.com/arangodb/kube-arangodb/pkg/deployment/resources"
|
2020-03-20 13:39:32 +00:00
|
|
|
"github.com/arangodb/kube-arangodb/pkg/util"
|
|
|
|
"github.com/arangodb/kube-arangodb/pkg/util/k8sutil"
|
|
|
|
)
|
|
|
|
|
2020-09-09 13:44:05 +00:00
|
|
|
type envFunc func() []core.EnvVar
|
|
|
|
|
2021-05-07 14:13:15 +00:00
|
|
|
func withEnvs(f ...envFunc) []core.EnvVar {
|
2020-09-09 13:44:05 +00:00
|
|
|
var e []core.EnvVar
|
|
|
|
|
|
|
|
for _, c := range f {
|
|
|
|
e = append(e, c()...)
|
|
|
|
}
|
|
|
|
|
|
|
|
return e
|
|
|
|
}
|
|
|
|
|
2021-05-07 14:13:15 +00:00
|
|
|
func withDefaultEnvs(t *testing.T, requirements core.ResourceRequirements) []core.EnvVar {
|
2020-09-09 13:44:05 +00:00
|
|
|
var q []envFunc
|
|
|
|
|
|
|
|
q = append(q, resourceLimitAsEnv(t, requirements))
|
|
|
|
|
2021-05-07 14:13:15 +00:00
|
|
|
return withEnvs(q...)
|
2020-09-09 13:44:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func resourceLimitAsEnv(t *testing.T, requirements core.ResourceRequirements) envFunc {
|
|
|
|
return func() []core.EnvVar {
|
|
|
|
var e []core.EnvVar
|
|
|
|
if _, ok := requirements.Limits[core.ResourceMemory]; ok {
|
|
|
|
e = append(e, resourceMemoryLimitAsEnv(t, requirements)()...)
|
|
|
|
}
|
|
|
|
if _, ok := requirements.Limits[core.ResourceCPU]; ok {
|
|
|
|
e = append(e, resourceCPULimitAsEnv(t, requirements)()...)
|
|
|
|
}
|
|
|
|
return e
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func resourceMemoryLimitAsEnv(t *testing.T, requirements core.ResourceRequirements) envFunc {
|
2020-03-20 13:39:32 +00:00
|
|
|
value, ok := requirements.Limits[core.ResourceMemory]
|
|
|
|
require.True(t, ok)
|
2020-09-09 13:44:05 +00:00
|
|
|
return func() []core.EnvVar {
|
|
|
|
return []core.EnvVar{{
|
|
|
|
Name: resources.ArangoDBOverrideDetectedTotalMemoryEnv,
|
|
|
|
Value: fmt.Sprintf("%d", value.Value()),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func resourceCPULimitAsEnv(t *testing.T, requirements core.ResourceRequirements) envFunc {
|
|
|
|
value, ok := requirements.Limits[core.ResourceCPU]
|
|
|
|
require.True(t, ok)
|
2020-03-20 13:39:32 +00:00
|
|
|
|
2020-09-09 13:44:05 +00:00
|
|
|
return func() []core.EnvVar {
|
|
|
|
return []core.EnvVar{{
|
|
|
|
Name: resources.ArangoDBOverrideDetectedNumberOfCoresEnv,
|
|
|
|
Value: fmt.Sprintf("%d", value.Value()),
|
|
|
|
},
|
|
|
|
}
|
2020-03-20 13:39:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestEnsurePod_ArangoDB_Resources(t *testing.T) {
|
|
|
|
testCases := []testCaseStruct{
|
|
|
|
{
|
|
|
|
Name: "DBserver POD with resource requirements",
|
|
|
|
ArangoDeployment: &api.ArangoDeployment{
|
|
|
|
Spec: api.DeploymentSpec{
|
|
|
|
Image: util.NewString(testImage),
|
|
|
|
Authentication: noAuthentication,
|
|
|
|
TLS: noTLS,
|
|
|
|
DBServers: api.ServerGroupSpec{
|
|
|
|
Resources: resourcesUnfiltered,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Helper: func(t *testing.T, deployment *Deployment, testCase *testCaseStruct) {
|
2022-08-25 11:44:28 +00:00
|
|
|
deployment.currentObjectStatus = &api.DeploymentStatus{
|
2020-03-20 13:39:32 +00:00
|
|
|
Members: api.DeploymentStatusMembers{
|
|
|
|
DBServers: api.MemberStatusList{
|
|
|
|
firstDBServerStatus,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Images: createTestImages(false),
|
|
|
|
}
|
2022-08-25 11:44:28 +00:00
|
|
|
deployment.currentObjectStatus.Members.DBServers[0].IsInitialized = true
|
2020-03-20 13:39:32 +00:00
|
|
|
|
|
|
|
testCase.createTestPodData(deployment, api.ServerGroupDBServers, firstDBServerStatus)
|
|
|
|
},
|
|
|
|
ExpectedEvent: "member dbserver is created",
|
|
|
|
ExpectedPod: core.Pod{
|
|
|
|
Spec: core.PodSpec{
|
|
|
|
Volumes: []core.Volume{
|
2022-03-28 13:53:58 +00:00
|
|
|
k8sutil.CreateVolumeEmptyDir(shared.ArangodVolumeName),
|
2020-03-20 13:39:32 +00:00
|
|
|
},
|
|
|
|
Containers: []core.Container{
|
|
|
|
{
|
2022-03-28 13:53:58 +00:00
|
|
|
Name: shared.ServerContainerName,
|
2020-03-20 13:39:32 +00:00
|
|
|
Image: testImage,
|
|
|
|
Command: createTestCommandForDBServer(firstDBServerStatus.ID, false, false, false),
|
|
|
|
Ports: createTestPorts(),
|
|
|
|
Resources: k8sutil.ExtractPodResourceRequirement(resourcesUnfiltered),
|
|
|
|
VolumeMounts: []core.VolumeMount{
|
|
|
|
k8sutil.ArangodVolumeMount(),
|
|
|
|
},
|
2022-03-28 13:53:58 +00:00
|
|
|
LivenessProbe: createTestLivenessProbe(httpProbe, false, "", shared.ArangoPort),
|
2020-03-20 13:39:32 +00:00
|
|
|
ImagePullPolicy: core.PullIfNotPresent,
|
|
|
|
SecurityContext: securityContext.NewSecurityContext(),
|
2020-09-09 13:44:05 +00:00
|
|
|
Env: withDefaultEnvs(t, resourcesUnfiltered),
|
2020-03-20 13:39:32 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
RestartPolicy: core.RestartPolicyNever,
|
|
|
|
TerminationGracePeriodSeconds: &defaultDBServerTerminationTimeout,
|
|
|
|
Hostname: testDeploymentName + "-" + api.ServerGroupDBServersString + "-" +
|
|
|
|
firstDBServerStatus.ID,
|
|
|
|
Subdomain: testDeploymentName + "-int",
|
|
|
|
Affinity: k8sutil.CreateAffinity(testDeploymentName, api.ServerGroupDBServersString,
|
|
|
|
false, ""),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "DBserver POD with resource requirements, with override flag",
|
|
|
|
ArangoDeployment: &api.ArangoDeployment{
|
|
|
|
Spec: api.DeploymentSpec{
|
|
|
|
Image: util.NewString(testImage),
|
|
|
|
Authentication: noAuthentication,
|
|
|
|
TLS: noTLS,
|
|
|
|
DBServers: api.ServerGroupSpec{
|
|
|
|
Resources: resourcesUnfiltered,
|
2020-09-09 13:44:05 +00:00
|
|
|
OverrideDetectedTotalMemory: util.NewBool(false),
|
2020-03-20 13:39:32 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Helper: func(t *testing.T, deployment *Deployment, testCase *testCaseStruct) {
|
2022-08-25 11:44:28 +00:00
|
|
|
deployment.currentObjectStatus = &api.DeploymentStatus{
|
2020-03-20 13:39:32 +00:00
|
|
|
Members: api.DeploymentStatusMembers{
|
|
|
|
DBServers: api.MemberStatusList{
|
|
|
|
firstDBServerStatus,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Images: createTestImages(false),
|
|
|
|
}
|
2022-08-25 11:44:28 +00:00
|
|
|
deployment.currentObjectStatus.Members.DBServers[0].IsInitialized = true
|
2020-03-20 13:39:32 +00:00
|
|
|
|
|
|
|
testCase.createTestPodData(deployment, api.ServerGroupDBServers, firstDBServerStatus)
|
|
|
|
},
|
|
|
|
ExpectedEvent: "member dbserver is created",
|
|
|
|
ExpectedPod: core.Pod{
|
|
|
|
Spec: core.PodSpec{
|
|
|
|
Volumes: []core.Volume{
|
2022-03-28 13:53:58 +00:00
|
|
|
k8sutil.CreateVolumeEmptyDir(shared.ArangodVolumeName),
|
2020-03-20 13:39:32 +00:00
|
|
|
},
|
|
|
|
Containers: []core.Container{
|
|
|
|
{
|
2022-03-28 13:53:58 +00:00
|
|
|
Name: shared.ServerContainerName,
|
2020-03-20 13:39:32 +00:00
|
|
|
Image: testImage,
|
|
|
|
Command: createTestCommandForDBServer(firstDBServerStatus.ID, false, false, false),
|
|
|
|
Ports: createTestPorts(),
|
|
|
|
Resources: k8sutil.ExtractPodResourceRequirement(resourcesUnfiltered),
|
|
|
|
VolumeMounts: []core.VolumeMount{
|
|
|
|
k8sutil.ArangodVolumeMount(),
|
|
|
|
},
|
2021-05-07 14:13:15 +00:00
|
|
|
Env: withEnvs(resourceCPULimitAsEnv(t, resourcesUnfiltered)),
|
2022-03-28 13:53:58 +00:00
|
|
|
LivenessProbe: createTestLivenessProbe(httpProbe, false, "", shared.ArangoPort),
|
2020-03-20 13:39:32 +00:00
|
|
|
ImagePullPolicy: core.PullIfNotPresent,
|
|
|
|
SecurityContext: securityContext.NewSecurityContext(),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
RestartPolicy: core.RestartPolicyNever,
|
|
|
|
TerminationGracePeriodSeconds: &defaultDBServerTerminationTimeout,
|
|
|
|
Hostname: testDeploymentName + "-" + api.ServerGroupDBServersString + "-" +
|
|
|
|
firstDBServerStatus.ID,
|
|
|
|
Subdomain: testDeploymentName + "-int",
|
|
|
|
Affinity: k8sutil.CreateAffinity(testDeploymentName, api.ServerGroupDBServersString,
|
|
|
|
false, ""),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "DBserver POD without resource requirements, with override flag",
|
|
|
|
ArangoDeployment: &api.ArangoDeployment{
|
|
|
|
Spec: api.DeploymentSpec{
|
|
|
|
Image: util.NewString(testImage),
|
|
|
|
Authentication: noAuthentication,
|
|
|
|
TLS: noTLS,
|
|
|
|
DBServers: api.ServerGroupSpec{
|
|
|
|
OverrideDetectedTotalMemory: util.NewBool(true),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Helper: func(t *testing.T, deployment *Deployment, testCase *testCaseStruct) {
|
2022-08-25 11:44:28 +00:00
|
|
|
deployment.currentObjectStatus = &api.DeploymentStatus{
|
2020-03-20 13:39:32 +00:00
|
|
|
Members: api.DeploymentStatusMembers{
|
|
|
|
DBServers: api.MemberStatusList{
|
|
|
|
firstDBServerStatus,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Images: createTestImages(false),
|
|
|
|
}
|
2022-08-25 11:44:28 +00:00
|
|
|
deployment.currentObjectStatus.Members.DBServers[0].IsInitialized = true
|
2020-03-20 13:39:32 +00:00
|
|
|
|
|
|
|
testCase.createTestPodData(deployment, api.ServerGroupDBServers, firstDBServerStatus)
|
|
|
|
},
|
|
|
|
ExpectedEvent: "member dbserver is created",
|
|
|
|
ExpectedPod: core.Pod{
|
|
|
|
Spec: core.PodSpec{
|
|
|
|
Volumes: []core.Volume{
|
2022-03-28 13:53:58 +00:00
|
|
|
k8sutil.CreateVolumeEmptyDir(shared.ArangodVolumeName),
|
2020-03-20 13:39:32 +00:00
|
|
|
},
|
|
|
|
Containers: []core.Container{
|
|
|
|
{
|
2022-03-28 13:53:58 +00:00
|
|
|
Name: shared.ServerContainerName,
|
2020-03-20 13:39:32 +00:00
|
|
|
Image: testImage,
|
|
|
|
Command: createTestCommandForDBServer(firstDBServerStatus.ID, false, false, false),
|
|
|
|
Ports: createTestPorts(),
|
|
|
|
Resources: emptyResources,
|
|
|
|
VolumeMounts: []core.VolumeMount{
|
|
|
|
k8sutil.ArangodVolumeMount(),
|
|
|
|
},
|
2022-03-28 13:53:58 +00:00
|
|
|
LivenessProbe: createTestLivenessProbe(httpProbe, false, "", shared.ArangoPort),
|
2020-03-20 13:39:32 +00:00
|
|
|
ImagePullPolicy: core.PullIfNotPresent,
|
|
|
|
SecurityContext: securityContext.NewSecurityContext(),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
RestartPolicy: core.RestartPolicyNever,
|
|
|
|
TerminationGracePeriodSeconds: &defaultDBServerTerminationTimeout,
|
|
|
|
Hostname: testDeploymentName + "-" + api.ServerGroupDBServersString + "-" +
|
|
|
|
firstDBServerStatus.ID,
|
|
|
|
Subdomain: testDeploymentName + "-int",
|
|
|
|
Affinity: k8sutil.CreateAffinity(testDeploymentName, api.ServerGroupDBServersString,
|
|
|
|
false, ""),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
runTestCases(t, testCases...)
|
|
|
|
}
|