diff --git a/README.md b/README.md index 57f4efc..f06e89f 100644 --- a/README.md +++ b/README.md @@ -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. diff --git a/mongoOperator/helpers/KubernetesResources.py b/mongoOperator/helpers/KubernetesResources.py index 9d25031..454a064 100644 --- a/mongoOperator/helpers/KubernetesResources.py +++ b/mongoOperator/helpers/KubernetesResources.py @@ -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 @@ -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 @@ -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, @@ -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)), diff --git a/mongoOperator/models/V1MongoClusterConfigurationSpecMongoDB.py b/mongoOperator/models/V1MongoClusterConfigurationSpecMongoDB.py index 46d5258..cc62470 100644 --- a/mongoOperator/models/V1MongoClusterConfigurationSpecMongoDB.py +++ b/mongoOperator/models/V1MongoClusterConfigurationSpecMongoDB.py @@ -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) diff --git a/tests/services/TestKubernetesService.py b/tests/services/TestKubernetesService.py index 9adf410..38eeef6 100644 --- a/tests/services/TestKubernetesService.py +++ b/tests/services/TestKubernetesService.py @@ -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: @@ -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): @@ -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