-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Support automatically port forwarding to flux instances in a Kubernetes cluster. #1212
Conversation
cmd/fluxctl/port_forward.go
Outdated
return err | ||
} | ||
|
||
go pf.ForwardPorts() |
This comment was marked as abuse.
This comment was marked as abuse.
Sorry, something went wrong.
Fission also has a port forward implementation: https://github.com/fission/fission/blob/master/fission/portforward/portforward.go This seems like something useful to a lot of projects, so I think it would be better for me to factor this out into a separate library and pull that in here. |
This is a great idea! If you don't mind a bit of back and forth, I have some suggestions. For fluxd we tend to prefix flags that pertain to Kubernetes machinery with There's an additional thing that might need a flag: Weave Cloud installations of fluxd use the name I can think of a couple of alternatives that cover these off (given with defaults):
Upside: accounts for both name and namespace. Downside: getting a little on the verbose side.
Upside: you can supply the namespace and name in one go. Downside: uses a non-standard syntax for the ID; you have to supply both namespace and name if you want to supply either. I'm not totally convinced by either of these, to be quite honest. But maybe we can come up with a synthesis (or you can argue that your formulation is better than both!)
That sounds sensible. I'd happily have it vendored here, then perhaps others could follow suit. |
Another thing that occurred to me: it'd be wonderful if this were the default mode of operation, so people have no reason to be tempted to expose the API. This is a bit tricky though, since we'd have to account for people that have already made shell scripts, etc. Though I am not adverse to a small amount of breakage in the interest of encouraging people to use port-forwarding instead of e.g., a NodePort, when either would work. |
I also saw I’m not sure which command line I prefer. The first one is nice because if we are searching by label hopefully all you need to change is the namespace so it’s less verbose. But the second one keeps in line with the rest of flux and is more flexible since it lets you pass in a resource name (especially if you have more than one flux in a namespace). I wasn’t sure about making it the default since it looks like weavcloud connect through the Weave Cloud API. Is that not the case?. |
On balance I think I like the first one, since it's progressive -- you can just flip the switch, or you can supply a namespace, or you can supply the whole lot if you need to.
That is the case, yup. You have to supply an auth token as well (either with |
I think I have conflated |
Okay, I switched to the vendored library. That means the last thing here (I think) is deciding on the command line interface that we want and modifying it to allow either of the common label sets.
Currently, the port forwarding works by looking for a pod with the labels For command line simplicity, I don't think we should allow people to specify the pod name or the labels and just have a fixed set of labels that we check for - it's easy enough to change your Fluxd if you have a non-standard set of labels. That makes the expectation for this that you can pass a namespace to it and it will be able to find the single flux instance in that namespace.
Next, we need to decide how the feature itself is turned on and off. It sounds like we want it to be on by default, unless a token is specified (in which case, we set the URL to weave cloud) or a URL has been manually specified on the command line. So I think the logic we will have is:
|
Thinking about this more, I don't actually think we'll break anything at all here! fluxctl with no arguments was never a valid configuration - you always had to either pass a token or a URL in order for fluxctl to work. Now, if you pass neither of those, it will attempt a port forward instead. |
Okay, I've implemented everything I described above:
This is good to go from my perspective! |
Demo: Default port forward fails, since my flux is in kube-system:
Search for flux in kube-system and create a port forward:
Try to connect to weave cloud with fake token:
Set a URL and connect directly:
|
…es cluster. Simplify accessing flux instances in a Kubernetes cluster by automatically creating port forwards, just like Helm does. This is based on the port forward implementations from kubectl and Helm: * https://github.com/kubernetes/helm/blob/master/pkg/kube/tunnel.go * https://github.com/kubernetes/kubernetes/blob/master/pkg/kubectl/cmd/portforward.go The flow is: * If `--url` is set, then fluxctl will connect directly to the specified URL. * If `--token` is set, then fluxctl will connect to Weave cloud with the token. * Otherwise, fluxctl will look for a pod in `--k8s-forward-namespace` (`default` by default) with the `name in (flux, fluxd, weave-flux-agent)` labels. * If the number of pods that match the labels in the namespace are not exactly 1, it will return an error. * It will find an empty port (by first binding to port 0 and then closing the port) and start a port forward on that port that forwards to the flux pod on port 3030. * It will set the flux url to `http://127.0.0.1:$port/api/flux`. * The port forward goroutine will get cleaned up automatically when fluxctl terminates. Use-cases: * Easier use of standalone flux instances. * Discourage exposing flux apis to the internet (hopefully nobody does this). * More easily manage large numbers of flux instances.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The only showstopper really is that this will break our own development envs (see the comment on tokens and URLs). See what you think, otherwise ..
cmd/fluxctl/root_cmd.go
Outdated
opts.URL = getFromEnvIfNotSet(cmd.Flags(), "url", opts.URL, envVariableURL) | ||
|
||
if opts.Token != "" { |
This comment was marked as abuse.
This comment was marked as abuse.
Sorry, something went wrong.
This comment was marked as abuse.
This comment was marked as abuse.
Sorry, something went wrong.
cmd/fluxctl/root_cmd.go
Outdated
@@ -74,11 +82,38 @@ func (opts *rootOpts) Command() *cobra.Command { | |||
} | |||
|
|||
func (opts *rootOpts) PersistentPreRunE(cmd *cobra.Command, _ []string) error { | |||
opts.Namespace = getFromEnvIfNotSet(cmd.Flags(), "k8s-forward-namespace", opts.Namespace, envVariableNamespace) |
This comment was marked as abuse.
This comment was marked as abuse.
Sorry, something went wrong.
This comment was marked as abuse.
This comment was marked as abuse.
Sorry, something went wrong.
This comment was marked as abuse.
This comment was marked as abuse.
Sorry, something went wrong.
cmd/fluxctl/root_cmd.go
Outdated
@@ -49,7 +54,10 @@ func (opts *rootOpts) Command() *cobra.Command { | |||
SilenceErrors: true, | |||
PersistentPreRunE: opts.PersistentPreRunE, | |||
} | |||
cmd.PersistentFlags().StringVarP(&opts.URL, "url", "u", "https://cloud.weave.works/api/flux", | |||
|
|||
cmd.PersistentFlags().StringVarP(&opts.Namespace, "k8s-forward-namespace", "f", "default", |
This comment was marked as abuse.
This comment was marked as abuse.
Sorry, something went wrong.
This comment was marked as abuse.
This comment was marked as abuse.
Sorry, something went wrong.
site/using.md
Outdated
insecure channel. Do not do this _unless_ you are operating Flux | ||
entirely locally; and arguably, only to try it out. | ||
If you are not able to use the port forward to connect, you can connect by URL with | ||
`--url` or `FLUX_URL`: |
This comment was marked as abuse.
This comment was marked as abuse.
Sorry, something went wrong.
This comment was marked as abuse.
This comment was marked as abuse.
Sorry, something went wrong.
…tting both URL and token.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM, and works seamlessly. Thank you for this -- it'll make using fluxctl
so much more pleasant.
Thanks for the review and merge! |
Simplify accessing flux instances in a Kubernetes cluster by automatically creating
port forwards, just like Helm does.
This is based on the port forward implementations from kubectl and Helm:
The flow is:
and everything will work as before.
that have the
name=flux
labels set.name=flux
in the namespace are not exactly 1, it willreturn an error.
start a port forward on that port that forwards to the flux pod on port 3030.
http://127.0.0.1:$port/api/flux
.Use-cases: