generated from amazon-archives/__template_Apache-2.0
-
Notifications
You must be signed in to change notification settings - Fork 37
Requeue instead of throwing an error when onwer of kind can't be found #417
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
k8s-ci-robot
merged 3 commits into
kubernetes-sigs:main
from
shapeblue:requeue-instead-of-logging-error
Mar 12, 2025
Merged
Changes from 1 commit
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,209 @@ | ||
| /* | ||
| Copyright 2022 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 utils_test | ||
|
|
||
| import ( | ||
| "context" | ||
|
|
||
| "github.com/go-logr/logr" | ||
| "github.com/golang/mock/gomock" | ||
| . "github.com/onsi/ginkgo/v2" | ||
| . "github.com/onsi/gomega" | ||
| metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
| "k8s.io/apimachinery/pkg/runtime" | ||
| "k8s.io/client-go/tools/record" | ||
| infrav1 "sigs.k8s.io/cluster-api-provider-cloudstack/api/v1beta3" | ||
| "sigs.k8s.io/cluster-api-provider-cloudstack/controllers/utils" | ||
| dummies "sigs.k8s.io/cluster-api-provider-cloudstack/test/dummies/v1beta3" | ||
| clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1" | ||
| ctrl "sigs.k8s.io/controller-runtime" | ||
| "sigs.k8s.io/controller-runtime/pkg/client" | ||
| "sigs.k8s.io/controller-runtime/pkg/client/fake" | ||
| ) | ||
|
|
||
| // mockConcreteRunner is a minimal implementation of the runner interface | ||
| type mockConcreteRunner struct{} | ||
|
|
||
| func (m *mockConcreteRunner) ReconcileDelete() (ctrl.Result, error) { | ||
| return ctrl.Result{}, nil | ||
| } | ||
|
|
||
| func (m *mockConcreteRunner) Reconcile() (ctrl.Result, error) { | ||
| return ctrl.Result{}, nil | ||
| } | ||
|
|
||
| var _ = Describe("ReconciliationRunner", func() { | ||
| var ( | ||
| mockCtrl *gomock.Controller | ||
| k8sClient client.Client | ||
| scheme *runtime.Scheme | ||
| baseRunner *utils.ReconciliationRunner | ||
| ctx context.Context | ||
| mockRunner *mockConcreteRunner | ||
| ) | ||
|
|
||
| BeforeEach(func() { | ||
| mockCtrl = gomock.NewController(GinkgoT()) | ||
| scheme = runtime.NewScheme() | ||
| Expect(infrav1.AddToScheme(scheme)).To(Succeed()) | ||
| Expect(clusterv1.AddToScheme(scheme)).To(Succeed()) | ||
|
|
||
| k8sClient = fake.NewClientBuilder().WithScheme(scheme).Build() | ||
| ctx = context.Background() | ||
| mockRunner = &mockConcreteRunner{} | ||
|
|
||
| // Create the base reconciler | ||
| base := utils.ReconcilerBase{ | ||
| K8sClient: k8sClient, | ||
| Scheme: scheme, | ||
| BaseLogger: logr.Discard(), | ||
| Recorder: record.NewFakeRecorder(10), | ||
| } | ||
|
|
||
| // Create a reconciliation runner with our mock concrete runner | ||
| baseRunner = utils.NewRunner(mockRunner, &infrav1.CloudStackMachine{}, "TestController") | ||
| baseRunner.UsingBaseReconciler(base) | ||
| baseRunner.WithRequestCtx(ctx) | ||
|
|
||
| // Setup a fake Request | ||
| baseRunner.ForRequest(ctrl.Request{ | ||
| NamespacedName: client.ObjectKey{ | ||
| Namespace: "default", | ||
| Name: "test-machine", | ||
| }, | ||
| }) | ||
| }) | ||
|
|
||
| AfterEach(func() { | ||
| mockCtrl.Finish() | ||
| }) | ||
|
|
||
| Describe("GetParent", func() { | ||
| var ( | ||
| child *infrav1.CloudStackMachine | ||
| ) | ||
|
|
||
| BeforeEach(func() { | ||
| dummies.SetDummyVars() | ||
|
|
||
| // Set up child object | ||
| child = &infrav1.CloudStackMachine{ | ||
| ObjectMeta: metav1.ObjectMeta{ | ||
| Name: "test-child", | ||
| Namespace: "default", | ||
| }, | ||
| } | ||
| }) | ||
|
|
||
| Context("when parent exists and is correctly referenced", func() { | ||
| var parent *clusterv1.Machine | ||
| BeforeEach(func() { | ||
| parent = &clusterv1.Machine{ | ||
| ObjectMeta: metav1.ObjectMeta{ | ||
| Name: "test-parent", | ||
| Namespace: "default", | ||
| }, | ||
| } | ||
| // Set up owner reference | ||
| child.OwnerReferences = []metav1.OwnerReference{ | ||
| { | ||
| APIVersion: clusterv1.GroupVersion.String(), | ||
| Kind: "Machine", | ||
| Name: parent.Name, | ||
| UID: "test-uid", | ||
| }, | ||
| } | ||
|
|
||
| // Create the objects in the fake client | ||
| Expect(k8sClient.Create(ctx, parent)).To(Succeed()) | ||
| Expect(k8sClient.Create(ctx, child)).To(Succeed()) | ||
| }) | ||
|
|
||
| It("should find the parent successfully", func() { | ||
| // Create an empty parent object to be filled | ||
| parentToFind := &clusterv1.Machine{} | ||
|
|
||
| // Call GetParent | ||
| result, err := baseRunner.GetParent(child, parentToFind)() | ||
|
|
||
| // Check results | ||
| Expect(err).NotTo(HaveOccurred()) | ||
| Expect(result).To(Equal(ctrl.Result{})) | ||
| Expect(parentToFind.Name).To(Equal(parent.Name)) | ||
| Expect(parentToFind.Namespace).To(Equal(parent.Namespace)) | ||
| }) | ||
| }) | ||
|
|
||
| Context("when parent doesn't exist", func() { | ||
| BeforeEach(func() { | ||
| // Set up owner reference to non-existent parent | ||
| child.OwnerReferences = []metav1.OwnerReference{ | ||
| { | ||
| APIVersion: clusterv1.GroupVersion.String(), | ||
| Kind: "Machine", | ||
| Name: "non-existent-parent", | ||
| UID: "test-uid", | ||
| }, | ||
| } | ||
|
|
||
| // Create only the child in the fake client | ||
| Expect(k8sClient.Create(ctx, child)).To(Succeed()) | ||
| }) | ||
|
|
||
| It("should return an error", func() { | ||
| // Create an empty parent object to be filled | ||
| parentToFind := &clusterv1.Machine{} | ||
|
|
||
| // Call GetParent | ||
| _, err := baseRunner.GetParent(child, parentToFind)() | ||
|
|
||
| // Check results | ||
| Expect(err).To(HaveOccurred()) | ||
| Expect(err.Error()).To(ContainSubstring("not found")) | ||
| }) | ||
| }) | ||
|
|
||
| Context("when no owner reference of requested kind exists", func() { | ||
| BeforeEach(func() { | ||
| // Set up owner reference to different kind | ||
| child.OwnerReferences = []metav1.OwnerReference{ | ||
| { | ||
| APIVersion: "different.api/v1", | ||
| Kind: "DifferentKind", | ||
| Name: "different-name", | ||
| UID: "test-uid", | ||
| }, | ||
| } | ||
|
|
||
| // Create only the child in the fake client | ||
| Expect(k8sClient.Create(ctx, child)).To(Succeed()) | ||
| }) | ||
|
|
||
| It("should requeue with error message", func() { | ||
| // Create an empty parent object to be filled | ||
| parentToFind := &clusterv1.Machine{} | ||
|
|
||
| // Call GetParent | ||
| result, err := baseRunner.GetParent(child, parentToFind)() | ||
|
|
||
| // Check results | ||
| Expect(err).NotTo(HaveOccurred()) | ||
| Expect(result.RequeueAfter).To(Equal(utils.RequeueTimeout)) | ||
| }) | ||
| }) | ||
| }) | ||
| }) |
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,29 @@ | ||
| /* | ||
| Copyright 2022 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 utils_test | ||
|
|
||
| import ( | ||
| "testing" | ||
|
|
||
| . "github.com/onsi/ginkgo/v2" | ||
| . "github.com/onsi/gomega" | ||
| ) | ||
|
|
||
| func TestUtils(t *testing.T) { | ||
| RegisterFailHandler(Fail) | ||
| RunSpecs(t, "Utils Suite") | ||
| } |
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
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.
Uh oh!
There was an error while loading. Please reload this page.