Beispiel #1
0
def setup_custom_resources(
    kube_client: KubeClient,
    kind: KubeKind,
    version: str,
    config_dicts: Mapping[str, Mapping[str, Any]],
    group: str,
    cluster: str,
    service: str = None,
    instance: str = None,
) -> bool:
    succeded = True
    if config_dicts:
        crs = list_custom_resources(
            kube_client=kube_client,
            kind=kind,
            version=version,
            group=group,
        )
    for svc, config in config_dicts.items():
        if service is not None and service != svc:
            continue
        if not reconcile_kubernetes_resource(
                kube_client=kube_client,
                service=svc,
                instance=instance,
                instance_configs=config,
                kind=kind,
                custom_resources=crs,
                version=version,
                group=group,
                cluster=cluster,
        ):
            succeded = False
    return succeded
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