-
Notifications
You must be signed in to change notification settings - Fork 62
OCPCLOUD-2992: machineset migration e2e #287
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
Closed
Closed
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 |
|---|---|---|
| @@ -0,0 +1,104 @@ | ||
| package framework | ||
|
|
||
| import ( | ||
| "errors" | ||
| "fmt" | ||
| "strings" | ||
|
|
||
| apierrors "k8s.io/apimachinery/pkg/api/errors" | ||
| awsv1 "sigs.k8s.io/cluster-api-provider-aws/v2/api/v1beta2" | ||
| azurev1 "sigs.k8s.io/cluster-api-provider-azure/api/v1beta1" | ||
| gcpv1 "sigs.k8s.io/cluster-api-provider-gcp/api/v1beta1" | ||
| "sigs.k8s.io/controller-runtime/pkg/client" | ||
| ) | ||
|
|
||
| // GetAWSMachineTemplateByName gets awsMachineTemplate by its name from the default cluster API namespace. | ||
| func GetAWSMachineTemplateByName(cl client.Client, name string) (*awsv1.AWSMachineTemplate, error) { | ||
| var awsMachineTemplate = &awsv1.AWSMachineTemplate{} | ||
|
|
||
| key := client.ObjectKey{Namespace: CAPINamespace, Name: name} | ||
| if err := cl.Get(ctx, key, awsMachineTemplate); err != nil { | ||
| return nil, err | ||
| } | ||
| return awsMachineTemplate, nil | ||
| } | ||
|
|
||
| // GetAWSMachineTemplateByPrefix gets awsMachineTemplate by its prefix from the default cluster API namespace. | ||
| func GetAWSMachineTemplateByPrefix(cl client.Client, prefix string) (*awsv1.AWSMachineTemplate, error) { | ||
| templateList := &awsv1.AWSMachineTemplateList{} | ||
| if err := cl.List(ctx, templateList, client.InNamespace(CAPINamespace)); err != nil { | ||
| return nil, fmt.Errorf("failed to list AWSMachineTemplates: %w", err) | ||
| } | ||
|
|
||
| var matches []*awsv1.AWSMachineTemplate | ||
| for i, t := range templateList.Items { | ||
| if strings.HasPrefix(t.Name, prefix) { | ||
| matches = append(matches, &templateList.Items[i]) | ||
| } | ||
| } | ||
|
|
||
| switch len(matches) { | ||
| case 0: | ||
| return nil, fmt.Errorf("no AWSMachineTemplate found with prefix %q", prefix) | ||
| case 1: | ||
| return matches[0], nil | ||
| default: | ||
| return nil, fmt.Errorf("multiple AWSMachineTemplates found with prefix %q (%d matches)", prefix, len(matches)) | ||
| } | ||
| } | ||
|
|
||
| // DeleteAWSMachineTemplateByPrefix deletes all AWSMachineTemplates with matching name prefix | ||
| func DeleteAWSMachineTemplateByPrefix(cl client.Client, prefix string) error { | ||
| templateList := &awsv1.AWSMachineTemplateList{} | ||
| if err := cl.List(ctx, templateList, client.InNamespace(CAPINamespace)); err != nil { | ||
| return fmt.Errorf("failed to list AWSMachineTemplates: %w", err) | ||
| } | ||
|
|
||
| var deleteErrors []error | ||
| var deletedCount int | ||
|
|
||
| for i := range templateList.Items { | ||
| if strings.HasPrefix(templateList.Items[i].Name, prefix) { | ||
| if err := cl.Delete(ctx, &templateList.Items[i]); err != nil { | ||
| if !apierrors.IsNotFound(err) { | ||
| deleteErrors = append(deleteErrors, fmt.Errorf("failed to delete %s: %w", templateList.Items[i].Name, err)) | ||
| } | ||
| continue | ||
| } | ||
| deletedCount++ | ||
| } | ||
| } | ||
|
|
||
| if len(deleteErrors) > 0 { | ||
| return fmt.Errorf("deleted %d templates, but encountered %d errors: %v", | ||
| deletedCount, len(deleteErrors), errors.Join(deleteErrors...)) | ||
| } | ||
|
|
||
| if deletedCount == 0 { | ||
| return fmt.Errorf("no templates found with prefix %q", prefix) | ||
| } | ||
|
|
||
| return nil | ||
| } | ||
|
|
||
| // GetAzureMachineTemplate gets azureMachineTemplate by its name from the default cluster API namespace. | ||
| func GetAzureMachineTemplate(cl client.Client, name string) (*azurev1.AzureMachineTemplate, error) { | ||
| var azureMachineTemplate = &azurev1.AzureMachineTemplate{} | ||
|
|
||
| key := client.ObjectKey{Namespace: CAPINamespace, Name: name} | ||
| if err := cl.Get(ctx, key, azureMachineTemplate); err != nil { | ||
| return nil, err | ||
| } | ||
| return azureMachineTemplate, nil | ||
| } | ||
|
|
||
| // GetGCPMachineTemplate gets gcpMachineTemplate by its name from the default cluster API namespace. | ||
| func GetGCPMachineTemplate(cl client.Client, name string) (*gcpv1.GCPMachineTemplate, error) { | ||
| var gcpMachineTemplate = &gcpv1.GCPMachineTemplate{} | ||
|
|
||
| key := client.ObjectKey{Namespace: CAPINamespace, Name: name} | ||
| if err := cl.Get(ctx, key, gcpMachineTemplate); err != nil { | ||
| return nil, err | ||
| } | ||
| return gcpMachineTemplate, nil | ||
| } |
This file contains hidden or 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
Oops, something went wrong.
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.
What if the object isn't found? Would this loop forever?