def initializeReplicaSet(
            self, cluster_object: V1MongoClusterConfiguration) -> None:
        """
        Initializes the replica set by sending an `initiate` command to the 1st Mongo pod.
        :param cluster_object: The cluster object from the YAML file.
        :raise ValueError: In case we receive an unexpected response from Mongo.
        :raise ApiException: In case we receive an unexpected response from Kubernetes.
        """
        cluster_name = cluster_object.metadata.name
        namespace = cluster_object.metadata.namespace

        create_replica_command = MongoResources.createReplicaInitiateCommand(
            cluster_object)

        create_replica_response = self._execInPod(0, cluster_name, namespace,
                                                  create_replica_command)

        logging.debug("Initializing replica, received %s",
                      repr(create_replica_response))

        if create_replica_response["ok"] == 1:
            logging.info("Initialized replica set %s @ ns/%s", cluster_name,
                         namespace)
        else:
            raise ValueError(
                "Unexpected response initializing replica set {} @ ns/{}:\n{}".
                format(cluster_name, namespace, create_replica_response))
Example #2
0
    def _initializeReplicaSet(
            cluster_object: V1MongoClusterConfiguration) -> None:
        """
        Initializes the replica set by sending an `initiate` command to the 1st Mongo pod.
        :param cluster_object: The cluster object from the YAML file.
        :raise ValueError: In case we receive an unexpected response from Mongo.
        :raise ApiException: In case we receive an unexpected response from Kubernetes.
        """
        cluster_name = cluster_object.metadata.name
        namespace = cluster_object.metadata.namespace

        logging.debug("Will initialize replicaset now.")
        master_connection = MongoClient(
            MongoResources.getMemberHostname(0, cluster_name, namespace),
            username='******',
            password=cluster_object.spec.users.admin_password,
            authSource='admin')
        create_replica_command, create_replica_args = MongoResources.createReplicaInitiateCommand(
            cluster_object)
        create_replica_response = master_connection.admin.command(
            create_replica_command, create_replica_args)

        if create_replica_response["ok"] == 1:
            logging.info("Initialized replica set %s @ ns/%s", cluster_name,
                         namespace)
            return

        logging.error("Initializing replica set failed, received %s",
                      repr(create_replica_response))
        raise ValueError(
            "Unexpected response initializing replica set {} @ ns/{}:\n{}".
            format(cluster_name, namespace, create_replica_response))
    def test__mongoAdminCommand_NodeNotFound(self, mongo_client_mock):
        mongo_client_mock.return_value.admin.command.side_effect = OperationFailure(
            "replSetInitiate quorum check failed because not all proposed set members responded affirmatively:"
        )

        with self.assertRaises(OperationFailure) as ex:
            mongo_command, mongo_args = MongoResources.createReplicaInitiateCommand(
                self.cluster_object)
            self.service._executeAdminCommand(self.cluster_object,
                                              mongo_command, mongo_args)

        self.assertIn("replSetInitiate quorum check failed", str(ex.exception))