-
Notifications
You must be signed in to change notification settings - Fork 24
/
client.go
181 lines (152 loc) · 4.55 KB
/
client.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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
/*
Copyright 2020 VMware, Inc.
SPDX-License-Identifier: Apache-2.0
*/
package vsphere
import (
"context"
"errors"
"io/ioutil"
"net/http"
"net/url"
"path/filepath"
"time"
"github.com/kelseyhightower/envconfig"
"github.com/vmware/govmomi"
"github.com/vmware/govmomi/session"
"github.com/vmware/govmomi/session/keepalive"
"github.com/vmware/govmomi/vapi/rest"
"github.com/vmware/govmomi/vim25"
"github.com/vmware/govmomi/vim25/methods"
"github.com/vmware/govmomi/vim25/soap"
"knative.dev/pkg/logging"
corev1 "k8s.io/api/core/v1"
)
const (
VolumeName = "vsphere-binding"
DefaultMountPath = "/var/bindings/vsphere" // filepath.Join isn't const.
keepaliveInterval = 5 * time.Minute // vCenter APIs keep-alive
)
type EnvConfig struct {
Insecure bool `envconfig:"VC_INSECURE" default:"false"`
Address string `envconfig:"VC_URL" required:"true"`
SecretPath string `envconfig:"VC_SECRET_PATH" default:""`
}
// ReadKey reads the key from the secret.
func ReadKey(key string) (string, error) {
var env EnvConfig
if err := envconfig.Process("", &env); err != nil {
return "", err
}
mountPath := DefaultMountPath
if env.SecretPath != "" {
mountPath = env.SecretPath
}
data, err := ioutil.ReadFile(filepath.Join(mountPath, key))
if err != nil {
return "", err
}
return string(data), nil
}
// NewSOAPClient returns a vCenter SOAP API client with active keep-alive. Use
// Logout() to release resources and perform a clean logout from vCenter.
func NewSOAPClient(ctx context.Context) (*govmomi.Client, error) {
var env EnvConfig
if err := envconfig.Process("", &env); err != nil {
return nil, err
}
parsedURL, err := soap.ParseURL(env.Address)
if err != nil {
return nil, err
}
// Read the username and password from the filesystem.
username, err := ReadKey(corev1.BasicAuthUsernameKey)
if err != nil {
return nil, err
}
password, err := ReadKey(corev1.BasicAuthPasswordKey)
if err != nil {
return nil, err
}
parsedURL.User = url.UserPassword(username, password)
return soapWithKeepalive(ctx, parsedURL, env.Insecure)
}
func soapWithKeepalive(ctx context.Context, url *url.URL, insecure bool) (*govmomi.Client, error) {
soapClient := soap.NewClient(url, insecure)
vimClient, err := vim25.NewClient(ctx, soapClient)
if err != nil {
return nil, err
}
vimClient.RoundTripper = keepalive.NewHandlerSOAP(vimClient.RoundTripper, keepaliveInterval, soapKeepAliveHandler(ctx, vimClient))
// explicitly create session to activate keep-alive handler via Login
m := session.NewManager(vimClient)
err = m.Login(ctx, url.User)
if err != nil {
return nil, err
}
c := govmomi.Client{
Client: vimClient,
SessionManager: m,
}
return &c, nil
}
func soapKeepAliveHandler(ctx context.Context, c *vim25.Client) func() error {
logger := logging.FromContext(ctx).With("rpc", "keepalive")
return func() error {
logger.Info("Executing SOAP keep-alive handler")
t, err := methods.GetCurrentTime(ctx, c)
if err != nil {
return err
}
logger.Infof("vCenter current time: %s", t.String())
return nil
}
}
// NewRESTClient returns a vCenter REST API client with active keep-alive. Use
// Logout() to release resources and perform a clean logout from vCenter.
func NewRESTClient(ctx context.Context) (*rest.Client, error) {
var env EnvConfig
if err := envconfig.Process("", &env); err != nil {
return nil, err
}
parsedURL, err := soap.ParseURL(env.Address)
if err != nil {
return nil, err
}
// Read the username and password from the filesystem.
username, err := ReadKey(corev1.BasicAuthUsernameKey)
if err != nil {
return nil, err
}
password, err := ReadKey(corev1.BasicAuthPasswordKey)
if err != nil {
return nil, err
}
parsedURL.User = url.UserPassword(username, password)
soapclient, err := soapWithKeepalive(ctx, parsedURL, env.Insecure)
if err != nil {
return nil, err
}
restclient := rest.NewClient(soapclient.Client)
restclient.Transport = keepalive.NewHandlerREST(restclient, keepaliveInterval, restKeepAliveHandler(ctx, restclient))
// Login activates the keep-alive handler
if err := restclient.Login(ctx, parsedURL.User); err != nil {
return nil, err
}
return restclient, nil
}
func restKeepAliveHandler(ctx context.Context, restclient *rest.Client) func() error {
logger := logging.FromContext(ctx).With("rpc", "keepalive")
return func() error {
logger.Info("Executing REST keep-alive handler")
ctx := context.Background()
s, err := restclient.Session(ctx)
if err != nil {
return err
}
if s != nil {
return nil
}
return errors.New(http.StatusText(http.StatusUnauthorized))
}
}