Fix reconnection when vCenter is rebooted (#6085)
This commit is contained in:
parent
f46a9c02c6
commit
10c31ca209
|
@ -61,28 +61,39 @@ func NewClientFactory(ctx context.Context, url *url.URL, parent *VSphere) *Clien
|
||||||
func (cf *ClientFactory) GetClient(ctx context.Context) (*Client, error) {
|
func (cf *ClientFactory) GetClient(ctx context.Context) (*Client, error) {
|
||||||
cf.mux.Lock()
|
cf.mux.Lock()
|
||||||
defer cf.mux.Unlock()
|
defer cf.mux.Unlock()
|
||||||
if cf.client == nil {
|
retrying := false
|
||||||
var err error
|
for {
|
||||||
if cf.client, err = NewClient(ctx, cf.url, cf.parent); err != nil {
|
if cf.client == nil {
|
||||||
return nil, err
|
var err error
|
||||||
|
if cf.client, err = NewClient(ctx, cf.url, cf.parent); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
// Execute a dummy call against the server to make sure the client is
|
// Execute a dummy call against the server to make sure the client is
|
||||||
// still functional. If not, try to log back in. If that doesn't work,
|
// still functional. If not, try to log back in. If that doesn't work,
|
||||||
// we give up.
|
// we give up.
|
||||||
ctx1, cancel1 := context.WithTimeout(ctx, cf.parent.Timeout.Duration)
|
ctx1, cancel1 := context.WithTimeout(ctx, cf.parent.Timeout.Duration)
|
||||||
defer cancel1()
|
defer cancel1()
|
||||||
if _, err := methods.GetCurrentTime(ctx1, cf.client.Client); err != nil {
|
if _, err := methods.GetCurrentTime(ctx1, cf.client.Client); err != nil {
|
||||||
log.Printf("I! [inputs.vsphere]: Client session seems to have time out. Reauthenticating!")
|
log.Printf("I! [inputs.vsphere]: Client session seems to have time out. Reauthenticating!")
|
||||||
ctx2, cancel2 := context.WithTimeout(ctx, cf.parent.Timeout.Duration)
|
ctx2, cancel2 := context.WithTimeout(ctx, cf.parent.Timeout.Duration)
|
||||||
defer cancel2()
|
defer cancel2()
|
||||||
if cf.client.Client.SessionManager.Login(ctx2, url.UserPassword(cf.parent.Username, cf.parent.Password)) != nil {
|
if err := cf.client.Client.SessionManager.Login(ctx2, url.UserPassword(cf.parent.Username, cf.parent.Password)); err != nil {
|
||||||
return nil, fmt.Errorf("Renewing authentication failed: %v", err)
|
if !retrying {
|
||||||
|
// The client went stale. Probably because someone rebooted vCenter. Clear it to
|
||||||
|
// force us to create a fresh one. We only get one chance at this. If we fail a second time
|
||||||
|
// we will simply skip this collection round and hope things have stabilized for the next one.
|
||||||
|
retrying = true
|
||||||
|
cf.client = nil
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
return nil, fmt.Errorf("Renewing authentication failed: %v", err)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
return cf.client, nil
|
return cf.client, nil
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewClient creates a new vSphere client based on the url and setting passed as parameters.
|
// NewClient creates a new vSphere client based on the url and setting passed as parameters.
|
||||||
|
|
Loading…
Reference in New Issue