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

Allow defining Resource Requirements per container in the ClusterSpec #227

Merged
merged 1 commit into from
Sep 28, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions pkg/apis/mysql/v1alpha1/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,11 +64,13 @@ type ClusterSpec struct {
// and server key for group replication SSL.
// +optional
SSLSecret *corev1.LocalObjectReference `json:"sslSecret,omitempty"`
// SecurityContext holds pod-level security attributes and common container settings.
// SecurityContext holds Pod-level security attributes and common Container settings.
SecurityContext *corev1.PodSecurityContext `json:"securityContext,omitempty"`
// Tolerations allows specifying a list of tolerations for controlling which
// set of nodes a pod can be scheduled on
// set of Nodes a Pod can be scheduled on
Tolerations *[]corev1.Toleration `json:"tolerations,omitempty"`
// Resources holds ResourceRequirements for the MySQL Agent & Server Containers
Resources *Resources `json:"resources,omitempty"`
}

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

// Resources holds ResourceRequirements for the MySQL Agent & Server Containers
type Resources struct {
Agent *corev1.ResourceRequirements `json:"agent,omitempty"`
Server *corev1.ResourceRequirements `json:"server,omitempty"`
}

// Database represents a database to backup.
type Database struct {
Name string `json:"name"`
Expand Down
14 changes: 14 additions & 0 deletions pkg/resources/statefulsets/statefulset.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (

apps "k8s.io/api/apps/v1beta1"
"k8s.io/api/core/v1"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/apimachinery/pkg/util/intstr"
Expand Down Expand Up @@ -193,6 +194,12 @@ func mysqlServerContainer(cluster *v1alpha1.Cluster, mysqlServerImage string, ro
# a unique server id for this instance.
index=$(cat /etc/hostname | grep -o '[^-]*$')
/entrypoint.sh %s`, baseServerID, entryPointArgs)

var resourceLimits corev1.ResourceRequirements
if cluster.Spec.Resources != nil && cluster.Spec.Resources.Server != nil {
resourceLimits = *cluster.Spec.Resources.Server
}

return v1.Container{
Name: MySQLServerName,
// TODO(apryde): Add BaseImage to cluster CRD.
Expand All @@ -215,6 +222,7 @@ func mysqlServerContainer(cluster *v1alpha1.Cluster, mysqlServerImage string, ro
Value: "true",
},
},
Resources: resourceLimits,
}
}

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

replicationGroupSeeds := getReplicationGroupSeeds(cluster.Name, members)

var resourceLimits corev1.ResourceRequirements
if cluster.Spec.Resources != nil && cluster.Spec.Resources.Agent != nil {
resourceLimits = *cluster.Spec.Resources.Agent
}

return v1.Container{
Name: MySQLAgentName,
Image: fmt.Sprintf("%s:%s", mysqlAgentImage, agentVersion),
Expand Down Expand Up @@ -262,6 +275,7 @@ func mysqlAgentContainer(cluster *v1alpha1.Cluster, mysqlAgentImage string, root
},
},
},
Resources: resourceLimits,
}
}

Expand Down
66 changes: 66 additions & 0 deletions pkg/resources/statefulsets/statefulset_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (

"github.com/stretchr/testify/assert"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/resource"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"

"github.com/oracle/mysql-operator/pkg/apis/mysql/v1alpha1"
Expand Down Expand Up @@ -221,3 +222,68 @@ func TestClusterWithTolerations(t *testing.T) {
}
}
}

func TestClusterWithResourceRequirements(t *testing.T) {
mysqlServerResourceRequirements := corev1.ResourceRequirements{
Limits: corev1.ResourceList{
corev1.ResourceCPU: resource.MustParse("2"),
corev1.ResourceMemory: resource.MustParse("2Gi"),
},
Requests: corev1.ResourceList{
corev1.ResourceCPU: resource.MustParse("500m"),
corev1.ResourceMemory: resource.MustParse("1Gi"),
},
}

mysqlAgentResourceRequirements := corev1.ResourceRequirements{
Limits: corev1.ResourceList{
corev1.ResourceCPU: resource.MustParse("500m"),
corev1.ResourceMemory: resource.MustParse("512Mi"),
},
Requests: corev1.ResourceList{
corev1.ResourceCPU: resource.MustParse("100m"),
corev1.ResourceMemory: resource.MustParse("128Mi"),
},
}

cluster := &v1alpha1.Cluster{
Spec: v1alpha1.ClusterSpec{
Resources: &v1alpha1.Resources{
Server: &mysqlServerResourceRequirements,
Agent: &mysqlAgentResourceRequirements,
},
},
}

statefulSet := NewForCluster(cluster, mockOperatorConfig().Images, "mycluster")

assert.Equal(t, mysqlServerResourceRequirements, statefulSet.Spec.Template.Spec.Containers[0].Resources, "MySQL-Server container resource requirements do not match expected.")
assert.Equal(t, mysqlAgentResourceRequirements, statefulSet.Spec.Template.Spec.Containers[1].Resources, "MySQL-Agent container resource requirements do not match expected.")
}

func TestClusterWithOnlyMysqlServerResourceRequirements(t *testing.T) {
mysqlServerResourceRequirements := corev1.ResourceRequirements{
Limits: corev1.ResourceList{
corev1.ResourceCPU: resource.MustParse("2"),
corev1.ResourceMemory: resource.MustParse("2Gi"),
},
Requests: corev1.ResourceList{
corev1.ResourceCPU: resource.MustParse("500m"),
corev1.ResourceMemory: resource.MustParse("1Gi"),
},
}

cluster := &v1alpha1.Cluster{
Spec: v1alpha1.ClusterSpec{
Resources: &v1alpha1.Resources{
Server: &mysqlServerResourceRequirements,
},
},
}

statefulSet := NewForCluster(cluster, mockOperatorConfig().Images, "mycluster")

assert.Equal(t, mysqlServerResourceRequirements, statefulSet.Spec.Template.Spec.Containers[0].Resources, "MySQL-Server container resource requirements do not match expected.")
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")
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")
}