Skip to content

Commit

Permalink
Merge pull request #60 from odenio/custom-domain-names
Browse files Browse the repository at this point in the history
Add a flag to customize hostnames
  • Loading branch information
cjimti authored May 16, 2019
2 parents 6fc5246 + e75d051 commit 10d7922
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 3 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -119,11 +119,13 @@ Examples:
kubefwd svc -n the-project -l env=dev,component=api
kubefwd svc -n default -l "app in (ws, api)"
kubefwd svc -n default -n the-project
kubefwd svc -n default -d internal.example.com
kubefwd svc -n the-project -x prod-cluster
Flags:
-x, --context strings specify a context to override the current context
-d, --domain string Append a pseudo domain name to generated host names.
--exitonfailure Exit(1) on failure. Useful for forcing a container restart.
-h, --help help for services
-c, --kubeconfig string absolute path to a kubectl config fil (default "/Users/cjimti/.kube/config")
Expand Down
13 changes: 13 additions & 0 deletions cmd/kubefwd/services/services.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ var namespaces []string
var contexts []string
var exitOnFail bool
var verbose bool
var domain string

func init() {
// override error output from k8s.io/apimachinery/pkg/util/runtime
Expand Down Expand Up @@ -73,6 +74,7 @@ func init() {
Cmd.Flags().StringP("selector", "l", "", "Selector (label query) to filter on; supports '=', '==', and '!=' (e.g. -l key1=value1,key2=value2).")
Cmd.Flags().BoolVarP(&exitOnFail, "exitonfailure", "", false, "Exit(1) on failure. Useful for forcing a container restart.")
Cmd.Flags().BoolVarP(&verbose, "verbose", "v", false, "Verbose output.")
Cmd.Flags().StringVarP(&domain, "domain", "d", "", "Append a pseudo domain name to generated host names.")

}

Expand All @@ -84,6 +86,7 @@ var Cmd = &cobra.Command{
Example: " kubefwd svc -n the-project\n" +
" kubefwd svc -n the-project -l app=wx,component=api\n" +
" kubefwd svc -n default -n the-project\n" +
" kubefwd svc -n default -d internal.example.com\n" +
" kubefwd svc -n the-project -x prod-cluster\n",
Run: func(cmd *cobra.Command, args []string) {

Expand Down Expand Up @@ -204,6 +207,7 @@ Try:
Remote: i > 0,
IpC: byte(ipC),
ExitOnFail: exitOnFail,
Domain: domain,
})
if err != nil {
log.Printf("Error forwarding service: %s\n", err.Error())
Expand Down Expand Up @@ -236,6 +240,7 @@ type FwdServiceOpts struct {
Remote bool
IpC byte
ExitOnFail bool
Domain string
}

func fwdServices(opts FwdServiceOpts) error {
Expand Down Expand Up @@ -326,6 +331,13 @@ func fwdServices(opts FwdServiceOpts) error {
serviceHostName = serviceHostName + "." + pod.Namespace
}

if opts.Domain != "" {
if verbose {
log.Printf("Using domain %s in generated hostnames", opts.Domain)
}
serviceHostName = serviceHostName + "." + opts.Domain
}

if opts.Remote {
serviceHostName = fmt.Sprintf("%s.svc.cluster.%s", serviceHostName, opts.Context)
}
Expand Down Expand Up @@ -360,6 +372,7 @@ func fwdServices(opts FwdServiceOpts) error {
ShortName: opts.ShortName,
Remote: opts.Remote,
ExitOnFail: exitOnFail,
Domain: domain,
}

opts.Wg.Add(1)
Expand Down
21 changes: 18 additions & 3 deletions pkg/fwdport/fwdport.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ type PortForwardOpts struct {
ExitOnFail bool
ShortName bool
Remote bool
Domain string
}

func PortForward(pfo *PortForwardOpts) error {
Expand Down Expand Up @@ -85,20 +86,30 @@ func PortForward(pfo *PortForwardOpts) error {
fullServiceName = fmt.Sprintf("%s.%s.svc.cluster.%s", pfo.Service, pfo.Namespace, pfo.Context)

pfo.Hostfile.RemoveHost(fullServiceName)
if pfo.Domain != "" {
pfo.Hostfile.AddHost(pfo.LocalIp.String(), pfo.Service+"."+pfo.Domain)
}
pfo.Hostfile.AddHost(pfo.LocalIp.String(), pfo.Service)

} else {

if pfo.ShortName {
if pfo.Domain != "" {
pfo.Hostfile.RemoveHost(localServiceName + "." + pfo.Domain)
pfo.Hostfile.AddHost(pfo.LocalIp.String(), localServiceName+"."+pfo.Domain)
}
pfo.Hostfile.RemoveHost(localServiceName)
pfo.Hostfile.AddHost(pfo.LocalIp.String(), localServiceName)
}

pfo.Hostfile.RemoveHost(nsServiceName)
pfo.Hostfile.RemoveHost(fullServiceName)

pfo.Hostfile.AddHost(pfo.LocalIp.String(), nsServiceName)
pfo.Hostfile.AddHost(pfo.LocalIp.String(), fullServiceName)
if pfo.Domain != "" {
pfo.Hostfile.RemoveHost(nsServiceName + "." + pfo.Domain)
pfo.Hostfile.AddHost(pfo.LocalIp.String(), nsServiceName+"."+pfo.Domain)
}
pfo.Hostfile.RemoveHost(nsServiceName)
pfo.Hostfile.AddHost(pfo.LocalIp.String(), nsServiceName)

}

Expand All @@ -111,6 +122,10 @@ func PortForward(pfo *PortForwardOpts) error {
<-signals
if stopChannel != nil {
if pfo.Remote == false {
if pfo.Domain != "" {
pfo.Hostfile.RemoveHost(localServiceName + "." + pfo.Domain)
pfo.Hostfile.RemoveHost(nsServiceName + "." + pfo.Domain)
}
pfo.Hostfile.RemoveHost(localServiceName)
pfo.Hostfile.RemoveHost(nsServiceName)
}
Expand Down

0 comments on commit 10d7922

Please sign in to comment.