Title: HTTP requests directly to pod in k8s cluster using k8s go-client
Tags: go;kubernetes-go-client
Question: What I need to do is to make request without forwarding the port to the HTTP server located in k8s pod.
I have tried to make this request:
res := clientset.CoreV1().RESTClient().Get().
Namespace(&quot;default&quot;).
Resource(&quot;pods&quot;).
Name(&quot;my_pod_name:8080&quot;).
SubResource(&quot;proxy&quot;).
Suffix(&quot;api&quot;).
Do(context.Background())
and it is working for simple HTTP GET but I also need to pass headers and query parameters and I cannot do it using SetHeader.
Does a better solution/client exist for that case?
Comment: If you want to send HTTP request to Pod directly, then **you will need to create a K8s service which routes traffic to it. To expose the service outside of the cluster, add an ingress resource**. Otherwise, you will always have to run port-forward command 1st in order to connect.
Check out these links (https://kubernetes.io/docs/concepts/services-networking/ingress/ & https://kubernetes.io/docs/concepts/services-networking/service/)

Comment: This service cannot by deployed in cluster, so I cannot do this directly. Outside the cluster I can forward port but it is not preferable for me.

Comment: I agree with that but this service cannot be exposed outside cluster and connection to it is only needed for maintenance. These solutions are known for me but I just wanted to extend this basic operation in k8s go-client (presented in question) which is working for very simple GET. I just need to extend it to add headers and query params.

Comment: Do you need to do this using the control-plane? Why not a http-request directly?

Comment: if sdk has not enough fine grained config. Do a normal http request. All you need to do is include the service account token. Its set as bearer header if I recall correctly.



{'source': 'StackExchangeDataset', 'subset': 'stackoverflow.com', 'metadata': {'AnswerCount': 0, 'CommentCount': 5, 'ContentLicense': 'CC BY-SA 4.0', 'CreationDate': '2022:00:08/27/22', 'FavoriteCount': None, 'PostTypeId': 1, 'Tags': '<go><kubernetes-go-client>', 'Title': 'HTTP requests directly to pod in k8s cluster using k8s go-client', 'ViewCount': 56, 'id': 73511112, 'user_AboutMe': None, 'user_AccountId': 25313613.0, 'user_DisplayName': 'Konrad', 'user_Reputation': 21.0, 'user_Views': 1.0, 'user_id': 19161839.0}}