def ensure_statefulset(api: client.AppsV1Api, stateful_set, namespace, name): if len( api.list_namespaced_stateful_set( namespace=namespace, field_selector=f'metadata.name={name}').items) == 0: logger.info(f'creating StatefulSet: {namespace}/{name}') api.create_namespaced_stateful_set(body=stateful_set, namespace=namespace) else: logger.info(f'StatefulSet exists: {namespace}/{name}')
def destroy_statefulset(api: client.AppsV1Api, core_api: client.CoreV1Api, namespace, name): for pvc in core_api.list_namespaced_persistent_volume_claim( namespace=namespace, label_selector=f'app={name}').items: core_api.delete_namespaced_persistent_volume_claim( name=pvc.metadata.name, namespace=namespace) if len( api.list_namespaced_stateful_set( namespace=namespace, field_selector=f'metadata.name={name}').items) == 1: logger.info(f'destroying StatefulSet: {namespace}/{name}') api.delete_namespaced_stateful_set(name=name, namespace=namespace) else: logger.info(f'cannot find StatefulSet to destroy: {namespace}/{name}')