Skip to content
This repository has been archived by the owner on Nov 1, 2022. It is now read-only.

Commit

Permalink
Merge pull request #1243 from justinbarrick/master
Browse files Browse the repository at this point in the history
Fluxctl should search for pod labels added by helm chart
  • Loading branch information
squaremo authored Jul 23, 2018
2 parents 756fc28 + e8b052e commit 95fe29e
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 10 deletions.
51 changes: 51 additions & 0 deletions cmd/fluxctl/portforward.go
Original file line number Diff line number Diff line change
@@ -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
}
17 changes: 7 additions & 10 deletions cmd/fluxctl/root_cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,13 @@ import (
"os"
"strings"

"github.com/justinbarrick/go-k8s-portforward"
"github.com/pkg/errors"
"github.com/spf13/cobra"
"github.com/spf13/pflag"

"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"
)

Expand Down Expand Up @@ -103,22 +101,21 @@ 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",
Operator: metav1.LabelSelectorOpIn,
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)
Expand Down

0 comments on commit 95fe29e

Please sign in to comment.