Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions pkg/driver/driver_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// SPDX-FileCopyrightText: SAP SE or an SAP affiliate company and Gardener contributors
//
// SPDX-License-Identifier: Apache-2.0

package driver

import (
"errors"

"github.com/gardener/machine-controller-manager/pkg/util/provider/machinecodes/status"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"

"github.com/gardener/machine-controller-manager-provider-openstack/pkg/driver/executor"
)

var _ = Describe("Driver", func() {

Context("mapErrorToCode", func() {
It("should map executor.ErrFlavorNotFound errors to ResourceExhausted error code", func() {
err1 := executor.ErrFlavorNotFound{Flavor: "flavor"}
err2 := status.Error(mapErrorToCode(err1), err1.Error())
Expect(err1).To(HaveOccurred())
Expect(err2).To(HaveOccurred())
Expect(errors.Is(err2, executor.ErrNotFound)).To(BeFalse())
})
})

})
11 changes: 11 additions & 0 deletions pkg/driver/executor/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,14 @@ var (
// OpenStack resources. In case this case, where a unique ID could not be determined an ErrMultipleFound is returned.
ErrMultipleFound = fmt.Errorf("multiple resources found")
)

// ErrFlavorNotFound is returned when there is no flavor can be matched with the specified flavor name.
// It can happen when certain flavor is not available in specified region and needs to be treated as ResourceExhausted
// to allow fallback to other flavors.
type ErrFlavorNotFound struct {
Flavor string
}

func (e ErrFlavorNotFound) Error() string {
return fmt.Sprintf("Unable to find flavor with name %s", e.Flavor)
}
8 changes: 7 additions & 1 deletion pkg/driver/executor/executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"fmt"
"time"

"github.com/gophercloud/gophercloud/v2"
"github.com/gophercloud/gophercloud/v2/openstack/blockstorage/v3/volumes"
"github.com/gophercloud/gophercloud/v2/openstack/compute/v2/keypairs"
"github.com/gophercloud/gophercloud/v2/openstack/compute/v2/servers"
Expand Down Expand Up @@ -276,7 +277,12 @@ func (ex *Executor) deployServer(ctx context.Context, machineName string, userDa
}
flavorRef, err := ex.Compute.FlavorIDFromName(ctx, flavorName)
if err != nil {
return nil, fmt.Errorf("error resolving flavor ID from flavor name %q: %v", imageName, err)
switch err.(type) {
case gophercloud.ErrResourceNotFound:
return nil, fmt.Errorf("error resolving flavor ID from flavor name %q: %w", flavorName, ErrFlavorNotFound{Flavor: flavorName})
default:
return nil, fmt.Errorf("error resolving flavor ID from flavor name %q: %v", flavorName, err)
}
}

createOpts := &servers.CreateOpts{
Expand Down
16 changes: 16 additions & 0 deletions pkg/driver/executor/executor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,22 @@ var _ = Describe("Executor", func() {
Expect(server.ProviderID).To(Equal(encodeProviderID(region, serverID)))
})

It("should raise a ErrResourceNotFound error when called with a missing flavor", func() {
ex := &Executor{
Compute: compute,
Network: network,
Config: cfg,
}

compute.EXPECT().ListServers(ctx, &servers.ListOpts{Name: machineName}).Return([]servers.Server{}, nil)
compute.EXPECT().ImageIDFromName(ctx, imageName).Return(images.Image{ID: "imageID"}, nil)
compute.EXPECT().FlavorIDFromName(ctx, flavorName).Return(flavorName, gophercloud.ErrResourceNotFound{Name: flavorName, ResourceType: "flavor"})
compute.EXPECT().ListServers(ctx, &servers.ListOpts{Name: machineName}).Return([]servers.Server{}, nil)

_, err := ex.CreateMachine(ctx, machineName, nil)
Expect(err).To(HaveOccurred())
})

It("should delete the server on failure", func() {
ex := &Executor{
Compute: compute,
Expand Down
4 changes: 4 additions & 0 deletions pkg/driver/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,10 @@ func mapErrorToCode(err error) codes.Code {
return codes.PermissionDenied
}

if errors.Is(err, executor.ErrFlavorNotFound{}) {
return codes.ResourceExhausted
}

return mapErrorMessageToCode(err)
}

Expand Down
Loading