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:
23
vendor/github.com/vmware/vic/lib/vspc/config.go
generated
vendored
Normal file
23
vendor/github.com/vmware/vic/lib/vspc/config.go
generated
vendored
Normal file
@@ -0,0 +1,23 @@
|
||||
// Copyright 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 vspc
|
||||
|
||||
var Config Configuration
|
||||
|
||||
// Configuration is a slice of the VCH config that is relevant to the vSPC
|
||||
type Configuration struct {
|
||||
// Turn on debug logging
|
||||
DebugLevel int `vic:"0.1" scope:"read-only" key:"init/diagnostics/debug"`
|
||||
}
|
||||
237
vendor/github.com/vmware/vic/lib/vspc/handlers.go
generated
vendored
Normal file
237
vendor/github.com/vmware/vic/lib/vspc/handlers.go
generated
vendored
Normal file
@@ -0,0 +1,237 @@
|
||||
// Copyright 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 vspc
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"crypto/rand"
|
||||
"fmt"
|
||||
"io"
|
||||
"net"
|
||||
"strings"
|
||||
|
||||
log "github.com/Sirupsen/logrus"
|
||||
|
||||
"github.com/vmware/vic/pkg/telnet"
|
||||
"github.com/vmware/vic/pkg/trace"
|
||||
)
|
||||
|
||||
// handler is the handler struct for the vspc
|
||||
type handler struct {
|
||||
vspc *Vspc
|
||||
}
|
||||
|
||||
func (h *handler) closeHdlr(tc *telnet.Conn) {
|
||||
h.vspc.vmManagerMu.Lock()
|
||||
defer h.vspc.vmManagerMu.Unlock()
|
||||
|
||||
cvm, exists := h.vspc.cvmFromTelnetConnUnlocked(tc)
|
||||
if exists {
|
||||
cvm.Lock()
|
||||
log.Debugf("(vspc) detected closed connection for VM %s", cvm)
|
||||
log.Debugf("(vspc) closing connection to the AttachServer")
|
||||
cvm.remoteConn.Close()
|
||||
log.Debugf("(vspc) deleting vm records from the vm manager %s", cvm)
|
||||
delete(h.vspc.vmManager, cvm.vmUUID)
|
||||
cvm.Unlock()
|
||||
}
|
||||
}
|
||||
|
||||
// dataHdlr is the telnet data handler
|
||||
func (h *handler) dataHdlr(w io.Writer, b []byte, tc *telnet.Conn) {
|
||||
cvm, exists := h.vspc.cvmFromTelnetConn(tc)
|
||||
if !exists {
|
||||
// the fsm will sense the closed connection and perform the necessary cleanup
|
||||
if tc.UnderlyingConnection() != nil {
|
||||
log.Errorln("cannot find a vm associated with this connection.")
|
||||
log.Infoln("closing connection")
|
||||
tc.UnderlyingConnection().Close()
|
||||
}
|
||||
return
|
||||
}
|
||||
cvm.remoteConn.Write(b)
|
||||
|
||||
}
|
||||
|
||||
// CmdHdlr is the telnet command handler
|
||||
func (h *handler) cmdHdlr(w io.Writer, b []byte, tc *telnet.Conn) {
|
||||
switch {
|
||||
case isKnownSuboptions(b):
|
||||
log.Infof("vspc received KNOWN-SUBOPTIONS command")
|
||||
h.knownSuboptions(w, b)
|
||||
case isDoProxy(b):
|
||||
log.Infof("vspc received DO-PROXY command")
|
||||
h.doProxy(w, b)
|
||||
case isVmotionBegin(b):
|
||||
log.Infof("vspc received VMOTION-BEGIN command")
|
||||
h.vmotionBegin(w, tc, b)
|
||||
case isVmotionPeer(b):
|
||||
log.Infof("vspc received VMOTION-PEER command")
|
||||
h.vmotionPeer(w, b)
|
||||
case isVMUUID(b):
|
||||
log.Infof("vspc received VMUUID command")
|
||||
h.cVMUUID(w, tc, b)
|
||||
case isVmotionComplete(b):
|
||||
log.Infof("vspc received VMOTION-COMPLETE command")
|
||||
h.vmotionComplete(w, tc, b)
|
||||
case isVmotionAbort(b):
|
||||
log.Infof("vspc received VMOTION-ABORT command")
|
||||
h.vmotionAbort(w, tc, b)
|
||||
default:
|
||||
// log an error here. this should never happen. all commands should be handled appropriately
|
||||
log.Errorf("(vspc) received unexpected command")
|
||||
}
|
||||
}
|
||||
|
||||
// handleVMUUID handles the telnet vm-uuid response
|
||||
func (h *handler) cVMUUID(w io.Writer, tc *telnet.Conn, b []byte) {
|
||||
defer trace.End(trace.Begin("handling VMUUID"))
|
||||
|
||||
vmuuid := strings.Replace(string(b[3:len(b)-1]), " ", "", -1)
|
||||
log.Infof("vmuuid of the connected containerVM: %s", vmuuid)
|
||||
// check if there exists another vm with the same vmuuid
|
||||
cvm, exists := h.vspc.cVM(vmuuid)
|
||||
if !exists || !cvm.isInVMotion() {
|
||||
// create a new vm associated with this telnet connection
|
||||
cvm = newCVM(tc)
|
||||
log.Infof("attempting to connect to the attach server")
|
||||
remoteConn, err := net.Dial("tcp", fmt.Sprintf("%s:%d", h.vspc.attachSrvrAddr, h.vspc.attachSrvrPort))
|
||||
if err != nil {
|
||||
log.Errorf("cannot connect to the attach-server: %v", err)
|
||||
}
|
||||
cvm.remoteConn = remoteConn
|
||||
cvm.vmUUID = vmuuid
|
||||
h.vspc.addCVM(vmuuid, cvm)
|
||||
// relay Reads from the remote system connection to the telnet connection associated with this vm
|
||||
go h.vspc.relayReads(cvm, remoteConn)
|
||||
} else { //the vm existed before and was shut down or vmotioned
|
||||
log.Infof("established second serial-port telnet connection with vm (vmuuid: %s)", cvm.vmUUID)
|
||||
cvm.Lock()
|
||||
defer cvm.Unlock()
|
||||
cvm.prevContainerConn = cvm.containerConn
|
||||
cvm.containerConn = tc
|
||||
}
|
||||
}
|
||||
|
||||
// handleKnownSuboptions handles the known suboptions telnet command
|
||||
func (h *handler) knownSuboptions(w io.Writer, b []byte) {
|
||||
defer trace.End(trace.Begin("handling KNOWN-SUBOPTIONS"))
|
||||
|
||||
var resp []byte
|
||||
suboptions := b[3 : len(b)-1]
|
||||
resp = append(resp, []byte{telnet.Iac, telnet.Sb, VmwareExt, KnownSuboptions2}...)
|
||||
resp = append(resp, suboptions...)
|
||||
resp = append(resp, telnet.Iac, telnet.Se)
|
||||
log.Debugf("response to KNOWN-SUBOPTIONS: %v", resp)
|
||||
|
||||
if bytes.IndexByte(suboptions, GetVMVCUUID) != -1 && bytes.IndexByte(suboptions, VMVCUUID) != -1 {
|
||||
resp = append(resp, getVMUUID()...)
|
||||
}
|
||||
w.Write(resp)
|
||||
}
|
||||
|
||||
// handleDoProxy handles the DO-PROXY telnet command
|
||||
func (h *handler) doProxy(w io.Writer, b []byte) {
|
||||
defer trace.End(trace.Begin("handling DO-PROXY"))
|
||||
|
||||
var resp []byte
|
||||
resp = append(resp, []byte{telnet.Iac, telnet.Sb, VmwareExt, WillProxy, telnet.Iac, telnet.Se}...)
|
||||
log.Debugf("response to DO-PROXY: %v", resp)
|
||||
w.Write(resp)
|
||||
}
|
||||
|
||||
// handleVmotionBegin handles the VMOTION-BEGIN telnet command
|
||||
func (h *handler) vmotionBegin(w io.Writer, tc *telnet.Conn, b []byte) {
|
||||
defer trace.End(trace.Begin("handling VMOTION-BEGIN"))
|
||||
|
||||
if cvm, exists := h.vspc.cvmFromTelnetConn(tc); exists {
|
||||
cvm.Lock()
|
||||
cvm.inVmotion = true
|
||||
cvm.Unlock()
|
||||
ch := make(chan struct{})
|
||||
cvm.vmotionStartedChan <- ch
|
||||
<-ch
|
||||
}
|
||||
seq := b[3 : len(b)-1]
|
||||
var escapedSeq []byte
|
||||
for _, v := range seq {
|
||||
if v == telnet.Iac {
|
||||
escapedSeq = append(escapedSeq, telnet.Iac)
|
||||
}
|
||||
escapedSeq = append(escapedSeq, v)
|
||||
}
|
||||
secret := make([]byte, 4)
|
||||
var escapedSecret []byte
|
||||
rand.Read(secret)
|
||||
// escaping Iac
|
||||
for _, v := range secret {
|
||||
if v == telnet.Iac {
|
||||
escapedSecret = append(escapedSecret, telnet.Iac)
|
||||
}
|
||||
escapedSecret = append(escapedSecret, v)
|
||||
}
|
||||
var resp []byte
|
||||
resp = append(resp, []byte{telnet.Iac, telnet.Sb, VmwareExt, VmotionGoahead}...)
|
||||
resp = append(resp, escapedSeq...)
|
||||
resp = append(resp, escapedSecret...)
|
||||
resp = append(resp, telnet.Iac, telnet.Se)
|
||||
log.Debugf("response to VMOTION-BEGIN: %v", resp)
|
||||
w.Write(resp)
|
||||
}
|
||||
|
||||
// handleVmotionPeer handles the VMOTION-PEER telnet command
|
||||
func (h *handler) vmotionPeer(w io.Writer, b []byte) {
|
||||
defer trace.End(trace.Begin("handling VMOTION-PEER"))
|
||||
|
||||
// cookie is the sequence + secret
|
||||
cookie := b[3 : len(b)-1]
|
||||
var resp []byte
|
||||
resp = append(resp, []byte{telnet.Iac, telnet.Sb, VmwareExt, VmotionPeerOK}...)
|
||||
resp = append(resp, cookie...)
|
||||
resp = append(resp, telnet.Iac, telnet.Se)
|
||||
log.Debugf("response to VMOTION-PEER: %v", resp)
|
||||
w.Write(resp)
|
||||
}
|
||||
|
||||
// handleVmotionComplete handles the VMOTION-Complete telnet command
|
||||
func (h *handler) vmotionComplete(w io.Writer, tc *telnet.Conn, b []byte) {
|
||||
defer trace.End(trace.Begin("handling VMOTION-COMPLETE"))
|
||||
|
||||
if cvm, exists := h.vspc.cvmFromTelnetConn(tc); exists {
|
||||
cvm.Lock()
|
||||
cvm.prevContainerConn = nil
|
||||
cvm.inVmotion = false
|
||||
cvm.Unlock()
|
||||
ch := make(chan struct{})
|
||||
cvm.vmotionCompletedChan <- ch
|
||||
<-ch
|
||||
log.Info("vMotion completed successfully")
|
||||
} else {
|
||||
log.Errorf("couldnt find previous information of vm after vmotion (vmuuid: %s)", cvm.vmUUID)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// handleVmotionAbort handles the VMOTION-abort telnet command
|
||||
func (h *handler) vmotionAbort(w io.Writer, tc *telnet.Conn, b []byte) {
|
||||
defer trace.End(trace.Begin("handling VMOTION-ABORT"))
|
||||
|
||||
log.Errorf("vMotion failed")
|
||||
if cvm, exists := h.vspc.cvmFromTelnetConn(tc); exists {
|
||||
cvm.Lock()
|
||||
cvm.inVmotion = false
|
||||
cvm.Unlock()
|
||||
}
|
||||
}
|
||||
99
vendor/github.com/vmware/vic/lib/vspc/helpers.go
generated
vendored
Normal file
99
vendor/github.com/vmware/vic/lib/vspc/helpers.go
generated
vendored
Normal file
@@ -0,0 +1,99 @@
|
||||
// Copyright 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 vspc
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net"
|
||||
|
||||
"github.com/vmware/vic/lib/constants"
|
||||
"github.com/vmware/vic/pkg/telnet"
|
||||
)
|
||||
|
||||
func isKnownSuboptions(cmd []byte) bool {
|
||||
if len(cmd) < 3 {
|
||||
return false
|
||||
}
|
||||
return cmd[0] == telnet.Sb && cmd[1] == VmwareExt && cmd[2] == KnownSuboptions1
|
||||
}
|
||||
|
||||
func isDoProxy(cmd []byte) bool {
|
||||
if len(cmd) < 3 {
|
||||
return false
|
||||
}
|
||||
return cmd[0] == telnet.Sb && cmd[1] == VmwareExt && cmd[2] == DoProxy
|
||||
}
|
||||
|
||||
func isVmotionBegin(cmd []byte) bool {
|
||||
if len(cmd) < 3 {
|
||||
return false
|
||||
}
|
||||
return cmd[0] == telnet.Sb && cmd[1] == VmwareExt && cmd[2] == VmotionBegin
|
||||
}
|
||||
|
||||
func isVmotionPeer(cmd []byte) bool {
|
||||
if len(cmd) < 3 {
|
||||
return false
|
||||
}
|
||||
return cmd[0] == telnet.Sb && cmd[1] == VmwareExt && cmd[2] == VmotionPeer
|
||||
}
|
||||
|
||||
func isVmotionComplete(cmd []byte) bool {
|
||||
if len(cmd) < 3 {
|
||||
return false
|
||||
}
|
||||
return cmd[0] == telnet.Sb && cmd[1] == VmwareExt && cmd[2] == VmotionComplete
|
||||
}
|
||||
|
||||
func isVmotionAbort(cmd []byte) bool {
|
||||
if len(cmd) < 3 {
|
||||
return false
|
||||
}
|
||||
return cmd[0] == telnet.Sb && cmd[1] == VmwareExt && cmd[2] == VmotionAbort
|
||||
}
|
||||
|
||||
func isVMName(cmd []byte) bool {
|
||||
if len(cmd) < 3 {
|
||||
return false
|
||||
}
|
||||
return cmd[0] == telnet.Sb && cmd[1] == VmwareExt && cmd[2] == VMName
|
||||
}
|
||||
|
||||
func isVMUUID(cmd []byte) bool {
|
||||
if len(cmd) < 3 {
|
||||
return false
|
||||
}
|
||||
return cmd[0] == telnet.Sb && cmd[1] == VmwareExt && cmd[2] == VMVCUUID
|
||||
}
|
||||
|
||||
func getVMUUID() []byte {
|
||||
return []byte{telnet.Iac, telnet.Sb, VmwareExt, GetVMVCUUID, telnet.Iac, telnet.Se}
|
||||
}
|
||||
|
||||
func lookupVCHIP() (net.IP, error) {
|
||||
ips, err := net.LookupIP(constants.ManagementHostName)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if len(ips) == 0 {
|
||||
return nil, fmt.Errorf("no ip found on %s", constants.ManagementHostName)
|
||||
}
|
||||
|
||||
if len(ips) > 1 {
|
||||
return nil, fmt.Errorf("multiple ips found on %s: %#v", constants.ManagementHostName, ips)
|
||||
}
|
||||
return ips[0], nil
|
||||
}
|
||||
275
vendor/github.com/vmware/vic/lib/vspc/vspc.go
generated
vendored
Normal file
275
vendor/github.com/vmware/vic/lib/vspc/vspc.go
generated
vendored
Normal file
@@ -0,0 +1,275 @@
|
||||
// Copyright 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 vspc
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"net"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
log "github.com/Sirupsen/logrus"
|
||||
|
||||
"github.com/vmware/vic/lib/constants"
|
||||
"github.com/vmware/vic/pkg/telnet"
|
||||
"github.com/vmware/vic/pkg/trace"
|
||||
"github.com/vmware/vic/pkg/vsphere/extraconfig"
|
||||
)
|
||||
|
||||
/* Vsphere telnet extension constants */
|
||||
const (
|
||||
VmwareExt byte = 232
|
||||
KnownSuboptions1 byte = 0
|
||||
KnownSuboptions2 byte = 1
|
||||
UnknownSuboptionRcvd1 byte = 2
|
||||
UnknownSuboptionRcvd2 byte = 3
|
||||
VmotionBegin byte = 40
|
||||
VmotionGoahead byte = 41
|
||||
VmotionNotNow byte = 43
|
||||
VmotionPeer byte = 44
|
||||
VmotionPeerOK byte = 45
|
||||
VmotionComplete byte = 46
|
||||
VmotionAbort byte = 48
|
||||
DoProxy byte = 70
|
||||
WillProxy byte = 71
|
||||
WontProxy byte = 73
|
||||
VMVCUUID byte = 80
|
||||
GetVMVCUUID byte = 81
|
||||
VMName byte = 82
|
||||
GetVMName byte = 83
|
||||
VMBiosUUID byte = 84
|
||||
GetVMBiosUUID byte = 85
|
||||
VMLocationUUID byte = 86
|
||||
GetsVMLocationUUID byte = 87
|
||||
vspcPort int = 2377
|
||||
)
|
||||
const remoteConnReadDeadline = 1 * time.Second
|
||||
|
||||
// cVM is a struct that represents the state of the containerVM
|
||||
type cVM struct {
|
||||
sync.Mutex
|
||||
// this is the current connection between the vspc and the containerVM serial port
|
||||
containerConn *telnet.Conn
|
||||
// in case of vmotion, at some point we will have two telnet connections to the containerVM
|
||||
// prevContainerConn is the connection of the source
|
||||
// containerConn will be the connection to the destination
|
||||
prevContainerConn *telnet.Conn
|
||||
// inVmotion is a boolean denoting whether this VM is in a state of vmotion or not
|
||||
inVmotion bool
|
||||
|
||||
// remoteConn is the remote-system connection
|
||||
// this is the connection between the vspc and the attach-server
|
||||
remoteConn net.Conn
|
||||
|
||||
vmUUID string
|
||||
vmotionStartedChan chan chan struct{}
|
||||
vmotionCompletedChan chan chan struct{}
|
||||
}
|
||||
|
||||
// newCVM is the constructor of the VM
|
||||
func newCVM(tc *telnet.Conn) *cVM {
|
||||
return &cVM{
|
||||
containerConn: tc,
|
||||
inVmotion: false,
|
||||
vmotionStartedChan: make(chan chan struct{}),
|
||||
vmotionCompletedChan: make(chan chan struct{}),
|
||||
}
|
||||
}
|
||||
|
||||
func (cvm *cVM) isInVMotion() bool {
|
||||
cvm.Lock()
|
||||
defer cvm.Unlock()
|
||||
return cvm.inVmotion
|
||||
}
|
||||
|
||||
func (cvm *cVM) String() string {
|
||||
return cvm.vmUUID
|
||||
}
|
||||
|
||||
// Vspc is all the vspc singletons
|
||||
type Vspc struct {
|
||||
vmManagerMu sync.Mutex
|
||||
vmManager map[string]*cVM
|
||||
|
||||
*telnet.Server
|
||||
|
||||
attachSrvrAddr string
|
||||
attachSrvrPort uint
|
||||
|
||||
doneCh chan bool
|
||||
|
||||
verbose bool
|
||||
}
|
||||
|
||||
// NewVspc is the constructor
|
||||
func NewVspc() *Vspc {
|
||||
defer trace.End(trace.Begin("new vspc"))
|
||||
|
||||
vchIP, err := lookupVCHIP()
|
||||
if err != nil {
|
||||
log.Fatalf("cannot retrieve vch-endpoint ip: %v", err)
|
||||
}
|
||||
address := vchIP.String()
|
||||
port := constants.SerialOverLANPort
|
||||
|
||||
vspc := &Vspc{
|
||||
vmManager: make(map[string]*cVM),
|
||||
|
||||
attachSrvrAddr: "127.0.0.1",
|
||||
attachSrvrPort: constants.AttachServerPort,
|
||||
|
||||
doneCh: make(chan bool),
|
||||
}
|
||||
hdlr := handler{vspc}
|
||||
opts := telnet.ServerOpts{
|
||||
Addr: fmt.Sprintf("%s:%d", address, port),
|
||||
ServerOpts: []byte{telnet.Binary, telnet.Sga, telnet.Echo},
|
||||
ClientOpts: []byte{telnet.Binary, telnet.Sga, VmwareExt},
|
||||
Handlers: telnet.Handlers{
|
||||
DataHandler: hdlr.dataHdlr,
|
||||
CmdHandler: hdlr.cmdHdlr,
|
||||
CloseHandler: hdlr.closeHdlr,
|
||||
},
|
||||
}
|
||||
vspc.Server = telnet.NewServer(opts)
|
||||
vspc.verbose = false
|
||||
|
||||
// load the vchconfig to get debug level
|
||||
if src, err := extraconfig.GuestInfoSource(); err == nil {
|
||||
extraconfig.Decode(src, &Config)
|
||||
if Config.DebugLevel > 2 {
|
||||
vspc.verbose = true
|
||||
}
|
||||
}
|
||||
|
||||
return vspc
|
||||
}
|
||||
|
||||
// Start starts the vspc server
|
||||
func (vspc *Vspc) Start() {
|
||||
defer trace.End(trace.Begin("start vspc"))
|
||||
|
||||
go func() {
|
||||
for {
|
||||
_, err := vspc.Accept()
|
||||
if err != nil {
|
||||
log.Errorf("vSPC cannot accept connections: %v", err)
|
||||
log.Errorf("vSPC exiting...")
|
||||
return
|
||||
}
|
||||
}
|
||||
}()
|
||||
log.Infof("vSPC started...")
|
||||
}
|
||||
|
||||
// Stop stops the vspc server
|
||||
func (vspc *Vspc) Stop() {
|
||||
defer trace.End(trace.Begin("stop vspc"))
|
||||
|
||||
vspc.doneCh <- true
|
||||
}
|
||||
|
||||
// cVM returns the VM struct from its uuid
|
||||
func (vspc *Vspc) cVM(uuid string) (*cVM, bool) {
|
||||
vspc.vmManagerMu.Lock()
|
||||
defer vspc.vmManagerMu.Unlock()
|
||||
|
||||
if vm, ok := vspc.vmManager[uuid]; ok {
|
||||
return vm, true
|
||||
}
|
||||
return nil, false
|
||||
}
|
||||
|
||||
// addVM adds a VM to the map
|
||||
func (vspc *Vspc) addCVM(uuid string, cvm *cVM) {
|
||||
vspc.vmManagerMu.Lock()
|
||||
defer vspc.vmManagerMu.Unlock()
|
||||
|
||||
vspc.vmManager[uuid] = cvm
|
||||
}
|
||||
|
||||
// relayReads reads from the AttachServer connection and relays the data to the telnet connection
|
||||
func (vspc *Vspc) relayReads(containervm *cVM, conn net.Conn) {
|
||||
vmotion := false
|
||||
var tmpBuf bytes.Buffer
|
||||
for {
|
||||
select {
|
||||
case ch := <-containervm.vmotionStartedChan:
|
||||
vmotion = true
|
||||
ch <- struct{}{}
|
||||
log.Debugf("vspc started to buffer data coming from the remote system")
|
||||
case ch := <-containervm.vmotionCompletedChan:
|
||||
vmotion = false
|
||||
ch <- struct{}{}
|
||||
log.Debugf("vspc stopped buffering data coming from the remote system")
|
||||
default:
|
||||
b := make([]byte, 4096)
|
||||
conn.SetReadDeadline(time.Now().Add(remoteConnReadDeadline))
|
||||
var n int
|
||||
var err error
|
||||
if n, err = conn.Read(b); n > 0 {
|
||||
if vspc.verbose {
|
||||
log.Debugf("vspc read %d bytes from the remote system connection", n)
|
||||
}
|
||||
if !vmotion {
|
||||
if tmpBuf.Len() > 0 {
|
||||
buf := tmpBuf.Bytes()
|
||||
tmpBuf.Reset()
|
||||
if err != nil {
|
||||
log.Errorf("read error from vspc temporary buffer: %v", err)
|
||||
}
|
||||
log.Debugf("vspc writing buffered data during vmotion to the containerVM")
|
||||
if n, err := containervm.containerConn.WriteData(buf); n == -1 {
|
||||
log.Errorf("vspc: RelayReads: %v", err)
|
||||
return
|
||||
}
|
||||
}
|
||||
if n, err := containervm.containerConn.WriteData(b[:n]); n == -1 {
|
||||
log.Errorf("vspc: RelayReads: %v", err)
|
||||
return
|
||||
}
|
||||
} else {
|
||||
tmpBuf.Write(b[:n])
|
||||
}
|
||||
}
|
||||
// if not timeout error exit this goroutine because connection is closed
|
||||
if err != nil {
|
||||
if err, ok := err.(net.Error); !ok || !err.Timeout() {
|
||||
log.Infof("(vspc) remote system connection closed: %v", err)
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (vspc *Vspc) cvmFromTelnetConn(tc *telnet.Conn) (*cVM, bool) {
|
||||
vspc.vmManagerMu.Lock()
|
||||
defer vspc.vmManagerMu.Unlock()
|
||||
|
||||
return vspc.cvmFromTelnetConnUnlocked(tc)
|
||||
}
|
||||
|
||||
// cvmFromTelnetConnUnlocked is the unlocked version of cvmFromTelnetConn
|
||||
// it expects caller to hold the lock
|
||||
func (vspc *Vspc) cvmFromTelnetConnUnlocked(tc *telnet.Conn) (*cVM, bool) {
|
||||
for _, v := range vspc.vmManager {
|
||||
if v.containerConn == tc {
|
||||
return v, true
|
||||
}
|
||||
}
|
||||
return nil, false
|
||||
}
|
||||
90
vendor/github.com/vmware/vic/lib/vspc/vspc_test.go
generated
vendored
Normal file
90
vendor/github.com/vmware/vic/lib/vspc/vspc_test.go
generated
vendored
Normal file
@@ -0,0 +1,90 @@
|
||||
// Copyright 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 vspc
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
|
||||
"github.com/vmware/vic/pkg/telnet"
|
||||
)
|
||||
|
||||
type dummyWriter struct {
|
||||
buf []byte
|
||||
}
|
||||
|
||||
func (w *dummyWriter) Write(b []byte) (int, error) {
|
||||
w.buf = b
|
||||
return len(b), nil
|
||||
}
|
||||
|
||||
func newTestItem() *Vspc {
|
||||
return &Vspc{
|
||||
vmManager: make(map[string]*cVM),
|
||||
doneCh: make(chan bool),
|
||||
}
|
||||
}
|
||||
|
||||
func TestAddCVM(t *testing.T) {
|
||||
vspc := newTestItem()
|
||||
cvm := newCVM(nil)
|
||||
assert.NotNil(t, cvm)
|
||||
vspc.addCVM("dummyid", cvm)
|
||||
assert.Equal(t, 1, len(vspc.vmManager))
|
||||
for k, v := range vspc.vmManager {
|
||||
assert.Equal(t, "dummyid", k)
|
||||
assert.Equal(t, cvm, v)
|
||||
}
|
||||
}
|
||||
|
||||
func TestGetCVM(t *testing.T) {
|
||||
vspc := newTestItem()
|
||||
cvm := newCVM(nil)
|
||||
assert.NotNil(t, cvm)
|
||||
vspc.addCVM("dummyid", cvm)
|
||||
storedCVM, exists := vspc.cVM("dummyid")
|
||||
assert.Equal(t, true, exists)
|
||||
assert.Equal(t, cvm, storedCVM)
|
||||
storedCVM, exists = vspc.cVM("dummyidnothere")
|
||||
assert.Equal(t, false, exists)
|
||||
assert.Nil(t, storedCVM)
|
||||
}
|
||||
|
||||
func TestNewVMStartsWithCorrectVmotion(t *testing.T) {
|
||||
vm := newCVM(nil)
|
||||
assert.Equal(t, vm.inVmotion, false)
|
||||
}
|
||||
|
||||
func TestHandleSubOptions(t *testing.T) {
|
||||
h := &handler{newTestItem()}
|
||||
w := &dummyWriter{}
|
||||
h.cmdHdlr(w, []byte{telnet.Sb, VmwareExt, KnownSuboptions1, 4, 5, 6, telnet.Se}, nil)
|
||||
assert.Equal(t, []byte{telnet.Iac, telnet.Sb, VmwareExt, KnownSuboptions2, 4, 5, 6, telnet.Iac, telnet.Se}, w.buf)
|
||||
}
|
||||
|
||||
func TestHandleDoProxy(t *testing.T) {
|
||||
h := &handler{newTestItem()}
|
||||
w := &dummyWriter{}
|
||||
h.cmdHdlr(w, []byte{telnet.Sb, VmwareExt, DoProxy, telnet.Se}, nil)
|
||||
assert.Equal(t, []byte{telnet.Iac, telnet.Sb, VmwareExt, WillProxy, telnet.Iac, telnet.Se}, w.buf)
|
||||
}
|
||||
|
||||
func TestHandleVmotionPeer(t *testing.T) {
|
||||
h := &handler{newTestItem()}
|
||||
w := &dummyWriter{}
|
||||
h.cmdHdlr(w, []byte{telnet.Sb, VmwareExt, VmotionPeer, telnet.Se}, nil)
|
||||
assert.Equal(t, []byte{telnet.Iac, telnet.Sb, VmwareExt, VmotionPeerOK, telnet.Iac, telnet.Se}, w.buf)
|
||||
}
|
||||
Reference in New Issue
Block a user