From e8b052e65a62cb44d94878ac6168956fcf357a44 Mon Sep 17 00:00:00 2001 From: Justin Barrick Date: Mon, 23 Jul 2018 01:54:38 +0000 Subject: [PATCH] Fluxctl should search for pod labels added by helm chart The Flux Helm chart does not have a name label on it, but we still want the flux instance to be discoverable by fluxctl. This change makes it so that if fluxctl does not find a pod by the labels `name in (flux,fluxd,weave-flux-agent)`, it searches for `app=flux`. If it does not find either label, it returns a nice message: ``` $ ./fluxctl --k8s-fwd-ns lol list-controllers Error: Flux pod not found for labels in namespace lol: name in (flux,fluxd,weave-flux-agent) app=flux Run 'fluxctl list-controllers --help' for usage. $ ``` Fixes #1242 --- cmd/fluxctl/portforward.go | 51 ++++++++++++++++++++++++++++++++++++++ cmd/fluxctl/root_cmd.go | 17 ++++++------- 2 files changed, 58 insertions(+), 10 deletions(-) create mode 100644 cmd/fluxctl/portforward.go diff --git a/cmd/fluxctl/portforward.go b/cmd/fluxctl/portforward.go new file mode 100644 index 000000000..021ff1be0 --- /dev/null +++ b/cmd/fluxctl/portforward.go @@ -0,0 +1,51 @@ +package main + +import ( + "fmt" + "strings" + + "github.com/justinbarrick/go-k8s-portforward" + "github.com/pkg/errors" + + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" +) + +// Attempt to create PortForwards to fluxes that match the label selectors until a Flux +// is found or an error is returned. +func tryPortforwards(ns string, selectors ...metav1.LabelSelector) (p *portforward.PortForward, err error) { + message := fmt.Sprintf("Flux pod not found for labels in namespace %s:", ns) + + for _, selector := range selectors { + p, err = tryPortforward(ns, selector) + if err == nil { + return + } + + if ! strings.Contains(err.Error(), "Could not find pod for selector") { + return + } else { + message = fmt.Sprintf("%s\n %s", message, metav1.FormatLabelSelector(&selector)) + } + } + + if err != nil { + err = errors.New(message) + } + + return +} + +// Attempt to create a portforward in the namespace for the provided LabelSelector +func tryPortforward(ns string, selector metav1.LabelSelector) (*portforward.PortForward, error) { + portforwarder, err := portforward.NewPortForwarder(ns, selector, 3030) + if err != nil { + return portforwarder, err + } + + err = portforwarder.Start() + if err != nil { + return portforwarder, err + } + + return portforwarder, nil +} diff --git a/cmd/fluxctl/root_cmd.go b/cmd/fluxctl/root_cmd.go index 6b6134081..8dc6c0ea7 100644 --- a/cmd/fluxctl/root_cmd.go +++ b/cmd/fluxctl/root_cmd.go @@ -7,7 +7,6 @@ import ( "os" "strings" - "github.com/justinbarrick/go-k8s-portforward" "github.com/pkg/errors" "github.com/spf13/cobra" "github.com/spf13/pflag" @@ -15,7 +14,6 @@ import ( "github.com/weaveworks/flux/api" transport "github.com/weaveworks/flux/http" "github.com/weaveworks/flux/http/client" - metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" ) @@ -103,7 +101,7 @@ func (opts *rootOpts) PersistentPreRunE(cmd *cobra.Command, _ []string) error { } if opts.URL == "" { - portforwarder, err := portforward.NewPortForwarder(opts.Namespace, metav1.LabelSelector{ + portforwarder, err := tryPortforwards(opts.Namespace, metav1.LabelSelector{ MatchExpressions: []metav1.LabelSelectorRequirement{ metav1.LabelSelectorRequirement{ Key: "name", @@ -111,14 +109,13 @@ func (opts *rootOpts) PersistentPreRunE(cmd *cobra.Command, _ []string) error { Values: []string{"flux", "fluxd", "weave-flux-agent"}, }, }, - }, 3030) - if err != nil { - return errors.Wrap(err, "initializing port forwarder") - } - - err = portforwarder.Start() + }, metav1.LabelSelector{ + MatchLabels: map[string]string{ + "app": "flux", + }, + }) if err != nil { - return errors.Wrap(err, "creating port forward") + return err } opts.URL = fmt.Sprintf("http://127.0.0.1:%d/api/flux", portforwarder.ListenPort)