Giter Site home page Giter Site logo

Comments (7)

AlbertoSoutullo avatar AlbertoSoutullo commented on August 17, 2024 1

@iciclespider That makes perfect sense now that I think about it. Thank you very much! I will try this and if I find any problem I will reopen this.

from python.

AlbertoSoutullo avatar AlbertoSoutullo commented on August 17, 2024 1

@AlbertoSoutullo did you find a solution that worked for you? If so, would you mind sharing?

Hello @matthewfischl ! I wanted to do what you mention in your message (I was hoping that doing so would provide a localhost url that requests could be sent to.).
What I implemented some time ago was something that helped me to keep advancing with what I wanted to do, but I am not still sure it is the β€œbest” approach.

From my understanding, and also something that they comment at the beginning of the pod port forward example, is that the behavior is not exactly the same. I mean, you will not open a port where you can send request command to that address:port.

What you will do is monkey patch the create_connection function from socket. So basically, every time you do a request, this will automatically be called underneath, creating a connection to your Kubernetes cluster without the need of opening ports. Note that I had to use urllib_request.urlopen(request) instead of your request.get().

You can see my code, it is public:

  • Here I am creating the function that will act inside urllib_request.urlopen(request).
  • Here I am doing the monkey patch.
  • Here is the function I use in the previous link, where I am querying Kubernetes.

An example of that request parameter would be something like:

http://thanos-query.svc.thanos.kubernetes:9090/api/v1/query_range?query=libp2p-peers&start=1715769060.0&end=1715770080.0&step=60s

I am using this to basically scrape a lot of metrics through Thanos, for which I have a service that cannot be access from outside the cluster unless you use a port forward, or this kind of approach. Then I just parse the data accordingly with what I want to do.

I hope this can help. Again, I am not sure if this is the optimal approach. Like you, I also had in mind something like "opening a local port where I can query with standard requests", but as far as I know, the port forward of this library doesn't work like that.

Let me know if I can help you with something else!

from python.

roycaihw avatar roycaihw commented on August 17, 2024

/assign @iciclespider

from python.

k8s-ci-robot avatar k8s-ci-robot commented on August 17, 2024

@roycaihw: GitHub didn't allow me to assign the following users: iciclespider.

Note that only kubernetes-client members with read permissions, repo collaborators and people who have commented on this issue/PR can be assigned. Additionally, issues/PRs can only have 10 assignees at the same time.
For more information please see the contributor guide

In response to this:

/assign @iciclespider

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes/test-infra repository.

from python.

iciclespider avatar iciclespider commented on August 17, 2024

@AlbertoSoutullo, use the kubernetes apis to obtain the service and then search for a pod that matches the port you are trying to attach to. This is exactly what the kubectl cli does behind the scenes. The example you linked to also does exactly that here: https://github.com/kubernetes-client/python/blob/master/examples/pod_portforward.py#L143-L171

from python.

matthewfischl avatar matthewfischl commented on August 17, 2024

@AlbertoSoutullo did you find a solution that worked for you? If so, would you mind sharing?

Not sure if our goals are the same, but I am currently using port forwarding to enable local testing of an API.

For example, PRIOR to running a python request to an API, I would need to do the following (I started doing this in a command line then shifted to executing these with a python subprocess):

  1. aws eks update-kubeconfig --region us-east-1 --name
  2. kubectl port-forward service/ : -n
  3. Then make a request in python like: request.get("http://localhost:local port")

However, this approach would require running kubectl for EVERY service that I would want to forward AND doing this with python subprocesses seems undesirable (at least how I implemented it!)

So, ultimately I am trying to replace step #2 with something from the Kubernetes client library. I was hoping that doing so would provide a localhost url that requests could be sent to. However, I'm not exactly sure if that is what the Kubernetes library is designed for?

So, are you trying to do something similar? Any thoughts? Thanks!

from python.

iciclespider avatar iciclespider commented on August 17, 2024

@matthewfischl The port forwarding ability of the python client does exactly what your step 2 is doing. Here is an example of your step 2, but just in the python code: https://github.com/kubernetes-client/python/blob/master/examples/pod_portforward.py

from python.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. πŸ“ŠπŸ“ˆπŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.