'Accessing a service in Kubernetes via the Kubernetes Python client

I have a service running in Kubernetes and currently, there are two ways of making GET requests to the REST API.

The first is

kubectl port-forward --namespace test service/test-svc 9090

and then running

curl http://localhost:9090/sub/path \
-d param1=abcd \
-d param2=efgh \
-G

For the second one, we do a kubctl proxy

kubectl proxy --port=8080

followed by

curl -lk 'http://127.0.0.1:8080/api/v1/namespaces/test/services/test-svc:9090/proxy/sub/path?param1=abcd&param2=efgh'

Both work nicely. However, my question is: How do we repeat one of these with the Python Kubernetes client (https://github.com/kubernetes-client/python)?

Many thanks for your support in advance!

Progress

I found a solution that brings us closer to the desired result:

from kubernetes import client, config


config.load_kube_config("~/.kube/config", context="my-context")

api_instance = client.CoreV1Api()



name = 'test-svc' # str | name of the ServiceProxyOptions
namespace = 'test' # str | object name and auth scope, such as for teams and projects

api_response = api_instance.api_client.call_api(
'/api/v1/namespaces/{namespace}/services/{name}/proxy/ping'.format(namespace=namespace, name=name), 'GET',
    auth_settings = ['BearerToken'], response_type='json', _preload_content=False
)

print(api_response)

yet the result is

(<urllib3.response.HTTPResponse object at 0x104529340>, 200, HTTPHeaderDict({'Audit-Id': '1ad9861c-f796-4e87-a16d-8328790c50c3', 'Cache-Control': 'no-cache, private', 'Content-Length': '16', 'Content-Type': 'application/json', 'Date': 'Thu, 27 Jan 2022 15:05:10 GMT', 'Server': 'uvicorn'}))

Whereas the desired output was

{
  "ping": "pong!"
}

Do you know how to extract it form here?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source