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

Improve DataProtection/BackupInstance tests and extension #3953

Merged
merged 2 commits into from
Apr 24, 2024
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -10,32 +10,23 @@ import (
"fmt"
"strings"

armdataprotection "github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/dataprotection/armdataprotection/v3"
"github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/dataprotection/armdataprotection/v3"
"github.com/go-logr/logr"
"github.com/pkg/errors"
"sigs.k8s.io/controller-runtime/pkg/conversion"

dataprotection "github.com/Azure/azure-service-operator/v2/api/dataprotection/v1api20231101/storage"
"github.com/Azure/azure-service-operator/v2/internal/genericarmclient"
. "github.com/Azure/azure-service-operator/v2/internal/logging"
"github.com/Azure/azure-service-operator/v2/internal/resolver"
"github.com/Azure/azure-service-operator/v2/internal/set"
"github.com/Azure/azure-service-operator/v2/internal/util/to"
"github.com/Azure/azure-service-operator/v2/pkg/genruntime"
"github.com/Azure/azure-service-operator/v2/pkg/genruntime/extensions"
"github.com/go-logr/logr"
"github.com/pkg/errors"
"sigs.k8s.io/controller-runtime/pkg/conversion"
)

var _ extensions.PostReconciliationChecker = &BackupVaultsBackupInstanceExtension{}

var nonRetryableStates = set.Make(
"ConfiguringProtectionFailed",
"Invalid",
"NotProtected",
"ProtectionConfigured",
"SoftDeleted",
"ProtectionStopped",
"BackupSchedulesSuspended",
"RetentionSchedulesSuspended",
)
var protectionError = "ProtectionError"

const (
BackupInstancePollerResumeTokenAnnotation = "serviceoperator.azure.com/bi-poller-resume-token"
Expand Down Expand Up @@ -78,81 +69,80 @@ func (extension *BackupVaultsBackupInstanceExtension) PostReconcileCheck(
// the hub type has been changed but this extension has not
var _ conversion.Hub = backupInstance

if owner != nil &&
backupInstance != nil &&
backupInstance.Status.Id != nil &&
backupInstance.Status.Properties != nil &&
backupInstance.Status.Properties.ProtectionStatus != nil &&
backupInstance.Status.Properties.ProtectionStatus.Status != nil {
if owner == nil ||
backupInstance == nil ||
backupInstance.Status.Id == nil ||
backupInstance.Status.Properties == nil ||
backupInstance.Status.Properties.ProtectionStatus == nil ||
backupInstance.Status.Properties.ProtectionStatus.Status == nil {
// We'll let the reconciler handle this case.
return next(ctx, obj, owner, resolver, armClient, log)
}

protectionStatus := *backupInstance.Status.Properties.ProtectionStatus.Status
log.V(Debug).Info(fmt.Sprintf("Protection Status is %q", protectionStatus))

// We only want to continue if protectionStatus == ProtectionError.
if !strings.EqualFold(protectionStatus, protectionError) {
log.V(Debug).Info("Returning PostReconcileCheckResultSuccess")
return next(ctx, obj, owner, resolver, armClient, log)
}

protectionStatus := *backupInstance.Status.Properties.ProtectionStatus.Status
log.V(Debug).Info(fmt.Sprintf("Protection Status is %q", protectionStatus))
// call sync api only when protection status is ProtectionError and error code is usererror
var protectionStatusErrorCode string
protectionStatusErrorCode = strings.ToLower(*backupInstance.Status.Properties.ProtectionStatus.ErrorDetails.Code)
log.V(Debug).Info(fmt.Sprintf("Protection Error code is %q", protectionStatusErrorCode))

if protectionStatusErrorCode != "" && strings.Contains(protectionStatusErrorCode, "usererror") {
id, _ := genruntime.GetAndParseResourceID(backupInstance)
subscription := id.SubscriptionID
rg := id.ResourceGroupName
vaultName := id.Parent.Name

clientFactory, err := armdataprotection.NewClientFactory(subscription, armClient.Creds(), armClient.ClientOptions())
if err != nil {
return extensions.PostReconcileCheckResultFailure("failed to create armdataprotection client"), err
}

var parameters armdataprotection.SyncBackupInstanceRequest
parameters.SyncType = to.Ptr(armdataprotection.SyncTypeDefault)

// get the resume token from the resource
pollerResumeToken, _ := GetPollerResumeToken(obj, log)

// BeginSyncBackupInstance is in-progress - poller resume token is available
log.V(Debug).Info("Starting BeginSyncBackupInstance")

poller, err := clientFactory.NewBackupInstancesClient().BeginSyncBackupInstance(ctx, rg, vaultName, backupInstance.AzureName(), parameters, &armdataprotection.BackupInstancesClientBeginSyncBackupInstanceOptions{
ResumeToken: pollerResumeToken,
})
if err != nil {
return extensions.PostReconcileCheckResultFailure("Failed Polling for BeginSyncBackupInstance to get the result"), err
}

if pollerResumeToken == "" {
resumeToken, resumeTokenErr := poller.ResumeToken()
if resumeTokenErr != nil {
return extensions.PostReconcileCheckResultFailure("couldn't create PUT resume token for resource"), resumeTokenErr
} else {
SetPollerResumeToken(obj, resumeToken, log)
}
}

// return success for reconcilation if the state is non-retryable
if nonRetryableStates.Contains(protectionStatus) {
log.V(Debug).Info("Returning PostReconcileCheckResultSuccess")
return next(ctx, obj, owner, resolver, armClient, log)
_, pollErr := poller.Poll(ctx)
if pollErr != nil {
return extensions.PostReconcileCheckResultFailure("couldn't create PUT resume token for resource"), pollErr
}

// call sync api only when protection status is ProtectionError and error code is usererror
if protectionStatus == "ProtectionError" {
var protectionStatusErrorCode string
protectionStatusErrorCode = strings.ToLower(*backupInstance.Status.Properties.ProtectionStatus.ErrorDetails.Code)
log.V(Debug).Info(fmt.Sprintf("Protection Error code is %q", protectionStatusErrorCode))

if protectionStatusErrorCode != "" && strings.Contains(protectionStatusErrorCode, "usererror") {
id, _ := genruntime.GetAndParseResourceID(backupInstance)
subscription := id.SubscriptionID
rg := id.ResourceGroupName
vaultName := id.Parent.Name

clientFactory, err := armdataprotection.NewClientFactory(subscription, armClient.Creds(), armClient.ClientOptions())
if err != nil {
return extensions.PostReconcileCheckResultFailure("failed to create armdataprotection client"), err
}

var parameters armdataprotection.SyncBackupInstanceRequest
parameters.SyncType = to.Ptr(armdataprotection.SyncTypeDefault)

// get the resume token from the resource
pollerResumeToken, _ := GetPollerResumeToken(obj, log)

// BeginSyncBackupInstance is in-progress - poller resume token is available

log.V(Debug).Info("Starting BeginSyncBackupInstance")

poller, err := clientFactory.NewBackupInstancesClient().BeginSyncBackupInstance(ctx, rg, vaultName, backupInstance.AzureName(), parameters, &armdataprotection.BackupInstancesClientBeginSyncBackupInstanceOptions{
ResumeToken: pollerResumeToken,
})
if err != nil {
return extensions.PostReconcileCheckResultFailure("Failed Polling for BeginSyncBackupInstance to get the result"), err
}

if pollerResumeToken == "" {
resumeToken, resumeTokenErr := poller.ResumeToken()
if resumeTokenErr != nil {
return extensions.PostReconcileCheckResultFailure("couldn't create PUT resume token for resource"), resumeTokenErr
} else {
SetPollerResumeToken(obj, resumeToken, log)
}
}

_, pollErr := poller.Poll(ctx)
if pollErr != nil {
return extensions.PostReconcileCheckResultFailure("couldn't create PUT resume token for resource"), pollErr
}

if poller.Done() {
log.V(Debug).Info("Polling is completed")
ClearPollerResumeToken(obj, log)
_, err := poller.Result(ctx)
if err != nil {
return extensions.PostReconcileCheckResultFailure("couldn't create PUT resume token for resource"), err
}
}
log.V(Debug).Info("Polling is in-progress")
if poller.Done() {
log.V(Debug).Info("Polling is completed")
ClearPollerResumeToken(obj, log)
_, err := poller.Result(ctx)
if err != nil {
return extensions.PostReconcileCheckResultFailure("couldn't create PUT resume token for resource"), err
}
}
log.V(Debug).Info("Polling is in-progress")
}
return extensions.PostReconcileCheckResultFailure("Backup Instance is in non terminal state"), nil
}
3 changes: 1 addition & 2 deletions v2/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ require (
github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/appconfiguration/armappconfiguration v1.1.1
github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/containerservice/armcontainerservice v1.0.0
github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/cosmos/armcosmos v1.0.0
github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/dataprotection/armdataprotection/v3 v3.0.0
Copy link
Member

Choose a reason for hiding this comment

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

Nice to see the consolidation to a single version

github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/eventgrid/armeventgrid v1.0.0
github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/eventhub/armeventhub v1.2.0
github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/iothub/armiothub v1.3.0
Expand Down Expand Up @@ -56,8 +57,6 @@ require (
require (
filippo.io/edwards25519 v1.1.0 // indirect
github.com/Azure/azure-sdk-for-go/sdk/internal v1.5.2 // indirect
github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/dataprotection/armdataprotection v1.0.0 // indirect
github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/dataprotection/armdataprotection/v3 v3.0.0 // indirect
github.com/AzureAD/microsoft-authentication-library-for-go v1.2.2 // indirect
github.com/antlr/antlr4/runtime/Go/antlr/v4 v4.0.0-20230305170008-8188dc5388df // indirect
github.com/beorn7/perks v1.0.1 // indirect
Expand Down
7 changes: 0 additions & 7 deletions v2/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@ github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/containerservice/armcontai
github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/containerservice/armcontainerservice v1.0.0/go.mod h1:TmlMW4W5OvXOmOyKNnor8nlMMiO1ctIyzmHme/VHsrA=
github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/cosmos/armcosmos v1.0.0 h1:Fv8iibGn1eSw0lt2V3cTsuokBEnOP+M//n8OiMcCgTM=
github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/cosmos/armcosmos v1.0.0/go.mod h1:Qpe/qN9d5IQ7WPtTXMRCd6+BWTnhi3sxXVys6oJ5Vho=
github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/dataprotection/armdataprotection v1.0.0 h1:VFqjVi532z3gdltbAkYrPl9Ez0czn3ZPM+bjmvLq6fk=
github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/dataprotection/armdataprotection v1.0.0/go.mod h1:CmZQSRwBPP7KNjDA+PHaoR2m8wgOsbTd9ncqZgSzgHA=
github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/dataprotection/armdataprotection/v3 v3.0.0 h1:U5kTYUBpSwd4lrzXIh4grgRPcbu6TMv2BS0kUGS9oIE=
github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/dataprotection/armdataprotection/v3 v3.0.0/go.mod h1:leRley5f3YKGJgPojFeSMVHqnjkn6RSUfxaan229UpA=
github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/eventgrid/armeventgrid v1.0.0 h1:w6b0+FygDpqM7g5cjbeyPoBzgxVHwwt2vCUvTz1oFY8=
Expand Down Expand Up @@ -81,7 +79,6 @@ github.com/felixge/httpsnoop v1.0.3 h1:s/nj+GCswXYzN5v2DpNMuMQYe+0DDwt5WVCU6CWBd
github.com/felixge/httpsnoop v1.0.3/go.mod h1:m8KPJKqk1gH5J9DgRY2ASl2lWCfGKXixSwevea8zH2U=
github.com/fsnotify/fsnotify v1.7.0 h1:8JEhPFa5W2WU7YfeZzPNqzMP6Lwt7L2715Ggo0nosvA=
github.com/fsnotify/fsnotify v1.7.0/go.mod h1:40Bi/Hjc2AVfZrqy+aj+yEI+/bRxZnMJyTJwOpGvigM=
github.com/go-logfmt/logfmt v0.5.1/go.mod h1:WYhtIu8zTZfxdn5+rREduYbwxfcBr/Vr6KEVveWlfTs=
github.com/go-logr/logr v1.2.2/go.mod h1:jdQByPbusPIv2/zmleS9BjJVeZ6kBagPoEUsqbVz/1A=
github.com/go-logr/logr v1.4.1 h1:pKouT5E8xu9zeFC39JXRDukb6JFQPXM5p5I91188VAQ=
github.com/go-logr/logr v1.4.1/go.mod h1:9T104GzyrTigFIr8wt5mBrctHMim0Nb2HLGrmQ40KvY=
Expand Down Expand Up @@ -146,13 +143,10 @@ github.com/jackc/pgx/v5 v5.5.5 h1:amBjrZVmksIdNjxGW/IiIMzxMKZFelXbUoPNb+8sjQw=
github.com/jackc/pgx/v5 v5.5.5/go.mod h1:ez9gk+OAat140fv9ErkZDYFWmXLfV+++K0uAOiwgm1A=
github.com/jackc/puddle/v2 v2.2.1 h1:RhxXJtFG022u4ibrCSMSiu5aOq1i77R3OHKNJj77OAk=
github.com/jackc/puddle/v2 v2.2.1/go.mod h1:vriiEXHvEE654aYKXXjOvZM39qJ0q+azkZFrfEOc3H4=
github.com/jessevdk/go-flags v1.4.0/go.mod h1:4FA24M0QyGHXBuZZK/XkWh8h0e1EYbRYJSGM75WSRxI=
github.com/josharian/intern v1.0.0 h1:vlS4z54oSdjm0bgjRigI+G1HpF+tI+9rE5LLzOg8HmY=
github.com/josharian/intern v1.0.0/go.mod h1:5DoeVV0s6jJacbCEi61lwdGj/aVlrQvzHFFd8Hwg//Y=
github.com/jpillora/backoff v1.0.0/go.mod h1:J/6gKK9jxlEcS3zixgDgUAsiuZ7yrSoa/FX5e0EB2j4=
github.com/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnrnM=
github.com/json-iterator/go v1.1.12/go.mod h1:e30LSqwooZae/UwlEbR2852Gd8hjQvJoHmT4TnhNGBo=
github.com/julienschmidt/httprouter v1.3.0/go.mod h1:JR6WtHb+2LUe8TCKY3cZOxFyyO8IZAc4RVcycCCAKdM=
github.com/kisielk/errcheck v1.5.0/go.mod h1:pFxgyoBC7bSaBwPgfKdkLd5X25qrDl4LWUI2bnpBCr8=
github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck=
github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE=
Expand All @@ -175,7 +169,6 @@ github.com/modern-go/reflect2 v1.0.2/go.mod h1:yWuevngMOJpCy52FWWMvUC8ws7m/LJsjY
github.com/modocache/gover v0.0.0-20171022184752-b58185e213c5/go.mod h1:caMODM3PzxT8aQXRPkAt8xlV/e7d7w8GM5g0fa5F0D8=
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 h1:C3w9PqII01/Oq1c1nUAm88MOHcQC9l5mIlSMApZMrHA=
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822/go.mod h1:+n7T8mK8HuQTcFwEeznm/DIxMOiR9yIdICNftLE1DvQ=
github.com/mwitkow/go-conntrack v0.0.0-20190716064945-2f068394615f/go.mod h1:qRWi+5nqEBWmkhHvq77mSJWrCKwh8bxhgT7d/eI7P4U=
github.com/onsi/ginkgo/v2 v2.15.0 h1:79HwNRBAZHOEwrczrgSOPy+eFTTlIGELKy5as+ClttY=
github.com/onsi/ginkgo/v2 v2.15.0/go.mod h1:HlxMHtYF57y6Dpf+mc5529KKmSq9h2FpCF+/ZkwUxKM=
github.com/onsi/gomega v1.32.0 h1:JRYU78fJ1LPxlckP6Txi/EYqJvjtMrDC04/MM5XRHPk=
Expand Down
Loading
Loading