Skip to content

Commit

Permalink
Adds field-selector as command line option
Browse files Browse the repository at this point in the history
  • Loading branch information
svavassori committed Jul 2, 2021
1 parent 6fb931e commit cb69df2
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 7 deletions.
1 change: 1 addition & 0 deletions cmd/kubefwd/kubefwd.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ func newRootCmd() *cobra.Command {
Example: " kubefwd services --help\n" +
" kubefwd svc -n the-project\n" +
" kubefwd svc -n the-project -l env=dev,component=api\n" +
" kubefwd svc -n the-project -f metadata.name=service-name\n" +
" kubefwd svc -n default -l \"app in (ws, api)\"\n" +
" kubefwd svc -n default -n the-project\n" +
" kubefwd svc -n the-project -m 80:8080 -m 443:1443\n",
Expand Down
12 changes: 5 additions & 7 deletions cmd/kubefwd/services/services.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,14 @@ limitations under the License.
package services

import (
"context"
"fmt"
"os"
"os/signal"
"strings"
"sync"
"syscall"
"time"
"context"

"github.com/bep/debounce"
"github.com/txn2/kubefwd/pkg/fwdcfg"
Expand All @@ -39,7 +39,6 @@ import (
authorizationv1 "k8s.io/api/authorization/v1"
v1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/fields"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/apimachinery/pkg/runtime"
utilRuntime "k8s.io/apimachinery/pkg/util/runtime"
Expand Down Expand Up @@ -68,6 +67,7 @@ func init() {
Cmd.Flags().StringSliceVarP(&contexts, "context", "x", []string{}, "specify a context to override the current context")
Cmd.Flags().StringSliceVarP(&namespaces, "namespace", "n", []string{}, "Specify a namespace. Specify multiple namespaces by duplicating this argument.")
Cmd.Flags().StringP("selector", "l", "", "Selector (label query) to filter on; supports '=', '==', and '!=' (e.g. -l key1=value1,key2=value2).")
Cmd.Flags().StringP("field-selector", "f", "", "Field selector to filter on; supports '=', '==', and '!=' (e.g. -f metadata.name=service-name).")
Cmd.Flags().BoolVarP(&verbose, "verbose", "v", false, "Verbose output.")
Cmd.Flags().StringVarP(&domain, "domain", "d", "", "Append a pseudo domain name to generated host names.")
Cmd.Flags().StringSliceVarP(&mappings, "mapping", "m", []string{}, "Specify a port mapping. Specify multiple mapping by duplicating this argument.")
Expand Down Expand Up @@ -197,11 +197,9 @@ Try:

// labels selector to filter services
// see: https://kubernetes.io/docs/concepts/overview/working-with-objects/labels/
selector := cmd.Flag("selector").Value.String()
listOptions := metav1.ListOptions{}
if selector != "" {
listOptions.LabelSelector = selector
}
listOptions.LabelSelector = cmd.Flag("selector").Value.String()
listOptions.FieldSelector = cmd.Flag("field-selector").Value.String()

// if no namespaces were specified via the flags, check config from the k8s context
// then explicitly set one to "default"
Expand Down Expand Up @@ -352,7 +350,7 @@ type NamespaceOpts struct {
func (opts *NamespaceOpts) watchServiceEvents(stopListenCh <-chan struct{}) {
// Apply filtering
optionsModifier := func(options *metav1.ListOptions) {
options.FieldSelector = fields.Everything().String()
options.FieldSelector = opts.ListOptions.FieldSelector
options.LabelSelector = opts.ListOptions.LabelSelector
}

Expand Down

0 comments on commit cb69df2

Please sign in to comment.