Skip to content
This repository has been archived by the owner on Dec 7, 2023. It is now read-only.

Support configuring BindAddress and Protocol for a PortMapping #299

Merged
merged 4 commits into from
Aug 8, 2019

Conversation

twelho
Copy link
Contributor

@twelho twelho commented Aug 8, 2019

Fixes #142. This enables configuring the the listening address on the host and the protocol used when specifying a port binding from both declaratively and from the CLI.

This PR is part of the API changes for v0.5.0.
TODO:

  • Duplicate VM port checks
  • Verify this works in practice

cc @luxas

@twelho twelho added kind/api-change Categorizes issue or PR as related to adding, removing, or otherwise changing an API kind/design Categorizes issue or PR as related to the design of the project. kind/enhancement Categorizes issue or PR as related to improving an existing feature. do-not-merge/wip The PR is still work in progress labels Aug 8, 2019
@twelho twelho added this to the v0.5.0 milestone Aug 8, 2019
Copy link
Contributor

@luxas luxas left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please use the go-connections function for the parsing here

@@ -9,49 +10,95 @@ import (

// PortMapping defines a port mapping between the VM and the host
type PortMapping struct {
HostPort uint64 `json:"hostPort"`
VMPort uint64 `json:"vmPort"`
BindAddress net.IP `json:"bindAddress,omitempty"`
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is optional; I'd prefer for this to be a pointer (although, technically maybe omitempty works here)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

net.IP is a []byte, which can be nil, so omitempty works just fine.

protocol := portMapping.Protocol
if len(protocol) == 0 {
// Docker uses TCP by default
protocol = meta.ProtocolTCP
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

create a const DefaultProtocol = ProtocolTCP in meta?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The default protocol is runtime implementation specific, for Docker it just happens to be TCP, so I wouldn't make it generic.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok, sure

BindAddress net.IP `json:"bindAddress,omitempty"`
HostPort uint64 `json:"hostPort"`
VMPort uint64 `json:"vmPort"`
Protocol Protocol `json:"protocol,omitempty"`
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm think that omitempty doesn't work with custom marshallers, but idk. We can test. If so, then we need to use a pointer here.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Now that we're using the standard mashaler and Docker's parser always adding the proto, this isn't an issue.

}

// PortMappings represents a list of port mappings
type PortMappings []PortMapping

var _ fmt.Stringer = PortMappings{}

var errInvalidPortMappingFormat = fmt.Errorf("port mappings must be of form [<bind address>:]<host port>:<VM port>[/<protocol>]")

func ParsePortMappings(input []string) (PortMappings, error) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm strongly of the opinion that we should not duplicate code here that we already have vendored.
Please use https://github.com/docker/go-connections/blob/master/nat/nat.go#L126 for this task; and turn map[Port][]PortBinding from there into meta.PortMappings.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done in a182160.

return string(p)
}

func (p Protocol) MarshalJSON() ([]byte, error) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is not needed; that's the default

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed in a182160.

@@ -105,8 +106,20 @@ func (dc *dockerClient) AttachContainer(container string) (err error) {
func (dc *dockerClient) RunContainer(image string, config *runtime.ContainerConfig, name string) (string, error) {
portBindings := make(nat.PortMap)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

factor this out to a portBindingsToPortMap function please?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Factored in a182160.

@luxas luxas self-assigned this Aug 8, 2019
@twelho
Copy link
Contributor Author

twelho commented Aug 8, 2019

This doesn't include validation for reading the port mappings declaratively, is that a blocker for merging? I'll add a TODO to implement it anyway as we've not had it until now either.

Copy link
Contributor

@luxas luxas left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM.
Can you open a new issue to track missing validation functions?

@luxas luxas merged commit 501de78 into weaveworks:master Aug 8, 2019
@twelho
Copy link
Contributor Author

twelho commented Aug 8, 2019

Can you open a new issue to track missing validation functions?

Sure thing: #301

@twelho twelho deleted the bind-configuration branch August 8, 2019 16:29
@luxas luxas removed do-not-merge/wip The PR is still work in progress kind/enhancement Categorizes issue or PR as related to improving an existing feature. labels Aug 12, 2019
@luxas luxas changed the title Support configuring the bind address and protocol for port binds Support configuring BindAddress and Protocol for a PortMapping Aug 12, 2019
@luxas luxas changed the title Support configuring BindAddress and Protocol for a PortMapping Support configuring BindAddress and Protocol for a PortMapping Aug 12, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
kind/api-change Categorizes issue or PR as related to adding, removing, or otherwise changing an API kind/design Categorizes issue or PR as related to the design of the project.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

support configuring listen address and protocol
2 participants