Reputation: 127
I am building a custom dashboard for monitoring more than 20 clusters. For that I need to get the status of GKE workloads as shown in the GCP UI using any of the programmatical way (api/sdk/gcloud). The image describes what info I am looking for. Also I can't use kubernetes API as I dont have access for cluster but just for GKE console/dashboard (GCP UI).
Upvotes: 1
Views: 985
Reputation: 127
from kubernetes import client, config
import os
os.environ['GOOGLE_APPLICATION_CREDENTIALS'] = 'test-devops-646fy5644.json'
import google.auth.transport.requests
request = google.auth.transport.requests.Request()
from google.cloud import container_v1beta1 as cv1
def get_cluster_endpoint():
# Create a client
client = cv1.ClusterManagerClient()
# Initialize request argument(s)
request = cv1.GetClusterRequest(
project_id="test-devops",
zone="us-central1-a",
cluster_id="cluster-test",
)
# Make the request
response = client.get_cluster(request=request).endpoint
# Handle the response
return response
def test_gke():
SCOPES = ['https://www.googleapis.com/auth/cloud-platform']
credentials = service_account.Credentials.from_service_account_file(os.getenv('GOOGLE_APPLICATION_CREDENTIALS'), scopes=SCOPES)
# cluster_manager_client = ClusterManagerClient(credentials=credentials)
# cluster = cluster_manager_client.get_cluster(project_id, zone, cluster_id)
configuration = client.Configuration()
configuration.host = "https://"+get_cluster_endpoint()+":443"
configuration.verify_ssl = False
credentials.refresh(request)
print(credentials.token)
configuration.api_key = {"authorization": "Bearer " + credentials.token}
client.Configuration.set_default(configuration)
v1 = client.AppsV1Api()
print("Listing pods with their IPs:")
deployment = v1.read_namespaced_deployment(name='nginx-deployment', namespace='default')
print(deployment)
test_gke()
Here, I have used just the GCP service account key(JSON) with KubernetesEngineAPIViewer Role for accessing KubernertesAPI(general-purpose) which solves the problem.
Upvotes: 2