Home > database >  How to retrieve kubernetes cluster data with googleapis in node?
How to retrieve kubernetes cluster data with googleapis in node?

Time:10-15

After researching the topic I found this documentation for retiring k8 clusters from GCP. However, I could not find any code examples of utilizing those API's and when I import google from googleapis I can't find the function in it that would be used for that purpose. For example, to get SQL data there is sqladmin, but nothing for retrieving k8 data. So what property of google do I need?

CodePudding user response:

The kubectl is the better tool to talk to a Kubernetes cluster.

But, you can retrieve some data from GKE. See below:

GCLOUD_TOKEN=$(gcloud auth print-access-token)
curl -sS -X GET -H "Authorization: Bearer ${GCLOUD_TOKEN}" "https://container.googleapis.com/v1beta1/projects/some-project-id/locations/us-east4-a/clusters"

So you get a JSON reply as desired.

CodePudding user response:

This is confusing.

There are 2 distinct APIs that you must use:

  1. Google's Kubernetes Engine API (see link). This API is used to create, read, update and delete Kubernetes clusters. Google provides a Node.js SDK documented here.
  2. The standard, generic Kubernetes API (see link). This API is used to create, read, update and delete resources on (any) Kubernetes client and, for obvious(ly good) reasons, it is the API that you must use to interact with (an existing) Kubernetes Engine cluster too (because these are just like every other Kubernetes cluster). Kubernetes provides official and community-supported libraries that implement the Kubernetes API. You'll need to pick one of the community-supported libraries for Node.js (as there's no official library).

The general process is to:

  1. Use the Kubernetes Engine API to e.g. projects.locations.cluster.get details of an existing GKE cluster
  2. Use the returned Cluster object to build a configuration object (the equivalent of building a context object in a kubeconfig file)go
  3. Use the context object with the Kubernetes API library to authenticate to the cluster and program it e.g. list Deployments, create Services etc.

go-- I have code for this step but it's written in Golang not JavaScript.

  • Related