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

View File

@@ -0,0 +1,207 @@
// 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 tasks wraps the operation of VC. It will invoke the operation and wait
// until it's finished, and then return the execution result or error message.
package tasks
import (
"context"
"math/rand"
"time"
"github.com/vmware/govmomi/task"
"github.com/vmware/govmomi/vim25/progress"
"github.com/vmware/govmomi/vim25/soap"
"github.com/vmware/govmomi/vim25/types"
"github.com/vmware/vic/pkg/trace"
)
const (
maxBackoffFactor = int64(16)
)
//FIXME: remove this type and refactor to use object.Task from govmomi
// this will require a lot of code being touched in a lot of places.
type Task interface {
Wait(ctx context.Context) error
WaitForResult(ctx context.Context, s progress.Sinker) (*types.TaskInfo, error)
}
type temporary interface {
Temporary() bool
}
// Wait wraps govmomi operations and wait the operation to complete
// Sample usage:
// info, err := Wait(ctx, func(ctx), (*object.Reference, *TaskInfo, error) {
// return vm, vm.Reconfigure(ctx, config)
// })
func Wait(ctx context.Context, f func(context.Context) (Task, error)) error {
_, err := WaitForResult(ctx, f)
return err
}
// WaitForResult wraps govmomi operations and wait the operation to complete.
// Return the operation result
// Sample usage:
// info, err := WaitForResult(ctx, func(ctx) (*TaskInfo, error) {
// return vm, vm.Reconfigure(ctx, config)
// })
func WaitForResult(ctx context.Context, f func(context.Context) (Task, error)) (*types.TaskInfo, error) {
var err error
var backoffFactor int64 = 1
op := trace.FromContext(ctx, "WaitForResult")
for {
var t Task
var info *types.TaskInfo
if t, err = f(op); err == nil {
if info, err = t.WaitForResult(op, nil); err == nil {
return info, nil
}
}
if !IsRetryError(op, err) {
return info, err
}
sleepValue := time.Duration(backoffFactor * (rand.Int63n(100) + int64(50)))
select {
case <-time.After(sleepValue * time.Millisecond):
backoffFactor *= 2
if backoffFactor > maxBackoffFactor {
backoffFactor = maxBackoffFactor
}
case <-op.Done():
return info, op.Err()
}
op.Warnf("retrying task")
}
}
const (
vimFault = "vim"
soapFault = "soap"
taskFault = "task"
)
// IsRetryErrors will return true for vSphere errors, which can be fixed by retry.
// Currently the error includes TaskInProgress, NetworkDisruptedAndConfigRolledBack and InvalidArgument
// Retry on NetworkDisruptedAndConfigRolledBack is to workaround vSphere issue
// Retry on InvalidArgument(invlid path) is to workaround vSAN bug: https://bugzilla.eng.vmware.com/show_bug.cgi?id=1770798. TODO: Should remove it after vSAN fixed the bug
func IsRetryError(op trace.Operation, err error) bool {
if soap.IsSoapFault(err) {
switch f := soap.ToSoapFault(err).VimFault().(type) {
case types.TaskInProgress:
return true
case types.NetworkDisruptedAndConfigRolledBack:
logExpectedFault(op, soapFault, f)
return true
case types.InvalidArgument:
logExpectedFault(op, soapFault, f)
return true
case types.VAppTaskInProgress:
logExpectedFault(op, soapFault, f)
return true
case types.FailToLockFaultToleranceVMs:
logExpectedFault(op, soapFault, f)
return true
case types.HostCommunication:
logExpectedFault(op, soapFault, f)
return true
default:
logSoapFault(op, f)
return false
}
}
if soap.IsVimFault(err) {
switch f := soap.ToVimFault(err).(type) {
case *types.TaskInProgress:
return true
case *types.NetworkDisruptedAndConfigRolledBack:
logExpectedFault(op, vimFault, f)
return true
case *types.InvalidArgument:
logExpectedFault(op, vimFault, f)
return true
case *types.VAppTaskInProgress:
logExpectedFault(op, soapFault, f)
return true
case *types.FailToLockFaultToleranceVMs:
logExpectedFault(op, soapFault, f)
return true
case *types.HostCommunication:
logExpectedFault(op, soapFault, f)
return true
default:
logFault(op, f)
return false
}
}
switch err := err.(type) {
case task.Error:
switch f := err.Fault().(type) {
case *types.TaskInProgress:
return true
case *types.NetworkDisruptedAndConfigRolledBack:
logExpectedFault(op, taskFault, f)
return true
case *types.InvalidArgument:
logExpectedFault(op, taskFault, f)
return true
case *types.HostCommunication:
logExpectedFault(op, taskFault, f)
return true
default:
logFault(op, err.Fault())
return false
}
default:
// retry the temporary errors
t, ok := err.(temporary)
if ok && t.Temporary() {
logExpectedError(op, err)
return true
}
logError(op, err)
return false
}
}
// Helper Functions
func logFault(op trace.Operation, fault types.BaseMethodFault) {
op.Errorf("unexpected fault on task retry: %#v", fault)
}
func logSoapFault(op trace.Operation, fault types.AnyType) {
op.Debugf("unexpected soap fault on task retry: %s", fault)
}
func logError(op trace.Operation, err error) {
op.Debugf("unexpected error on task retry: %s", err)
}
func logExpectedFault(op trace.Operation, kind string, fault interface{}) {
op.Debugf("task retry on expected %s fault: %#v", kind, fault)
}
func logExpectedError(op trace.Operation, err error) {
op.Debugf("task retry on expected error %s", err)
}

View File

@@ -0,0 +1,440 @@
// 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 tasks
import (
"context"
"strings"
"testing"
"time"
log "github.com/Sirupsen/logrus"
"github.com/stretchr/testify/assert"
"github.com/vmware/govmomi"
"github.com/vmware/govmomi/find"
"github.com/vmware/govmomi/simulator"
"github.com/vmware/govmomi/task"
"github.com/vmware/govmomi/vim25/methods"
"github.com/vmware/govmomi/vim25/progress"
"github.com/vmware/govmomi/vim25/soap"
"github.com/vmware/govmomi/vim25/types"
"github.com/vmware/vic/pkg/errors"
"github.com/vmware/vic/pkg/trace"
)
func TestMain(m *testing.M) {
log.SetLevel(log.DebugLevel)
m.Run()
}
type MyTask struct {
success bool
}
func (t *MyTask) Wait(ctx context.Context) error {
_, err := t.WaitForResult(ctx, nil)
return err
}
func (t *MyTask) WaitForResult(ctx context.Context, s progress.Sinker) (*types.TaskInfo, error) {
if t.success {
return nil, nil
}
return nil, errors.Errorf("Wait failed")
}
func createFailedTask(context.Context) (Task, error) {
return nil, errors.Errorf("Create VM failed")
}
func createFailedResultWaiter(context.Context) (Task, error) {
task := &MyTask{
false,
}
return task, nil
}
func createResultWaiter(context.Context) (Task, error) {
task := &MyTask{
true,
}
return task, nil
}
func TestFailedInvokeResult(t *testing.T) {
ctx := context.TODO()
_, err := WaitForResult(ctx, func(ctx context.Context) (Task, error) {
return createFailedTask(ctx)
})
if err == nil || !strings.Contains(err.Error(), "Create VM failed") {
t.Errorf("Not expected error message")
}
}
func TestFailedWaitResult(t *testing.T) {
ctx := context.TODO()
_, err := WaitForResult(ctx, func(ctx context.Context) (Task, error) {
return createFailedResultWaiter(ctx)
})
log.Debugf("got error: %s", err.Error())
if err == nil || !strings.Contains(err.Error(), "Wait failed") {
t.Errorf("Not expected error message")
}
}
func TestSuccessWaitResult(t *testing.T) {
ctx := context.TODO()
_, err := WaitForResult(ctx, func(ctx context.Context) (Task, error) {
return createResultWaiter(ctx)
})
if err != nil {
t.Errorf("Unexpected error: %s", err.Error())
}
}
func createFailed(context.Context) (Task, error) {
return nil, errors.Errorf("Create VM failed")
}
func createFailedWaiter(context.Context) (Task, error) {
task := &MyTask{
false,
}
return task, nil
}
func createWaiter(context.Context) (Task, error) {
task := &MyTask{
true,
}
return task, nil
}
func TestFailedInvoke(t *testing.T) {
ctx := context.TODO()
err := Wait(ctx, func(ctx context.Context) (Task, error) {
return createFailed(ctx)
})
if err == nil || !strings.Contains(err.Error(), "Create VM failed") {
t.Errorf("Not expected error message")
}
}
func TestFailedWait(t *testing.T) {
ctx := context.TODO()
err := Wait(ctx, func(ctx context.Context) (Task, error) {
return createFailedWaiter(ctx)
})
log.Debugf("got error: %s", err.Error())
if err == nil || !strings.Contains(err.Error(), "Wait failed") {
t.Errorf("Not expected error message")
}
}
func TestSuccessWait(t *testing.T) {
ctx := context.TODO()
err := Wait(ctx, func(ctx context.Context) (Task, error) {
return createWaiter(ctx)
})
if err != nil {
t.Errorf("Unexpected error: %s", err.Error())
}
}
var taskInProgressFault = task.Error{
LocalizedMethodFault: &types.LocalizedMethodFault{
Fault: &types.TaskInProgress{},
},
}
type taskInProgressTask struct {
cur, max int
err error
info *types.TaskInfo
}
func (t *taskInProgressTask) Wait(ctx context.Context) error {
t.cur++
if t.cur == t.max {
return t.err
}
return taskInProgressFault
}
func (t *taskInProgressTask) WaitForResult(ctx context.Context, s progress.Sinker) (*types.TaskInfo, error) {
return t.info, t.Wait(ctx)
}
func mustRunInTime(t *testing.T, d time.Duration, f func()) {
done := make(chan bool)
go func() {
f()
close(done)
}()
ctx, cancel := context.WithTimeout(context.Background(), d)
defer cancel()
select {
case <-done: // ran within alloted time
case <-ctx.Done():
t.Fatalf("test did not run in alloted time %s", d)
}
}
func TestRetry(t *testing.T) {
mustRunInTime(t, 2*time.Second, func() {
ctx := context.Background()
i := 0
ti, err := WaitForResult(ctx, func(_ context.Context) (Task, error) {
i++
return nil, assert.AnError
})
assert.Nil(t, ti)
assert.Equal(t, i, 1)
assert.Error(t, err)
assert.Equal(t, err, assert.AnError)
// error != TaskInProgress during task creation
i = 0
e := &task.Error{
LocalizedMethodFault: &types.LocalizedMethodFault{
Fault: &types.RuntimeFault{}, // random fault != TaskInProgress
LocalizedMessage: "random fault",
},
}
ti, err = WaitForResult(ctx, func(_ context.Context) (Task, error) {
i++
return nil, e
})
assert.Nil(t, ti)
assert.Equal(t, i, 1)
assert.Error(t, err)
assert.Equal(t, err, e)
// context cancelled after two retries
i = 0
ctx, cancel := context.WithCancel(ctx)
ti, err = WaitForResult(ctx, func(_ context.Context) (Task, error) {
i++
if i == 2 {
cancel()
}
return nil, taskInProgressFault
})
assert.Nil(t, ti)
assert.Equal(t, i, 2)
assert.Error(t, err)
assert.Equal(t, err, ctx.Err())
// TaskInProgress from task creation for 2 iterations and
// then nil error
tsk := &taskInProgressTask{
max: 1,
info: &types.TaskInfo{
Task: types.ManagedObjectReference{
Type: "task",
Value: "foo",
},
},
}
i = 0
ti, err = WaitForResult(context.Background(), func(_ context.Context) (Task, error) {
i++
if i == 2 {
return tsk, nil
}
return nil, taskInProgressFault
})
assert.Equal(t, tsk.info, ti)
assert.Equal(t, i, 2)
assert.NoError(t, err)
// return TaskInPregress from task.WaitForResult for 2 iterations
// and then return assert.AnError
tsk = &taskInProgressTask{
max: 2,
err: assert.AnError,
info: &types.TaskInfo{
Task: types.ManagedObjectReference{
Type: "task",
Value: "foo",
},
},
}
ti, err = WaitForResult(context.Background(), func(_ context.Context) (Task, error) {
return tsk, nil
})
assert.Equal(t, tsk.info, ti)
assert.Equal(t, tsk.max, tsk.cur)
assert.Error(t, err)
assert.Equal(t, err, tsk.err)
// return TaskInPregress from task.WaitForResult for 2 iterations
// and then return nil error
tsk.cur = 0
tsk.err = nil
ti, err = WaitForResult(context.Background(), func(_ context.Context) (Task, error) {
return tsk, nil
})
assert.Equal(t, tsk.info, ti)
assert.Equal(t, tsk.info, ti)
assert.Equal(t, tsk.cur, tsk.max)
assert.NoError(t, err)
})
}
// faultyVirtualMachine wrap simulator.VirtualMachine with fault injection
type faultyVirtualMachine struct {
simulator.VirtualMachine
fault types.BaseMethodFault
}
// Run implements simulator.TaskRunner and always returns vm.fault
func (vm *faultyVirtualMachine) Run(task *simulator.Task) (types.AnyType, types.BaseMethodFault) {
return nil, vm.fault
}
// Override PowerOffVMTask to inject a fault
func (vm *faultyVirtualMachine) PowerOffVMTask(c *types.PowerOffVM_Task) soap.HasFault {
r := &methods.PowerOffVM_TaskBody{}
task := simulator.NewTask(vm)
r.Res = &types.PowerOffVM_TaskResponse{
Returnval: task.Self,
}
task.Run()
return r
}
// MarkAsTemplate implements a non-Task method to inject vm.fault
func (vm *faultyVirtualMachine) MarkAsTemplate(c *types.MarkAsTemplate) soap.HasFault {
return &methods.MarkAsTemplateBody{
Fault_: simulator.Fault("nope", vm.fault),
}
}
// TestSoapFaults covers the various soap fault checking paths
func TestSoapFaults(t *testing.T) {
op := trace.NewOperation(context.Background(), "TestSoapFaults")
// Nothing VC specific in this test, so we use the simpler ESX model
model := simulator.ESX()
model.Autostart = false
defer model.Remove()
err := model.Create()
if err != nil {
t.Fatal(err)
}
server := model.Service.NewServer()
defer server.Close()
client, err := govmomi.NewClient(op, server.URL, true)
if err != nil {
t.Fatal(err)
}
// Any VM will do
finder := find.NewFinder(client.Client, false)
vm, err := finder.VirtualMachine(op, "/ha-datacenter/vm/*_VM0")
if err != nil {
t.Fatal(err)
}
// Test the success path
err = Wait(op, func(ctx context.Context) (Task, error) {
return vm.PowerOn(ctx)
})
if err != nil {
t.Fatal(err)
}
// Wrap existing vm MO with faultyVirtualMachine
ref := simulator.Map.Get(vm.Reference())
fvm := &faultyVirtualMachine{*ref.(*simulator.VirtualMachine), nil}
simulator.Map.Put(fvm)
// Inject TaskInProgress fault
fvm.fault = new(types.TaskInProgress)
task, err := vm.PowerOff(op)
if err != nil {
t.Fatal(err)
}
// Test the task.Error path
res, err := task.WaitForResult(op, nil)
if !IsRetryError(op, err) {
t.Error(err)
}
// Test the soap.IsVimFault() path
if !IsRetryError(op, soap.WrapVimFault(res.Error.Fault)) {
t.Errorf("fault=%#v", res.Error.Fault)
}
// Test the soap.IsSoapFault() path
err = vm.MarkAsTemplate(op)
if !IsRetryError(op, err) {
t.Error(err)
}
// Test a fault other than TaskInProgress
fvm.fault = &types.QuestionPending{
Text: "now why would you want to do such a thing?",
}
err = Wait(op, func(ctx context.Context) (Task, error) {
return vm.PowerOff(ctx)
})
if err == nil {
t.Error("expected error")
}
if IsRetryError(op, err) {
t.Error(err)
}
// Test with retry
fvm.fault = new(types.TaskInProgress)
called := 0
err = Wait(op, func(ctx context.Context) (Task, error) {
called++
if called > 1 {
simulator.Map.Put(ref) // remove fault injection
}
return vm.PowerOff(ctx)
})
if err != nil {
t.Error(err)
}
if called != 2 {
t.Errorf("called=%d", called)
}
}