Skip to content

Commit 3112b79

Browse files
authored
K8SPS 388 small refactor on InitContainer, reuse it when needed - e2e test introduction for the case (#811)
* K8SPS-388 Introduce container resources where needed for when Resource Quotas are specified * reuse init container * add e2e test * adjust rq resources * give more memory to the namespace level quota * increase limit memory more * adjust resources once again * adjust resources once more
1 parent b58b089 commit 3112b79

File tree

11 files changed

+150
-34
lines changed

11 files changed

+150
-34
lines changed

e2e-tests/tests/init-deploy/00-assert.yaml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,11 @@ apiVersion: kuttl.dev/v1beta1
22
kind: TestAssert
33
timeout: 120
44
---
5+
kind: ResourceQuota
6+
apiVersion: v1
7+
metadata:
8+
name: init-deploy-resource-quota
9+
---
510
apiVersion: apiextensions.k8s.io/v1
611
kind: CustomResourceDefinition
712
metadata:

e2e-tests/tests/init-deploy/00-deploy-operator.yaml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,14 @@
1+
apiVersion: v1
2+
kind: ResourceQuota
3+
metadata:
4+
name: init-deploy-resource-quota
5+
spec:
6+
hard:
7+
requests.cpu: "8"
8+
requests.memory: "10Gi"
9+
limits.cpu: "10"
10+
limits.memory: "12Gi"
11+
---
112
apiVersion: kuttl.dev/v1beta1
213
kind: TestStep
314
timeout: 10

e2e-tests/tests/init-deploy/01-create-cluster.yaml

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,30 @@ commands:
99
source ../../functions
1010
1111
get_cr \
12-
| yq eval '.spec.mysql.clusterType="async"' - \
13-
| yq eval '.spec.mysql.size=3' - \
14-
| yq eval '.spec.proxy.haproxy.enabled=true' - \
15-
| yq eval '.spec.proxy.haproxy.size=3' - \
16-
| yq eval '.spec.orchestrator.enabled=true' - \
17-
| yq eval '.spec.orchestrator.size=3' - \
18-
| kubectl -n "${NAMESPACE}" apply -f -
12+
| yq eval '.spec.mysql.clusterType="async"' - \
13+
| yq eval '.spec.mysql.size=3' - \
14+
| yq eval '.spec.proxy.haproxy.enabled=true' - \
15+
| yq eval '.spec.proxy.haproxy.size=3' - \
16+
| yq eval '.spec.orchestrator.enabled=true' - \
17+
| yq eval '.spec.mysql.resources.limits.cpu="500m"' - \
18+
| yq eval '.spec.mysql.resources.limits.memory="1G"' - \
19+
| yq eval '.spec.mysql.resources.requests.cpu="400m"' - \
20+
| yq eval '.spec.mysql.resources.requests.memory="1G"' - \
21+
| yq eval '.spec.backup.resources.limits.cpu="200m"' - \
22+
| yq eval '.spec.backup.resources.limits.memory="256Mi"' - \
23+
| yq eval '.spec.backup.resources.requests.cpu="100m"' - \
24+
| yq eval '.spec.backup.resources.requests.memory="128Mi"' - \
25+
| yq eval '.spec.toolkit.resources.limits.cpu="150m"' - \
26+
| yq eval '.spec.toolkit.resources.limits.memory="256Mi"' - \
27+
| yq eval '.spec.toolkit.resources.requests.cpu="100m"' - \
28+
| yq eval '.spec.toolkit.resources.requests.memory="128Mi"' - \
29+
| yq eval '.spec.proxy.haproxy.resources.limits.cpu="600m"' - \
30+
| yq eval '.spec.proxy.haproxy.resources.limits.memory="512Mi"' - \
31+
| yq eval '.spec.proxy.haproxy.resources.requests.cpu="500m"' - \
32+
| yq eval '.spec.proxy.haproxy.resources.requests.memory="256Mi"' - \
33+
| yq eval '.spec.orchestrator.resources.limits.cpu="200m"' - \
34+
| yq eval '.spec.orchestrator.resources.limits.memory="256Mi"' - \
35+
| yq eval '.spec.orchestrator.resources.requests.cpu="100m"' - \
36+
| yq eval '.spec.orchestrator.resources.requests.memory="128Mi"' - \
37+
| yq eval '.spec.orchestrator.size=3' - \
38+
| kubectl -n "${NAMESPACE}" apply -f -

pkg/binlogserver/binlog_server.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ const (
2222
configVolumeName = "config"
2323
configMountPath = "/etc/binlog_server/config"
2424
storageCredsVolumeName = "storage"
25-
storageCredsMountPath = "/etc/binlog_server/storage"
2625
ConfigKey = "config.json"
2726
CustomConfigKey = "custom.json"
2827
)
@@ -87,6 +86,7 @@ func StatefulSet(cr *apiv1alpha1.PerconaServerMySQL, initImage, configHash strin
8786
spec.ImagePullPolicy,
8887
spec.ContainerSecurityContext,
8988
spec.Resources,
89+
nil,
9090
),
9191
},
9292
Containers: containers(cr),

pkg/haproxy/haproxy.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,7 @@ func StatefulSet(cr *apiv1alpha1.PerconaServerMySQL, initImage, configHash, tlsH
173173
cr.Spec.Proxy.HAProxy.ImagePullPolicy,
174174
cr.Spec.Proxy.HAProxy.ContainerSecurityContext,
175175
cr.Spec.Proxy.HAProxy.Resources,
176+
nil,
176177
),
177178
},
178179
Containers: containers(cr, secret),

pkg/k8s/init_image.go

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -16,22 +16,34 @@ type ComponentWithInit interface {
1616
GetInitImage() string
1717
}
1818

19-
func InitContainer(component, image string, pullPolicy corev1.PullPolicy, secCtx *corev1.SecurityContext, resList corev1.ResourceRequirements) corev1.Container {
20-
return corev1.Container{
21-
Name: component + "-init",
22-
Image: image,
23-
ImagePullPolicy: pullPolicy,
24-
VolumeMounts: []corev1.VolumeMount{
25-
{
26-
Name: apiv1alpha1.BinVolumeName,
27-
MountPath: apiv1alpha1.BinVolumePath,
28-
},
19+
func InitContainer(component, image string,
20+
pullPolicy corev1.PullPolicy,
21+
secCtx *corev1.SecurityContext,
22+
resources corev1.ResourceRequirements,
23+
extraVolumeMounts []corev1.VolumeMount,
24+
) corev1.Container {
25+
26+
volumeMounts := []corev1.VolumeMount{
27+
{
28+
Name: apiv1alpha1.BinVolumeName,
29+
MountPath: apiv1alpha1.BinVolumePath,
2930
},
31+
}
32+
33+
if len(extraVolumeMounts) > 0 {
34+
volumeMounts = append(volumeMounts, extraVolumeMounts...)
35+
}
36+
37+
return corev1.Container{
38+
Name: component + "-init",
39+
Image: image,
40+
ImagePullPolicy: pullPolicy,
41+
VolumeMounts: volumeMounts,
3042
Command: []string{"/opt/percona-server-mysql-operator/ps-init-entrypoint.sh"},
3143
TerminationMessagePath: "/dev/termination-log",
3244
TerminationMessagePolicy: corev1.TerminationMessageReadFile,
3345
SecurityContext: secCtx,
34-
Resources: resList,
46+
Resources: resources,
3547
}
3648
}
3749

pkg/k8s/init_image_test.go

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
package k8s
2+
3+
import (
4+
"github.com/stretchr/testify/assert"
5+
corev1 "k8s.io/api/core/v1"
6+
"k8s.io/apimachinery/pkg/api/resource"
7+
"testing"
8+
)
9+
10+
func TestInitContainer(t *testing.T) {
11+
componentName := "example-component"
12+
image := "example-image:latest"
13+
pullPolicy := corev1.PullAlways
14+
secCtx := &corev1.SecurityContext{}
15+
16+
expectedVolumeMounts := []corev1.VolumeMount{
17+
{
18+
Name: "bin",
19+
MountPath: "/opt/percona",
20+
},
21+
}
22+
expectedCommand := []string{"/opt/percona-server-mysql-operator/ps-init-entrypoint.sh"}
23+
expectedTerminationMessagePath := "/dev/termination-log"
24+
expectedTerminationMessagePolicy := corev1.TerminationMessageReadFile
25+
26+
expectedResources := corev1.ResourceRequirements{
27+
Limits: corev1.ResourceList{
28+
corev1.ResourceCPU: resource.MustParse("500m"),
29+
corev1.ResourceMemory: resource.MustParse("256Mi"),
30+
},
31+
Requests: corev1.ResourceList{
32+
corev1.ResourceCPU: resource.MustParse("250m"),
33+
corev1.ResourceMemory: resource.MustParse("128Mi"),
34+
},
35+
}
36+
37+
tests := map[string]struct {
38+
inputVolumes []corev1.VolumeMount
39+
expectedVolumes []corev1.VolumeMount
40+
}{
41+
"default volumes": {
42+
expectedVolumes: expectedVolumeMounts,
43+
},
44+
"additional volumes": {
45+
inputVolumes: []corev1.VolumeMount{
46+
{
47+
Name: "dataVolumeName",
48+
MountPath: "dataMountPath",
49+
},
50+
},
51+
expectedVolumes: append(expectedVolumeMounts,
52+
corev1.VolumeMount{
53+
Name: "dataVolumeName",
54+
MountPath: "dataMountPath"}),
55+
},
56+
}
57+
for name, tt := range tests {
58+
t.Run(name, func(t *testing.T) {
59+
container := InitContainer(componentName, image, pullPolicy, secCtx, expectedResources, tt.inputVolumes)
60+
61+
assert.Equal(t, componentName+"-init", container.Name)
62+
assert.Equal(t, image, container.Image)
63+
assert.Equal(t, pullPolicy, container.ImagePullPolicy)
64+
assert.Equal(t, tt.expectedVolumes, container.VolumeMounts)
65+
assert.Equal(t, expectedCommand, container.Command)
66+
assert.Equal(t, expectedTerminationMessagePath, container.TerminationMessagePath)
67+
assert.Equal(t, expectedTerminationMessagePolicy, container.TerminationMessagePolicy)
68+
assert.Equal(t, secCtx, container.SecurityContext)
69+
assert.Equal(t, expectedResources, container.Resources)
70+
})
71+
}
72+
}

pkg/mysql/mysql.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,7 @@ func StatefulSet(cr *apiv1alpha1.PerconaServerMySQL, initImage, configHash, tlsH
164164
spec.ImagePullPolicy,
165165
spec.ContainerSecurityContext,
166166
spec.Resources,
167+
nil,
167168
),
168169
},
169170
Containers: containers(cr, secret),

pkg/orchestrator/orchestrator.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,7 @@ func StatefulSet(cr *apiv1alpha1.PerconaServerMySQL, initImage, tlsHash string)
146146
spec.ImagePullPolicy,
147147
spec.ContainerSecurityContext,
148148
spec.Resources,
149+
nil,
149150
),
150151
},
151152
NodeSelector: cr.Spec.Orchestrator.NodeSelector,

pkg/router/router.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,7 @@ func Deployment(cr *apiv1alpha1.PerconaServerMySQL, initImage, configHash, tlsHa
182182
spec.ImagePullPolicy,
183183
spec.ContainerSecurityContext,
184184
spec.Resources,
185+
nil,
185186
),
186187
},
187188
Containers: containers(cr),

0 commit comments

Comments
 (0)