Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix PV allocation on non-English vSphere #73115

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 4 additions & 0 deletions pkg/cloudprovider/providers/vsphere/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ go_library(
"//vendor/github.com/vmware/govmomi/vapi/tags:go_default_library",
"//vendor/github.com/vmware/govmomi/vim25:go_default_library",
"//vendor/github.com/vmware/govmomi/vim25/mo:go_default_library",
"//vendor/github.com/vmware/govmomi/vim25/soap:go_default_library",
"//vendor/github.com/vmware/govmomi/vim25/types:go_default_library",
"//vendor/gopkg.in/gcfg.v1:go_default_library",
"//vendor/k8s.io/klog:go_default_library",
],
Expand All @@ -41,6 +43,7 @@ go_test(
srcs = [
"credentialmanager_test.go",
"vsphere_test.go",
"vsphere_util_test.go",
],
embed = [":go_default_library"],
deps = [
Expand All @@ -55,6 +58,7 @@ go_test(
"//staging/src/k8s.io/client-go/informers:go_default_library",
"//staging/src/k8s.io/client-go/kubernetes/fake:go_default_library",
"//staging/src/k8s.io/cloud-provider:go_default_library",
"//vendor/github.com/vmware/govmomi:go_default_library",
"//vendor/github.com/vmware/govmomi/lookup/simulator:go_default_library",
"//vendor/github.com/vmware/govmomi/property:go_default_library",
"//vendor/github.com/vmware/govmomi/simulator:go_default_library",
Expand Down
21 changes: 17 additions & 4 deletions pkg/cloudprovider/providers/vsphere/vsphere_util.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,13 @@ import (
"io/ioutil"
"os"
"path/filepath"
"regexp"
"strings"
"time"

"github.com/vmware/govmomi/vim25"
"github.com/vmware/govmomi/vim25/mo"
"github.com/vmware/govmomi/vim25/soap"
"github.com/vmware/govmomi/vim25/types"
"k8s.io/klog"

"k8s.io/api/core/v1"
Expand Down Expand Up @@ -325,10 +326,9 @@ func getcanonicalVolumePath(ctx context.Context, dc *vclib.Datacenter, volumePat
// Querying a non-existent dummy disk on the datastore folder.
// It would fail and return an folder ID in the error message.
_, err := dc.GetVirtualDiskPage83Data(ctx, dummyDiskVolPath)
canonicalVolumePath, err = getPathFromFileNotFound(err)
if err != nil {
re := regexp.MustCompile("File (.*?) was not found")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rather than match the error message at all, we can use the fault type like so:

// fileNotFound returns the value of File if err is a FileNotFound soap fault, an empty string otherwise.
func fileNotFound(err error) string {
	if soap.IsSoapFault(err) {
		fault := soap.ToSoapFault(err)
		if f, ok := fault.VimFault().(types.FileNotFound); ok {
			return f.File
		}
	}
	return ""
}

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note that this can be tested against vcsim, with the error returned here for example:

_, err = dc.GetVirtualDiskPage83Data(ctx, diskPath+testNameNotFound)
if err == nil {
t.Error("expected error")
}

match := re.FindStringSubmatch(err.Error())
canonicalVolumePath = match[1]
return "", fmt.Errorf("failed to get path from dummy request: %v", err)
}
}
diskPath := vclib.GetPathFromVMDiskPath(canonicalVolumePath)
Expand All @@ -342,6 +342,19 @@ func getcanonicalVolumePath(ctx context.Context, dc *vclib.Datacenter, volumePat
return canonicalVolumePath, nil
}

// getPathFromFileNotFound returns the path from a fileNotFound error
func getPathFromFileNotFound(err error) (string, error) {
if soap.IsSoapFault(err) {
fault := soap.ToSoapFault(err)
f, ok := fault.VimFault().(types.FileNotFound)
if !ok {
return "", fmt.Errorf("%v is not a FileNotFound error", err)
}
return f.File, nil
}
return "", fmt.Errorf("%v is not a soap fault", err)
}

func setdatastoreFolderIDMap(
datastoreFolderIDMap map[string]map[string]string,
datastore string,
Expand Down
72 changes: 72 additions & 0 deletions pkg/cloudprovider/providers/vsphere/vsphere_util_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
/*
Copyright 2019 The Kubernetes Authors.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package vsphere

import (
"context"
"fmt"
"testing"

"k8s.io/kubernetes/pkg/cloudprovider/providers/vsphere/vclib"

"github.com/vmware/govmomi"
"github.com/vmware/govmomi/simulator"
)

func TestGetPathFromFileNotFound(t *testing.T) {
ctx := context.Background()

// vCenter model + initial set of objects (cluster, hosts, VMs, network, datastore, etc)
model := simulator.VPX()

defer model.Remove()
err := model.Create()
if err != nil {
t.Fatal(err)
}

s := model.Service.NewServer()
defer s.Close()

c, err := govmomi.NewClient(ctx, s.URL, true)
if err != nil {
t.Fatal(err)
}

vc := &vclib.VSphereConnection{Client: c.Client}

dc, err := vclib.GetDatacenter(ctx, vc, vclib.TestDefaultDatacenter)
if err != nil {
t.Errorf("failed to get datacenter: %v", err)
}

requestDiskPath := fmt.Sprintf("[%s] %s", vclib.TestDefaultDatastore, DummyDiskName)
_, err = dc.GetVirtualDiskPage83Data(ctx, requestDiskPath)
if err == nil {
t.Error("expected error when calling GetVirtualDiskPage83Data")
}

_, err = getPathFromFileNotFound(err)
if err != nil {
t.Errorf("expected err to be nil but was %v", err)
}

_, err = getPathFromFileNotFound(nil)
if err == nil {
t.Errorf("expected err when calling getPathFromFileNotFound with nil err")
}
}