Skip to content
This repository was archived by the owner on May 28, 2021. It is now read-only.

Commit 194e751

Browse files
KashifSaadatprydie
authored andcommitted
Allow defining Resource Requirements per Container in the ClusterSpec (#227)
Signed-off-by: Kashif Saadat <kashifsaadat@gmail.com>
1 parent 7f26060 commit 194e751

File tree

3 files changed

+90
-2
lines changed

3 files changed

+90
-2
lines changed

pkg/apis/mysql/v1alpha1/types.go

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,11 +64,13 @@ type ClusterSpec struct {
6464
// and server key for group replication SSL.
6565
// +optional
6666
SSLSecret *corev1.LocalObjectReference `json:"sslSecret,omitempty"`
67-
// SecurityContext holds pod-level security attributes and common container settings.
67+
// SecurityContext holds Pod-level security attributes and common Container settings.
6868
SecurityContext *corev1.PodSecurityContext `json:"securityContext,omitempty"`
6969
// Tolerations allows specifying a list of tolerations for controlling which
70-
// set of nodes a pod can be scheduled on
70+
// set of Nodes a Pod can be scheduled on
7171
Tolerations *[]corev1.Toleration `json:"tolerations,omitempty"`
72+
// Resources holds ResourceRequirements for the MySQL Agent & Server Containers
73+
Resources *Resources `json:"resources,omitempty"`
7274
}
7375

7476
// ClusterConditionType represents a valid condition of a Cluster.
@@ -125,6 +127,12 @@ type ClusterList struct {
125127
Items []Cluster `json:"items"`
126128
}
127129

130+
// Resources holds ResourceRequirements for the MySQL Agent & Server Containers
131+
type Resources struct {
132+
Agent *corev1.ResourceRequirements `json:"agent,omitempty"`
133+
Server *corev1.ResourceRequirements `json:"server,omitempty"`
134+
}
135+
128136
// Database represents a database to backup.
129137
type Database struct {
130138
Name string `json:"name"`

pkg/resources/statefulsets/statefulset.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import (
2222

2323
apps "k8s.io/api/apps/v1beta1"
2424
"k8s.io/api/core/v1"
25+
corev1 "k8s.io/api/core/v1"
2526
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2627
"k8s.io/apimachinery/pkg/runtime/schema"
2728
"k8s.io/apimachinery/pkg/util/intstr"
@@ -193,6 +194,12 @@ func mysqlServerContainer(cluster *v1alpha1.Cluster, mysqlServerImage string, ro
193194
# a unique server id for this instance.
194195
index=$(cat /etc/hostname | grep -o '[^-]*$')
195196
/entrypoint.sh %s`, baseServerID, entryPointArgs)
197+
198+
var resourceLimits corev1.ResourceRequirements
199+
if cluster.Spec.Resources != nil && cluster.Spec.Resources.Server != nil {
200+
resourceLimits = *cluster.Spec.Resources.Server
201+
}
202+
196203
return v1.Container{
197204
Name: MySQLServerName,
198205
// TODO(apryde): Add BaseImage to cluster CRD.
@@ -215,6 +222,7 @@ func mysqlServerContainer(cluster *v1alpha1.Cluster, mysqlServerImage string, ro
215222
Value: "true",
216223
},
217224
},
225+
Resources: resourceLimits,
218226
}
219227
}
220228

@@ -226,6 +234,11 @@ func mysqlAgentContainer(cluster *v1alpha1.Cluster, mysqlAgentImage string, root
226234

227235
replicationGroupSeeds := getReplicationGroupSeeds(cluster.Name, members)
228236

237+
var resourceLimits corev1.ResourceRequirements
238+
if cluster.Spec.Resources != nil && cluster.Spec.Resources.Agent != nil {
239+
resourceLimits = *cluster.Spec.Resources.Agent
240+
}
241+
229242
return v1.Container{
230243
Name: MySQLAgentName,
231244
Image: fmt.Sprintf("%s:%s", mysqlAgentImage, agentVersion),
@@ -262,6 +275,7 @@ func mysqlAgentContainer(cluster *v1alpha1.Cluster, mysqlAgentImage string, root
262275
},
263276
},
264277
},
278+
Resources: resourceLimits,
265279
}
266280
}
267281

pkg/resources/statefulsets/statefulset_test.go

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020

2121
"github.com/stretchr/testify/assert"
2222
corev1 "k8s.io/api/core/v1"
23+
"k8s.io/apimachinery/pkg/api/resource"
2324
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2425

2526
"github.com/oracle/mysql-operator/pkg/apis/mysql/v1alpha1"
@@ -221,3 +222,68 @@ func TestClusterWithTolerations(t *testing.T) {
221222
}
222223
}
223224
}
225+
226+
func TestClusterWithResourceRequirements(t *testing.T) {
227+
mysqlServerResourceRequirements := corev1.ResourceRequirements{
228+
Limits: corev1.ResourceList{
229+
corev1.ResourceCPU: resource.MustParse("2"),
230+
corev1.ResourceMemory: resource.MustParse("2Gi"),
231+
},
232+
Requests: corev1.ResourceList{
233+
corev1.ResourceCPU: resource.MustParse("500m"),
234+
corev1.ResourceMemory: resource.MustParse("1Gi"),
235+
},
236+
}
237+
238+
mysqlAgentResourceRequirements := corev1.ResourceRequirements{
239+
Limits: corev1.ResourceList{
240+
corev1.ResourceCPU: resource.MustParse("500m"),
241+
corev1.ResourceMemory: resource.MustParse("512Mi"),
242+
},
243+
Requests: corev1.ResourceList{
244+
corev1.ResourceCPU: resource.MustParse("100m"),
245+
corev1.ResourceMemory: resource.MustParse("128Mi"),
246+
},
247+
}
248+
249+
cluster := &v1alpha1.Cluster{
250+
Spec: v1alpha1.ClusterSpec{
251+
Resources: &v1alpha1.Resources{
252+
Server: &mysqlServerResourceRequirements,
253+
Agent: &mysqlAgentResourceRequirements,
254+
},
255+
},
256+
}
257+
258+
statefulSet := NewForCluster(cluster, mockOperatorConfig().Images, "mycluster")
259+
260+
assert.Equal(t, mysqlServerResourceRequirements, statefulSet.Spec.Template.Spec.Containers[0].Resources, "MySQL-Server container resource requirements do not match expected.")
261+
assert.Equal(t, mysqlAgentResourceRequirements, statefulSet.Spec.Template.Spec.Containers[1].Resources, "MySQL-Agent container resource requirements do not match expected.")
262+
}
263+
264+
func TestClusterWithOnlyMysqlServerResourceRequirements(t *testing.T) {
265+
mysqlServerResourceRequirements := corev1.ResourceRequirements{
266+
Limits: corev1.ResourceList{
267+
corev1.ResourceCPU: resource.MustParse("2"),
268+
corev1.ResourceMemory: resource.MustParse("2Gi"),
269+
},
270+
Requests: corev1.ResourceList{
271+
corev1.ResourceCPU: resource.MustParse("500m"),
272+
corev1.ResourceMemory: resource.MustParse("1Gi"),
273+
},
274+
}
275+
276+
cluster := &v1alpha1.Cluster{
277+
Spec: v1alpha1.ClusterSpec{
278+
Resources: &v1alpha1.Resources{
279+
Server: &mysqlServerResourceRequirements,
280+
},
281+
},
282+
}
283+
284+
statefulSet := NewForCluster(cluster, mockOperatorConfig().Images, "mycluster")
285+
286+
assert.Equal(t, mysqlServerResourceRequirements, statefulSet.Spec.Template.Spec.Containers[0].Resources, "MySQL-Server container resource requirements do not match expected.")
287+
assert.Nil(t, statefulSet.Spec.Template.Spec.Containers[1].Resources.Limits, "MySQL-Agent container has resource limits set which were not initially defined in the spec")
288+
assert.Nil(t, statefulSet.Spec.Template.Spec.Containers[1].Resources.Requests, "MySQL-Agent container has resource requests set which were not initially defined in the spec")
289+
}

0 commit comments

Comments
 (0)