Skip to content
This repository has been archived by the owner on Jul 9, 2019. It is now read-only.

Commit

Permalink
Merge pull request #29 from Ultimaker/fix_cpu_requests
Browse files Browse the repository at this point in the history
Add CPU and Memory 'request' settings
  • Loading branch information
ChrisTerBeke authored Mar 14, 2019
2 parents f1371b1 + 198c6c3 commit ef6aace
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 7 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,9 @@ The following options are available to use in the `spec` section of the `yaml` c
| `mongodb.storage_data_path` | /data/db | The path on which the persistent volumes are mounted in the Mongo containers. |
| `mongodb.storage_class_name` | - | The name of the storage class to use to create the persistent values. If not passed it will use the Kubernetes cluster default storage class name. |
| `mongodb.cpu_limit` | 1 | The CPU limit of each container. |
| `mongodb.cpu_request` | 0.5 | The CPU request of each container. |
| `mongodb.memory_limit` | 2Gi | The memory limit of each container. |
| `mongodb.memory_request` | 1Gi | The memory request of each container. |
| `mongodb.wired_tiger_cache_size` | 0.25 | The wired tiger cache size. |
| `mongodb.replicas` | - | The amount of MongoDB replicas that should be available in the replica set. Must be an uneven positive integer and minimum 3. |
| * `backups.cron` | - | The cron on which to create a backup to cloud storage.
Expand Down
16 changes: 10 additions & 6 deletions mongoOperator/helpers/KubernetesResources.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,9 @@ class KubernetesResources:
# Default resource allocation.
# See https://docs.mongodb.com/manual/administration/production-notes/#allocate-sufficient-ram-and-cpu.
DEFAULT_CPU_LIMIT = "1"
DEFAULT_CPU_REQUEST = "0.5"
DEFAULT_MEMORY_LIMIT = "2Gi"
DEFAULT_MEMORY_REQUEST = "1Gi"
DEFAULT_CACHE_SIZE = "0.25"

@classmethod
Expand Down Expand Up @@ -106,14 +108,14 @@ def createStatefulSet(cls, cluster_object: V1MongoClusterConfiguration) -> clien

# Parse cluster data object.
name = cluster_object.metadata.name
namespace = cluster_object.metadata.namespace
replicas = cluster_object.spec.mongodb.replicas
storage_name = cluster_object.spec.mongodb.storage_name or cls.DEFAULT_STORAGE_NAME
storage_size = cluster_object.spec.mongodb.storage_size or cls.DEFAULT_STORAGE_SIZE
storage_mount_path = cluster_object.spec.mongodb.storage_data_path or cls.DEFAULT_STORAGE_MOUNT_PATH
storage_class_name = cluster_object.spec.mongodb.storage_class_name or cls.DEFAULT_STORAGE_CLASS_NAME
cpu_limit = cluster_object.spec.mongodb.cpu_limit or cls.DEFAULT_CPU_LIMIT
cpu_request = cluster_object.spec.mongodb.cpu_request or cls.DEFAULT_CPU_REQUEST
memory_limit = cluster_object.spec.mongodb.memory_limit or cls.DEFAULT_MEMORY_LIMIT
memory_request = cluster_object.spec.mongodb.memory_request or cls.DEFAULT_MEMORY_REQUEST
wired_tiger_cache_size = cluster_object.spec.mongodb.wired_tiger_cache_size or cls.DEFAULT_CACHE_SIZE

# create container
Expand All @@ -128,7 +130,8 @@ def createStatefulSet(cls, cluster_object: V1MongoClusterConfiguration) -> clien
)
)
)],
command=cls.MONGO_COMMAND.format(name=name, cache_size=wired_tiger_cache_size).split(),
command=cls.MONGO_COMMAND.format(name=name,
cache_size=wired_tiger_cache_size).split(),
image=cls.MONGO_IMAGE,
ports=[client.V1ContainerPort(
name=cls.MONGO_NAME,
Expand All @@ -142,15 +145,16 @@ def createStatefulSet(cls, cluster_object: V1MongoClusterConfiguration) -> clien
)],
resources=client.V1ResourceRequirements(
limits={"cpu": cpu_limit, "memory": memory_limit},
requests={"cpu": cpu_limit, "memory": memory_limit}
requests={"cpu": cpu_request, "memory": memory_request}
)
)

# Create stateful set.
return client.V1beta1StatefulSet(
metadata = client.V1ObjectMeta(name=name, namespace=namespace, labels=cls.createDefaultLabels(name)),
metadata = client.V1ObjectMeta(name=name, namespace=cluster_object.metadata.namespace,
labels=cls.createDefaultLabels(name)),
spec = client.V1beta1StatefulSetSpec(
replicas = replicas,
replicas = cluster_object.spec.mongodb.replicas,
service_name = name,
template = client.V1PodTemplateSpec(
metadata = client.V1ObjectMeta(labels=cls.createDefaultLabels(name)),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,15 @@ class V1MongoClusterConfigurationSpecMongoDB(BaseModel):
# Kubernetes CPU limit of each Mongo container. Defaults to 1 (vCPU).
cpu_limit = StringField(required=False)

# Kubernetes CPU request of each Mongo container. Defaults to 0.5 (vCPU).
cpu_request = StringField(required=False)

# Kubernetes memory limit of each Mongo container. Defaults to 2Gi.
memory_limit = StringField(required=False)

# Kubernetes memory request of each Mongo container. Defaults to 1Gi.
memory_request = StringField(required=False)

# Amount of Mongo container replicas. Defaults to 3.
replicas = MongoReplicaCountField(required=True)

Expand Down
6 changes: 5 additions & 1 deletion tests/services/TestKubernetesService.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,9 @@ def setUp(self):
self.name = self.cluster_object.metadata.name
self.namespace = self.cluster_object.metadata.namespace
self.cpu_limit = "100m"
self.cpu_request = "0.5"
self.memory_limit = "64Mi"
self.memory_request = "1Gi"
self.stateful_set = self._createStatefulSet()

def _createStatefulSet(self) -> V1beta1StatefulSet:
Expand Down Expand Up @@ -85,7 +87,7 @@ def _createMeta(self, name: str) -> V1ObjectMeta:
def _createResourceLimits(self) -> V1ResourceRequirements:
return V1ResourceRequirements(
limits={"cpu": self.cpu_limit, "memory": self.memory_limit},
requests={"cpu": self.cpu_limit, "memory": self.memory_limit}
requests={"cpu": self.cpu_request, "memory": self.memory_request}
)

def test___init__(self, client_mock):
Expand Down Expand Up @@ -445,7 +447,9 @@ def test_createStatefulSet_no_optional_fields(self, client_mock):
del self.cluster_dict["spec"]["mongodb"]["cpu_limit"]
del self.cluster_dict["spec"]["mongodb"]["memory_limit"]
self.cpu_limit = "1"
self.cpu_request = "0.5"
self.memory_limit = "2Gi"
self.memory_request = "1Gi"
self.cluster_object = V1MongoClusterConfiguration(**self.cluster_dict)
self.stateful_set = self._createStatefulSet() # update with the new resource requirements

Expand Down

0 comments on commit ef6aace

Please sign in to comment.