def checkReplicaSetOrInitialize( self, cluster_object: V1MongoClusterConfiguration) -> None: """ Checks that the replica set is initialized, or initializes it otherwise. :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 create_status_command = MongoResources.createStatusCommand() create_status_response = self._execInPod(0, cluster_name, namespace, create_status_command) logging.debug("Checking replicas, received %s", repr(create_status_response)) # If the replica set is not initialized yet, we initialize it if create_status_response["ok"] == 0 and create_status_response[ "codeName"] == "NotYetInitialized": return self.initializeReplicaSet(cluster_object) elif create_status_response["ok"] == 1: logging.info( "The replica set %s @ ns/%s seems to be working properly with %s/%s pods.", cluster_name, namespace, len(create_status_response["members"]), replicas) if replicas != len(create_status_response["members"]): self.reconfigureReplicaSet(cluster_object) else: raise ValueError( "Unexpected response trying to check replicas: '{}'".format( repr(create_status_response)))
def checkOrCreateReplicaSet( self, cluster_object: V1MongoClusterConfiguration) -> None: """ Checks that the replica set is initialized, or initializes it otherwise. :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 create_status_command = MongoResources.createStatusCommand() try: logging.debug("Will execute status command.") create_status_response = self._executeAdminCommand( cluster_object, create_status_command) logging.debug("Checking replicas, received %s", repr(create_status_response)) # The replica set could not be checked if create_status_response["ok"] != 1: raise ValueError( "Unexpected response trying to check replicas: '{}'". format(repr(create_status_response))) logging.info( "The replica set %s @ ns/%s seems to be working properly with %s/%s pods.", cluster_name, namespace, len(create_status_response["members"]), replicas) # The amount of replicas is not the same as configured, we need to fix this if replicas != len(create_status_response["members"]): self._reconfigureReplicaSet(cluster_object) except OperationFailure as err: logging.debug("Failed with %s", err) if str(err) != self.NO_REPLICA_SET_RESPONSE: logging.debug("No replicaset response.") raise # If the replica set is not initialized yet, we initialize it logging.debug( "Replicaset is not initialized, will initialize now.") self._initializeReplicaSet(cluster_object)