Exemple #1
0
    def createUsers(self, cluster_object: V1MongoClusterConfiguration) -> None:
        """
        Creates the users required for each of the pods in the replica.
        :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

        secret_name = AdminSecretChecker.getSecretName(cluster_name)
        admin_credentials = self._kubernetes_service.getSecret(
            secret_name, namespace)
        create_admin_command, create_admin_args, create_admin_kwargs = MongoResources.createCreateAdminCommand(
            admin_credentials)

        if not self.userExists(cluster_object, create_admin_args):
            create_admin_response = self._executeAdminCommand(
                cluster_object, create_admin_command, create_admin_args,
                **create_admin_kwargs)
            logging.info("Created admin user: %s", create_admin_response)
        else:
            logging.info("No need to create admin user, it already exists")
    def createUsers(self, cluster_object: V1MongoClusterConfiguration) -> None:
        """
        Creates the users required for each of the pods in the replica.
        :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
        replicas = cluster_object.spec.mongodb.replicas

        secret_name = AdminSecretChecker.getSecretName(cluster_name)
        admin_credentials = self.kubernetes_service.getSecret(
            secret_name, namespace)
        create_admin_command = MongoResources.createCreateAdminCommand(
            admin_credentials)

        logging.info("Creating users for %s pods", replicas)

        for _ in range(self.EXEC_IN_POD_RETRIES):
            for i in range(replicas):
                # see tests for examples of these responses.
                try:
                    exec_response = self._execInPod(i, cluster_name, namespace,
                                                    create_admin_command)
                    if "user" in exec_response:
                        logging.info("Created users for pod %s-%s @ ns/%s",
                                     cluster_name, i, namespace)
                        return

                    raise ValueError(
                        "Unexpected response creating users for pod {}-{} @ ns/{}:\n{}"
                        .format(cluster_name, i, namespace, exec_response))

                except ValueError as err:
                    err_str = str(err)

                    if "couldn't add user: not master" in err_str:
                        # most of the time member 0 is elected master, otherwise we get this error and need to loop through
                        # members until we find the master
                        logging.info(
                            "The user could not be created in pod %s-%s because it's not master.",
                            cluster_name, i)
                        continue

                    if "already exists" in err_str:
                        logging.info("User creation not necessary: %s",
                                     err_str)
                        return

                    raise

            logging.info(
                "Could not create users in any of the %s pods of cluster %s @ ns/%s. We wait %s seconds "
                "before retrying.", replicas, cluster_name, namespace,
                self.EXEC_IN_POD_WAIT)
            sleep(self.EXEC_IN_POD_WAIT)

        raise TimeoutError(
            "Could not create users in any of the {} pods of cluster {} @ ns/{}."
            .format(replicas, cluster_name, namespace))