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

152
vendor/github.com/vmware/vic/pkg/vsphere/diag/diag.go generated vendored Normal file
View File

@@ -0,0 +1,152 @@
// 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 diag
import (
"bytes"
"context"
"crypto/tls"
"io"
"io/ioutil"
"net/http"
"net/url"
"strings"
"time"
"github.com/vmware/vic/pkg/trace"
)
// StatusCodeFatalThreshold defines a threshold after which all codes can be treated as fatal.
const StatusCodeFatalThreshold = 64
const (
// VCStatusOK vSphere API is available.
VCStatusOK = 0
// VCStatusInvalidURL Provided vSphere API URL is wrong.
VCStatusInvalidURL = 64
// VCStatusErrorQuery Error happened trying to query vSphere API
VCStatusErrorQuery = 65
// VCStatusErrorResponse Received response doesn't contain expected data.
VCStatusErrorResponse = 66
// VCStatusIncorrectResponse Received in case if returned data from server is different from expected.
VCStatusIncorrectResponse = 67
// VCStatusNotXML Received response is not XML
VCStatusNotXML = 68
// VCStatusUnknownHost is returned in case if DNS failed to resolve name.
VCStatusUnknownHost = 69
// VCStatusHostIsNotReachable
VCStatusHostIsNotReachable = 70
)
// UserReadableVCAPITestDescription convert API test code into user readable text
func UserReadableVCAPITestDescription(code int) string {
switch code {
case VCStatusOK:
return "vSphere API target responds as expected"
case VCStatusInvalidURL:
return "vSphere API target url is invalid"
case VCStatusErrorQuery:
return "vSphere API target failed to respond to the query"
case VCStatusIncorrectResponse:
return "vSphere API target returns unexpected response"
case VCStatusErrorResponse:
return "vSphere API target returns error"
case VCStatusNotXML:
return "vSphere API target returns non XML response"
case VCStatusUnknownHost:
return "vSphere API target can not be resolved from VCH"
case VCStatusHostIsNotReachable:
return "vSphere API target is out of reach. Wrong routing table?"
default:
return "vSphere API target test returned unknown code"
}
}
// CheckAPIAvailability accesses vSphere API to ensure it is a correct end point that is up and running.
func CheckAPIAvailability(targetURL string) int {
op := trace.NewOperation(context.Background(), "api test")
errorCode := VCStatusErrorQuery
u, err := url.Parse(targetURL)
if err != nil {
return VCStatusInvalidURL
}
u.Path = "/sdk/vimService.wsdl"
apiURL := u.String()
op.Debugf("Checking access to: %s", apiURL)
for attempts := 5; errorCode != VCStatusOK && attempts > 0; attempts-- {
// #nosec: TLS InsecureSkipVerify set true
c := http.Client{
Transport: &http.Transport{
TLSClientConfig: &tls.Config{
InsecureSkipVerify: true,
},
},
// Is 20 seconds enough to receive any response from vSphere target server?
Timeout: time.Second * 20,
}
errorCode = queryAPI(op, c.Get, apiURL)
}
return errorCode
}
func queryAPI(op trace.Operation, getter func(string) (*http.Response, error), apiURL string) int {
resp, err := getter(apiURL)
if err != nil {
errTxt := err.Error()
op.Errorf("Query error: %s", err)
if strings.Contains(errTxt, "no such host") {
return VCStatusUnknownHost
}
if strings.Contains(errTxt, "no route to host") {
return VCStatusHostIsNotReachable
}
if strings.Contains(errTxt, "host is down") {
return VCStatusHostIsNotReachable
}
return VCStatusErrorQuery
}
data := make([]byte, 65636)
n, err := io.ReadFull(resp.Body, data)
if err != nil && err != io.EOF && err != io.ErrUnexpectedEOF {
op.Errorf("Query error: %s", err)
return VCStatusErrorResponse
}
if n >= len(data) {
// #nosec: Errors unhandled.
io.Copy(ioutil.Discard, resp.Body)
}
// #nosec: Errors unhandled.
resp.Body.Close()
contentType := strings.ToLower(resp.Header.Get("Content-Type"))
if !strings.Contains(contentType, "text/xml") {
op.Errorf("Unexpected content type %s, should be text/xml", contentType)
op.Errorf("Response from the server: %s", string(data))
return VCStatusNotXML
}
// we just want to make sure that response contains something familiar that we could
// use as vSphere API marker.
if !bytes.Contains(data, []byte("urn:vim25Service")) {
op.Errorf("Server response doesn't contain 'urn:vim25Service': %s", string(data))
return VCStatusIncorrectResponse
}
return VCStatusOK
}

View File

@@ -0,0 +1,117 @@
// 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 diag
import (
"bytes"
"context"
"errors"
"net/http"
"testing"
"github.com/stretchr/testify/assert"
"github.com/vmware/vic/pkg/trace"
)
func TestCheckAPIAvailability(t *testing.T) {
assert.Equal(t, VCStatusErrorQuery, CheckAPIAvailability("http://127.0.0.1:65535"))
assert.Equal(t, VCStatusErrorQuery, CheckAPIAvailability("http://127.0.0.1:65536"))
}
func TestCheckAPIAvailabilityQueryWithGetterError(t *testing.T) {
op := trace.NewOperation(context.Background(), "test")
f := func(s string) (*http.Response, error) { return nil, errors.New("wrong query") }
code := queryAPI(op, f, "testurl")
assert.Equal(t, VCStatusErrorQuery, code)
}
type readerWithError struct {
err error
data *bytes.Reader
}
func (r *readerWithError) Read(b []byte) (int, error) {
if r.err != nil {
return 0, r.err
}
return r.data.Read(b)
}
func (r *readerWithError) Close() error {
return r.err
}
func TestCheckAPIAvailabilityQueryReadError(t *testing.T) {
op := trace.NewOperation(context.Background(), "test")
f := func(s string) (*http.Response, error) {
hr := &http.Response{
Body: &readerWithError{
err: errors.New("read error happened"),
},
}
return hr, nil
}
code := queryAPI(op, f, "testurl")
assert.Equal(t, VCStatusErrorResponse, code)
}
func TestCheckAPIAvailabilityQueryIncorrectDataType(t *testing.T) {
op := trace.NewOperation(context.Background(), "test")
f := func(s string) (*http.Response, error) {
hr := &http.Response{
Body: &readerWithError{
data: bytes.NewReader([]byte("some data")),
},
}
return hr, nil
}
code := queryAPI(op, f, "testurl")
assert.Equal(t, VCStatusNotXML, code)
}
func TestCheckAPIAvailabilityQueryIncorrectData(t *testing.T) {
op := trace.NewOperation(context.Background(), "test")
f := func(s string) (*http.Response, error) {
hr := &http.Response{
Body: &readerWithError{
data: bytes.NewReader([]byte("some data")),
},
Header: http.Header{"Content-Type": []string{"text/xml"}},
}
return hr, nil
}
code := queryAPI(op, f, "testurl")
assert.Equal(t, VCStatusIncorrectResponse, code)
}
func TestCheckAPIAvailabilityQueryCorrectData(t *testing.T) {
op := trace.NewOperation(context.Background(), "test")
f := func(s string) (*http.Response, error) {
hr := &http.Response{
Body: &readerWithError{
data: bytes.NewReader([]byte("some urn:vim25Service data")),
},
Header: http.Header{"Content-Type": []string{"text/xml"}},
}
return hr, nil
}
code := queryAPI(op, f, "testurl")
assert.Equal(t, VCStatusOK, code)
}
func TestCheckAPIAvailabilityIncorrectDNSName(t *testing.T) {
assert.Equal(t, VCStatusUnknownHost, CheckAPIAvailability("https://example.notexisting.domain"))
}