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

162
providers/vic/cache/pod_cache.go vendored Normal file
View File

@@ -0,0 +1,162 @@
package cache
import (
"fmt"
"sync"
"github.com/vmware/vic/pkg/trace"
vicpod "github.com/virtual-kubelet/virtual-kubelet/providers/vic/pod"
)
type PodCache interface {
Rehydrate(op trace.Operation) error
Get(op trace.Operation, namespace, name string) (*vicpod.VicPod, error)
GetAll(op trace.Operation) []*vicpod.VicPod
Add(op trace.Operation, namespace, name string, pod *vicpod.VicPod) error
Delete(op trace.Operation, namespace, name string) error
}
type VicPodCache struct {
cache map[string]*vicpod.VicPod
lock sync.Mutex
}
type CacheError string
func (c CacheError) Error() string {return string(c)}
const (
PodCachePodNameError = CacheError("PodCache called with empty pod name")
PodCacheNilPodError = CacheError("PodCache called with nil pod")
)
func NewVicPodCache() PodCache {
v := &VicPodCache{}
v.cache = make(map[string]*vicpod.VicPod, 0)
return v
}
// Rehydrate replenishes the cache in the event of a virtual kubelet restart.
// NOT YET IMPLEMENTED
//
// arguments:
// op operation trace logger
// returns:
// error
func (v *VicPodCache) Rehydrate(op trace.Operation) error {
return nil
}
// Get returns the pod definition for a running pod
//
// arguments:
// op operation trace logger
// namespace namespace of the pod. Empty namespace assumes default.
// name name of the pod
// returns:
// error
func (v *VicPodCache) Get(op trace.Operation, namespace, name string) (*vicpod.VicPod, error) {
defer trace.End(trace.Begin(name, op))
if name == "" {
op.Errorf(PodCachePodNameError.Error())
return nil, PodCachePodNameError
}
//TODO: handle namespaces
pod, ok := v.cache[name]
if !ok {
err := fmt.Errorf("Pod %s not found in cache", name)
op.Info(err)
return nil, err
}
return pod, nil
}
// GetAll returns the pod definitions for all running pods
//
// arguments:
// op operation trace logger
// returns:
// error
func (v *VicPodCache) GetAll(op trace.Operation) []*vicpod.VicPod {
defer trace.End(trace.Begin("", op))
defer v.lock.Unlock()
v.lock.Lock()
list := make([]*vicpod.VicPod, 0)
for _, vp := range v.cache {
list = append(list, vp)
}
return list
}
// Add saves the pod definition of a running pod
//
// arguments:
// op operation trace logger
// namespace namespace of the pod. Empty namespace assumes default.
// name name of the pod
// pod pod definition
// returns:
// error
func (v *VicPodCache) Add(op trace.Operation, namespace, name string, pod *vicpod.VicPod) error {
defer trace.End(trace.Begin(name, op))
defer v.lock.Unlock()
v.lock.Lock()
if name == "" {
op.Errorf(PodCachePodNameError.Error())
return PodCachePodNameError
}
if pod == nil {
op.Errorf(PodCacheNilPodError.Error())
return PodCacheNilPodError
}
//TODO: handle namespaces
_, ok := v.cache[name]
if ok {
err := fmt.Errorf("Pod %s already cached. Duplicate pod.", name)
op.Error(err)
return err
}
v.cache[name] = pod
return nil
}
// Delete removes a pod definition from the cache. It does not stop/delete the
// actual pod.
//
// arguments:
// op operation trace logger
// namespace namespace of the pod. Empty namespace assumes default.
// name name of the pod
// returns:
// error
func (v *VicPodCache) Delete(op trace.Operation, namespace, name string) error {
defer trace.End(trace.Begin(name, op))
defer v.lock.Unlock()
v.lock.Lock()
if name == "" {
op.Errorf(PodCachePodNameError.Error())
return PodCachePodNameError
}
//TODO: handle namespaces
delete(v.cache, name)
return nil
}

139
providers/vic/cache/pod_cache_test.go vendored Normal file
View File

@@ -0,0 +1,139 @@
package cache
import (
"context"
"testing"
"github.com/stretchr/testify/assert"
"github.com/virtual-kubelet/virtual-kubelet/providers/vic/pod"
"k8s.io/api/core/v1"
"github.com/vmware/vic/pkg/trace"
)
var (
testpod *v1.Pod
)
func init() {
testpod = &v1.Pod{}
}
func setup(t *testing.T, op trace.Operation) PodCache {
c := NewVicPodCache()
assert.NotNil(t, c, "NewPod did not return a valid cache")
//populate with dummy data
vp := pod.VicPod{
ID: "123",
Pod: testpod,
}
c.Add(op, "namespace1", "testpod1a", &vp)
c.Add(op, "namespace1", "testpod1b", &vp)
c.Add(op, "namespace2", "testpod2a", &vp)
c.Add(op, "namespace2", "testpod2b", &vp)
return c
}
func TestRehydrate(t *testing.T) {
op := trace.NewOperation(context.Background(), "")
c := NewVicPodCache()
assert.NotNil(t, c, "NewPod did not return a valid cache")
err := c.Rehydrate(op)
assert.Nil(t, err, "PodCache.Rehydrate failed with error: %s", err)
}
func TestAdd(t *testing.T) {
var err error
op := trace.NewOperation(context.Background(), "")
c := NewVicPodCache()
assert.NotNil(t, c, "NewPod did not return a valid cache")
//populate with dummy data
vp := pod.VicPod{
ID: "123",
Pod: testpod,
}
// Positive cases
err = c.Add(op, "namespace1", "testpod1a", &vp)
assert.Nil(t, err, "PodCache.Add failed with error: %s", err)
// Negative cases
err = c.Add(op, "namespace1", "", &vp)
assert.NotNil(t, err, "PodCache.Add expected error for empty name")
assert.Equal(t, err, PodCachePodNameError)
err = c.Add(op, "namespace1", "test2", nil)
assert.NotNil(t, err, "PodCache.Add expected error for nil pod")
assert.Equal(t, err, PodCacheNilPodError)
}
func TestGet(t *testing.T) {
var err error
var vpod *pod.VicPod
op := trace.NewOperation(context.Background(), "")
c := setup(t, op)
// Positive cases
vpod, err = c.Get(op, "namespace1", "testpod1a")
assert.Nil(t, err, "PodCache.Get failed with error: %s", err)
assert.NotNil(t, vpod, "PodCache.Get expected to return non-nil pod but received nil")
vpod, err = c.Get(op, "namespace2", "testpod2a")
assert.Nil(t, err, "PodCache.Get failed with error: %s", err)
assert.NotNil(t, vpod, "PodCache.Get expected to return non-nil pod but received nil")
// Negative cases
vpod, err = c.Get(op, "namespace1", "")
assert.Equal(t, err, PodCachePodNameError)
assert.Nil(t, vpod, "PodCache.Get expected to return nil pod but received non-nil")
//TODO: uncomment out once namespace support added to cache
//vpod, err = c.Get(op, "namespace1", "testpod2a")
//assert.NotNil(t, err, "PodCache.Get did not respect namespace: %s", err)
//vpod, err = c.Get(op, "", "testpod1a")
//assert.NotNil(t, err, "PodCache.Get did not respect namespace: %s", err)
}
func TestGetAll(t *testing.T) {
op := trace.NewOperation(context.Background(), "")
c := setup(t, op)
vps := c.GetAll(op)
assert.NotNil(t, vps, "PodCache.GetAll returned nil slice")
assert.Len(t, vps, 4, "PodCache.Get did not return all pod definitions. Returned %d pods.", len(vps))
}
func TestDelete(t *testing.T) {
var err error
op := trace.NewOperation(context.Background(), "")
c := setup(t, op)
// Positive cases
err = c.Delete(op, "namespace1", "testpod1a")
assert.Nil(t, err, "PodCache.Delete failed with error: %s", err)
vps := c.GetAll(op)
assert.Len(t, vps, 3, "PodCache.Delete did not delete pod.")
// Negative cases
err = c.Delete(op, "namespace2", "")
assert.Equal(t, err, PodCachePodNameError)
//TODO: uncomment the tests below once namespace support added to cache
//vps = c.GetAll(op)
//currCount := len(vps)
//err = c.Delete(op, "", "testpod1b")
//assert.NotNil(t, err, "PodCache.Delete expected to return error but received nil")
//vps = c.GetAll(op)
//assert.Len(t, vps, currCount, "PodCache.Delete ignored namespace")
}