-
Notifications
You must be signed in to change notification settings - Fork 209
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 asoctl aborting import when an error occurs #3212
Merged
Merged
Changes from 5 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
139d5eb
Split resource import from discovery of child resources
theunrepentantgeek 765b2d8
Update ARM resource for modified interface
theunrepentantgeek da3e1b1
Update resource importer
theunrepentantgeek 72c916a
Code gardening
theunrepentantgeek cb08cc4
Remove testing code
theunrepentantgeek 60e115e
Code gardening
theunrepentantgeek 5ef9b72
Merge branch 'main' into fix/asoctl-resource-listing
theunrepentantgeek File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,6 +8,7 @@ package importing | |
import ( | ||
"context" | ||
"fmt" | ||
kerrors "k8s.io/apimachinery/pkg/util/errors" | ||
"net/http" | ||
"reflect" | ||
"strings" | ||
|
@@ -68,7 +69,7 @@ func NewImportableARMResource( | |
} | ||
|
||
// GroupKind returns the GroupKind of the resource being imported. | ||
// (may be empty if the GK can't be determined) | ||
// Returned value may be empty if the GK can't be determined. | ||
func (i *importableARMResource) GroupKind() schema.GroupKind { | ||
gk, _ := FindGroupKindForResourceType(i.armID.ResourceType.String()) | ||
return gk | ||
|
@@ -94,11 +95,44 @@ func (i *importableARMResource) Resource() genruntime.MetaObject { | |
// ctx is the context to use for the import. | ||
// Returns a slice of child resources needing to be imported (if any), and/or an error. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. minor: fix docs |
||
// Both are returned to allow returning partial results in the case of a partial failure. | ||
func (i *importableARMResource) Import(ctx context.Context, bar *mpb.Bar) ([]ImportableResource, error) { | ||
var ref genruntime.ResourceReference | ||
ref, err := i.importResource(ctx, i.armID) | ||
func (i *importableARMResource) Import(ctx context.Context, bar *mpb.Bar) error { | ||
// Create an importable blank object into which we capture the current state of the resource | ||
importable, err := i.createImportableObjectFromID(i.owner, i.armID) | ||
if err != nil { | ||
// Error doesn't need additional context | ||
return err | ||
} | ||
|
||
loader := i.createImportFunction(importable) | ||
result, err := loader(ctx, importable, i.owner) | ||
if err != nil { | ||
return nil, err | ||
return err | ||
} | ||
|
||
if because, skipped := result.Skipped(); skipped { | ||
gk := importable.GetObjectKind().GroupVersionKind().GroupKind() | ||
return NewImportSkippedError(gk, i.armID.Name, because) | ||
} | ||
|
||
i.resource = importable | ||
bar.SetCurrent(1) | ||
|
||
return nil | ||
} | ||
|
||
func (i *importableARMResource) FindChildren(ctx context.Context, bar *mpb.Bar) ([]ImportableResource, error) { | ||
if i.resource == nil { | ||
// Nothing to do | ||
return nil, nil | ||
theunrepentantgeek marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
gvk := i.resource.GetObjectKind().GroupVersionKind() | ||
|
||
ref := genruntime.ResourceReference{ | ||
Group: gvk.Group, | ||
Kind: gvk.Kind, | ||
Name: i.resource.GetName(), | ||
ARMID: i.armID.String(), | ||
} | ||
|
||
var result []ImportableResource | ||
|
@@ -120,56 +154,28 @@ func (i *importableARMResource) Import(ctx context.Context, bar *mpb.Bar) ([]Imp | |
total := int64(len(childTypes) + 1) | ||
bar.SetTotal(total, false) | ||
|
||
bar.SetCurrent(1) | ||
|
||
// While we're looking for subresources, we need to treat any errors that occur as independent. | ||
// Some potential subresource types can have limited accessibility (e.g. the subscriber may not | ||
// be onboarded to a preview API), so we don't want to fail the entire import if we can't import | ||
// a single candidate subresource type. | ||
var errs []error | ||
for _, subType := range childTypes { | ||
subResources, err := i.importChildResources(ctx, ref, subType) | ||
if err != nil { | ||
gk, _ := FindGroupKindForResourceType(subType) // If this was going to error, it would have already | ||
return nil, errors.Wrapf(err, "importing %s/%s for resource %s", gk.Group, gk.Kind, i.armID) | ||
errs = append(errs, errors.Wrapf(err, "importing %s/%s", gk.Group, gk.Kind)) | ||
continue | ||
} | ||
|
||
result = append(result, subResources...) | ||
bar.Increment() | ||
} | ||
|
||
return result, nil | ||
} | ||
|
||
// importResource imports the actual resource, returning a reference to the resource | ||
func (i *importableARMResource) importResource( | ||
ctx context.Context, | ||
id *arm.ResourceID, | ||
) (genruntime.ResourceReference, error) { | ||
// Create an importable blank object into which we capture the current state of the resource | ||
importable, err := i.createImportableObjectFromID(i.owner, id) | ||
if err != nil { | ||
// Error doesn't need additional context | ||
return genruntime.ResourceReference{}, err | ||
} | ||
|
||
loader := i.createImportFunction(importable) | ||
result, err := loader(ctx, importable, i.owner) | ||
if err != nil { | ||
return genruntime.ResourceReference{}, err | ||
} | ||
|
||
if because, skipped := result.Skipped(); skipped { | ||
gk := importable.GetObjectKind().GroupVersionKind().GroupKind() | ||
return genruntime.ResourceReference{}, NewImportSkippedError(gk, id.Name, because) | ||
} | ||
|
||
gvk := importable.GetObjectKind().GroupVersionKind() | ||
i.resource = importable | ||
|
||
ref := genruntime.ResourceReference{ | ||
Group: gvk.Group, | ||
Kind: gvk.Kind, | ||
Name: importable.GetName(), | ||
ARMID: i.armID.String(), | ||
} | ||
|
||
return ref, nil | ||
return result, | ||
errors.Wrapf( | ||
kerrors.NewAggregate(errs), | ||
"importing childresources of %s", | ||
i.armID) | ||
} | ||
|
||
func (i *importableARMResource) createImportFunction( | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
minor: Wrong ordering, should be below with the non-stdlib imports.