Example #1
0
def _merge_configs(configs):
    """
    Merge one or more ``KubeConfig`` objects.

    :param list[KubeConfig] configs: The configurations to merge.

    :return KubeConfig: A single configuration object with the merged
        configuration.
    """
    result = {
        u"contexts": [],
        u"users": [],
        u"clusters": [],
        u"current-context": None,
    }
    for config in configs:
        for k in {u"contexts", u"users", u"clusters"}:
            try:
                values = config.doc[k]
            except KeyError:
                pass
            else:
                result[k].extend(values)

        if result[u"current-context"] is None:
            try:
                result[u"current-context"] = config.doc[u"current-context"]
            except KeyError:
                pass

    return KubeConfig(result)
Example #2
0
 def get_clusters(self):
     # Kubernetes Python client expects "vintage" string path
     config_file = str(self._path) if self._path else None
     config = KubeConfig.from_file(config_file)
     for context in config.contexts:
         if self._contexts and context not in self._contexts:
             # filter out
             continue
         # create a new KubeConfig with new "current context"
         context_config = KubeConfig(config.doc, context)
         client = HTTPClient(context_config)
         cluster = Cluster(context, client)
         yield cluster