-
Notifications
You must be signed in to change notification settings - Fork 309
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
tiltfile: custom host in port forwards #2382
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,6 +2,7 @@ package tiltfile | |
|
||
import ( | ||
"fmt" | ||
"regexp" | ||
"sort" | ||
"strconv" | ||
"strings" | ||
|
@@ -775,6 +776,7 @@ func (s *tiltfileState) portForward(thread *starlark.Thread, fn *starlark.Builti | |
type portForward struct { | ||
local int | ||
container int | ||
host *string | ||
} | ||
|
||
var _ starlark.Value = portForward{} | ||
|
@@ -800,35 +802,54 @@ func (f portForward) Hash() (uint32, error) { | |
func intToPortForward(i starlark.Int) (portForward, error) { | ||
n, ok := i.Int64() | ||
if !ok { | ||
return portForward{}, fmt.Errorf("portForward value %v is not representable as an int64", i) | ||
return portForward{}, fmt.Errorf("portForward port value %v is not representable as an int64", i) | ||
} | ||
if n < 0 || n > 65535 { | ||
return portForward{}, fmt.Errorf("portForward value %v is not in the range for a port [0-65535]", n) | ||
return portForward{}, fmt.Errorf("portForward port value %v is not in the valid range [0-65535]", n) | ||
} | ||
return portForward{local: int(n)}, nil | ||
} | ||
|
||
const ipReStr = `^(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])$` | ||
const hostnameReStr = `^(([a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9\-]*[a-zA-Z0-9])\.)*([A-Za-z0-9]|[A-Za-z0-9][A-Za-z0-9\-]*[A-Za-z0-9])$` | ||
|
||
var validHost = regexp.MustCompile(ipReStr + "|" + hostnameReStr) | ||
|
||
func stringToPortForward(s starlark.String) (portForward, error) { | ||
parts := strings.SplitN(string(s), ":", 2) | ||
local, err := strconv.Atoi(parts[0]) | ||
parts := strings.SplitN(string(s), ":", 3) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. where did this syntax come from? it's probably ok, but seems different from the i guess the advantage of the separate address flag is that you can specify multiple There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Just as with the previous discussion, not sure what the exact origin of this syntax is, but it seems to be the most commonly used way to specify hostname for port forwarding, one example is OpenSSH. |
||
|
||
var host *string | ||
var localString string | ||
if len(parts) == 3 { | ||
localString = parts[1] | ||
host = &parts[0] | ||
if !validHost.MatchString(*host) { | ||
return portForward{}, fmt.Errorf("portForward host value %q is not a valid hostname or IP address", localString) | ||
} | ||
} else { | ||
localString = parts[0] | ||
} | ||
|
||
local, err := strconv.Atoi(localString) | ||
if err != nil || local < 0 || local > 65535 { | ||
return portForward{}, fmt.Errorf("portForward value %q is not in the range for a port [0-65535]", parts[0]) | ||
return portForward{}, fmt.Errorf("portForward port value %q is not in the valid range [0-65535]", localString) | ||
} | ||
|
||
var container int | ||
if len(parts) == 2 { | ||
container, err = strconv.Atoi(parts[1]) | ||
if len(parts) > 1 { | ||
last := parts[len(parts)-1] | ||
container, err = strconv.Atoi(last) | ||
if err != nil || container < 0 || container > 65535 { | ||
return portForward{}, fmt.Errorf("portForward value %q is not in the range for a port [0-65535]", parts[1]) | ||
return portForward{}, fmt.Errorf("portForward port value %q is not in the valid range [0-65535]", last) | ||
} | ||
} | ||
return portForward{local: local, container: container}, nil | ||
return portForward{local: local, container: container, host: host}, nil | ||
} | ||
|
||
func (s *tiltfileState) portForwardsToDomain(r *k8sResource) []model.PortForward { | ||
var result []model.PortForward | ||
for _, pf := range r.portForwards { | ||
result = append(result, model.PortForward{LocalPort: pf.local, ContainerPort: pf.container}) | ||
result = append(result, model.PortForward{LocalPort: pf.local, ContainerPort: pf.container, Host: pf.host}) | ||
} | ||
return result | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -368,12 +368,15 @@ func ToRuns(cmds []Cmd) []Run { | |
} | ||
|
||
type PortForward struct { | ||
// The port to expose on localhost of the current machine. | ||
LocalPort int | ||
|
||
// The port to connect to inside the deployed container. | ||
// If 0, we will connect to the first containerPort. | ||
ContainerPort int | ||
|
||
// The port to expose on the current machine. | ||
LocalPort int | ||
|
||
// Optional host to bind to on the current machine (localhost by default) | ||
Host *string | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. what's the purpose of making it a pointer? would we want to treat the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Two thoughts: |
||
} | ||
|
||
var imageTargetAllowUnexported = cmp.AllowUnexported(ImageTarget{}) | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
where did these regexps come from?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not sure what exactly you're asking. These are pretty standard regexps for IPs and hostnames, not sure if it's possible to pinpoint their exact source, cause they are pretty standard, based on relevant RFCs. Here are examples of where they can be encountered online:
IP: http://ipregex.com
Hostname: https://www.regextester.com/23
I've been using them for a while and I don't remember where I took them from, but again, they are pretty standard, so probably shouldn't matter.