Ejemplo n.º 1
0
  def test_wait_for_statefulset(self):
    api_client = mock.MagicMock(spec=k8s_client.ApiClient)

    response = k8s_client.V1beta1StatefulSet()
    response.status = k8s_client.V1beta1StatefulSetStatus(
      ready_replicas=1, replicas=1)
    api_client.call_api.return_value = response
    result = util.wait_for_statefulset(api_client, "some-namespace", "some-set")
    self.assertIsNotNone(result)
Ejemplo n.º 2
0
    def create_stateful_set(self, replicas=3, volumeSize=10, namespace=None):
        """
        Create the StatefulSet for the Mongo cluster within a namespace.
        :param replicas: Number of replicas for the StatefulSet.
        :params volumeSize: The size of the volume to be generated via PVC and attached to pods
                            within the statefulSet.
        :param namespace: Namespace to deploy to.
        :return: N/A
        """
        self._create_mongo_configmap(namespace=namespace)

        stateful_set = client.V1beta1StatefulSet(
            metadata=client.V1ObjectMeta(name='mongo',
                                         namespace=namespace,
                                         labels={"app": "MongoStatefulSet"}),
            status=client.V1beta1StatefulSetStatus(replicas=replicas),
            spec=client.V1beta1StatefulSetSpec(
                volume_claim_templates=[
                    client.V1PersistentVolumeClaim(
                        spec=client.V1PersistentVolumeClaimSpec(
                            access_modes=['ReadWriteOnce'],
                            resources=client.V1ResourceRequirements(
                                requests={
                                    "storage": '{0}Gi'.format(volumeSize)
                                })))
                ],
                replicas=replicas,
                selector=client.V1LabelSelector(
                    match_labels={"app": "MongoStatefulSet"}),
                service_name="mongo-service",
                template=client.V1PodTemplateSpec(
                    metadata=client.V1ObjectMeta(
                        name='mongo-pod',
                        namespace=namespace,
                        labels={"app": "MongoStatefulSet"}),
                    spec=client.V1PodSpec(
                        containers=[
                            client.V1Container(
                                name='mongo-container',
                                image=Config.MONGO_IMAGE,
                                image_pull_policy='Always',
                                ports=[
                                    client.V1ContainerPort(
                                        name='mongo',
                                        container_port=27017,
                                        protocol='TCP')
                                ],
                                volume_mounts=[
                                    client.V1VolumeMount(
                                        name='mongo-config-volume',
                                        mount_path='/etc/mongo')
                                ],
                                command=[
                                    "mongod", "-f", "/etc/mongo/mongod.conf"
                                ])
                        ],
                        volumes=[
                            client.V1Volume(
                                name='mongo-config-volume',
                                config_map=client.V1ConfigMapVolumeSource(
                                    default_mode=0o555,
                                    name='mongo-configmap'))
                        ]))))

        try:
            self.apps_v1_api.create_namespaced_stateful_set(
                namespace=namespace, body=stateful_set)
        except ApiException as E:
            if E.status == 409:
                print('Stateful Set already exists.')
            else:
                raise E