Skip to content
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

Resolve resource reference(initial PR) #2337

Merged
merged 3 commits into from
Jul 30, 2024

Conversation

gemmahou
Copy link
Collaborator

@gemmahou gemmahou commented Jul 22, 2024

Change description

Rebased on #2352

  1. Move resolve<resource> functions into apis/refs/ package.
  2. Make GetResourceID, GetLocation helper functions.

TODO:
Draft a tool to auto-generate the resolve logic.

Note:
I only include ComputeNetworkRef and ComputeTargetHTTPProxyRef in this PR. More references will be required for ComputeForwardingRule.

Tests you have done

  • Run make ready-pr to ensure this PR is ready for review.
  • Perform necessary E2E testing for changed resources.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we want to get rid of a generic resourceRef but using more dedicated kind. Any reasons to switch it back?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

by reading the code, I think this is just a naming thing. Maybe move the functions to the computenetoworkref.go file

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed generic resourceRef

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not a big fan of util.go. How about adapter.go (convert from the alpha resourceRef to beta Ref)? And move the GetResourceID and location to another file. And I think the GetResourceID and GetLocation shall not be under the ref dir eventually.

Copy link
Collaborator Author

@gemmahou gemmahou Jul 25, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I moved GetResourceID GetLocation and GetProjectID(not resolve project) to helper.go(or maybe a better name), those methods can be reused.

"sigs.k8s.io/controller-runtime/pkg/client"
)

func ComputeNetworkRef_ConvertToExternal(ctx context.Context, reader client.Reader, src client.Object, ref *v1alpha1.ResourceRef) (*v1alpha1.ResourceRef, error) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like this name! And I also think we can actually having our tools to auto-generate this functions when adding new resources from proto!

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

by reading the code, I think this is just a naming thing. Maybe move the functions to the computenetoworkref.go file

@gemmahou gemmahou changed the title feat: Resolve resource reference WIP: feat: Resolve resource reference Jul 24, 2024
@gemmahou gemmahou force-pushed the resolve-ref branch 2 times, most recently from 0e8e619 to 009d76b Compare July 25, 2024 18:25
@gemmahou gemmahou changed the title WIP: feat: Resolve resource reference feat: Resolve resource reference Jul 25, 2024
@@ -90,7 +91,7 @@ func (m *model) AdapterForObject(ctx context.Context, reader client.Reader, u *u
}

// Get GCP Project
projectRef, err := refs.ResolveProject(ctx, reader, obj, obj.Spec.ProjectRef)
projectRef, err := resolverefs.ResolveProjectRef(ctx, reader, obj, obj.Spec.ProjectRef)
Copy link
Collaborator

@justinsb justinsb Jul 26, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So I moved the resolver functions into the API package recently. The intent was that we could then have the resolvers implement an interface, and make a lot of this generic.

WDYT - can we put the resolver functions into the refs package? (We could even just add a ResolveRef function to each ref in this PR?)

Copy link
Collaborator Author

@gemmahou gemmahou Jul 26, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Regarding the placement of resolver functions, I don't have a strong preference. It could be api/refs/projectref.go or direct/refs/projectref.go. I think we should maintain consistency in the future. I'm open to moving the resolver functions to api/refs if that's preferable.

My main concern here is avoiding placing these functions in individual controller packages, for example direct/compute/networkref.go. Some references are used in multiple resources, like ComputeNetWork, which is referenced by CloudBuildWorkerPool and ComputeForwardingRule (and potentially others).

return location, nil
}

func GetProjectID(ctx context.Context, reader client.Reader, obj *unstructured.Unstructured) (string, error) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So this was called something like "ResolveProjectID" as a hint that we might have to go chase down some references etc. I don't mind renaming it if it's confusing, but maybe add a comment to the function?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I reverted it back to ResolveProjectID, will address in another PR if needed.

if len(tokens) == 5 && tokens[0] == "projects" && tokens[2] == "global" && tokens[3] == "targetHttpProxies" {
return &ComputeTargetHTTPProxy{
Project: tokens[1],
ComputeTargetHTTPProxyID: tokens[4]}, nil
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like we should set Location == global here, so it round trips?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, fixed!

ProjectID string
}

// ResolveProjectRef will resolve a ProjectRef to a Project, with the ProjectID.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TBD if this should actually just return a ProjectRef, to follow the pattern we seem to have built since. But no need to address in this PR (unless you want to)

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I reverted it back, will address in another PR if needed.

@gemmahou gemmahou force-pushed the resolve-ref branch 6 times, most recently from d69b507 to 48a7303 Compare July 29, 2024 20:27
@gemmahou
Copy link
Collaborator Author

Chatted with @yuwenma offline and reverted the function name change.

I will have a followup PR to resolve other refs(i.e. compute address, subnetwork etc), and a tool to auto-generate the resolver functions.

@gemmahou gemmahou changed the title feat: Resolve resource reference Resolve resource reference(initial PR) Jul 29, 2024
Copy link
Collaborator

@yuwenma yuwenma left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

/lgtm

@@ -30,3 +29,16 @@ func GetResourceID(u *unstructured.Unstructured) (string, error) {
}
return resourceID, nil
}

// TODO(yuhou): Location can be optional. Use provider default location when it's unset.
func GetLocation(obj *unstructured.Unstructured) (string, error) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If only called once, it is better to put the code inside ResolveTargetHTTPProxy or change this to a private function, in case it is misused by other functions (more context, I think more work needs to be done for location so it may not be ready to share yet)

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done! I think we'll eventually make GetLocation a helper function because it will be used in resolving more references. I agree that we need to do so when it's fully functional.

@google-oss-prow google-oss-prow bot added the lgtm label Jul 30, 2024
Copy link
Contributor

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: justinsb

The full list of commands accepted by this bot can be found here.

The pull request process is described here

Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@justinsb
Copy link
Collaborator

Ooops ... didn't realize @yuwenma was looking at this also.

/hold

(for yuwen's reviews)

@yuwenma
Copy link
Collaborator

yuwenma commented Jul 30, 2024

/hold cancel
Looks good! Thank you for the changes!

@google-oss-prow google-oss-prow bot merged commit 2b0f384 into GoogleCloudPlatform:master Jul 30, 2024
13 checks passed
@gemmahou gemmahou deleted the resolve-ref branch July 30, 2024 21:42
@yuwenma yuwenma added this to the 1.121 milestone Oct 25, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants