-
Notifications
You must be signed in to change notification settings - Fork 4.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Separate subnet utils into a standalone package
- Loading branch information
1 parent
c22274a
commit 17a2c47
Showing
11 changed files
with
207 additions
and
173 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
load("@io_bazel_rules_go//go:def.bzl", "go_library", "go_test") | ||
|
||
go_library( | ||
name = "go_default_library", | ||
srcs = ["subnet.go"], | ||
importpath = "k8s.io/kops/pkg/util/subnet", | ||
visibility = ["//visibility:public"], | ||
) | ||
|
||
go_test( | ||
name = "go_default_test", | ||
srcs = ["subnet_test.go"], | ||
embed = [":go_default_library"], | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
/* | ||
Copyright 2016 The Kubernetes Authors. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package subnet | ||
|
||
import ( | ||
"encoding/binary" | ||
"fmt" | ||
"net" | ||
) | ||
|
||
// Overlap checks if two subnets overlap | ||
func Overlap(l, r *net.IPNet) bool { | ||
return l.Contains(r.IP) || r.Contains(l.IP) | ||
} | ||
|
||
// BelongsTo checks if child is a subnet of parent | ||
func BelongsTo(parent *net.IPNet, child *net.IPNet) bool { | ||
parentOnes, parentBits := parent.Mask.Size() | ||
childOnes, childBits := child.Mask.Size() | ||
if childBits != parentBits { | ||
return false | ||
} | ||
if parentOnes > childOnes { | ||
return false | ||
} | ||
childMasked := child.IP.Mask(parent.Mask) | ||
parentMasked := parent.IP.Mask(parent.Mask) | ||
return childMasked.Equal(parentMasked) | ||
} | ||
|
||
// SplitInto8 splits the parent IPNet into 8 subnets | ||
func SplitInto8(parent *net.IPNet) ([]*net.IPNet, error) { | ||
networkLength, _ := parent.Mask.Size() | ||
networkLength += 3 | ||
|
||
var subnets []*net.IPNet | ||
for i := 0; i < 8; i++ { | ||
ip4 := parent.IP.To4() | ||
if ip4 != nil { | ||
n := binary.BigEndian.Uint32(ip4) | ||
n += uint32(i) << uint(32-networkLength) | ||
subnetIP := make(net.IP, len(ip4)) | ||
binary.BigEndian.PutUint32(subnetIP, n) | ||
|
||
subnets = append(subnets, &net.IPNet{ | ||
IP: subnetIP, | ||
Mask: net.CIDRMask(networkLength, 32), | ||
}) | ||
} else { | ||
return nil, fmt.Errorf("Unexpected IP address type: %s", parent) | ||
} | ||
} | ||
|
||
return subnets, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
/* | ||
Copyright 2016 The Kubernetes Authors. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package subnet | ||
|
||
import ( | ||
"net" | ||
"reflect" | ||
"testing" | ||
) | ||
|
||
func Test_BelongsTo(t *testing.T) { | ||
grid := []struct { | ||
L string | ||
R string | ||
Belongs bool | ||
}{ | ||
{ | ||
L: "192.168.1.0/24", | ||
R: "192.168.0.0/24", | ||
Belongs: false, | ||
}, | ||
{ | ||
L: "192.168.0.0/16", | ||
R: "192.168.0.0/24", | ||
Belongs: true, | ||
}, | ||
{ | ||
L: "192.168.0.0/24", | ||
R: "192.168.0.0/16", | ||
Belongs: false, | ||
}, | ||
{ | ||
L: "192.168.0.0/16", | ||
R: "192.168.0.0/16", | ||
Belongs: true, // Not a strict subnet | ||
}, | ||
{ | ||
L: "192.168.0.1/16", | ||
R: "192.168.0.0/24", | ||
Belongs: true, | ||
}, | ||
{ | ||
L: "0.0.0.0/0", | ||
R: "101.0.1.0/32", | ||
Belongs: true, | ||
}, | ||
} | ||
for _, g := range grid { | ||
_, l, err := net.ParseCIDR(g.L) | ||
if err != nil { | ||
t.Fatalf("error parsing %q: %v", g.L, err) | ||
} | ||
_, r, err := net.ParseCIDR(g.R) | ||
if err != nil { | ||
t.Fatalf("error parsing %q: %v", g.R, err) | ||
} | ||
actual := BelongsTo(l, r) | ||
if actual != g.Belongs { | ||
t.Errorf("isSubnet(%q, %q) = %v, expected %v", g.L, g.R, actual, g.Belongs) | ||
} | ||
} | ||
} | ||
|
||
func Test_SplitInto8(t *testing.T) { | ||
tests := []struct { | ||
parent string | ||
expected []string | ||
}{ | ||
{ | ||
parent: "1.2.3.0/24", | ||
expected: []string{"1.2.3.0/27", "1.2.3.32/27", "1.2.3.64/27", "1.2.3.96/27", "1.2.3.128/27", "1.2.3.160/27", "1.2.3.192/27", "1.2.3.224/27"}, | ||
}, | ||
{ | ||
parent: "1.2.3.0/27", | ||
expected: []string{"1.2.3.0/30", "1.2.3.4/30", "1.2.3.8/30", "1.2.3.12/30", "1.2.3.16/30", "1.2.3.20/30", "1.2.3.24/30", "1.2.3.28/30"}, | ||
}, | ||
} | ||
for _, test := range tests { | ||
_, parent, err := net.ParseCIDR(test.parent) | ||
if err != nil { | ||
t.Fatalf("error parsing parent cidr %q: %v", test.parent, err) | ||
} | ||
|
||
subnets, err := SplitInto8(parent) | ||
if err != nil { | ||
t.Fatalf("error splitting parent cidr %q: %v", parent, err) | ||
} | ||
|
||
var actual []string | ||
for _, subnet := range subnets { | ||
actual = append(actual, subnet.String()) | ||
} | ||
if !reflect.DeepEqual(actual, test.expected) { | ||
t.Fatalf("unexpected result of split: actual=%v, expected=%v", actual, test.expected) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.