Skip to content

Commit

Permalink
initial working of hostsubnetlength per cidr
Browse files Browse the repository at this point in the history
  • Loading branch information
JacobTanenbaum committed Jul 6, 2017
1 parent 59412d9 commit 713e518
Show file tree
Hide file tree
Showing 11 changed files with 142 additions and 94 deletions.
139 changes: 82 additions & 57 deletions pkg/sdn/apis/network/v1/generated.pb.go

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

2 changes: 2 additions & 0 deletions pkg/sdn/apis/network/v1/generated.proto

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

4 changes: 2 additions & 2 deletions pkg/sdn/apis/network/v1/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ type ClusterNetwork struct {

// PLACEHOLDER
type ClusterNetworkEntry struct {
CIDR string `json:"CIDR" protobuf:"bytes,1,opt,name=CIDR"`
HostSubnetLength uint32 `json:"hostsubnetlength"`
CIDR string `json:"CIDR" protobuf:"bytes,1,opt,name=CIDR"`
HostSubnetLength uint32 `json:"hostsubnetlength" protobuf:"varint,2,opt,name=hostsubnetlength"`
}

// ClusterNetworkList is a collection of ClusterNetworks
Expand Down
54 changes: 37 additions & 17 deletions pkg/sdn/plugin/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,18 +41,32 @@ func cidrListContains(cidrList []*net.IPNet, ipaddr net.IP) (*net.IPNet, bool) {
return nil, false
}

func clusterNetworkListContains(clusterNetworks []ClusterNetwork, ipaddr net.IP) (*net.IPNet, bool) {
for _, cn := range clusterNetworks {
if cn.ClusterCIDR.Contains(ipaddr) {
return cn.ClusterCIDR, true
}
}
return nil, false
}

type NetworkInfo struct {
ClusterNetwork []*net.IPNet
ClusterNetworks []ClusterNetwork
ServiceNetwork *net.IPNet
}

type ClusterNetwork struct {
ClusterCIDR *net.IPNet
HostSubnetLength uint32
}

//determine if two cidr addresses intersect
func intersect(cidr1, cidr2 *net.IPNet) bool {
return cidr2.Contains(cidr1.IP) || cidr1.Contains(cidr2.IP)
}

func parseNetworkInfo(clusterNetwork []osapi.ClusterNetworkEntry, serviceNetwork string) (*NetworkInfo, error) {
var cn []*net.IPNet
var cns []ClusterNetwork

for _, entry := range clusterNetwork {
clusterAddress, err := netutils.ParseCIDRMask(entry.CIDR)
Expand All @@ -63,12 +77,12 @@ func parseNetworkInfo(clusterNetwork []osapi.ClusterNetworkEntry, serviceNetwork
}
glog.Errorf("Configured clusterNetworkCIDR value %q is invalid; treating it as %q", entry.CIDR, clusterAddress.String())
}
for _, cidr := range cn {
if intersect(cidr, clusterAddress) {
return nil, fmt.Errorf("Two of the cidr addresses overlap: %s, %s", cidr.String(), clusterAddress.String())
for _, cn := range cns {
if intersect(cn.ClusterCIDR, clusterAddress) {
return nil, fmt.Errorf("Two of the cidr addresses overlap: %s, %s", cn.ClusterCIDR.String(), clusterAddress.String())
}
}
cn = append(cn, clusterAddress)
cns = append(cns, ClusterNetwork{ClusterCIDR: clusterAddress, HostSubnetLength: entry.HostSubnetLength})
}

sn, err := netutils.ParseCIDRMask(serviceNetwork)
Expand All @@ -81,7 +95,7 @@ func parseNetworkInfo(clusterNetwork []osapi.ClusterNetworkEntry, serviceNetwork
}

return &NetworkInfo{
ClusterNetwork: cn,
ClusterNetworks: cns,
ServiceNetwork: sn,
}, nil
}
Expand All @@ -98,9 +112,14 @@ func (ni *NetworkInfo) validateNodeIP(nodeIP string) error {
return fmt.Errorf("failed to parse node IP %s", nodeIP)
}

if clusterIP, contains := cidrListContains(ni.ClusterNetwork, ipaddr); contains {
return fmt.Errorf("node IP %s conflicts with cluster network address %s", nodeIP, clusterIP.String())
for _, clusterNetwork := range ni.ClusterNetworks {
if clusterNetwork.ClusterCIDR.Contains(ipaddr){
return fmt.Errorf("node IP %s conflicts with cluster network address %s", nodeIP, clusterNetwork.ClusterCIDR.String())
}
}
// if clusterIP, contains := cidrListContains(ni.ClusterNetwork, ipaddr); contains {
// return fmt.Errorf("node IP %s conflicts with cluster network address %s", nodeIP, clusterIP.String())
// }
if ni.ServiceNetwork.Contains(ipaddr) {
return fmt.Errorf("node IP %s conflicts with service network %s", nodeIP, ni.ServiceNetwork.String())
}
Expand All @@ -111,13 +130,14 @@ func (ni *NetworkInfo) validateNodeIP(nodeIP string) error {
func (ni *NetworkInfo) checkHostNetworks(hostIPNets []*net.IPNet) error {
errList := []error{}
for _, ipNet := range hostIPNets {
for _, clusterCIDR := range ni.ClusterNetwork {
if ipNet.Contains(clusterCIDR.IP) {
errList = append(errList, fmt.Errorf("cluster IP: %s conflicts with host network: %s", clusterCIDR.IP.String(), ipNet.String()))
for _, clusterNetwork := range ni.ClusterNetworks {
if ipNet.Contains(clusterNetwork.ClusterCIDR.IP) {
errList = append(errList, fmt.Errorf("cluster IP: %s conflicts with host network: %s", clusterNetwork.ClusterCIDR.IP.String(), ipNet.String()))
}
if clusterNetwork.ClusterCIDR.Contains(ipNet.IP) {

errList = append(errList, fmt.Errorf("host network with IP: %s conflicts with cluster network address: %s", ipNet.IP.String(), clusterNetwork.ClusterCIDR.String()))
}
}
if clusterCIDR, contains := cidrListContains(ni.ClusterNetwork, ipNet.IP); contains {
errList = append(errList, fmt.Errorf("host network with IP: %s conflicts with cluster network address: %s", ipNet.IP.String(), clusterCIDR.String()))
}
if ipNet.Contains(ni.ServiceNetwork.IP) {
errList = append(errList, fmt.Errorf("service IP: %s conflicts with host network: %s", ni.ServiceNetwork.String(), ipNet.String()))
Expand All @@ -136,7 +156,7 @@ func (ni *NetworkInfo) checkClusterObjects(subnets []osapi.HostSubnet, pods []ka
subnetIP, _, _ := net.ParseCIDR(subnet.Subnet)
if subnetIP == nil {
errList = append(errList, fmt.Errorf("failed to parse network address: %s", subnet.Subnet))
} else if _, contains := cidrListContains(ni.ClusterNetwork, subnetIP); !contains {
} else if _, contains := clusterNetworkListContains(ni.ClusterNetworks, subnetIP); !contains {
errList = append(errList, fmt.Errorf("existing node subnet: %s in not part of any cluster network CIDR", subnet.Subnet))
}
if len(errList) >= 10 {
Expand All @@ -147,7 +167,7 @@ func (ni *NetworkInfo) checkClusterObjects(subnets []osapi.HostSubnet, pods []ka
if pod.Spec.SecurityContext != nil && pod.Spec.SecurityContext.HostNetwork {
continue
}
if _, contains := cidrListContains(ni.ClusterNetwork, net.ParseIP(pod.Status.PodIP)); !contains && pod.Status.PodIP != "" {
if _, contains := clusterNetworkListContains(ni.ClusterNetworks, net.ParseIP(pod.Status.PodIP)); !contains && pod.Status.PodIP != "" {
errList = append(errList, fmt.Errorf("existing pod %s:%s with IP %s is not part of cluster network", pod.Namespace, pod.Name, pod.Status.PodIP))
if len(errList) >= 10 {
break
Expand Down
4 changes: 2 additions & 2 deletions pkg/sdn/plugin/master.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ func StartMaster(networkConfig osconfigapi.MasterNetworkConfig, osClient *osclie
var err error
var clusterNetworkEntries []osapi.ClusterNetworkEntry
for _, cidr := range networkConfig.ClusterNetworks {
clusterNetworkEntries = append(clusterNetworkEntries, osapi.ClusterNetworkEntry{CIDR: cidr.CIDR})
clusterNetworkEntries = append(clusterNetworkEntries, osapi.ClusterNetworkEntry{CIDR: cidr.CIDR, HostSubnetLength: cidr.HostSubnetLength})
}
master.networkInfo, err = parseNetworkInfo(clusterNetworkEntries, networkConfig.ServiceNetworkCIDR)
if err != nil {
Expand Down Expand Up @@ -119,7 +119,7 @@ func StartMaster(networkConfig osconfigapi.MasterNetworkConfig, osClient *osclie
return err
}

if err = master.SubnetStartMaster(master.networkInfo.ClusterNetwork, networkConfig.HostSubnetLength); err != nil {
if err = master.SubnetStartMaster(master.networkInfo.ClusterNetworks); err != nil {
return err
}

Expand Down
Loading

0 comments on commit 713e518

Please sign in to comment.