|
| 1 | +/* |
| 2 | +Copyright 2025 The Kubernetes Authors. |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | +package vc |
| 17 | + |
| 18 | +import ( |
| 19 | + "context" |
| 20 | + "fmt" |
| 21 | + neturl "net/url" |
| 22 | + |
| 23 | + "github.com/onsi/gomega" |
| 24 | + "github.com/vmware/govmomi" |
| 25 | + "github.com/vmware/govmomi/vim25" |
| 26 | + "sigs.k8s.io/vsphere-csi-driver/v3/tests/e2e/constants" |
| 27 | + "sigs.k8s.io/vsphere-csi-driver/v3/tests/e2e/env" |
| 28 | +) |
| 29 | + |
| 30 | +const ( |
| 31 | + RoundTripperDefaultCount = 3 |
| 32 | +) |
| 33 | + |
| 34 | +// This function creates a new client for vpxd connection. |
| 35 | +func NewClient(ctx context.Context, vCenterIp string, |
| 36 | + vCenterIpPort string, user string, password string) *govmomi.Client { |
| 37 | + isPrivateNetwork := env.GetBoolEnvVarOrDefault("IS_PRIVATE_NETWORK", false) |
| 38 | + if isPrivateNetwork { |
| 39 | + vCenterIp = env.GetStringEnvVarOrDefault("LOCAL_HOST_IP", constants.DefaultlocalhostIP) |
| 40 | + } |
| 41 | + url, err := neturl.Parse(fmt.Sprintf("https://%s:%s/sdk", |
| 42 | + vCenterIp, vCenterIpPort)) |
| 43 | + gomega.Expect(err).NotTo(gomega.HaveOccurred()) |
| 44 | + url.User = neturl.UserPassword(user, password) |
| 45 | + client, err := govmomi.NewClient(ctx, url, true) |
| 46 | + gomega.Expect(err).NotTo(gomega.HaveOccurred()) |
| 47 | + err = client.UseServiceVersion(constants.VsanNamespace) |
| 48 | + gomega.Expect(err).NotTo(gomega.HaveOccurred()) |
| 49 | + client.RoundTripper = vim25.Retry(client.RoundTripper, vim25.TemporaryNetworkError(RoundTripperDefaultCount)) |
| 50 | + return client |
| 51 | +} |
0 commit comments