-
Notifications
You must be signed in to change notification settings - Fork 549
OCPBUGS-63720: orphan machines when managed identity is invalid on clus… #8296
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
Merged
openshift-merge-bot
merged 1 commit into
openshift:main
from
patilsuraj767:fix-OCPBUGS-63720
Jul 13, 2026
Merged
Changes from all commits
Commits
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
Some comments aren't visible on the classic Files Changed page.
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -5,6 +5,7 @@ import ( | |
| "encoding/json" | ||
| "fmt" | ||
| "testing" | ||
| "time" | ||
|
|
||
| . "github.com/onsi/gomega" | ||
|
|
||
|
|
@@ -17,6 +18,7 @@ import ( | |
| metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
|
|
||
| capiazure "sigs.k8s.io/cluster-api-provider-azure/api/v1beta1" | ||
| capiv1 "sigs.k8s.io/cluster-api/api/core/v1beta1" | ||
| "sigs.k8s.io/controller-runtime/pkg/client" | ||
| "sigs.k8s.io/controller-runtime/pkg/client/fake" | ||
| "sigs.k8s.io/controller-runtime/pkg/controller/controllerutil" | ||
|
|
@@ -527,3 +529,198 @@ func TestReconcileKMSConfigSecret(t *testing.T) { | |
| }) | ||
| } | ||
| } | ||
|
|
||
| func TestDeleteOrphanedMachines(t *testing.T) { | ||
| controlPlaneNamespace := "test-cp-namespace" | ||
|
|
||
| managedIdentitiesHC := &hyperv1.HostedCluster{ | ||
| Spec: hyperv1.HostedClusterSpec{ | ||
| Platform: hyperv1.PlatformSpec{ | ||
| Azure: &hyperv1.AzurePlatformSpec{ | ||
| AzureAuthenticationConfig: hyperv1.AzureAuthenticationConfiguration{ | ||
| ManagedIdentities: &hyperv1.AzureResourceManagedIdentities{}, | ||
| }, | ||
| }, | ||
| }, | ||
| }, | ||
| } | ||
|
|
||
| // staleDeletionTimestamp simulates a machine that has been pending deletion beyond the threshold. | ||
| staleDeletionTimestamp := metav1.NewTime(time.Now().Add(-(deletionFailedThreshold + time.Minute))) | ||
| recentDeletionTimestamp := metav1.NewTime(time.Now()) | ||
|
|
||
| deletionFailedConditions := capiv1.Conditions{ | ||
| { | ||
| Type: capiv1.ReadyCondition, | ||
| Status: corev1.ConditionFalse, | ||
| Reason: capiazure.DeletionFailedReason, | ||
| }, | ||
| } | ||
|
|
||
| testCases := []struct { | ||
| name string | ||
| hostedCluster *hyperv1.HostedCluster | ||
| azureMachines []capiazure.AzureMachine | ||
| expectedFinalizersRemoved bool | ||
| expectedError bool | ||
| }{ | ||
| { | ||
| name: "when ManagedIdentities is nil it should return early without modifying machines", | ||
| hostedCluster: &hyperv1.HostedCluster{ | ||
| Spec: hyperv1.HostedClusterSpec{ | ||
| Platform: hyperv1.PlatformSpec{ | ||
| Azure: &hyperv1.AzurePlatformSpec{ | ||
| AzureAuthenticationConfig: hyperv1.AzureAuthenticationConfiguration{}, | ||
| }, | ||
| }, | ||
| }, | ||
| }, | ||
| azureMachines: []capiazure.AzureMachine{ | ||
| { | ||
| ObjectMeta: metav1.ObjectMeta{ | ||
| Name: "machine-1", | ||
| Namespace: controlPlaneNamespace, | ||
| Finalizers: []string{capiazure.MachineFinalizer}, | ||
| DeletionTimestamp: &staleDeletionTimestamp, | ||
| }, | ||
| Status: capiazure.AzureMachineStatus{ | ||
| Conditions: deletionFailedConditions, | ||
| }, | ||
| }, | ||
| }, | ||
| expectedFinalizersRemoved: false, | ||
| expectedError: false, | ||
| }, | ||
| { | ||
| name: "when there are no machines it should succeed", | ||
| hostedCluster: managedIdentitiesHC, | ||
| azureMachines: []capiazure.AzureMachine{}, | ||
| expectedFinalizersRemoved: false, | ||
| expectedError: false, | ||
| }, | ||
| { | ||
| name: "when a machine has a stale DeletionTimestamp with DeletionFailed condition it should remove finalizers", | ||
| hostedCluster: managedIdentitiesHC, | ||
| azureMachines: []capiazure.AzureMachine{ | ||
| { | ||
| ObjectMeta: metav1.ObjectMeta{ | ||
| Name: "machine-1", | ||
| Namespace: controlPlaneNamespace, | ||
| Finalizers: []string{capiazure.MachineFinalizer}, | ||
| DeletionTimestamp: &staleDeletionTimestamp, | ||
| }, | ||
| Status: capiazure.AzureMachineStatus{ | ||
| Conditions: deletionFailedConditions, | ||
| }, | ||
| }, | ||
| }, | ||
| expectedFinalizersRemoved: true, | ||
| expectedError: false, | ||
| }, | ||
| { | ||
| name: "when a machine has a recent DeletionTimestamp with DeletionFailed condition it should not remove finalizers", | ||
| hostedCluster: managedIdentitiesHC, | ||
| azureMachines: []capiazure.AzureMachine{ | ||
| { | ||
| ObjectMeta: metav1.ObjectMeta{ | ||
| Name: "machine-1", | ||
| Namespace: controlPlaneNamespace, | ||
| Finalizers: []string{capiazure.MachineFinalizer}, | ||
| DeletionTimestamp: &recentDeletionTimestamp, | ||
| }, | ||
| Status: capiazure.AzureMachineStatus{ | ||
| Conditions: deletionFailedConditions, | ||
| }, | ||
| }, | ||
| }, | ||
| expectedFinalizersRemoved: false, | ||
| expectedError: false, | ||
| }, | ||
|
patilsuraj767 marked this conversation as resolved.
|
||
| { | ||
| name: "when a machine has a stale DeletionTimestamp without DeletionFailed condition it should not remove finalizers", | ||
| hostedCluster: managedIdentitiesHC, | ||
| azureMachines: []capiazure.AzureMachine{ | ||
| { | ||
| ObjectMeta: metav1.ObjectMeta{ | ||
| Name: "machine-1", | ||
| Namespace: controlPlaneNamespace, | ||
| Finalizers: []string{capiazure.MachineFinalizer}, | ||
| DeletionTimestamp: &staleDeletionTimestamp, | ||
| }, | ||
| Status: capiazure.AzureMachineStatus{ | ||
| Conditions: capiv1.Conditions{ | ||
| { | ||
| Type: capiv1.ReadyCondition, | ||
| Status: corev1.ConditionTrue, | ||
| }, | ||
| }, | ||
| }, | ||
| }, | ||
| }, | ||
| expectedFinalizersRemoved: false, | ||
| expectedError: false, | ||
| }, | ||
| { | ||
| name: "when a machine is not pending deletion it should not remove finalizers regardless of conditions", | ||
| hostedCluster: managedIdentitiesHC, | ||
| azureMachines: []capiazure.AzureMachine{ | ||
| { | ||
| ObjectMeta: metav1.ObjectMeta{ | ||
| Name: "machine-1", | ||
| Namespace: controlPlaneNamespace, | ||
| Finalizers: []string{capiazure.MachineFinalizer}, | ||
| }, | ||
| Status: capiazure.AzureMachineStatus{ | ||
| Conditions: deletionFailedConditions, | ||
| }, | ||
| }, | ||
| }, | ||
| expectedFinalizersRemoved: false, | ||
| expectedError: false, | ||
| }, | ||
| } | ||
|
|
||
| for _, tc := range testCases { | ||
| t.Run(tc.name, func(t *testing.T) { | ||
|
Contributor
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. nit: missing |
||
| t.Parallel() | ||
| g := NewWithT(t) | ||
| ctx := context.Background() | ||
|
|
||
| objects := make([]client.Object, len(tc.azureMachines)) | ||
| for i := range tc.azureMachines { | ||
| objects[i] = &tc.azureMachines[i] | ||
| } | ||
|
|
||
| fakeClient := fake.NewClientBuilder(). | ||
| WithScheme(api.Scheme). | ||
| WithObjects(objects...). | ||
| WithStatusSubresource(objects...). | ||
| Build() | ||
|
|
||
| azure := Azure{} | ||
|
|
||
| err := azure.DeleteOrphanedMachines(ctx, fakeClient, tc.hostedCluster, controlPlaneNamespace) | ||
|
|
||
| if tc.expectedError { | ||
| g.Expect(err).To(HaveOccurred()) | ||
| } else { | ||
| g.Expect(err).ToNot(HaveOccurred()) | ||
| } | ||
|
|
||
| azureMachineList := &capiazure.AzureMachineList{} | ||
| g.Expect(fakeClient.List(ctx, azureMachineList, client.InNamespace(controlPlaneNamespace))).To(Succeed()) | ||
|
|
||
| if tc.expectedFinalizersRemoved { | ||
| for _, machine := range azureMachineList.Items { | ||
| if !machine.DeletionTimestamp.IsZero() { | ||
| g.Expect(machine.Finalizers).To(BeEmpty(), "finalizers should be removed for machines with DeletionFailed condition") | ||
| } | ||
| } | ||
| } else { | ||
| for _, machine := range azureMachineList.Items { | ||
| g.Expect(machine.Finalizers).To(Equal(tc.azureMachines[0].Finalizers), "finalizers should not be modified") | ||
| } | ||
| } | ||
| }) | ||
| } | ||
| } | ||
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.
Do we have a Kusto query showing our distribution of deletion times? If so, please leave it here so someone can re-run the analysis later, and specify what percentile 10 minutes is - p99? p99.99?