Skip to content

Commit c187643

Browse files
committed
Implement vc session manager on vc client
1 parent d278279 commit c187643

File tree

3 files changed

+42
-12
lines changed

3 files changed

+42
-12
lines changed

pkg/common/connectionmanager/connectionmanager.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ func (connMgr *ConnectionManager) Connect(ctx context.Context, vcInstance *VSphe
161161
klog.Error("Failed to get credentials from Secret Credential Manager with err:", err)
162162
return err
163163
}
164-
vcInstance.Conn.UpdateCredentials(credentials.User, credentials.Password)
164+
vcInstance.Conn.UpdateCredentials(credentials.User, credentials.Password, credentials.VCSessionManagerURL, credentials.VCSessionManagerToken)
165165
return vcInstance.Conn.Connect(ctx)
166166
}
167167

pkg/common/connectionmanager/zones.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -292,6 +292,11 @@ func (cm *ConnectionManager) getDIFromMultiVCorDC(ctx context.Context,
292292

293293
func withTagsClient(ctx context.Context, connection *vclib.VSphereConnection, f func(c *rest.Client) error) error {
294294
c := rest.NewClient(connection.Client)
295+
if connection.SessionManagerURL != "" && connection.SessionManagerToken != "" {
296+
c.SessionID(connection.Client.SessionCookie().Value)
297+
return nil
298+
}
299+
295300
signer, err := connection.Signer(ctx, connection.Client)
296301
if err != nil {
297302
return err
@@ -307,6 +312,11 @@ func withTagsClient(ctx context.Context, connection *vclib.VSphereConnection, f
307312
}
308313

309314
defer func() {
315+
// When using shared session manager we don't need to logout
316+
if connection.SessionManagerURL != "" && connection.SessionManagerToken != "" {
317+
return
318+
}
319+
310320
if err := c.Logout(ctx); err != nil {
311321
klog.Errorf("failed to logout: %v", err)
312322
}

pkg/common/vclib/connection.go

Lines changed: 31 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -37,16 +37,18 @@ const (
3737

3838
// VSphereConnection contains information for connecting to vCenter
3939
type VSphereConnection struct {
40-
Client *vim25.Client
41-
Username string
42-
Password string
43-
Hostname string
44-
Port string
45-
CACert string
46-
Thumbprint string
47-
Insecure bool
48-
RoundTripperCount uint
49-
credentialsLock sync.Mutex
40+
Client *vim25.Client
41+
Username string
42+
Password string
43+
Hostname string
44+
Port string
45+
CACert string
46+
Thumbprint string
47+
Insecure bool
48+
SessionManagerURL string
49+
SessionManagerToken string
50+
RoundTripperCount uint
51+
credentialsLock sync.Mutex
5052
}
5153

5254
var (
@@ -132,6 +134,22 @@ func (connection *VSphereConnection) login(ctx context.Context, client *vim25.Cl
132134
connection.credentialsLock.Lock()
133135
defer connection.credentialsLock.Unlock()
134136

137+
if connection.SessionManagerURL != "" && connection.SessionManagerToken != "" {
138+
token, err := GetSharedToken(ctx, SharedTokenOptions{
139+
URL: connection.SessionManagerURL,
140+
Token: connection.SessionManagerToken,
141+
})
142+
if err != nil {
143+
klog.Errorf("error getting shared session token: %s", err)
144+
return err
145+
}
146+
if err := m.CloneSession(ctx, token); err != nil {
147+
klog.Errorf("error getting shared cloned session token: %s", err)
148+
return err
149+
}
150+
return nil
151+
}
152+
135153
signer, err := connection.Signer(ctx, client)
136154
if err != nil {
137155
return err
@@ -196,9 +214,11 @@ func (connection *VSphereConnection) NewClient(ctx context.Context) (*vim25.Clie
196214

197215
// UpdateCredentials updates username and password.
198216
// Note: Updated username and password will be used when there is no session active
199-
func (connection *VSphereConnection) UpdateCredentials(username string, password string) {
217+
func (connection *VSphereConnection) UpdateCredentials(username string, password string, sessionmgrURL string, sessionmgrToken string) {
200218
connection.credentialsLock.Lock()
201219
defer connection.credentialsLock.Unlock()
202220
connection.Username = username
203221
connection.Password = password
222+
connection.SessionManagerURL = sessionmgrURL
223+
connection.SessionManagerToken = sessionmgrToken
204224
}

0 commit comments

Comments
 (0)