def setup_all_custom_resources(
    kube_client: KubeClient,
    soa_dir: str,
    cluster: str,
    custom_resource_definitions: Sequence[CustomResourceDefinition],
    service: str = None,
    instance: str = None,
) -> bool:
    cluster_crds = {
        crd.spec.names.kind
        for crd in kube_client.apiextensions.list_custom_resource_definition(
            label_selector=paasta_prefixed("service")).items
    }
    log.debug(f"CRDs found: {cluster_crds}")
    results = []
    for crd in custom_resource_definitions:
        if crd.kube_kind.singular not in cluster_crds:
            # TODO: kube_kind.singular seems to correspond to `crd.names.kind`
            # and not `crd.names.singular`
            log.warning(f"CRD {crd.kube_kind.singular} "
                        f"not found in {cluster}")
            continue

        # by convention, entries where key begins with _ are used as templates
        raw_config_dicts = load_all_configs(cluster=cluster,
                                            file_prefix=crd.file_prefix,
                                            soa_dir=soa_dir)
        config_dicts = {}
        for svc, raw_sdict in raw_config_dicts.items():
            sdict = {
                inst: idict
                for inst, idict in raw_sdict.items() if inst[0] != "_"
            }
            if sdict:
                config_dicts[svc] = sdict
        if not config_dicts:
            continue

        ensure_namespace(kube_client=kube_client,
                         namespace=f"paasta-{crd.kube_kind.plural}")
        results.append(
            setup_custom_resources(
                kube_client=kube_client,
                kind=crd.kube_kind,
                crd=crd,
                config_dicts=config_dicts,
                version=crd.version,
                group=crd.group,
                cluster=cluster,
                service=service,
                instance=instance,
            ))
    return any(results) if results else True
def cleanup_all_custom_resources(
    kube_client: KubeClient,
    soa_dir: str,
    cluster: str,
    custom_resource_definitions: Sequence[CustomResourceDefinition],
) -> bool:
    cluster_crds = {
        crd.spec.names.kind
        for crd in kube_client.apiextensions.list_custom_resource_definition(
            label_selector="paasta.yelp.com/service").items
    }
    log.debug(f"CRDs found: {cluster_crds}")
    results = []
    for crd in custom_resource_definitions:
        if crd.kube_kind.singular not in cluster_crds:
            # TODO: kube_kind.singular seems to correspond to `crd.names.kind`
            # and not `crd.names.singular`
            log.warning(f"CRD {crd.kube_kind.singular} "
                        f"not found in {cluster}")
            continue
        config_dicts = load_all_configs(cluster=cluster,
                                        file_prefix=crd.file_prefix,
                                        soa_dir=soa_dir)
        if not config_dicts:
            continue
        crs = list_custom_resources(
            kube_client=kube_client,
            kind=crd.kube_kind,
            version=crd.version,
            group=crd.group,
        )
        for cr in crs:
            service = config_dicts.get(cr.service)
            if service is not None:
                instance = service.get(cr.instance)
                if instance is not None:
                    continue
            result = False
            try:
                delete_custom_resource(
                    kube_client=kube_client,
                    name=cr.name,
                    namespace=cr.namespace,
                    plural=crd.kube_kind.plural,
                    version=crd.version,
                    group=crd.group,
                )
                result = True
            except Exception:
                log.exception("Error while deleting CR {cr.name}")
            results.append(result)
    return all(results) if results else True
Beispiel #3
0
def setup_all_custom_resources(
    kube_client: KubeClient,
    soa_dir: str,
    cluster: str,
    custom_resource_definitions: Sequence[CustomResourceDefinition],
    service: str = None,
    instance: str = None,
) -> bool:
    cluster_crds = {
        crd.spec.names.kind
        for crd in kube_client.apiextensions.list_custom_resource_definition(
            label_selector="paasta.yelp.com/service").items
    }
    log.debug(f"CRDs found: {cluster_crds}")
    results = []
    for crd in custom_resource_definitions:
        if crd.kube_kind.singular not in cluster_crds:
            # TODO: kube_kind.singular seems to correspond to `crd.names.kind`
            # and not `crd.names.singular`
            log.warning(f"CRD {crd.kube_kind.singular} "
                        f"not found in {cluster}")
            continue
        config_dicts = load_all_configs(cluster=cluster,
                                        file_prefix=crd.file_prefix,
                                        soa_dir=soa_dir)
        if not config_dicts:
            continue
        ensure_namespace(kube_client=kube_client,
                         namespace=f"paasta-{crd.kube_kind.plural}")
        results.append(
            setup_custom_resources(
                kube_client=kube_client,
                kind=crd.kube_kind,
                config_dicts=config_dicts,
                version=crd.version,
                group=crd.group,
                cluster=cluster,
                service=service,
                instance=instance,
            ))
    return all(results) if results else True