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:
Loc Nguyen
2018-06-04 15:41:32 -07:00
committed by Ria Bhatia
parent 98a111e8b7
commit 513cebe7b7
6296 changed files with 1123685 additions and 8 deletions

433
vendor/github.com/vmware/vic/lib/dhcp/client/client.go generated vendored Normal file
View File

@@ -0,0 +1,433 @@
// 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 client
import (
"bytes"
"fmt"
"net"
"syscall"
"time"
log "github.com/Sirupsen/logrus"
"github.com/d2g/dhcp4"
"github.com/d2g/dhcp4client"
"github.com/vmware/vic/lib/dhcp"
"github.com/vmware/vic/pkg/ip"
"github.com/vmware/vic/pkg/trace"
)
// Client represents a DHCP client
type Client interface {
// SetTimeout sets the timeout for a subsequent DHCP request
SetTimeout(t time.Duration)
// Request sends a full DHCP request, resulting in a DHCP lease.
// On a successful lease, returns a DHCP acknowledgment packet
Request() error
// Renew renews an existing DHCP lease. Returns a new acknowledgment
// packet on success.
Renew() error
// Release releases an existing DHCP lease.
Release() error
// SetParamterRequestList sets the DHCP parameter request list
// per RFC 2132, section 9.8
SetParameterRequestList(...byte)
// LastAck returns the last ack packet from a request or renew operation.
LastAck() *dhcp.Packet
}
type client struct {
timeout time.Duration
id ID
params []byte
ack dhcp4.Packet
}
// The default timeout for the client
const defaultTimeout = 10 * time.Second
// NewClient creates a new DHCP client. Note the returned object is not thread-safe.
func NewClient(ifIndex int, hwaddr net.HardwareAddr) (Client, error) {
defer trace.End(trace.Begin(""))
id, err := NewID(ifIndex, hwaddr)
if err != nil {
return nil, err
}
return &client{
id: id,
timeout: defaultTimeout,
}, nil
}
func (c *client) SetTimeout(t time.Duration) {
defer trace.End(trace.Begin(""))
c.timeout = t
}
// Note that the Go runtime sets SA_RESTART for syscalls which retries them automatically if they interrupted.
// However, from the signal man page:
//
// The following interfaces are never restarted after being interrupted
// by a signal handler, regardless of the use of SA_RESTART; they always
// fail with the error EINTR when interrupted by a signal handler:
//
// * "Input" socket interfaces, when a timeout (SO_RCVTIMEO) has been
// set on the socket using setsockopt(2): accept(2), recv(2),
// recvfrom(2), recvmmsg(2) (also with a non-NULL timeout argument),
// and recvmsg(2).
//
// * "Output" socket interfaces, when a timeout (SO_RCVTIMEO) has been
// set on the socket using setsockopt(2): connect(2), send(2),
// sendto(2), and sendmsg(2).
func withRetry(name string, op func() error) error {
defer trace.End(trace.Begin(""))
for {
if err := op(); err != nil {
if errno, ok := err.(syscall.Errno); ok {
if errno == syscall.EAGAIN || errno == syscall.EINTR {
log.Debugf("retrying %q: errno=%d, error=%s", name, errno, err)
continue
}
}
return err
}
return nil
}
}
func (c *client) isCompletePacket(p *dhcp.Packet) bool {
complete := !ip.IsUnspecifiedIP(p.YourIP()) &&
!ip.IsUnspecifiedIP(p.ServerIP())
if !complete {
return false
}
for _, param := range c.params {
switch dhcp4.OptionCode(param) {
case dhcp4.OptionSubnetMask:
ones, bits := p.SubnetMask().Size()
if ones == 0 || bits == 0 {
return false
}
case dhcp4.OptionRouter:
if ip.IsUnspecifiedIP(p.Gateway()) {
return false
}
case dhcp4.OptionDomainNameServer:
if len(p.DNS()) == 0 {
return false
}
}
}
if p.LeaseTime().Seconds() == 0 {
return false
}
return true
}
func (c *client) discoverPacket(cl *dhcp4client.Client) (dhcp4.Packet, error) {
defer trace.End(trace.Begin(""))
dp := cl.DiscoverPacket()
return c.setOptions(dp)
}
func (c *client) requestPacket(cl *dhcp4client.Client, op *dhcp4.Packet) (dhcp4.Packet, error) {
defer trace.End(trace.Begin(""))
rp := cl.RequestPacket(op)
return c.setOptions(rp)
}
func logDHCPPacketNoOptions(p dhcp4.Packet) {
log.Debugf("OpCode: %d, HType: %d, HLen: %d, Hops: %d, XId: %+v, Secs: %+v, Flags: %+v, CIAddr: %s, "+
"YIAddr: %s, SIAddr: %s, GIAddr: %s, CHAddr: %s, Cookie: %+v, Broadcast: %v",
p.OpCode(), p.HType(), p.HLen(), p.Hops(), p.XId(), p.Secs(), p.Flags(),
p.CIAddr().String(), p.YIAddr().String(), p.SIAddr().String(), p.GIAddr().String(), p.CHAddr().String(),
p.Cookie(), p.Broadcast())
}
func logDHCPPacketAndOptions(p dhcp4.Packet, o dhcp4.Options) {
logDHCPPacketNoOptions(p)
log.Debugf("Options: %+v", o)
}
func logDHCPPacket(p dhcp4.Packet) {
o := p.ParseOptions()
logDHCPPacketAndOptions(p, o)
}
func (c *client) request(cl *dhcp4client.Client) (bool, dhcp4.Packet, error) {
defer trace.End(trace.Begin(""))
dp, err := c.discoverPacket(cl)
if err != nil {
return false, nil, err
}
dp.PadToMinSize()
if err = cl.SendPacket(dp); err != nil {
return false, nil, err
}
var op dhcp4.Packet
for {
op, err = cl.GetOffer(&dp)
if err != nil {
return false, nil, err
}
if c.isCompletePacket(dhcp.NewPacket([]byte(op))) {
break
}
}
rp, err := c.requestPacket(cl, &op)
if err != nil {
return false, nil, err
}
rp.PadToMinSize()
if err = cl.SendPacket(rp); err != nil {
return false, nil, err
}
ack, err := cl.GetAcknowledgement(&rp)
if err != nil {
return false, nil, err
}
opts := ack.ParseOptions()
logDHCPPacketAndOptions(ack, opts)
if dhcp4.MessageType(opts[dhcp4.OptionDHCPMessageType][0]) == dhcp4.NAK {
return false, nil, fmt.Errorf("Got NAK from DHCP server")
}
return true, ack, nil
}
func (c *client) Request() error {
defer trace.End(trace.Begin(""))
log.Debugf("id: %+v", c.id)
// send the request over a raw socket
raw, err := dhcp4client.NewPacketSock(c.id.IfIndex)
if err != nil {
return err
}
rawc, err := dhcp4client.New(
dhcp4client.Connection(raw),
dhcp4client.Timeout(c.timeout),
dhcp4client.HardwareAddr(c.id.HardwareAddr))
if err != nil {
return err
}
defer rawc.Close()
success := false
var p dhcp4.Packet
err = withRetry("DHCP request", func() error {
var err error
success, p, err = c.request(rawc)
return err
})
if err != nil {
return err
}
if !success {
return fmt.Errorf("failed dhcp request")
}
log.Debugf("%+v", p)
c.ack = p
return nil
}
func (c *client) newClient() (*dhcp4client.Client, error) {
defer trace.End(trace.Begin(""))
ack := dhcp.NewPacket(c.ack)
conn, err := dhcp4client.NewInetSock(dhcp4client.SetRemoteAddr(net.UDPAddr{IP: ack.ServerIP(), Port: 67}))
if err != nil {
return nil, err
}
cl, err := dhcp4client.New(dhcp4client.Connection(conn), dhcp4client.Timeout(c.timeout))
if err != nil {
return nil, err
}
return cl, nil
}
func (c *client) renew(cl *dhcp4client.Client) (dhcp4.Packet, error) {
defer trace.End(trace.Begin(""))
rp := cl.RenewalRequestPacket(&c.ack)
rp, err := c.setOptions(rp)
if err != nil {
return nil, err
}
rp.PadToMinSize()
if err = cl.SendPacket(rp); err != nil {
return nil, err
}
newack, err := cl.GetAcknowledgement(&rp)
if err != nil {
return nil, err
}
opts := newack.ParseOptions()
if dhcp4.MessageType(opts[dhcp4.OptionDHCPMessageType][0]) == dhcp4.NAK {
return nil, fmt.Errorf("received NAK from DHCP server")
}
return newack, nil
}
func (c *client) Renew() error {
defer trace.End(trace.Begin(""))
if c.ack == nil {
return fmt.Errorf("no ack packet, call Request first")
}
cl, err := c.newClient()
if err != nil {
return err
}
defer cl.Close()
var newack dhcp4.Packet
err = withRetry("DHCP renew", func() error {
var err error
newack, err = c.renew(cl)
return err
})
if err != nil {
return err
}
c.ack = newack
return nil
}
func (c *client) Release() error {
defer trace.End(trace.Begin(""))
if len(c.ack) == 0 {
return fmt.Errorf("no ack packet, call Request first")
}
cl, err := c.newClient()
if err != nil {
return err
}
defer cl.Close()
return withRetry("DHCP release", func() error {
return cl.Release(c.ack)
})
}
// SetParamterRequestList sets the DHCP parameter request list
// per RFC 2132, section 9.8
func (c *client) SetParameterRequestList(params ...byte) {
defer trace.End(trace.Begin(""))
c.params = make([]byte, len(params))
copy(c.params, params)
log.Debugf("c.params=%#v", c.params)
}
// setOptions sets dhcp options on a dhcp packet
func (c *client) setOptions(p dhcp4.Packet) (dhcp4.Packet, error) {
defer trace.End(trace.Begin(""))
dirty := false
opts := p.ParseOptions()
// the current parameter request list
rl := opts[dhcp4.OptionParameterRequestList]
// figure out if there are any new parameters
for _, p := range c.params {
if bytes.IndexByte(rl, p) == -1 {
dirty = true
rl = append(rl, p)
}
}
opts[dhcp4.OptionParameterRequestList] = rl
if _, ok := opts[dhcp4.OptionClientIdentifier]; !ok {
b, err := c.id.MarshalBinary()
if err != nil {
return p, err
}
opts[dhcp4.OptionClientIdentifier] = b
dirty = true
}
// finally reset the options on the packet, if necessary
if dirty {
// strip out all options, and add them back in with the new changed options;
// this is the only way currently to delete/modify a packet option
p.StripOptions()
// have to copy since values in opts (of type []byte) are still pointing into p
var newp dhcp4.Packet
newp = make([]byte, len(p))
copy(newp, p)
log.Debugf("opts=%#v", opts)
for o, v := range opts {
newp.AddOption(o, v)
}
p = newp
}
return p, nil
}
func (c *client) LastAck() *dhcp.Packet {
defer trace.End(trace.Begin(""))
if c.ack == nil {
return nil
}
return dhcp.NewPacket(c.ack)
}

View File

@@ -0,0 +1,193 @@
// 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 client
import (
"bufio"
"errors"
"net"
"os"
"strings"
"syscall"
"testing"
"io/ioutil"
log "github.com/Sirupsen/logrus"
"github.com/d2g/dhcp4"
"github.com/google/uuid"
"github.com/stretchr/testify/assert"
"github.com/vmware/vic/lib/system"
)
var dummyHWAddr net.HardwareAddr = []byte{0x6, 0x0, 0x0, 0x0, 0x0, 0x0}
func TestMain(m *testing.M) {
Sys = system.System{
UUID: uuid.New().String(),
}
os.Exit(m.Run())
}
func TestSetOptions(t *testing.T) {
id, err := NewID(0, dummyHWAddr)
assert.NoError(t, err)
c := &client{
id: id,
}
p := dhcp4.NewPacket(dhcp4.BootRequest)
var tests = []struct {
prl []byte
}{
{
prl: []byte{
byte(dhcp4.OptionSubnetMask),
byte(dhcp4.OptionRouter),
},
},
{
prl: []byte{
byte(dhcp4.OptionSubnetMask),
byte(dhcp4.OptionRouter),
byte(dhcp4.OptionNameServer),
},
},
}
for _, te := range tests {
c.SetParameterRequestList(te.prl...)
p, err := c.setOptions(p)
assert.NoError(t, err)
assert.NotEmpty(t, p)
opts := p.ParseOptions()
prl := opts[dhcp4.OptionParameterRequestList]
assert.NotNil(t, prl)
assert.EqualValues(t, te.prl, prl)
// packet should have client id set
cid := opts[dhcp4.OptionClientIdentifier]
assert.NotNil(t, cid)
b, _ := id.MarshalBinary()
assert.EqualValues(t, cid, b)
}
}
func TestWithRetry(t *testing.T) {
errors := []error{
syscall.Errno(syscall.EAGAIN),
syscall.Errno(syscall.EINTR),
errors.New("fail"),
}
i := 0
err := withRetry("test fail", func() error {
e := errors[i]
i++
return e
})
if err != errors[len(errors)-1] {
t.Errorf("err=%s", err)
}
err = withRetry("test ok", func() error {
return nil
})
if err != nil {
t.Errorf("err=%s", err)
}
}
const packetStr string = "OpCode: 1, HType: 2, HLen: 14, Hops: 3, XId: [1 2 3 4], Secs: [0 0], Flags: [5 6], " +
"CIAddr: 1.2.3.4, YIAddr: 0.0.0.0, SIAddr: 0.0.0.0, GIAddr: 0.0.0.0, " + "" +
"CHAddr: 01:02:03:04:05:06:07:08:09:0a:0b:0c:0d:0e, " +
"Cookie: [7 8 9 10], Broadcast: false"
const optionStr string = "Options: map"
func TestLog(t *testing.T) {
id, err := NewID(0, dummyHWAddr)
assert.NoError(t, err)
c := &client{
id: id,
}
p := dhcp4.NewPacket(dhcp4.BootRequest)
p.SetHType(2)
p.SetHops(3)
p.SetXId([]byte{1, 2, 3, 4})
p.SetFlags([]byte{5, 6})
p.SetCookie([]byte{7, 8, 9, 10})
p.SetCIAddr([]byte{1, 2, 3, 4})
p.SetCHAddr([]byte{0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7, 0x8, 0x9, 0xa, 0xb, 0xc, 0xd, 0xe})
var opts = struct {
prl []byte
}{
prl: []byte{
byte(dhcp4.OptionSubnetMask),
byte(dhcp4.OptionRouter),
byte(dhcp4.OptionNameServer),
},
}
f, err := ioutil.TempFile("", "DhcpClient-")
defer os.Remove(f.Name())
if err == nil {
log.SetOutput(f)
log.SetLevel(log.DebugLevel)
}
c.SetParameterRequestList(opts.prl...)
p, err = c.setOptions(p)
assert.NoError(t, err)
assert.NotEmpty(t, p)
// Log packet
logDHCPPacket(p)
// Log packet and options
o := p.ParseOptions()
logDHCPPacketAndOptions(p, o)
res := verifyLog(f)
assert.True(t, res)
}
func verifyLog(lFile *os.File) bool {
f, err := os.Open(lFile.Name())
if err != nil {
return false
}
countBody := 0
countOptions := 0
scanner := bufio.NewScanner(f)
for scanner.Scan() {
line := scanner.Text()
if strings.Contains(line, packetStr) {
countBody++
} else if strings.Contains(line, optionStr) {
countOptions++
}
}
return countBody == 2 && countOptions == 2
}

152
vendor/github.com/vmware/vic/lib/dhcp/client/id.go generated vendored Normal file
View File

@@ -0,0 +1,152 @@
// 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 client
import (
"bytes"
"encoding/binary"
"fmt"
"net"
"github.com/dchest/siphash"
"github.com/google/uuid"
"github.com/vmware/vic/lib/system"
)
var Sys system.System
func init() {
Sys = system.New()
}
// Duid is a vendor based DUID per https://tools.ietf.org/html/rfc3315#section-9.3
type Duid struct {
Type uint16
PEN uint32
ID uint64
}
// Iaid is an opaque 32-bit identifier unique to a network interface; see https://tools.ietf.org/html/rfc4361#section-6.1
type Iaid []byte
// ID is a DHCPv4 client ID
type ID struct {
Type uint8
Iaid Iaid
Duid Duid
IfIndex int
HardwareAddr net.HardwareAddr
}
// VMwarePEN is VMware's PEN (Private Enterprise Number); see http://www.iana.org/assignments/enterprise-numbers/enterprise-numbers
const VMwarePEN uint32 = 6876
// DuidEn is the DUID type; see https://tools.ietf.org/html/rfc3315#section-9.3
const DuidEn uint16 = 2
var key = []byte{0xc4, 0xeb, 0x38, 0x9e, 0x4e, 0xd9, 0x48, 0x12, 0x93, 0xa6, 0xb7, 0x0b, 0x9b, 0x07, 0xcc, 0x2b}
// NewID generates a DHCPv4 client ID, per https://tools.ietf.org/html/rfc4361#section-6.1
func NewID(ifindex int, hw net.HardwareAddr) (ID, error) {
iaid := makeIaid(ifindex, hw)
duid, err := makeDuid()
if err != nil {
return ID{}, err
}
return ID{
Type: 255,
Iaid: iaid,
Duid: duid,
IfIndex: ifindex,
HardwareAddr: hw,
}, nil
}
// MarshalBinary implements the BinaryMarshaler interface
func (i ID) MarshalBinary() ([]byte, error) {
var b []byte
b = append(b, i.Type)
ib, err := i.Iaid.MarshalBinary()
if err != nil {
return nil, err
}
b = append(b, ib...)
db, err := i.Duid.MarshalBinary()
if err != nil {
return nil, err
}
return append(b, db...), nil
}
// MarshalBinary implements the BinaryMarshaler interface
func (ia Iaid) MarshalBinary() ([]byte, error) {
return ia[:], nil
}
// MarshalBinary implements the BinaryMarshaler interface
func (d Duid) MarshalBinary() ([]byte, error) {
b := new(bytes.Buffer)
// #nosec: Errors unhandled.
binary.Write(b, binary.BigEndian, d.Type)
// #nosec: Errors unhandled.
binary.Write(b, binary.BigEndian, d.PEN)
// #nosec: Errors unhandled.
binary.Write(b, binary.BigEndian, d.ID)
return b.Bytes(), nil
}
// makeIaid constructs a new IAID. Ported from systemd's
// implementation here: https://github.com/systemd/systemd/blob/master/src/libsystemd-network/dhcp-identifier.c
func makeIaid(ifindex int, hw net.HardwareAddr) Iaid {
h := siphash.New(key)
// #nosec: Errors unhandled.
h.Write(hw)
id := h.Sum64()
// fold into 32 bits
iaid := make(Iaid, 4)
binary.BigEndian.PutUint32(iaid, uint32(id&0xffffffff)^uint32(id>>32))
return iaid
}
func getMachineID() ([]byte, error) {
id := Sys.UUID
if id == "" {
return nil, fmt.Errorf("could not get machine id")
}
uid, err := uuid.Parse(id)
if err != nil {
return nil, err
}
return uid.MarshalBinary()
}
// makeDuid constructs a new DUID. Adapted from systemd's implemenation here: https://github.com/systemd/systemd/blob/master/src/libsystemd-network/dhcp-identifier.c
func makeDuid() (Duid, error) {
id, err := getMachineID()
if err != nil {
return Duid{}, err
}
h := siphash.New(key)
// #nosec: Errors unhandled.
h.Write(id)
return Duid{Type: DuidEn, PEN: VMwarePEN, ID: h.Sum64()}, nil
}