Skip to content

Commit

Permalink
fix-extra-port-mapping
Browse files Browse the repository at this point in the history
Signed-off-by: Daman Arora <aroradaman@gmail.com>
  • Loading branch information
aroradaman committed Jul 14, 2023
1 parent decbc46 commit fb8fe10
Show file tree
Hide file tree
Showing 5 changed files with 144 additions and 53 deletions.
6 changes: 6 additions & 0 deletions pkg/apis/config/defaults/networking.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,3 +47,9 @@ const ServiceSubnetIPv6 = "fd00:10:96::/112"

// ServiceSubnetDualStack is the default DualStack subnet for the Networking.ServiceSubnet field
const ServiceSubnetDualStack = "10.96.0.0/16,fd00:10:96::/112"

// ExtraPortMappingListenAddressIPv4 is the default IPv4/DualStack listen address for ExtraPortMappings
const ExtraPortMappingListenAddressIPv4 = "0.0.0.0"

// ExtraPortMappingListenAddressIPv6 is the default IPv6 listen address for ExtraPortMappings
const ExtraPortMappingListenAddressIPv6 = "::"
27 changes: 22 additions & 5 deletions pkg/apis/config/v1alpha4/default.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,17 @@ func SetDefaultsCluster(obj *Cluster) {
},
}
}

if obj.Networking.IPFamily == "" {
obj.Networking.IPFamily = IPv4Family
}

// default the nodes
for i := range obj.Nodes {
a := &obj.Nodes[i]
SetDefaultsNode(a)
}
if obj.Networking.IPFamily == "" {
obj.Networking.IPFamily = IPv4Family
SetDefaultsNode(a, obj.Networking.IPFamily)
}

// default the API server address
if obj.Networking.APIServerAddress == "" {
obj.Networking.APIServerAddress = defaults.APIServerAddressIPv4
Expand Down Expand Up @@ -75,12 +78,26 @@ func SetDefaultsCluster(obj *Cluster) {
}

// SetDefaultsNode sets uninitialized fields to their default value.
func SetDefaultsNode(obj *Node) {
func SetDefaultsNode(obj *Node, ipFamily ClusterIPFamily) {
if obj.Image == "" {
obj.Image = defaults.Image
}

if obj.Role == "" {
obj.Role = ControlPlaneRole
}

for i := 0; i < len(obj.ExtraPortMappings); i++ {
if obj.ExtraPortMappings[i].ListenAddress == "" {
if ipFamily == IPv6Family {
obj.ExtraPortMappings[i].ListenAddress = defaults.ExtraPortMappingListenAddressIPv6
} else {
obj.ExtraPortMappings[i].ListenAddress = defaults.ExtraPortMappingListenAddressIPv4
}
}

if string(obj.ExtraPortMappings[i].Protocol) == "" {
obj.ExtraPortMappings[i].Protocol = PortMappingProtocolTCP
}
}
}
25 changes: 20 additions & 5 deletions pkg/internal/apis/config/default.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 8 additions & 2 deletions pkg/internal/apis/config/validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"fmt"
"net"
"regexp"
"strconv"
"strings"

"sigs.k8s.io/kind/pkg/errors"
Expand Down Expand Up @@ -137,7 +138,7 @@ func (n *Node) Validate() error {
}

func validatePortMappings(portMappings []PortMapping) error {
errMsg := "port mapping with same listen address, port and protocol already configured"
errMsg := "port mapping with same listen address, host port and protocol already configured"

wildcardAddrIPv4 := net.ParseIP("0.0.0.0")
wildcardAddrIPv6 := net.ParseIP("::")
Expand All @@ -152,11 +153,16 @@ func validatePortMappings(portMappings []PortMapping) error {
}

for _, portMapping := range portMappings {
// skipping validation if host port is not defined
if portMapping.HostPort == 0 {
continue
}

addr := net.ParseIP(portMapping.ListenAddress)
addrString := addr.String()

portProtocol := formatPortProtocol(portMapping.HostPort, portMapping.Protocol)
possibleErr := fmt.Errorf("%s: %s:%s", errMsg, addrString, portProtocol)
possibleErr := fmt.Errorf("%s: %s/%s", errMsg, net.JoinHostPort(addrString, strconv.Itoa(int(portMapping.HostPort))), portMapping.Protocol)

// in golang 0.0.0.0 and [::] are equivalent, convert [::] -> 0.0.0.0
// https://github.com/golang/go/issues/48723
Expand Down
Loading

0 comments on commit fb8fe10

Please sign in to comment.