Skip to content

OCPBUGS-56168: [release-4.17] Update virtualmachines service to armcompute/v5 SDK #146

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

Open
wants to merge 2 commits into
base: release-4.17
Choose a base branch
from
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
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ toolchain go1.22.1

require (
github.com/Azure/azure-sdk-for-go v68.0.0+incompatible
github.com/Azure/azure-sdk-for-go-extensions v0.1.8
github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/compute/armcompute/v5 v5.7.0
github.com/Azure/go-autorest/autorest v0.11.29
github.com/Azure/go-autorest/autorest/to v0.4.0
github.com/ghodss/yaml v1.0.0
Expand Down
8 changes: 8 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,20 @@ cloud.google.com/go/storage v1.14.0/go.mod h1:GrKmX003DSIwi9o29oFT7YDnHYwZoctc3f
dmitri.shuralyov.com/gpu/mtl v0.0.0-20190408044501-666a987793e9/go.mod h1:H6x//7gZCb22OMCxBHrMx7a5I7Hp++hsVxbQ4BYO7hU=
github.com/Azure/azure-sdk-for-go v68.0.0+incompatible h1:fcYLmCpyNYRnvJbPerq7U0hS+6+I79yEDJBqVNcqUzU=
github.com/Azure/azure-sdk-for-go v68.0.0+incompatible/go.mod h1:9XXNKU+eRnpl9moKnB4QOLf1HestfXbmab5FXxiDBjc=
github.com/Azure/azure-sdk-for-go-extensions v0.1.8 h1:x8Vu78C4r8mh6V2yQKQRSWLU+EYBVwFsf3XECddyb6s=
github.com/Azure/azure-sdk-for-go-extensions v0.1.8/go.mod h1:4su5NjJwhqFH2B/5zJSKOz7hazfr2y38Iu6W4ZK0HYA=
github.com/Azure/azure-sdk-for-go/sdk/azcore v1.12.0 h1:1nGuui+4POelzDwI7RG56yfQJHCnKvwfMoU7VsEp+Zg=
github.com/Azure/azure-sdk-for-go/sdk/azcore v1.12.0/go.mod h1:99EvauvlcJ1U06amZiksfYz/3aFGyIhWGHVyiZXtBAI=
github.com/Azure/azure-sdk-for-go/sdk/azidentity v1.6.0 h1:U2rTu3Ef+7w9FHKIAXM6ZyqF3UOWJZ12zIm8zECAFfg=
github.com/Azure/azure-sdk-for-go/sdk/azidentity v1.6.0/go.mod h1:9kIvujWAA58nmPmWB1m23fyWic1kYZMxD9CxaWn4Qpg=
github.com/Azure/azure-sdk-for-go/sdk/internal v1.9.0 h1:H+U3Gk9zY56G3u872L82bk4thcsy2Gghb9ExT4Zvm1o=
github.com/Azure/azure-sdk-for-go/sdk/internal v1.9.0/go.mod h1:mgrmMSgaLp9hmax62XQTd0N4aAqSE5E0DulSpVYK7vc=
github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/compute/armcompute/v5 v5.7.0 h1:LkHbJbgF3YyvC53aqYGR+wWQDn2Rdp9AQdGndf9QvY4=
github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/compute/armcompute/v5 v5.7.0/go.mod h1:QyiQdW4f4/BIfB8ZutZ2s+28RAgfa/pT+zS++ZHyM1I=
github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/internal/v2 v2.0.0 h1:PTFGRSlMKCQelWwxUyYVEUqseBJVemLyqWJjvMyt0do=
github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/internal/v2 v2.0.0/go.mod h1:LRr2FzBTQlONPPa5HREE5+RjSCTXl7BwOvYOaWTqCaI=
github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/resources/armresources v1.1.1 h1:7CBQ+Ei8SP2c6ydQTGCCrS35bDxgTMfoP2miAwK++OU=
github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/resources/armresources v1.1.1/go.mod h1:c/wcGeGx5FUPbM/JltUYHZcKmigwyVLJlDq+4HdtXaw=
github.com/Azure/go-ansiterm v0.0.0-20230124172434-306776ec8161 h1:L/gRVlceqvL25UVaW/CKtUDjefjrs0SPonmDGUVOYP0=
github.com/Azure/go-ansiterm v0.0.0-20230124172434-306776ec8161/go.mod h1:xomTg63KZ2rFqZQzSB4Vz2SUXa1BpHTVz9L5PTmPC4E=
github.com/Azure/go-autorest v14.2.0+incompatible h1:V5VMDjClD3GiElqLWO7mz2MxNAK/vTfRHdAubSIPRgs=
Expand Down
35 changes: 35 additions & 0 deletions pkg/cloud/azure/actuators/clients.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,47 @@ limitations under the License.
package actuators

import (
"net/http"

"github.com/Azure/azure-sdk-for-go/sdk/azcore"
"github.com/Azure/azure-sdk-for-go/sdk/azcore/arm"
"github.com/Azure/azure-sdk-for-go/sdk/azcore/cloud"
"github.com/Azure/azure-sdk-for-go/sdk/azcore/policy"
"github.com/Azure/go-autorest/autorest"

"github.com/openshift/machine-api-provider-azure/pkg/cloud/azure"
)

// AzureClients contains all the Azure clients used by the scopes.
type AzureClients struct {
SubscriptionID string
Authorizer autorest.Authorizer
ResourceManagerEndpoint string

// For SDK v2
Token azcore.TokenCredential
Cloud cloud.Configuration
}

// ARMClientOptions returns default ARM client options for CAPZ SDK v2 requests.
func (c *AzureClients) ARMClientOptions() *arm.ClientOptions {
opts := &arm.ClientOptions{}
opts.Cloud = c.Cloud
opts.PerCallPolicies = []policy.Policy{
userAgentPolicy{},
}
opts.Retry.MaxRetries = -1 // Less than zero means one try and no retries.

return opts
}

// userAgentPolicy extends the "User-Agent" header on requests.
// It implements the policy.Policy interface.
type userAgentPolicy struct{}

// Do extends the "User-Agent" header of a request by appending CAPZ's user agent.
func (p userAgentPolicy) Do(req *policy.Request) (*http.Response, error) {
// FIXME: Ought to include a version after our UA string if we have one
req.Raw().Header.Set("User-Agent", req.Raw().UserAgent()+" "+azure.UserAgent)
return req.Next()
}
10 changes: 4 additions & 6 deletions pkg/cloud/azure/actuators/machine/actuator.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import (
"fmt"
"time"

"github.com/Azure/go-autorest/autorest"
azureerrors "github.com/Azure/azure-sdk-for-go-extensions/pkg/errors"
machinev1 "github.com/openshift/api/machine/v1beta1"
machineapierrors "github.com/openshift/machine-api-operator/pkg/controller/machine"
"github.com/openshift/machine-api-provider-azure/pkg/cloud/azure"
Expand Down Expand Up @@ -106,15 +106,13 @@ func (a *Actuator) Create(ctx context.Context, machine *machinev1.Machine) error
klog.Errorf("Error storing machine info: %v", err)
}

var detailedError autorest.DetailedError
if errors.As(err, &detailedError) {
statusCode, ok := detailedError.StatusCode.(int)
if azErr := azureerrors.IsResponseError(err); azErr != nil {
// Any 4xx error that isn't invalid credentials should be a terminal failure.
// Invalid Credentials implies that the credentials expired between the scope creation and API calls,
// this may happen when CCO is refreshing credentials simultaneously.
// In this case we should retry as the credentials should be updated in the secret.
if ok && statusCode >= 400 && statusCode < 500 && !azure.InvalidCredentials(err) {
return a.handleMachineError(machine, machineapierrors.InvalidMachineConfiguration("failed to reconcile machine %q: %v", machine.Name, detailedError), createEventAction)
if azErr.StatusCode >= 400 && azErr.StatusCode < 500 && !azure.InvalidCredentials(err) {
return a.handleMachineError(machine, machineapierrors.InvalidMachineConfiguration("failed to reconcile machine %q: %v", machine.Name, err), createEventAction)
}
}

Expand Down
59 changes: 30 additions & 29 deletions pkg/cloud/azure/actuators/machine/actuator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@ import (
"strings"
"testing"

"github.com/Azure/azure-sdk-for-go/services/compute/mgmt/2021-11-01/compute"
"github.com/Azure/azure-sdk-for-go/sdk/azcore"
"github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/compute/armcompute/v5"
"github.com/Azure/azure-sdk-for-go/services/network/mgmt/2021-02-01/network"
"github.com/Azure/go-autorest/autorest"
"github.com/Azure/go-autorest/autorest/to"
"github.com/ghodss/yaml"
"github.com/golang/mock/gomock"
Expand Down Expand Up @@ -181,21 +181,22 @@ type FakeVMService struct {
// Get returns fake success.
func (s *FakeVMService) Get(ctx context.Context, spec azure.Spec) (interface{}, error) {
s.GetCallCount++
return compute.VirtualMachine{
vm := armcompute.VirtualMachine{
ID: to.StringPtr(s.ID),
Name: to.StringPtr(s.Name),
VirtualMachineProperties: &compute.VirtualMachineProperties{
Properties: &armcompute.VirtualMachineProperties{
ProvisioningState: to.StringPtr(s.ProvisioningState),
NetworkProfile: &compute.NetworkProfile{
NetworkInterfaces: &[]compute.NetworkInterfaceReference{
NetworkProfile: &armcompute.NetworkProfile{
NetworkInterfaces: []*armcompute.NetworkInterfaceReference{
{
ID: to.StringPtr("machine-test-nic"),
NetworkInterfaceReferenceProperties: &compute.NetworkInterfaceReferenceProperties{},
ID: to.StringPtr("machine-test-nic"),
Properties: &armcompute.NetworkInterfaceReferenceProperties{},
},
},
},
},
}, nil
}
return armcompute.VirtualMachinesClientGetResponse{VirtualMachine: vm}, nil
}

// CreateOrUpdate returns fake success.
Expand Down Expand Up @@ -393,9 +394,10 @@ func (s *FakeVMCheckZonesService) Get(ctx context.Context, spec azure.Spec) (int
return nil, errors.New("vm not found")
}

return &compute.VirtualMachine{
VirtualMachineProperties: &compute.VirtualMachineProperties{},
}, nil
vm := armcompute.VirtualMachine{
Properties: &armcompute.VirtualMachineProperties{},
}
return armcompute.VirtualMachinesClientGetResponse{VirtualMachine: vm}, nil
}

// CreateOrUpdate returns fake success.
Expand Down Expand Up @@ -731,19 +733,20 @@ func TestMachineEvents(t *testing.T) {
})

networkSvc.EXPECT().CreateOrUpdate(gomock.Any(), gomock.Any()).Return(nil).AnyTimes()
vmSvc.EXPECT().Get(gomock.Any(), gomock.Any()).Return(compute.VirtualMachine{
vm := armcompute.VirtualMachine{
ID: ptr.To[string]("vm-id"),
Name: ptr.To[string]("vm-name"),
VirtualMachineProperties: &compute.VirtualMachineProperties{
Properties: &armcompute.VirtualMachineProperties{
ProvisioningState: ptr.To[string]("Succeeded"),
HardwareProfile: &compute.HardwareProfile{
VMSize: compute.VirtualMachineSizeTypesStandardB2ms,
HardwareProfile: &armcompute.HardwareProfile{
VMSize: ptr.To(armcompute.VirtualMachineSizeTypesStandardB2Ms),
},
OsProfile: &compute.OSProfile{
OSProfile: &armcompute.OSProfile{
ComputerName: ptr.To[string]("vm-name"),
},
},
}, nil).AnyTimes()
}
vmSvc.EXPECT().Get(gomock.Any(), gomock.Any()).Return(armcompute.VirtualMachinesClientGetResponse{VirtualMachine: vm}, nil).AnyTimes()
vmSvc.EXPECT().Delete(gomock.Any(), gomock.Any()).Return(nil).AnyTimes()
disksSvc.EXPECT().Delete(gomock.Any(), gomock.Any()).Return(nil).AnyTimes()
networkSvc.EXPECT().Delete(gomock.Any(), gomock.Any()).Return(nil).AnyTimes()
Expand Down Expand Up @@ -788,25 +791,21 @@ func TestStatusCodeBasedCreationErrors(t *testing.T) {
cases := []struct {
name string
operation func(actuator *Actuator, machine *machinev1.Machine)
event string
statusCode interface{}
statusCode int
requeable bool
}{
{
name: "InvalidConfig",
event: "Warning FailedCreate InvalidConfiguration: failed to reconcile machine \"azure-actuator-testing-machine\": compute.VirtualMachinesClient#CreateOrUpdate: MOCK: StatusCode=400",
statusCode: 400,
requeable: false,
},
{
name: "CreateMachine",
event: "Warning FailedCreate CreateError: failed to reconcile machine \"azure-actuator-testing-machine\"s: failed to create vm azure-actuator-testing-machine: failed to create VM: failed to create or get machine: compute.VirtualMachinesClient#CreateOrUpdate: MOCK: StatusCode=300",
statusCode: 300,
requeable: true,
},
{
name: "CreateMachine",
event: "Warning FailedCreate CreateError: failed to reconcile machine \"azure-actuator-testing-machine\"s: failed to create vm azure-actuator-testing-machine: failed to create VM: failed to create or get machine: compute.VirtualMachinesClient#CreateOrUpdate: MOCK: StatusCode=401",
statusCode: 401,
requeable: true,
},
Expand Down Expand Up @@ -850,11 +849,13 @@ func TestStatusCodeBasedCreationErrors(t *testing.T) {
})

networkSvc.EXPECT().CreateOrUpdate(gomock.Any(), gomock.Any()).Return(nil).Times(1)
azureErr := autorest.NewError("compute.VirtualMachinesClient", "CreateOrUpdate", "MOCK")
azureErr.StatusCode = tc.statusCode
var azureErr error = &azcore.ResponseError{
ErrorCode: fmt.Sprintf("StatusCode=%d", tc.statusCode),
StatusCode: tc.statusCode,
}
wrapErr := fmt.Errorf("failed to create or get machine: %w", azureErr)
vmSvc.EXPECT().CreateOrUpdate(gomock.Any(), gomock.Any()).Return(wrapErr).Times(1)
vmSvc.EXPECT().Get(gomock.Any(), gomock.Any()).Return(nil, autorest.NewError("compute.VirtualMachinesClient", "Get", "MOCK")).Times(1)
vmSvc.EXPECT().Get(gomock.Any(), gomock.Any()).Return(nil, fmt.Errorf("mock compute.VirtualMachinesClient Get error")).Times(1)
availabilityZonesSvc.EXPECT().Get(gomock.Any(), gomock.Any()).Return([]string{"testzone"}, nil).Times(1)

_, ok := machineActuator.Create(context.TODO(), machine).(*machineapierrors.RequeueAfterError)
Expand All @@ -867,11 +868,11 @@ func TestStatusCodeBasedCreationErrors(t *testing.T) {

select {
case event := <-eventsChannel:
if event != tc.event {
t.Errorf("Expected %q event, got %q", tc.event, event)
if !strings.Contains(event, azureErr.Error()) {
t.Errorf("Expected event to contain error %q, got %q", azureErr.Error(), event)
}
default:
t.Errorf("Expected %q event, got none", tc.event)
t.Errorf("Expected an event containing error %q, got none", azureErr.Error())
}
})
}
Expand Down
Loading