def _delete_resources(namespace): # installation of certain operators on the cluster may break 'oc delete all' # oc("delete", "all", "--all", n=namespace) # delete the ClowdApps in this namespace oc("delete", "clowdapp", "--all", n=namespace) # delete the ClowdEnvironment for this namespace if get_json("clowdenvironment", conf.ENV_NAME_FORMAT.format(namespace=namespace)): oc( "delete", "clowdenvironment", conf.ENV_NAME_FORMAT.format(namespace=namespace), ) # delete other specific resource types from the namespace resources_to_delete = [ "secret", "configmap", "pvc", "pod", "deployment", "deploymentconfig", "statefulset", "daemonset", "replicaset", "cronjob", "job", "service", "route", ] for resource in resources_to_delete: oc("delete", resource, "--all", n=namespace)
def get_namespaces_for_reconciler(): """ Query app-interface to get list of namespaces the reconciler operates on. """ ephemeral_namespace_names = get_namespaces_for_env(conf.EPHEMERAL_ENV_NAME) log.debug( "namespaces found for env '%s': %s", conf.EPHEMERAL_ENV_NAME, ephemeral_namespace_names ) all_namespaces = get_json("project")["items"] log.debug( "all namespaces found on cluster: %s", [ns["metadata"]["name"] for ns in all_namespaces] ) ephemeral_namespaces = [] for ns in all_namespaces: ns_name = ns["metadata"]["name"] if ns_name == conf.BASE_NAMESPACE_NAME: log.debug("ns '%s' is base namespace, will not reconcile it") continue if ns_name not in ephemeral_namespace_names: log.debug( "ns '%s' is not a member of env '%s', will not reconcile it", ns_name, conf.EPHEMERAL_ENV_NAME, ) continue if not conf.RESERVABLE_NAMESPACE_REGEX.match(ns_name): log.debug( "ns '%s' does not match reservable namespace regex, will not reconcile it", ns_name ) continue ns = Namespace(namespace_data=ns) ephemeral_namespaces.append(ns) return ephemeral_namespaces
def get_cdapp_config(self, app_name): if app_name not in self._cdapp_config_for: secret = get_json("secret", app_name, namespace=self.namespace) if not secret: raise ValueError(f"secret '{app_name}' not found in namespace") content = json.loads(base64.b64decode(secret["data"]["cdappconfig.json"])) self._cdapp_config_for[app_name] = AppConfig.dictToObject(content) return self._cdapp_config_for[app_name]
def get_clowdenv_status(self, app_name): if app_name not in self._status_for: status = get_json("clowdenvironment", ENV_NAME_FORMAT.format(namespace=self.namespace))[ "status" ] for app in status.get("apps", []): self._status_for[app["name"]] = app if app_name not in self._status_for: raise ValueError(f"app '{app_name}' not found in status") return self._status_for[app_name]
def _import_secret(secret_name, secret_data): # get existing secret in the ns (if it exists) current_secret = get_json("secret", secret_name) or {} # avoid race conditions when running multiple processes by comparing the data if current_secret.get("data") != secret_data.get("data"): log.info("replacing secret '%s' using local copy", secret_name) # delete from dst ns so that applying 'null' values will work oc("delete", "--ignore-not-found", "secret", secret_name, _silent=True) oc("apply", "-f", "-", _silent=True, _in=json.dumps(secret_data))
def _get_env_ready_status(): clowd_env_ready_for_ns = {} clowd_envs = get_json("clowdenvironment") for clowd_env in clowd_envs["items"]: status = clowd_env.get("status", {}) target_ns = status.get("targetNamespace") ready = status.get("ready", False) clowd_env_ready_for_ns[target_ns] = ready if not ready: log.debug("found target ns '%s' with env status not ready", target_ns) return clowd_env_ready_for_ns
def _delete_resources(namespace): # delete some of our own operator resources first resources_to_delete = [ "cyndipipelines", # delete this first to prevent hanging "xjoinpipelines", "clowdjobinvocations", "clowdapps", ] for resource in resources_to_delete: oc("delete", resource, "--all", n=namespace, timeout="60s") # delete the ClowdEnvironment for this namespace if get_json("clowdenvironment", conf.ENV_NAME_FORMAT.format(namespace=namespace)): oc( "delete", "clowdenvironment", conf.ENV_NAME_FORMAT.format(namespace=namespace), timeout="60s", ) # delete any other lingering specific resource types from the namespace resources_to_delete = [ "elasticsearches", "horizontalpodautoscalers", "kafkabridges", "kafkaconnectors", "kafkaconnects", "kafkaconnects2is", "kafkamirrormaker2s", "kafkamirrormakers", "kafkarebalances", "kafkas", "kafkatopics", "kafkausers", "deployments", "deploymentconfigs", "statefulsets", "daemonsets", "replicasets", "cronjobs", "jobs", "services", "routes", "pods", "secrets", "configmaps", "persistentvolumeclaims", ] for resource in resources_to_delete: oc("delete", resource, "--all", n=namespace, timeout="60s")
def get_namespaces_for_reconciler(): """ Query app-interface to get list of namespaces the reconciler operates on. """ ephemeral_namespace_names = get_namespaces_for_env(conf.EPHEMERAL_ENV_NAME) ephemeral_namespace_names.remove(conf.BASE_NAMESPACE_NAME) all_namespaces = get_json("project")["items"] ephemeral_namespaces = [] for ns in all_namespaces: if ns["metadata"]["name"] not in ephemeral_namespace_names: continue if not conf.RESERVABLE_NAMESPACE_REGEX.match(ns["metadata"]["name"]): continue ns = Namespace(namespace_data=ns) return ephemeral_namespaces
def _delete_resources(namespace): # installation of certain operators on the cluster may break 'oc delete all' # oc("delete", "all", "--all", n=namespace) # delete the ClowdApps in this namespace oc("delete", "clowdapp", "--all", n=namespace) # delete the ClowdEnvironment for this namespace if get_json("clowdenvironment", conf.ENV_NAME_FORMAT.format(namespace=namespace)): oc( "delete", "clowdenvironment", conf.ENV_NAME_FORMAT.format(namespace=namespace), ) # delete other specific resource types from the namespace resources_to_delete = [ "clowdapps", "clowdjobinvocations", "cyndipipelines", "kafkabridges", "kafkaconnectors", "kafkaconnects", "kafkaconnects2is", "kafkamirrormaker2s", "kafkamirrormakers", "kafkarebalances", "kafkas", "kafkatopics", "kafkausers", "deployments", "deploymentconfigs", "statefulsets", "daemonsets", "replicasets", "cronjobs", "jobs", "services", "routes", "pods", "secrets", "configmaps", "persistentvolumeclaims", ] for resource in resources_to_delete: oc("delete", resource, "--all", n=namespace)
def get_namespaces(available_only=False, mine=False): ephemeral_namespace_names = get_namespaces_for_env(conf.EPHEMERAL_ENV_NAME) ephemeral_namespace_names.remove(conf.BASE_NAMESPACE_NAME) # Use 'oc get project' since we cannot list all 'namespace' resources in a cluster all_namespaces = get_json("project")["items"] ephemeral_namespaces = [] for ns in all_namespaces: if ns["metadata"]["name"] not in ephemeral_namespace_names: continue if not conf.RESERVABLE_NAMESPACE_REGEX.match(ns["metadata"]["name"]): continue ns = Namespace(namespace_data=ns) if mine: if ns.owned_by_me: ephemeral_namespaces.append(ns) elif not available_only or ns.available: ephemeral_namespaces.append(ns) return ephemeral_namespaces
def refresh(self): self.__init__(namespace_data=get_json("namespace", self.name)) return self