From c5ca3c1a9a999a49089fa60c671c2f609952aaf7 Mon Sep 17 00:00:00 2001 From: Jeremy Lewi Date: Tue, 14 Nov 2017 09:51:51 -0800 Subject: [PATCH] Create a repo for https://github.com/kubernetes-incubator/client-python/issues/339 --- py/deploy.py | 1 - py/release.py | 1 + test-infra/airflow/dags/credentials_repo.py | 54 +++++++++++++++++++-- 3 files changed, 50 insertions(+), 6 deletions(-) diff --git a/py/deploy.py b/py/deploy.py index 5b916bcf86..6843948584 100755 --- a/py/deploy.py +++ b/py/deploy.py @@ -79,7 +79,6 @@ def setup(args): util.configure_kubectl(project, zone, cluster_name) k8s_config.load_kube_config() - # Create an API client object to talk to the K8s master. api_client = k8s_client.ApiClient() diff --git a/py/release.py b/py/release.py index b4a5a7a3fc..7fe9867510 100755 --- a/py/release.py +++ b/py/release.py @@ -252,6 +252,7 @@ def build_and_push_artifacts(go_dir, src_dir, registry, publish_path=None, ] if publish_path: + logging.info("GOOGLE_APPLICATION_CREDENTIALS=%s", os.getenv("GOOGLE_APPLICATION_CREDENTIALS", "")) gcs_client = storage.Client(project=gcb_project) bucket_name, base_path = util.split_gcs_uri(publish_path) bucket = gcs_client.get_bucket(bucket_name) diff --git a/test-infra/airflow/dags/credentials_repo.py b/test-infra/airflow/dags/credentials_repo.py index fc8cc2d88e..c5972430ed 100644 --- a/test-infra/airflow/dags/credentials_repo.py +++ b/test-infra/airflow/dags/credentials_repo.py @@ -1,10 +1,54 @@ import google.auth import google.auth.transport import google.auth.transport.requests + +import kubernetes +from kubernetes import client as k8s_client +from kubernetes import config as k8s_config +from kubernetes.config import kube_config +from kubernetes.client import ApiClient, ConfigurationObject, configuration import os -print(os.getenv("GOOGLE_APPLICATION_CREDENTIALS", "")) -#credentials, project_id = google.auth.default(scopes=["https://www.googleapis.com/auth/userinfo.email"]) -credentials, project_id = google.auth.default() -request = google.auth.transport.requests.Request() -credentials.refresh(request) +import yaml +from py import util + +def load_kube_config(config_file=None, context=None, + client_configuration=configuration, + persist_config=True, **kwargs): + """Loads authentication and cluster information from kube-config file + and stores them in kubernetes.client.configuration. + + :param config_file: Name of the kube-config file. + :param context: set the active context. If is set to None, current_context + from config file will be used. + :param client_configuration: The kubernetes.client.ConfigurationObject to + set configs to. + :param persist_config: If True, config file will be updated when changed + (e.g GCP token refresh). + """ + + if config_file is None: + config_file = os.path.expanduser(kube_config.KUBE_CONFIG_DEFAULT_LOCATION) + + config_persister = None + if persist_config: + def _save_kube_config(config_map): + with open(config_file, 'w') as f: + yaml.safe_dump(config_map, f, default_flow_style=False) + config_persister = _save_kube_config + + kube_config._get_kube_config_loader_for_yaml_file( + config_file, active_context=context, + client_configuration=client_configuration, + config_persister=config_persister, **kwargs).load_and_set() + +def refresh_credentials(): + #credentials, project_id = google.auth.default(scopes=["https://www.googleapis.com/auth/cloud-platform"]) + credentials, project_id = google.auth.default(scopes=["https://www.googleapis.com/auth/userinfo.email"]) + request = google.auth.transport.requests.Request() + credentials.refresh(request) + return credentials +refresh_credentials() +load_kube_config(get_google_credentials=refresh_credentials) +# Create an API client object to talk to the K8s master. +api_client = k8s_client.ApiClient()