Skip to content

Commit

Permalink
asset/*: fix ip address calculations
Browse files Browse the repository at this point in the history
The issue was a result of IPNet's ParseCIDR() always returning a 16-byte
address, while some third-party libraries assume that the address length
corresponds to whether or not IPv6 is in use.
  • Loading branch information
crawford committed Sep 24, 2018
1 parent 762d577 commit ba9d7e4
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 4 deletions.
4 changes: 1 addition & 3 deletions pkg/asset/manifests/kube-core-operator.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package manifests

import (
"fmt"
"net"
"strings"

"github.com/ghodss/yaml"
Expand Down Expand Up @@ -85,8 +84,7 @@ func (kco *kubeCoreOperator) coreConfig() (*kubecore.OperatorConfig, error) {
coreConfig.AuthConfig.OIDCGroupsClaim = authConfigOIDCGroupsClaim
coreConfig.AuthConfig.OIDCUsernameClaim = authConfigOIDCUsernameClaim

svcCidr := kco.installConfig.Networking.ServiceCIDR
ip, err := cidr.Host(&net.IPNet{IP: svcCidr.IP, Mask: svcCidr.Mask}, 10)
ip, err := cidr.Host(&kco.installConfig.Networking.ServiceCIDR.IPNet, 10)
if err != nil {
return nil, err
}
Expand Down
14 changes: 13 additions & 1 deletion pkg/ipnet/ipnet.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,19 @@ func (ipnet *IPNet) UnmarshalJSON(b []byte) (err error) {
if err != nil {
return err
}
ipnet.IP = ip

// This check is needed in order to work around a strange quirk in the Go
// standard library. All of the addresses returned by net.ParseCIDR() are
// 16-byte addresses. This does _not_ imply that they are IPv6 addresses,
// which is what some libraries (e.g. github.com/apparentlymart/go-cidr)
// assume. By forcing the address to be the expected length, we can work
// around these bugs.
if ip.To4() != nil {
ipnet.IP = ip.To4()
} else {
ipnet.IP = ip
}
ipnet.Mask = net.Mask

return nil
}

0 comments on commit ba9d7e4

Please sign in to comment.