Skip to content

Commit

Permalink
rename and clean up flag names
Browse files Browse the repository at this point in the history
  • Loading branch information
tzneal committed Dec 9, 2021
1 parent 30c80c2 commit bb6ca30
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 13 deletions.
6 changes: 4 additions & 2 deletions cmd/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ easy construction of the configuration.`,
return
}
cfg := model.Config{}
includeAll, _ := cmd.Flags().GetBool("all")
includeAll, _ := cmd.Flags().GetBool(flagAll)
pl := model.NewPortLookup(cs)
for _, svc := range svcList.Items {
// skip kube-system services by default
Expand Down Expand Up @@ -75,7 +75,9 @@ func writeConfig(cfg model.Config, outputFile string) {
}
}

const flagAll = "all"

func init() {
configCmd.AddCommand(createCmd)
createCmd.Flags().BoolP("all", "A", false, "If true, include items in the kube-system namespace")
createCmd.Flags().BoolP(flagAll, "A", false, "If true, include items in the kube-system namespace")
}
4 changes: 2 additions & 2 deletions cmd/exposeAll.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ every exposed service and port.`,
util.LogError("error reading services: %s", err)
return
}
localIp, err := cmd.Flags().GetIP("localip")
localIp, err := cmd.Flags().GetIP(flagLocalIP)
if err != nil {
util.LogError("error determining listen ip: %s", err)
return
Expand Down Expand Up @@ -102,6 +102,6 @@ every exposed service and port.`,

func init() {

exposeAllCmd.Flags().IP("localip", net.IPv4(127, 0, 0, 1), "IP address that is used to listen")
exposeAllCmd.Flags().IP(flagLocalIP, net.IPv4(127, 0, 0, 1), "IP address that is used to listen")
rootCmd.AddCommand(exposeAllCmd)
}
15 changes: 9 additions & 6 deletions cmd/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,9 +125,9 @@ inside the cluster as described by the configuration file.`,
svc.Spec.Selector = nil
svc.Spec.Ports = nil

ip, err := cmd.Flags().GetIP("ip")
ip, err := cmd.Flags().GetIP(flagExternalIP)
if err != nil {
util.LogError("error getting IP: %s", err)
util.LogError("error getting external IP: %s", err)
return
}

Expand Down Expand Up @@ -155,7 +155,7 @@ inside the cluster as described by the configuration file.`,

// Prepare to recreate a new service without a selector. I attempted to just remove the selector
// on the existing service, which somewhat worked but it would then load-balance across the existing service
// and our replacement. Removing the service and
// and our replacement. Removing the service seems to make this more reliable.
prepareServiceForCreation(&svc)

_, err = cs.CoreV1().Services(svc.Namespace).Create(ctx, &svc, metav1.CreateOptions{})
Expand Down Expand Up @@ -197,7 +197,7 @@ inside the cluster as described by the configuration file.`,
supplantingAtLeastOne = true
}

localIp, err := cmd.Flags().GetIP("localip")
localIp, err := cmd.Flags().GetIP(flagLocalIP)
if err != nil {
util.LogError("error determining listen ip: %s", err)
return
Expand Down Expand Up @@ -304,15 +304,18 @@ func prepareServiceForCreation(svc *v1.Service) {
svc.ObjectMeta.CreationTimestamp = metav1.Time{}
}

const flagExternalIP = "externalip"
const flagLocalIP = "localip"

func init() {
rootCmd.AddCommand(runCmd)

ip, err := getOutboundIP()
if err != nil {
ip = net.IP{}
}
runCmd.Flags().IP("ip", ip, "IP address that services within the cluster will connect to")
runCmd.Flags().IP("localip", net.IPv4(127, 0, 0, 1), "IP address that is used to listen")
runCmd.Flags().IP(flagExternalIP, ip, "IP address that services within the cluster will connect to")
runCmd.Flags().IP(flagLocalIP, net.IPv4(127, 0, 0, 1), "IP address that is used to listen")
}

func getOutboundIP() (net.IP, error) {
Expand Down
6 changes: 3 additions & 3 deletions kube/portforward.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ package kube

import (
"fmt"
"log"
"net"
"net/http"
"time"

"github.com/tzneal/supplant/util"
"k8s.io/cli-runtime/pkg/genericclioptions"
"k8s.io/client-go/kubernetes/scheme"
"k8s.io/client-go/tools/portforward"
Expand All @@ -23,8 +23,8 @@ type PortForwarder struct {
Forwarder *portforward.PortForwarder
}

// PortForward opens up a socket for the given local IP address and port and forwards it to the specified service and target port.
func PortForward(f cmdutil.Factory, namespace string, svcName string, targetPort int32, localIP net.IP, localPort int32) (PortForwarder, error) {

builder := f.NewBuilder().WithScheme(scheme.Scheme, scheme.Scheme.PrioritizedVersionsAllGroups()...).
ContinueOnError().NamespaceParam(namespace)
builder.ResourceNames("pods", fmt.Sprintf("service/%s", svcName))
Expand Down Expand Up @@ -75,7 +75,7 @@ func PortForward(f cmdutil.Factory, namespace string, svcName string, targetPort
go func() {
err := fw.ForwardPorts()
if err != nil {
log.Printf("error forwarding ports for %s: %s", svcName, err)
util.LogError("error forwarding ports for %s: %s", svcName, err)
}
}()

Expand Down

0 comments on commit bb6ca30

Please sign in to comment.