-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ensure finalizers are resilient on reconciliation
- Loading branch information
Aditya Bhatia
authored and
Aditya Bhatia
committed
Sep 20, 2023
1 parent
2639f2c
commit f0daa7d
Showing
3 changed files
with
146 additions
and
3 deletions.
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
/* | ||
Copyright 2023 The Kubernetes Authors. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package framework | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"sigs.k8s.io/cluster-api/util/patch" | ||
|
||
. "github.com/onsi/gomega" | ||
|
||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" | ||
clusterctlcluster "sigs.k8s.io/cluster-api/cmd/clusterctl/client/cluster" | ||
"sigs.k8s.io/controller-runtime/pkg/client" | ||
) | ||
|
||
// ValidateFinalizersResilience checks that expected finalizers are in place, deletes them, and verifies that expected finalizers are properly added again. | ||
func ValidateFinalizersResilience(ctx context.Context, proxy ClusterProxy, namespace, clusterName string, assertFuncs ...map[string]func(reference []metav1.OwnerReference) error) { | ||
// Get the initial list of finalizers | ||
initialFinalizers := getFinalizersFromAllObjs(ctx, proxy, namespace) | ||
|
||
clusterKey := client.ObjectKey{Namespace: namespace, Name: clusterName} | ||
|
||
// Removes all the finalizers. | ||
// We are testing the worst-case scenario, i.e. all finalizers are deleted, even when reconciliation is paused. | ||
// Deletion of the finalizers can either happen from a system generated attack or by mistake, which is an edge case. | ||
// The reconciliation loop should be able to recover from this, by adding the required finalizers back. | ||
setClusterPause(ctx, proxy.GetClient(), clusterKey, true) | ||
|
||
// Once all Clusters are paused remove the finalizers from all objects in the graph. | ||
removeFinalizers(ctx, proxy, namespace) | ||
|
||
// Unpause the cluster. | ||
setClusterPause(ctx, proxy.GetClient(), clusterKey, false) | ||
|
||
// Annotate the clusterClass, if one is in use, to speed up reconciliation. This ensures ClusterClass finalizers | ||
// are re-reconciled before asserting the owner reference graph. | ||
forceClusterClassReconcile(ctx, proxy.GetClient(), clusterKey) | ||
|
||
// Check that the finalizers are as expected after further reconciliations. | ||
assertFinalizersExist(ctx, proxy, namespace, initialFinalizers) | ||
} | ||
|
||
func getFinalizersFromAllObjs(ctx context.Context, proxy ClusterProxy, namespace string) map[string][]string { | ||
finalizers := map[string][]string{} | ||
graph, err := clusterctlcluster.GetOwnerGraph(ctx, proxy.GetKubeconfigPath(), namespace) | ||
Expect(err).To(BeNil()) | ||
for _, v := range graph { | ||
obj := &unstructured.Unstructured{} | ||
obj.SetAPIVersion(v.Object.APIVersion) | ||
obj.SetKind(v.Object.Kind) | ||
err := proxy.GetClient().Get(ctx, client.ObjectKey{Namespace: v.Object.Namespace, Name: v.Object.Name}, obj) | ||
Expect(err).To(BeNil()) | ||
if obj.GetFinalizers() != nil { | ||
finalizers[fmt.Sprintf("%s/%s", v.Object.Kind, client.ObjectKey{Name: v.Object.Name, Namespace: v.Object.Namespace}.String())] = obj.GetFinalizers() | ||
} | ||
} | ||
return finalizers | ||
} | ||
|
||
func removeFinalizers(ctx context.Context, proxy ClusterProxy, namespace string) { | ||
graph, err := clusterctlcluster.GetOwnerGraph(ctx, namespace, proxy.GetKubeconfigPath()) | ||
Expect(err).ToNot(HaveOccurred()) | ||
for _, object := range graph { | ||
ref := object.Object | ||
obj := new(unstructured.Unstructured) | ||
obj.SetAPIVersion(ref.APIVersion) | ||
obj.SetKind(ref.Kind) | ||
obj.SetName(ref.Name) | ||
|
||
Expect(proxy.GetClient().Get(ctx, client.ObjectKey{Namespace: namespace, Name: object.Object.Name}, obj)).To(Succeed()) | ||
helper, err := patch.NewHelper(obj, proxy.GetClient()) | ||
Expect(err).ToNot(HaveOccurred()) | ||
obj.SetFinalizers([]string{}) | ||
Expect(helper.Patch(ctx, obj)).To(Succeed()) | ||
} | ||
} | ||
|
||
func assertFinalizersExist(ctx context.Context, proxy ClusterProxy, namespace string, initialFinalizers map[string][]string) { | ||
Eventually(func() { | ||
afterDeleteFinalizers := getFinalizersFromAllObjs(ctx, proxy, namespace) | ||
|
||
missing := map[string][]string{} | ||
for k, v := range initialFinalizers { | ||
if _, ok := afterDeleteFinalizers[k]; !ok { | ||
missing[k] = v | ||
} | ||
} | ||
missingString := "" | ||
for k, v := range missing { | ||
missingString = fmt.Sprintf("%s\n%s %s", missingString, k, v) | ||
} | ||
Expect(len(missing)).To(Equal(0), missingString) | ||
}) | ||
} |