mirror of https://github.com/docker/cli.git
Make --publish-rm precedes --publish-add, so that add wins
`--publish-add 8081:81 --publish-add 8082:82 --publish-rm 80 --publish-rm 81/tcp --publish-rm 82/tcp` would thus result in 81 and 82 to be published. Signed-off-by: Vincent Demeester <vincent@sbr.pm>
This commit is contained in:
parent
7fbc616b47
commit
b4a6d83dc2
|
@ -630,15 +630,7 @@ func (r byPortConfig) Less(i, j int) bool {
|
|||
|
||||
func portConfigToString(portConfig *swarm.PortConfig) string {
|
||||
protocol := portConfig.Protocol
|
||||
if protocol == "" {
|
||||
protocol = "tcp"
|
||||
}
|
||||
|
||||
mode := portConfig.PublishMode
|
||||
if mode == "" {
|
||||
mode = "ingress"
|
||||
}
|
||||
|
||||
return fmt.Sprintf("%v:%v/%s/%s", portConfig.PublishedPort, portConfig.TargetPort, protocol, mode)
|
||||
}
|
||||
|
||||
|
@ -663,28 +655,18 @@ func validatePublishRemove(val string) (string, error) {
|
|||
func updatePorts(flags *pflag.FlagSet, portConfig *[]swarm.PortConfig) error {
|
||||
// The key of the map is `port/protocol`, e.g., `80/tcp`
|
||||
portSet := map[string]swarm.PortConfig{}
|
||||
// Check to see if there are any conflict in flags.
|
||||
if flags.Changed(flagPublishAdd) {
|
||||
ports := flags.Lookup(flagPublishAdd).Value.(*opts.PortOpt).Value()
|
||||
|
||||
for _, port := range ports {
|
||||
if v, ok := portSet[portConfigToString(&port)]; ok && v != port {
|
||||
return fmt.Errorf("conflicting port mapping between %v:%v/%s and %v:%v/%s", port.PublishedPort, port.TargetPort, port.Protocol, v.PublishedPort, v.TargetPort, v.Protocol)
|
||||
}
|
||||
portSet[portConfigToString(&port)] = port
|
||||
}
|
||||
}
|
||||
|
||||
// Override previous PortConfig in service if there is any duplicate
|
||||
// Build the current list of portConfig
|
||||
for _, entry := range *portConfig {
|
||||
if _, ok := portSet[portConfigToString(&entry)]; !ok {
|
||||
portSet[portConfigToString(&entry)] = entry
|
||||
}
|
||||
}
|
||||
|
||||
toRemove := flags.Lookup(flagPublishRemove).Value.(*opts.PortOpt).Value()
|
||||
|
||||
newPorts := []swarm.PortConfig{}
|
||||
|
||||
// Clean current ports
|
||||
toRemove := flags.Lookup(flagPublishRemove).Value.(*opts.PortOpt).Value()
|
||||
portLoop:
|
||||
for _, port := range portSet {
|
||||
for _, pConfig := range toRemove {
|
||||
|
@ -698,6 +680,23 @@ portLoop:
|
|||
newPorts = append(newPorts, port)
|
||||
}
|
||||
|
||||
// Check to see if there are any conflict in flags.
|
||||
if flags.Changed(flagPublishAdd) {
|
||||
ports := flags.Lookup(flagPublishAdd).Value.(*opts.PortOpt).Value()
|
||||
|
||||
for _, port := range ports {
|
||||
if v, ok := portSet[portConfigToString(&port)]; ok {
|
||||
if v != port {
|
||||
fmt.Println("v", v)
|
||||
return fmt.Errorf("conflicting port mapping between %v:%v/%s and %v:%v/%s", port.PublishedPort, port.TargetPort, port.Protocol, v.PublishedPort, v.TargetPort, v.Protocol)
|
||||
}
|
||||
continue
|
||||
}
|
||||
//portSet[portConfigToString(&port)] = port
|
||||
newPorts = append(newPorts, port)
|
||||
}
|
||||
}
|
||||
|
||||
// Sort the PortConfig to avoid unnecessary updates
|
||||
sort.Sort(byPortConfig(newPorts))
|
||||
*portConfig = newPorts
|
||||
|
|
|
@ -220,28 +220,18 @@ func TestUpdatePorts(t *testing.T) {
|
|||
assert.Equal(t, targetPorts[1], 1000)
|
||||
}
|
||||
|
||||
func TestUpdatePortsDuplicateEntries(t *testing.T) {
|
||||
func TestUpdatePortsDuplicate(t *testing.T) {
|
||||
// Test case for #25375
|
||||
flags := newUpdateCommand(nil).Flags()
|
||||
flags.Set("publish-add", "80:80")
|
||||
|
||||
portConfigs := []swarm.PortConfig{
|
||||
{TargetPort: 80, PublishedPort: 80},
|
||||
}
|
||||
|
||||
err := updatePorts(flags, &portConfigs)
|
||||
assert.Equal(t, err, nil)
|
||||
assert.Equal(t, len(portConfigs), 1)
|
||||
assert.Equal(t, portConfigs[0].TargetPort, uint32(80))
|
||||
}
|
||||
|
||||
func TestUpdatePortsDuplicateKeys(t *testing.T) {
|
||||
// Test case for #25375
|
||||
flags := newUpdateCommand(nil).Flags()
|
||||
flags.Set("publish-add", "80:80")
|
||||
|
||||
portConfigs := []swarm.PortConfig{
|
||||
{TargetPort: 80, PublishedPort: 80},
|
||||
{
|
||||
TargetPort: 80,
|
||||
PublishedPort: 80,
|
||||
Protocol: swarm.PortConfigProtocolTCP,
|
||||
PublishMode: swarm.PortConfigPublishModeIngress,
|
||||
},
|
||||
}
|
||||
|
||||
err := updatePorts(flags, &portConfigs)
|
||||
|
@ -355,13 +345,19 @@ func TestUpdatePortsRmWithProtocol(t *testing.T) {
|
|||
flags.Set("publish-rm", "82/udp")
|
||||
|
||||
portConfigs := []swarm.PortConfig{
|
||||
{TargetPort: 80, PublishedPort: 8080, Protocol: swarm.PortConfigProtocolTCP},
|
||||
{
|
||||
TargetPort: 80,
|
||||
PublishedPort: 8080,
|
||||
Protocol: swarm.PortConfigProtocolTCP,
|
||||
PublishMode: swarm.PortConfigPublishModeIngress,
|
||||
},
|
||||
}
|
||||
|
||||
err := updatePorts(flags, &portConfigs)
|
||||
assert.Equal(t, err, nil)
|
||||
assert.Equal(t, len(portConfigs), 1)
|
||||
assert.Equal(t, portConfigs[0].TargetPort, uint32(82))
|
||||
assert.Equal(t, len(portConfigs), 2)
|
||||
assert.Equal(t, portConfigs[0].TargetPort, uint32(81))
|
||||
assert.Equal(t, portConfigs[1].TargetPort, uint32(82))
|
||||
}
|
||||
|
||||
// FIXME(vdemeester) port to opts.PortOpt
|
||||
|
|
Loading…
Reference in New Issue