VMware vSphere Integrated Containers provider (#206)
* Add Virtual Kubelet provider for VIC Initial virtual kubelet provider for VMware VIC. This provider currently handles creating and starting of a pod VM via the VIC portlayer and persona server. Image store handling via the VIC persona server. This provider currently requires the feature/wolfpack branch of VIC. * Added pod stop and delete. Also added node capacity. Added the ability to stop and delete pod VMs via VIC. Also retrieve node capacity information from the VCH. * Cleanup and readme file Some file clean up and added a Readme.md markdown file for the VIC provider. * Cleaned up errors, added function comments, moved operation code 1. Cleaned up error handling. Set standard for creating errors. 2. Added method prototype comments for all interface functions. 3. Moved PodCreator, PodStarter, PodStopper, and PodDeleter to a new folder. * Add mocking code and unit tests for podcache, podcreator, and podstarter Used the unit test framework used in VIC to handle assertions in the provider's unit test. Mocking code generated using OSS project mockery, which is compatible with the testify assertion framework. * Vendored packages for the VIC provider Requires feature/wolfpack branch of VIC and a few specific commit sha of projects used within VIC. * Implementation of POD Stopper and Deleter unit tests (#4) * Updated files for initial PR
This commit is contained in:
212
vendor/github.com/vmware/vic/pkg/ip/ip.go
generated
vendored
Normal file
212
vendor/github.com/vmware/vic/pkg/ip/ip.go
generated
vendored
Normal file
@@ -0,0 +1,212 @@
|
||||
// Copyright 2016-2017 VMware, Inc. All Rights Reserved.
|
||||
//
|
||||
// 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 ip
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"math"
|
||||
"net"
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type Range struct {
|
||||
FirstIP net.IP `vic:"0.1" scope:"read-only" key:"first"`
|
||||
LastIP net.IP `vic:"0.1" scope:"read-only" key:"last"`
|
||||
}
|
||||
|
||||
func NewRange(first, last net.IP) *Range {
|
||||
return &Range{FirstIP: first, LastIP: last}
|
||||
}
|
||||
|
||||
func (i *Range) Overlaps(other Range) bool {
|
||||
if (bytes.Compare(i.FirstIP, other.FirstIP) <= 0 && bytes.Compare(other.FirstIP, i.LastIP) <= 0) ||
|
||||
(bytes.Compare(i.FirstIP, other.LastIP) <= 0 && bytes.Compare(other.FirstIP, i.LastIP) <= 0) {
|
||||
return true
|
||||
}
|
||||
|
||||
return false
|
||||
}
|
||||
|
||||
func (i *Range) String() string {
|
||||
n := i.Network()
|
||||
if n == nil {
|
||||
return fmt.Sprintf("%s-%s", i.FirstIP, i.LastIP)
|
||||
}
|
||||
|
||||
return n.String()
|
||||
}
|
||||
|
||||
func (i *Range) Equal(other *Range) bool {
|
||||
return i.FirstIP.Equal(other.FirstIP) && i.LastIP.Equal(other.LastIP)
|
||||
}
|
||||
|
||||
// Network returns the network that this range represents, if any
|
||||
func (i *Range) Network() *net.IPNet {
|
||||
// only works for ipv4
|
||||
first := i.FirstIP.To4()
|
||||
last := i.LastIP.To4()
|
||||
diff := net.IPv4(0, 0, 0, 0).To4()
|
||||
for j := 0; j < net.IPv4len; j++ {
|
||||
diff[j] = first[j] ^ last[j]
|
||||
}
|
||||
|
||||
var m uint
|
||||
for j := net.IPv4len - 1; j >= 0; j-- {
|
||||
var k uint
|
||||
for ; k < 8; k++ {
|
||||
if diff[j]>>k == 0 {
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
m += k
|
||||
if k < 8 {
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
if m == 0 {
|
||||
return nil
|
||||
}
|
||||
|
||||
mask := net.CIDRMask(32-int(m), 32)
|
||||
for j, f := range first {
|
||||
l := f | ^mask[j]
|
||||
if l != last[j] {
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
return &net.IPNet{IP: first, Mask: mask}
|
||||
}
|
||||
|
||||
func ParseRange(r string) *Range {
|
||||
var first, last net.IP
|
||||
// check if its a CIDR
|
||||
// #nosec: Errors unhandled
|
||||
_, ipnet, _ := net.ParseCIDR(r)
|
||||
if ipnet != nil {
|
||||
first = ipnet.IP
|
||||
last := make(net.IP, len(first))
|
||||
for i, f := range first {
|
||||
last[i] = f | ^ipnet.Mask[i]
|
||||
}
|
||||
|
||||
return &Range{
|
||||
FirstIP: first,
|
||||
LastIP: last,
|
||||
}
|
||||
}
|
||||
|
||||
comps := strings.Split(r, "-")
|
||||
if len(comps) != 2 {
|
||||
return nil
|
||||
}
|
||||
|
||||
first = net.ParseIP(comps[0])
|
||||
if first == nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
last = net.ParseIP(comps[1])
|
||||
if last == nil {
|
||||
var end int
|
||||
end, err := strconv.Atoi(comps[1])
|
||||
if err != nil || end <= int(first[15]) || end > math.MaxUint8 {
|
||||
return nil
|
||||
}
|
||||
|
||||
last = net.IPv4(first[12], first[13], first[14], byte(end))
|
||||
}
|
||||
|
||||
if bytes.Compare(first, last) > 0 {
|
||||
return nil
|
||||
}
|
||||
|
||||
return &Range{
|
||||
FirstIP: first,
|
||||
LastIP: last,
|
||||
}
|
||||
}
|
||||
|
||||
// MarshalText implements the encoding.TextMarshaler interface
|
||||
func (i *Range) MarshalText() ([]byte, error) {
|
||||
return []byte(i.String()), nil
|
||||
}
|
||||
|
||||
// UmarshalText implements the encoding.TextUnmarshaler interface
|
||||
func (i *Range) UnmarshalText(text []byte) error {
|
||||
s := string(text)
|
||||
r := ParseRange(s)
|
||||
if r == nil {
|
||||
return fmt.Errorf("parse error: %s", s)
|
||||
}
|
||||
|
||||
*i = *r
|
||||
return nil
|
||||
}
|
||||
|
||||
// ParseIPandMask parses a CIDR format address (e.g. 1.1.1.1/8)
|
||||
func ParseIPandMask(s string) (net.IPNet, error) {
|
||||
var i net.IPNet
|
||||
ip, ipnet, err := net.ParseCIDR(s)
|
||||
if err != nil {
|
||||
return i, err
|
||||
}
|
||||
|
||||
i.IP = ip
|
||||
i.Mask = ipnet.Mask
|
||||
return i, nil
|
||||
}
|
||||
|
||||
// Empty determines if net.IPNet is empty
|
||||
func Empty(i net.IPNet) bool {
|
||||
return i.IP == nil && i.Mask == nil
|
||||
}
|
||||
|
||||
func IsUnspecifiedIP(ip net.IP) bool {
|
||||
return len(ip) == 0 || ip.IsUnspecified()
|
||||
}
|
||||
|
||||
func IsUnspecifiedSubnet(n *net.IPNet) bool {
|
||||
if n == nil || IsUnspecifiedIP(n.IP) {
|
||||
return true
|
||||
}
|
||||
|
||||
ones, bits := n.Mask.Size()
|
||||
return bits == 0 || ones == 0
|
||||
}
|
||||
|
||||
// AllZerosAddr returns the all-zeros address for a subnet
|
||||
func AllZerosAddr(subnet *net.IPNet) net.IP {
|
||||
return subnet.IP.Mask(subnet.Mask)
|
||||
}
|
||||
|
||||
// AllOnesAddr returns the all-ones address for a subnet
|
||||
func AllOnesAddr(subnet *net.IPNet) net.IP {
|
||||
ones := net.IPv4(0, 0, 0, 0)
|
||||
ip := subnet.IP.To16()
|
||||
for i := range ip[12:] {
|
||||
ones[12+i] = ip[12+i] | ^subnet.Mask[i]
|
||||
}
|
||||
|
||||
return ones
|
||||
}
|
||||
|
||||
func IsRoutableIP(ip net.IP, subnet *net.IPNet) bool {
|
||||
return subnet.Contains(ip) && !ip.Equal(AllZerosAddr(subnet)) && !ip.Equal(AllOnesAddr(subnet))
|
||||
}
|
||||
159
vendor/github.com/vmware/vic/pkg/ip/ip_test.go
generated
vendored
Normal file
159
vendor/github.com/vmware/vic/pkg/ip/ip_test.go
generated
vendored
Normal file
@@ -0,0 +1,159 @@
|
||||
// Copyright 2016 VMware, Inc. All Rights Reserved.
|
||||
//
|
||||
// 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 ip
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestRangeMarshalText(t *testing.T) {
|
||||
var tests = []struct {
|
||||
ipr *Range
|
||||
s string
|
||||
err error
|
||||
}{
|
||||
{&Range{net.ParseIP("10.10.10.10"), net.ParseIP("10.10.10.24")}, "10.10.10.10-10.10.10.24", nil},
|
||||
}
|
||||
|
||||
for _, te := range tests {
|
||||
b, err := te.ipr.MarshalText()
|
||||
if te.err != nil && err == nil {
|
||||
t.Fatalf("MarshalText() => (%v, nil) want (nil, err)", b)
|
||||
continue
|
||||
}
|
||||
|
||||
if string(b) != te.s {
|
||||
t.Fatalf("MarshalText() => (%s, %s) want (%s, nil)", string(b), err, te.s)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestRangeUnmarshalText(t *testing.T) {
|
||||
|
||||
var tests = []struct {
|
||||
r string
|
||||
ipr *Range
|
||||
err error
|
||||
}{
|
||||
{"10.10.10.10-9", nil, fmt.Errorf("")},
|
||||
{"10.10.10.10-10.10.10.9", nil, fmt.Errorf("")},
|
||||
{"10.10.10.10-24", &Range{net.ParseIP("10.10.10.10"), net.ParseIP("10.10.10.24")}, nil},
|
||||
{"10.10.10.10-10.10.10.24", &Range{net.ParseIP("10.10.10.10"), net.ParseIP("10.10.10.24")}, nil},
|
||||
{"10.10.10.0/24", &Range{net.ParseIP("10.10.10.0"), net.ParseIP("10.10.10.255")}, nil},
|
||||
}
|
||||
|
||||
for _, te := range tests {
|
||||
ipr := &Range{}
|
||||
err := ipr.UnmarshalText([]byte(te.r))
|
||||
if te.err != nil {
|
||||
if err == nil {
|
||||
t.Fatalf("UnmarshalText(%s) => nil want err", te.r)
|
||||
}
|
||||
|
||||
continue
|
||||
}
|
||||
|
||||
if !te.ipr.FirstIP.Equal(ipr.FirstIP) ||
|
||||
!te.ipr.LastIP.Equal(ipr.LastIP) {
|
||||
t.Fatalf("UnmarshalText(%s) => %#v want %#v", te.r, ipr, te.ipr)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestRangeOverlap(t *testing.T) {
|
||||
var tests = []struct {
|
||||
ipr1, ipr2 Range
|
||||
res bool
|
||||
}{
|
||||
{Range{net.ParseIP("10.10.10.10"), net.ParseIP("10.10.10.24")}, Range{net.ParseIP("10.10.10.10"), net.ParseIP("10.10.10.24")}, true},
|
||||
{Range{net.ParseIP("10.10.10.10"), net.ParseIP("10.10.10.24")}, Range{net.ParseIP("10.10.10.15"), net.ParseIP("10.10.10.24")}, true},
|
||||
{Range{net.ParseIP("10.10.10.10"), net.ParseIP("10.10.10.24")}, Range{net.ParseIP("10.10.10.15"), net.ParseIP("10.10.10.20")}, true},
|
||||
{Range{net.ParseIP("10.10.10.10"), net.ParseIP("10.10.10.24")}, Range{net.ParseIP("10.10.10.9"), net.ParseIP("10.10.10.25")}, true},
|
||||
{Range{net.ParseIP("10.10.10.10"), net.ParseIP("10.10.10.24")}, Range{net.ParseIP("10.10.10.24"), net.ParseIP("10.10.10.25")}, true},
|
||||
{Range{net.ParseIP("10.10.10.10"), net.ParseIP("10.10.10.24")}, Range{net.ParseIP("10.10.10.25"), net.ParseIP("10.10.10.50")}, false},
|
||||
}
|
||||
|
||||
for _, te := range tests {
|
||||
res := te.ipr1.Overlaps(te.ipr2)
|
||||
if res != te.res {
|
||||
t.Fatalf("(%s).Overlaps(%s) => %t want %t", te.ipr1, te.ipr2, res, te.res)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestAllZerosAddr(t *testing.T) {
|
||||
var tests = []struct {
|
||||
subnet *net.IPNet
|
||||
addr net.IP
|
||||
}{
|
||||
{&net.IPNet{IP: net.ParseIP("192.168.0.0"), Mask: net.CIDRMask(16, 32)}, net.ParseIP("192.168.0.0")},
|
||||
{&net.IPNet{IP: net.ParseIP("192.168.100.0"), Mask: net.CIDRMask(24, 32)}, net.ParseIP("192.168.100.0")},
|
||||
}
|
||||
|
||||
for _, te := range tests {
|
||||
addr := AllZerosAddr(te.subnet)
|
||||
if !te.addr.Equal(addr) {
|
||||
t.Fatalf("AllZerosAddr(%s) => got %s, want %s", te.subnet, addr, te.addr)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestAllOnesAddr(t *testing.T) {
|
||||
var tests = []struct {
|
||||
subnet *net.IPNet
|
||||
addr net.IP
|
||||
}{
|
||||
{&net.IPNet{IP: net.ParseIP("192.168.0.0"), Mask: net.CIDRMask(16, 32)}, net.ParseIP("192.168.255.255")},
|
||||
{&net.IPNet{IP: net.ParseIP("192.168.100.0"), Mask: net.CIDRMask(24, 32)}, net.ParseIP("192.168.100.255")},
|
||||
}
|
||||
|
||||
for _, te := range tests {
|
||||
addr := AllOnesAddr(te.subnet)
|
||||
if !te.addr.Equal(addr) {
|
||||
t.Fatalf("AllOnesAddr(%s) => got %s, want %s", te.subnet, addr, te.addr)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestRangeNetwork(t *testing.T) {
|
||||
var tests = []struct {
|
||||
r *Range
|
||||
n *net.IPNet
|
||||
}{
|
||||
{ParseRange("10.10.10.10/24"), &net.IPNet{IP: net.ParseIP("10.10.10.0"), Mask: net.CIDRMask(24, 32)}},
|
||||
{ParseRange("10.10.10.10-10.10.14.11"), nil},
|
||||
{ParseRange("10.10.10.10-10.10.10.11"), &net.IPNet{IP: net.ParseIP("10.10.10.10"), Mask: net.CIDRMask(31, 32)}},
|
||||
}
|
||||
|
||||
for _, te := range tests {
|
||||
n := te.r.Network()
|
||||
if te.n != nil {
|
||||
assert.NotNil(t, n)
|
||||
} else {
|
||||
assert.Nil(t, n)
|
||||
continue
|
||||
}
|
||||
|
||||
if !n.IP.Equal(te.n.IP) {
|
||||
assert.FailNow(t, fmt.Sprintf("got %s, want %s", n, te.n))
|
||||
}
|
||||
|
||||
assert.EqualValues(t, n.Mask, te.n.Mask)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user