Skip to content

Commit 1a1a4fc

Browse files
authored
Merge pull request #6685 from thaJeztah/less_nat
remove some uses of go-connections/nat package
2 parents 65cf876 + 6f75c0c commit 1a1a4fc

File tree

3 files changed

+13
-7
lines changed

3 files changed

+13
-7
lines changed

cli/command/container/opts.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -426,6 +426,9 @@ func parse(flags *pflag.FlagSet, copts *containerOptions, serverOS string) (*con
426426
return nil, err
427427
}
428428

429+
// short syntax ([ip:]public:private[/proto])
430+
//
431+
// TODO(thaJeztah): we need an equivalent that handles the "ip-address" part without depending on the nat package.
429432
ports, natPortBindings, err := nat.ParsePortSpecs(convertedOpts)
430433
if err != nil {
431434
return nil, err

cli/compose/loader/loader.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -918,8 +918,9 @@ var transformStringToDuration TransformerFunc = func(value any) (any, error) {
918918
}
919919

920920
func toServicePortConfigs(value string) ([]any, error) {
921-
var portConfigs []any
922-
921+
// short syntax ([ip:]public:private[/proto])
922+
//
923+
// TODO(thaJeztah): we need an equivalent that handles the "ip-address" part without depending on the nat package.
923924
ports, portBindings, err := nat.ParsePortSpecs([]string{value})
924925
if err != nil {
925926
return nil, err
@@ -931,6 +932,7 @@ func toServicePortConfigs(value string) ([]any, error) {
931932
}
932933
sort.Strings(keys)
933934

935+
var portConfigs []any
934936
for _, key := range keys {
935937
// Reuse ConvertPortToPortConfig so that it is consistent
936938
port, err := network.ParsePort(key)

opts/swarmopts/port.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,9 @@ func (p *PortOpt) Set(value string) error {
100100

101101
p.ports = append(p.ports, pConfig)
102102
} else {
103-
// short syntax
103+
// short syntax ([ip:]public:private[/proto])
104+
//
105+
// TODO(thaJeztah): we need an equivalent that handles the "ip-address" part without depending on the nat package.
104106
ports, portBindingMap, err := nat.ParsePortSpecs([]string{value})
105107
if err != nil {
106108
return err
@@ -162,18 +164,17 @@ func ConvertPortToPortConfig(
162164
logrus.Warnf("ignoring IP-address (%s:%s) service will listen on '0.0.0.0'", net.JoinHostPort(binding.HostIP, binding.HostPort), portProto.String())
163165
}
164166

165-
startHostPort, endHostPort, err := nat.ParsePortRange(binding.HostPort)
166-
167+
pr, err := network.ParsePortRange(binding.HostPort)
167168
if err != nil && binding.HostPort != "" {
168169
return nil, fmt.Errorf("invalid hostport binding (%s) for port (%d)", binding.HostPort, portProto.Num())
169170
}
170171

171-
for i := startHostPort; i <= endHostPort; i++ {
172+
for p := range pr.All() {
172173
ports = append(ports, swarm.PortConfig{
173174
// TODO Name: ?
174175
Protocol: portProto.Proto(),
175176
TargetPort: uint32(portProto.Num()),
176-
PublishedPort: uint32(i),
177+
PublishedPort: uint32(p.Num()),
177178
PublishMode: swarm.PortConfigPublishModeIngress,
178179
})
179180
}

0 commit comments

Comments
 (0)