-
Notifications
You must be signed in to change notification settings - Fork 33
Cyclomatic complexity: globalize fetch dependency helper #621
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
Open
eshulman2
wants to merge
1
commit into
k-orc:main
Choose a base branch
from
eshulman2:depHelper
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
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
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 |
|---|---|---|
| @@ -0,0 +1,66 @@ | ||
| /* | ||
| Copyright 2025 The ORC 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 dependency | ||
|
|
||
| import ( | ||
| "context" | ||
| "fmt" | ||
|
|
||
| apierrors "k8s.io/apimachinery/pkg/api/errors" | ||
| "sigs.k8s.io/controller-runtime/pkg/client" | ||
|
|
||
| orcv1alpha1 "github.com/k-orc/openstack-resource-controller/v2/api/v1alpha1" | ||
| "github.com/k-orc/openstack-resource-controller/v2/internal/controllers/generic/progress" | ||
| ) | ||
|
|
||
| // FetchDependency fetches a resource by name and checks if it's ready. | ||
| // Unlike GetDependency on DeletionGuardDependency, this doesn't add finalizers | ||
| // and is suitable for one-off lookups like resolving refs in import filters. | ||
| // | ||
| // If name is empty, returns (nil, nil) - no fetch is performed. | ||
| // | ||
| // Returns: | ||
| // - The fetched object (nil if name is empty, not found, or not ready) | ||
| // - ReconcileStatus indicating wait state or error (nil if name is empty) | ||
| func FetchDependency[TP DependencyType[T], T any]( | ||
| ctx context.Context, | ||
| k8sClient client.Client, | ||
| namespace string, | ||
| name *orcv1alpha1.KubernetesNameRef, | ||
| kind string, | ||
| isReady func(TP) bool, | ||
| ) (TP, progress.ReconcileStatus) { | ||
| if name == nil { | ||
| return nil, nil | ||
| } | ||
|
|
||
| var obj TP = new(T) | ||
| objectKey := client.ObjectKey{Name: string(*name), Namespace: namespace} | ||
|
|
||
| if err := k8sClient.Get(ctx, objectKey, obj); err != nil { | ||
| if apierrors.IsNotFound(err) { | ||
| return nil, progress.NewReconcileStatus().WaitingOnObject(kind, string(*name), progress.WaitingOnCreation) | ||
| } | ||
| return nil, progress.WrapError(fmt.Errorf("fetching %s %s: %w", kind, string(*name), err)) | ||
| } | ||
|
|
||
| if !isReady(obj) { | ||
| return nil, progress.NewReconcileStatus().WaitingOnObject(kind, string(*name), progress.WaitingOnReady) | ||
| } | ||
|
|
||
| return obj, nil | ||
| } |
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.
Was this a bug in the original template? I'm unclean why this is changed while
ImportDependenciesremains identical.