|
| 1 | +# Copyright 2025 Google LLC |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +# [START connectgateway_get_namespace] |
| 16 | +import os |
| 17 | +import sys |
| 18 | + |
| 19 | +from google.api_core import exceptions |
| 20 | +import google.auth |
| 21 | +from google.auth.transport import requests |
| 22 | +from google.cloud.gkeconnect import gateway_v1 |
| 23 | +from kubernetes import client |
| 24 | + |
| 25 | + |
| 26 | +SCOPES = ['https://www.googleapis.com/auth/cloud-platform'] |
| 27 | + |
| 28 | + |
| 29 | +def get_gateway_url(membership_name: str, location: str) -> str: |
| 30 | + """Fetches the GKE Connect Gateway URL for the specified membership.""" |
| 31 | + try: |
| 32 | + client_options = {} |
| 33 | + if location != "global": |
| 34 | + # If the location is not global, the endpoint needs to be set to the regional endpoint. |
| 35 | + regional_endpoint = f"{location}-connectgateway.googleapis.com" |
| 36 | + client_options = {"api_endpoint": regional_endpoint} |
| 37 | + gateway_client = gateway_v1.GatewayControlClient(client_options=client_options) |
| 38 | + request = gateway_v1.GenerateCredentialsRequest() |
| 39 | + request.name = membership_name |
| 40 | + response = gateway_client.generate_credentials(request=request) |
| 41 | + print(f'GKE Connect Gateway Endpoint: {response.endpoint}') |
| 42 | + if not response.endpoint: |
| 43 | + print("Error: GKE Connect Gateway Endpoint is empty.") |
| 44 | + sys.exit(1) |
| 45 | + return response.endpoint |
| 46 | + except exceptions.NotFound as e: |
| 47 | + print(f'Membership not found: {e}') |
| 48 | + sys.exit(1) |
| 49 | + except Exception as e: |
| 50 | + print(f'Error fetching GKE Connect Gateway URL: {e}') |
| 51 | + sys.exit(1) |
| 52 | + |
| 53 | + |
| 54 | +def configure_kubernetes_client(gateway_url: str) -> client.CoreV1Api: |
| 55 | + """Configures the Kubernetes client with the GKE Connect Gateway URL and credentials.""" |
| 56 | + |
| 57 | + configuration = client.Configuration() |
| 58 | + |
| 59 | + # Configure the API client with the custom host. |
| 60 | + configuration.host = gateway_url |
| 61 | + |
| 62 | + # Configure API key using default auth. |
| 63 | + credentials, _ = google.auth.default(scopes=SCOPES) |
| 64 | + auth_req = requests.Request() |
| 65 | + credentials.refresh(auth_req) |
| 66 | + configuration.api_key = {'authorization': f'Bearer {credentials.token}'} |
| 67 | + |
| 68 | + api_client = client.ApiClient(configuration=configuration) |
| 69 | + return client.CoreV1Api(api_client) |
| 70 | + |
| 71 | + |
| 72 | +def get_default_namespace(api_client: client.CoreV1Api) -> None: |
| 73 | + """Get default namespace in the Kubernetes cluster.""" |
| 74 | + try: |
| 75 | + namespace = api_client.read_namespace(name="default") |
| 76 | + return namespace |
| 77 | + except client.ApiException as e: |
| 78 | + print(f"Error getting default namespace: {e}\nStatus: {e.status}\nReason: {e.reason}") |
| 79 | + sys.exit(1) |
| 80 | + |
| 81 | + |
| 82 | +def get_namespace(membership_name: str, location: str) -> None: |
| 83 | + """Main function to connect to the cluster and get the default namespace.""" |
| 84 | + gateway_url = get_gateway_url(membership_name, location) |
| 85 | + core_v1_api = configure_kubernetes_client(gateway_url) |
| 86 | + namespace = get_default_namespace(core_v1_api) |
| 87 | + print(f"\nDefault Namespace:\n{namespace}") |
| 88 | + |
| 89 | + # [END connectgateway_get_namespace] |
| 90 | + |
| 91 | + return namespace |
| 92 | + |
| 93 | + |
| 94 | +if __name__ == "__main__": |
| 95 | + MEMBERSHIP_NAME = os.environ.get('MEMBERSHIP_NAME') |
| 96 | + MEMBERSHIP_LOCATION = os.environ.get("MEMBERSHIP_LOCATION") |
| 97 | + namespace = get_namespace(MEMBERSHIP_NAME, MEMBERSHIP_LOCATION) |
0 commit comments