-
Notifications
You must be signed in to change notification settings - Fork 181
/
connection.go
100 lines (87 loc) · 3.01 KB
/
connection.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
/*
Copyright 2019 The Kubernetes Authors.
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 e2e
import (
"context"
"fmt"
neturl "net/url"
"sync"
gomega "github.com/onsi/gomega"
"github.com/vmware/govmomi"
"github.com/vmware/govmomi/session"
"github.com/vmware/govmomi/vim25"
"github.com/vmware/govmomi/vim25/soap"
vimtypes "github.com/vmware/govmomi/vim25/types"
e2elog "k8s.io/kubernetes/test/e2e/framework"
)
type cnsClient struct {
*soap.Client
}
const (
vsanNamespace = "vsan"
vsanHealthPath = "/vsanHealth"
roundTripperDefaultCount = 3
)
var (
clientMutex sync.Mutex
cnsVolumeManagerInstance = vimtypes.ManagedObjectReference{
Type: "CnsVolumeManager",
Value: "cns-volume-manager",
}
clientLock sync.Mutex
)
// connect helps make a connection to vCenter Server
// No actions are taken if a connection exists and alive. Otherwise, a new client will be created.
func connect(ctx context.Context, vs *vSphere) {
clientLock.Lock()
var err error
defer clientLock.Unlock()
if vs.Client == nil {
vs.Client = newClient(ctx, vs)
}
manager := session.NewManager(vs.Client.Client)
userSession, err := manager.UserSession(ctx)
gomega.Expect(err).NotTo(gomega.HaveOccurred())
if userSession != nil {
return
}
e2elog.Logf("Creating new client session since the existing session is not valid or not authenticated")
vs.Client.Logout(ctx)
vs.Client = newClient(ctx, vs)
}
// newClient creates a new client for vSphere connection
func newClient(ctx context.Context, vs *vSphere) *govmomi.Client {
url, err := neturl.Parse(fmt.Sprintf("https://%s:%s/sdk", vs.Config.Global.VCenterHostname, vs.Config.Global.VCenterPort))
gomega.Expect(err).NotTo(gomega.HaveOccurred())
url.User = neturl.UserPassword(vs.Config.Global.User, vs.Config.Global.Password)
client, err := govmomi.NewClient(ctx, url, true)
gomega.Expect(err).NotTo(gomega.HaveOccurred())
client.RoundTripper = vim25.Retry(client.RoundTripper, vim25.TemporaryNetworkError(roundTripperDefaultCount))
return client
}
// newCnsClient creates a new CNS client
func newCnsClient(ctx context.Context, c *vim25.Client) (*cnsClient, error) {
sc := c.Client.NewServiceClient(vsanHealthPath, vsanNamespace)
return &cnsClient{sc}, nil
}
// connectCns creates a CNS client for the virtual center.
func connectCns(ctx context.Context, vs *vSphere) error {
var err error
clientMutex.Lock()
defer clientMutex.Unlock()
if vs.CnsClient == nil {
vs.CnsClient, err = newCnsClient(ctx, vs.Client.Client)
gomega.Expect(err).NotTo(gomega.HaveOccurred())
}
return nil
}