connection.go 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. /*
  2. Copyright 2018 The Kubernetes Authors.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package vsphere
  14. import (
  15. "context"
  16. "fmt"
  17. neturl "net/url"
  18. "sync"
  19. "github.com/vmware/govmomi"
  20. "github.com/vmware/govmomi/session"
  21. "github.com/vmware/govmomi/vim25"
  22. "k8s.io/klog"
  23. )
  24. const (
  25. roundTripperDefaultCount = 3
  26. )
  27. var (
  28. clientLock sync.Mutex
  29. )
  30. // Connect makes connection to vSphere
  31. // No actions are taken if a connection exists and alive. Otherwise, a new client will be created.
  32. func Connect(ctx context.Context, vs *VSphere) error {
  33. var err error
  34. clientLock.Lock()
  35. defer clientLock.Unlock()
  36. if vs.Client == nil {
  37. vs.Client, err = NewClient(ctx, vs)
  38. if err != nil {
  39. klog.Errorf("Failed to create govmomi client. err: %+v", err)
  40. return err
  41. }
  42. return nil
  43. }
  44. manager := session.NewManager(vs.Client.Client)
  45. userSession, err := manager.UserSession(ctx)
  46. if err != nil {
  47. klog.Errorf("Error while obtaining user session. err: %+v", err)
  48. return err
  49. }
  50. if userSession != nil {
  51. return nil
  52. }
  53. klog.Warningf("Creating new client session since the existing session is not valid or not authenticated")
  54. vs.Client.Logout(ctx)
  55. vs.Client, err = NewClient(ctx, vs)
  56. if err != nil {
  57. klog.Errorf("Failed to create govmomi client. err: %+v", err)
  58. return err
  59. }
  60. return nil
  61. }
  62. // NewClient creates a new client for vSphere connection
  63. func NewClient(ctx context.Context, vs *VSphere) (*govmomi.Client, error) {
  64. url, err := neturl.Parse(fmt.Sprintf("https://%s:%s/sdk", vs.Config.Hostname, vs.Config.Port))
  65. if err != nil {
  66. klog.Errorf("Failed to parse URL: %s. err: %+v", url, err)
  67. return nil, err
  68. }
  69. url.User = neturl.UserPassword(vs.Config.Username, vs.Config.Password)
  70. client, err := govmomi.NewClient(ctx, url, true)
  71. if err != nil {
  72. klog.Errorf("Failed to create new client. err: %+v", err)
  73. return nil, err
  74. }
  75. if vs.Config.RoundTripperCount == 0 {
  76. vs.Config.RoundTripperCount = roundTripperDefaultCount
  77. }
  78. client.RoundTripper = vim25.Retry(client.RoundTripper, vim25.TemporaryNetworkError(int(vs.Config.RoundTripperCount)))
  79. return client, nil
  80. }