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:
75
vendor/github.com/vmware/vic/pkg/kvstore/backend.go
generated
vendored
Normal file
75
vendor/github.com/vmware/vic/pkg/kvstore/backend.go
generated
vendored
Normal file
@@ -0,0 +1,75 @@
|
||||
// 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 kvstore
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"os"
|
||||
|
||||
log "github.com/Sirupsen/logrus"
|
||||
"github.com/docker/docker/pkg/stringutils"
|
||||
|
||||
"github.com/vmware/vic/pkg/vsphere/datastore"
|
||||
)
|
||||
|
||||
type Backend interface {
|
||||
// Save saves data to the specified path
|
||||
Save(ctx context.Context, r io.Reader, path string) error
|
||||
// Load loads data from the specified path
|
||||
Load(ctx context.Context, path string) (io.ReadCloser, error)
|
||||
}
|
||||
|
||||
func NewDatastoreBackend(ds *datastore.Helper) Backend {
|
||||
return &dsBackend{ds: ds}
|
||||
}
|
||||
|
||||
type dsBackend struct {
|
||||
ds *datastore.Helper
|
||||
}
|
||||
|
||||
// Save saves data to the specified path
|
||||
func (d *dsBackend) Save(ctx context.Context, r io.Reader, path string) error {
|
||||
// upload to an ephemeral file
|
||||
tmpfile := fmt.Sprintf("%s-%s.tmp", path, stringutils.GenerateRandomAlphaOnlyString(10))
|
||||
if err := d.ds.Upload(ctx, r, tmpfile); err != nil {
|
||||
return err
|
||||
}
|
||||
log.Debugf("kv store upload of file (%s) was successful", tmpfile)
|
||||
|
||||
return d.ds.Mv(ctx, tmpfile, path)
|
||||
}
|
||||
|
||||
func toOsError(err error) error {
|
||||
switch err.Error() {
|
||||
case fmt.Sprintf("%d %s", http.StatusNotFound, http.StatusText(http.StatusNotFound)):
|
||||
return os.ErrNotExist
|
||||
}
|
||||
|
||||
return err
|
||||
}
|
||||
|
||||
// Load loads data from the specified path
|
||||
func (d *dsBackend) Load(ctx context.Context, path string) (io.ReadCloser, error) {
|
||||
rc, err := d.ds.Download(ctx, path)
|
||||
if err != nil {
|
||||
return nil, toOsError(err)
|
||||
}
|
||||
log.Debugf("kv store download of file (%s) was successful", path)
|
||||
|
||||
return rc, err
|
||||
}
|
||||
216
vendor/github.com/vmware/vic/pkg/kvstore/kvstore.go
generated
vendored
Normal file
216
vendor/github.com/vmware/vic/pkg/kvstore/kvstore.go
generated
vendored
Normal file
@@ -0,0 +1,216 @@
|
||||
// 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 kvstore
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"os"
|
||||
"regexp"
|
||||
"sync"
|
||||
|
||||
log "github.com/Sirupsen/logrus"
|
||||
)
|
||||
|
||||
var (
|
||||
ErrKeyNotFound = errors.New("key not found")
|
||||
)
|
||||
|
||||
// This package implements a very basic key/value store. It is up to the
|
||||
// caller to provision the namespace.
|
||||
|
||||
type KeyValueStore interface {
|
||||
// Set adds a new key or modifies an existing key in the key-value store
|
||||
Put(ctx context.Context, key string, value []byte) error
|
||||
|
||||
// Get gets an existing key in the key-value store. Returns ErrKeyNotFound
|
||||
// if key does not exist the key-value store.
|
||||
Get(key string) ([]byte, error)
|
||||
|
||||
// List lists the key-value pairs whose keys match the regular expression
|
||||
// passed in.
|
||||
List(re string) (map[string][]byte, error)
|
||||
|
||||
// Delete deletes existing keys from the key-value store. Returns ErrKeyNotFound
|
||||
// if key does not exist the key-value store.
|
||||
Delete(ctx context.Context, key string) error
|
||||
|
||||
// Save saves the key-value store data to the backend.
|
||||
Save(ctx context.Context) error
|
||||
|
||||
// Name returns the unique identifier/name for the key-value store. This is
|
||||
// used to determine the path that is passed to the backend operations.
|
||||
Name() string
|
||||
}
|
||||
|
||||
type kv struct {
|
||||
b Backend
|
||||
kv map[string][]byte
|
||||
name string
|
||||
l sync.RWMutex
|
||||
}
|
||||
|
||||
func fileName(name string) string {
|
||||
return fmt.Sprintf("%s.dat", name)
|
||||
}
|
||||
|
||||
// Create a new KeyValueStore instance using the given Backend with the given
|
||||
// file. If the file exists on the Backend, it is restored.
|
||||
func NewKeyValueStore(ctx context.Context, store Backend, name string) (KeyValueStore, error) {
|
||||
p := &kv{
|
||||
b: store,
|
||||
kv: make(map[string][]byte),
|
||||
name: name,
|
||||
}
|
||||
|
||||
if err := p.restore(ctx); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
log.Infof("NewKeyValueStore(%s) restored %d keys", name, len(p.kv))
|
||||
|
||||
return p, nil
|
||||
}
|
||||
|
||||
func (p *kv) Name() string {
|
||||
return p.name
|
||||
}
|
||||
|
||||
func (p *kv) restore(ctx context.Context) error {
|
||||
p.l.Lock()
|
||||
defer p.l.Unlock()
|
||||
|
||||
rc, err := p.b.Load(ctx, fileName(p.name))
|
||||
if err != nil {
|
||||
if os.IsNotExist(err) {
|
||||
return nil
|
||||
}
|
||||
|
||||
return err
|
||||
}
|
||||
defer rc.Close()
|
||||
|
||||
if err = json.NewDecoder(rc).Decode(&p.kv); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// Set a key to the KeyValueStore with the given value. If they key already
|
||||
// exists, the value is overwritten.
|
||||
func (p *kv) Put(ctx context.Context, key string, value []byte) error {
|
||||
p.l.Lock()
|
||||
defer p.l.Unlock()
|
||||
|
||||
// get the old value in case we need to roll back
|
||||
oldvalue, ok := p.kv[key]
|
||||
|
||||
if ok && bytes.Compare(oldvalue, value) == 0 {
|
||||
// NOOP
|
||||
return nil
|
||||
}
|
||||
|
||||
p.kv[key] = value
|
||||
|
||||
if err := p.save(ctx); err != nil && ok {
|
||||
// revert if failure
|
||||
p.kv[key] = oldvalue
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// Get retrieves a key from the KeyValueStore.
|
||||
func (p *kv) Get(key string) ([]byte, error) {
|
||||
p.l.RLock()
|
||||
defer p.l.RUnlock()
|
||||
|
||||
v, ok := p.kv[key]
|
||||
if !ok {
|
||||
return []byte{}, ErrKeyNotFound
|
||||
}
|
||||
|
||||
return v, nil
|
||||
}
|
||||
|
||||
func (p *kv) List(re string) (map[string][]byte, error) {
|
||||
p.l.RLock()
|
||||
defer p.l.RUnlock()
|
||||
|
||||
regex, err := regexp.Compile(re)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
kv := make(map[string][]byte)
|
||||
for k, v := range p.kv {
|
||||
if regex.MatchString(k) {
|
||||
kv[k] = v
|
||||
}
|
||||
}
|
||||
|
||||
if len(kv) == 0 {
|
||||
return nil, ErrKeyNotFound
|
||||
}
|
||||
|
||||
return kv, nil
|
||||
}
|
||||
|
||||
// Delete removes a key from the KeyValueStore.
|
||||
func (p *kv) Delete(ctx context.Context, key string) error {
|
||||
p.l.Lock()
|
||||
defer p.l.Unlock()
|
||||
|
||||
oldvalue, ok := p.kv[key]
|
||||
if !ok {
|
||||
return ErrKeyNotFound
|
||||
}
|
||||
|
||||
delete(p.kv, key)
|
||||
|
||||
if err := p.save(ctx); err != nil {
|
||||
// restore the key
|
||||
p.kv[key] = oldvalue
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// Save persists the KeyValueStore to the Backend.
|
||||
func (p *kv) Save(ctx context.Context) error {
|
||||
p.l.Lock()
|
||||
defer p.l.Unlock()
|
||||
return p.save(ctx)
|
||||
}
|
||||
|
||||
func (p *kv) save(ctx context.Context) error {
|
||||
buf, err := json.Marshal(p.kv)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
r := bytes.NewReader(buf)
|
||||
if err = p.b.Save(ctx, r, fileName(p.name)); err != nil {
|
||||
return fmt.Errorf("Error uploading %s: %s", fileName(p.name), err)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
170
vendor/github.com/vmware/vic/pkg/kvstore/kvstore_test.go
generated
vendored
Normal file
170
vendor/github.com/vmware/vic/pkg/kvstore/kvstore_test.go
generated
vendored
Normal file
@@ -0,0 +1,170 @@
|
||||
// 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 kvstore
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"strconv"
|
||||
"sync"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
|
||||
"github.com/vmware/vic/pkg/trace"
|
||||
)
|
||||
|
||||
func save(t *testing.T, kv KeyValueStore, key string, expectedvalue []byte) {
|
||||
op := trace.NewOperation(context.Background(), "save")
|
||||
|
||||
if !assert.NoError(t, kv.Put(op, key, expectedvalue)) {
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
func get(t *testing.T, kv KeyValueStore, key string, expectedval []byte) {
|
||||
// get the value we added
|
||||
v, err := kv.Get(key)
|
||||
if !assert.NoError(t, err) || !assert.NotNil(t, v) || !assert.Equal(t, expectedval, v) {
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
func TestAddAndGet(t *testing.T) {
|
||||
mb := &MockBackend{}
|
||||
|
||||
op := trace.NewOperation(context.Background(), "testaddsaveget")
|
||||
|
||||
// Save some entries in parallel
|
||||
entries := 500
|
||||
wg := sync.WaitGroup{}
|
||||
wg.Add(entries)
|
||||
|
||||
expected := make(map[string][]byte)
|
||||
|
||||
firstkv, err := NewKeyValueStore(op, mb, "datfile")
|
||||
if !assert.NoError(t, err) || !assert.NotNil(t, firstkv) {
|
||||
return
|
||||
}
|
||||
|
||||
for i := 0; i < entries; i++ {
|
||||
k := fmt.Sprintf("key-%d", i)
|
||||
v := []byte(strconv.Itoa(i))
|
||||
|
||||
expected[k] = v
|
||||
go func() {
|
||||
defer wg.Done()
|
||||
save(t, firstkv, k, v)
|
||||
get(t, firstkv, k, v)
|
||||
}()
|
||||
}
|
||||
wg.Wait()
|
||||
|
||||
if t.Failed() {
|
||||
return
|
||||
}
|
||||
|
||||
// Restart the kv store by creating a new one and attempt to get the same
|
||||
// entries.
|
||||
secondkv, err := NewKeyValueStore(op, mb, "datfile")
|
||||
if !assert.NoError(t, err) || !assert.NotNil(t, secondkv) {
|
||||
return
|
||||
}
|
||||
|
||||
wg.Add(entries)
|
||||
for k, v := range expected {
|
||||
go func(key string, value []byte) {
|
||||
defer wg.Done()
|
||||
get(t, secondkv, key, value)
|
||||
}(k, v)
|
||||
}
|
||||
wg.Wait()
|
||||
|
||||
if t.Failed() {
|
||||
return
|
||||
}
|
||||
|
||||
// Ovewrite all of the values and verify again
|
||||
wg.Add(entries)
|
||||
for k := range expected {
|
||||
newval := []byte("ddddd")
|
||||
|
||||
expected[k] = newval
|
||||
go func(key string, value []byte) {
|
||||
defer wg.Done()
|
||||
save(t, secondkv, key, value)
|
||||
get(t, secondkv, key, value)
|
||||
}(k, newval)
|
||||
}
|
||||
wg.Wait()
|
||||
|
||||
if t.Failed() {
|
||||
return
|
||||
}
|
||||
|
||||
// Restart and verify the overwritten values match the expected
|
||||
thirdkv, err := NewKeyValueStore(op, mb, "datfile")
|
||||
if !assert.NoError(t, err) || !assert.NotNil(t, thirdkv) {
|
||||
return
|
||||
}
|
||||
|
||||
wg.Add(entries)
|
||||
for k, v := range expected {
|
||||
go func(key string, value []byte) {
|
||||
defer wg.Done()
|
||||
get(t, thirdkv, key, value)
|
||||
}(k, v)
|
||||
}
|
||||
wg.Wait()
|
||||
|
||||
if t.Failed() {
|
||||
return
|
||||
}
|
||||
|
||||
// Remove all of the entries and assert nothing can be found
|
||||
wg.Add(entries)
|
||||
for k := range expected {
|
||||
go func(key string) {
|
||||
defer wg.Done()
|
||||
if !assert.NoError(t, thirdkv.Delete(op, key)) {
|
||||
return
|
||||
}
|
||||
|
||||
_, err := thirdkv.Get(key)
|
||||
if !assert.Error(t, err) {
|
||||
return
|
||||
}
|
||||
}(k)
|
||||
}
|
||||
wg.Wait()
|
||||
|
||||
if t.Failed() {
|
||||
return
|
||||
}
|
||||
|
||||
// Check the kv is empty after restart
|
||||
fourthkv, err := NewKeyValueStore(op, mb, "datfile")
|
||||
wg.Add(entries)
|
||||
for k := range expected {
|
||||
go func(key string) {
|
||||
defer wg.Done()
|
||||
_, err := fourthkv.Get(key)
|
||||
if !assert.Error(t, err) {
|
||||
return
|
||||
}
|
||||
}(k)
|
||||
}
|
||||
wg.Wait()
|
||||
}
|
||||
158
vendor/github.com/vmware/vic/pkg/kvstore/mocks.go
generated
vendored
Normal file
158
vendor/github.com/vmware/vic/pkg/kvstore/mocks.go
generated
vendored
Normal file
@@ -0,0 +1,158 @@
|
||||
// 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 kvstore
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"io"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
|
||||
"github.com/stretchr/testify/mock"
|
||||
)
|
||||
|
||||
type MockBackend struct {
|
||||
buf []byte
|
||||
}
|
||||
|
||||
// Creates path and ovewrites whatever is there
|
||||
func (m *MockBackend) Save(ctx context.Context, r io.Reader, pth string) error {
|
||||
buf, err := ioutil.ReadAll(r)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
m.buf = buf
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (m *MockBackend) Load(ctx context.Context, pth string) (io.ReadCloser, error) {
|
||||
if len(m.buf) == 0 {
|
||||
return nil, os.ErrNotExist
|
||||
}
|
||||
|
||||
return ioutil.NopCloser(bytes.NewReader(m.buf)), nil
|
||||
}
|
||||
|
||||
// MockKeyValueStore is an autogenerated mock type for the KeyValueStore type
|
||||
type MockKeyValueStore struct {
|
||||
mock.Mock
|
||||
}
|
||||
|
||||
// Delete provides a mock function with given fields: ctx, key
|
||||
func (_m *MockKeyValueStore) Delete(ctx context.Context, key string) error {
|
||||
ret := _m.Called(ctx, key)
|
||||
|
||||
var r0 error
|
||||
if rf, ok := ret.Get(0).(func(context.Context, string) error); ok {
|
||||
r0 = rf(ctx, key)
|
||||
} else {
|
||||
r0 = ret.Error(0)
|
||||
}
|
||||
|
||||
return r0
|
||||
}
|
||||
|
||||
// Get provides a mock function with given fields: key
|
||||
func (_m *MockKeyValueStore) Get(key string) ([]byte, error) {
|
||||
ret := _m.Called(key)
|
||||
|
||||
var r0 []byte
|
||||
if rf, ok := ret.Get(0).(func(string) []byte); ok {
|
||||
r0 = rf(key)
|
||||
} else {
|
||||
if ret.Get(0) != nil {
|
||||
r0 = ret.Get(0).([]byte)
|
||||
}
|
||||
}
|
||||
|
||||
var r1 error
|
||||
if rf, ok := ret.Get(1).(func(string) error); ok {
|
||||
r1 = rf(key)
|
||||
} else {
|
||||
r1 = ret.Error(1)
|
||||
}
|
||||
|
||||
return r0, r1
|
||||
}
|
||||
|
||||
// List provides a mock function with given fields: re
|
||||
func (_m *MockKeyValueStore) List(re string) (map[string][]byte, error) {
|
||||
ret := _m.Called(re)
|
||||
|
||||
var r0 map[string][]byte
|
||||
if rf, ok := ret.Get(0).(func(string) map[string][]byte); ok {
|
||||
r0 = rf(re)
|
||||
} else {
|
||||
if ret.Get(0) != nil {
|
||||
r0 = ret.Get(0).(map[string][]byte)
|
||||
}
|
||||
}
|
||||
|
||||
var r1 error
|
||||
if rf, ok := ret.Get(1).(func(string) error); ok {
|
||||
r1 = rf(re)
|
||||
} else {
|
||||
r1 = ret.Error(1)
|
||||
}
|
||||
|
||||
return r0, r1
|
||||
}
|
||||
|
||||
// Name provides a mock function with given fields:
|
||||
func (_m *MockKeyValueStore) Name() string {
|
||||
ret := _m.Called()
|
||||
|
||||
var r0 string
|
||||
if rf, ok := ret.Get(0).(func() string); ok {
|
||||
r0 = rf()
|
||||
} else {
|
||||
r0 = ret.Get(0).(string)
|
||||
}
|
||||
|
||||
return r0
|
||||
}
|
||||
|
||||
// Save provides a mock function with given fields: ctx
|
||||
func (_m *MockKeyValueStore) Save(ctx context.Context) error {
|
||||
ret := _m.Called(ctx)
|
||||
|
||||
var r0 error
|
||||
if rf, ok := ret.Get(0).(func(context.Context) error); ok {
|
||||
r0 = rf(ctx)
|
||||
} else {
|
||||
r0 = ret.Error(0)
|
||||
}
|
||||
|
||||
return r0
|
||||
}
|
||||
|
||||
// Set provides a mock function with given fields: ctx, key, value
|
||||
func (_m *MockKeyValueStore) Put(ctx context.Context, key string, value []byte) error {
|
||||
ret := _m.Called(ctx, key, value)
|
||||
|
||||
var r0 error
|
||||
if rf, ok := ret.Get(0).(func(context.Context, string, []byte) error); ok {
|
||||
r0 = rf(ctx, key, value)
|
||||
} else {
|
||||
r0 = ret.Error(0)
|
||||
}
|
||||
|
||||
return r0
|
||||
}
|
||||
|
||||
var _ KeyValueStore = (*MockKeyValueStore)(nil)
|
||||
Reference in New Issue
Block a user