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

Create a go module for the api package #37266

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

alvaroaleman
Copy link
Contributor

@alvaroaleman alvaroaleman commented Jan 25, 2025

If someone imports the Cilium API types, they currently end up with a transient dependency to everything Cilium depends on, which is a lot. In order to avoid this, this change creates a go module for the api. That doesn't yet fully solve the problem, because that go module still depends on the main cilium module. We can clean that up in a follow-up as well as adding a check to forbid adding that.

After this change is merged, anytime we tag a new release we have to make sure that the same git commit gets tagged for the main module as it already happens today, as well as for the submodule in the form pkg/k8s/apis/cilium.io/v2/v1.2.3, ref. I am happy to look into this if someone points me to the release tooling.

Ref #36265 but I'd like to keep the issue open until any dependency from the api module to the main module is cleaned up.

cc @joestringer

Please ensure your pull request adheres to the following guidelines:

  • For first time contributors, read Submitting a pull request
  • All code is covered by unit and/or runtime tests where feasible.
  • All commits contain a well written commit description including a title,
    description and a Fixes: #XXX line if the commit addresses a particular
    GitHub issue.
  • If your commit description contains a Fixes: <commit-id> tag, then
    please add the commit author[s] as reviewer[s] to this issue.
  • All commits are signed off. See the section Developer’s Certificate of Origin
  • Provide a title or release-note blurb suitable for the release notes.
  • Are you a user of Cilium? Please add yourself to the Users doc
  • Thanks for contributing!

@alvaroaleman alvaroaleman requested review from a team as code owners January 25, 2025 00:18
@maintainer-s-little-helper
Copy link

Commit 682ae69 does not match "(?m)^Signed-off-by:".

Please follow instructions provided in https://docs.cilium.io/en/stable/contributing/development/contributing_guide/#developer-s-certificate-of-origin

@maintainer-s-little-helper maintainer-s-little-helper bot added dont-merge/needs-sign-off The author needs to add signoff to their commits before merge. dont-merge/needs-release-note-label The author needs to describe the release impact of these changes. labels Jan 25, 2025
@github-actions github-actions bot added the kind/community-contribution This was a contribution made by a community member. label Jan 25, 2025
@maintainer-s-little-helper
Copy link

Commit 682ae69 does not match "(?m)^Signed-off-by:".

Please follow instructions provided in https://docs.cilium.io/en/stable/contributing/development/contributing_guide/#developer-s-certificate-of-origin

@maintainer-s-little-helper maintainer-s-little-helper bot removed the dont-merge/needs-sign-off The author needs to add signoff to their commits before merge. label Jan 25, 2025
If someone imports the Cilium API types, they currently endup with a
transient dependency to everything Cilium depends on, which is a lot.
In order to avoid this, this change creates a go module for the api.
That doesn't yet fully solve the problem, because that go module still
depends on the main cilium module. We can clean that up in a follow-up
as well as adding a check to forbid adding that.

After this change is merged, anytime we tag a new release we have to
make sure that the same git commit gets tagged for the main module as it
already happens today, as well as for the submodule in the form
` pkg/k8s/apis/cilium.io/v2/v1.2.3`, ref[0]. I am happy to look into
this if someone points me to the release tooling.

Ref cilium#36265 but I'd like to keep
the issue open until any dependency from the api module to the main
module is cleaned up.

[0]: https://go.dev/wiki/Modules#faqs--multi-module-repositories

Signed-off-by: Alvaro Aleman <alvaroaleman@users.noreply.github.com>
@ovidiutirla ovidiutirla requested a review from aanm January 27, 2025 08:44
@aanm aanm added the dont-merge/discussion A discussion is ongoing and should be resolved before merging, regardless of reviews & tests status. label Jan 27, 2025
@rolinh rolinh self-requested a review January 29, 2025 15:53
@squeed
Copy link
Contributor

squeed commented Jan 29, 2025

I totally support this effort! Does it make sense to use go workspaces?

@squeed
Copy link
Contributor

squeed commented Jan 29, 2025

The policy tree is probably going to be the biggest cause of problems here:

  1. A lot of the exported API types are actually defined in pkg/policy/api, rather than pkg/k8s/api
  2. pkg/policy/api has a lot of implementation, not just type definitions. And that code has random imports about the code base.
  3. The policy API is also exposed via the REST api, so it's not just for k8s

Additionally, we should consider splitting the API types and internal types completely; there's no reason for an api type to have unexported bookkeeping fields.

So, we need to do a few things:

  • Consider splitting policy types to API and internal
  • Move all implementation from pkg/policy/api to somewhere else
  • Centralize API definitions (busy work)

@joestringer
Copy link
Member

Does it make sense to use go workspaces?

There's some discussion about workspaces in the link below which highlights some drawbacks. I'm not sure if those drawbacks are painful enough to cause us to choose a different solution (as I don't fully grasp the consequences of the alternative), but it might shed some light:

#35015 (comment)

@squeed
Copy link
Contributor

squeed commented Jan 30, 2025

There's some discussion about workspaces in the link below which highlights some drawbacks.

Reading the workspaces wiki, it explicitly calls out the client API as a good use for workspaces :-).

Two example scenarios where it can make sense to have more than one go.mod in a repository: ... if you have a repository with a complex set of dependencies, but you have a client API with a smaller set of dependencies.

Copy link
Member

@rolinh rolinh left a comment

Choose a reason for hiding this comment

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

Thanks for bringing this discussion forward @alvaroaleman! I'm sorry I couldn't join the community meeting last Wednesday. Nonetheless, I'll provide my thoughts in written form below.

In principle, I (and the Hubble team at large) am very much in favor of creating a separate module for the api package for all the reasons you mentioned and more (for instance, importing cilium in your project requires keeping track of all the replace directives which is tedious and prone to creating issues if not done; having a separate module without any replace directive would remove that hurdle).

However, we need to take careful considerations before actually doing that. I'll list below some of the things that I think must be taken into consideration:

  • How do we pull off this change without breaking developers workflows? The answer could be to use replace directives from the main go.mod to the api's one or to push a go.work file to the tree to leverage Go workspaces).
  • How do we avoid breaking downstream projects which rely on the API package? When creating the module (or modules) we may want to re-organize/re-group the various APIs we have under api/v1.
  • What would need to be updated in the release process? For instance, we would need to tag v1.17.0 and api/v1.17.0 at the time of releasing v1.17.0), potentially remove replace directives (see comment below), etc.
  • How do we ensure that the API module does NOT depend on other Cilium packages? That's potentially a hard one to address but it's in my opinion almost pointless to create a separate module if it ends up requiring Cilium as a dependency.
  • Do we want a single api module or should we actually create different ones for the protobuf APIs and the Cilium API client?
  • We need to update Renovate and any process that takes care of updates and Renovate is known to not always work well with Go workspaces and projects with multiple modules (as pointed out by Chance here).

This list may not be exhaustive but I hope it can serve as a good starting points for things to consider. I remember we (the Hubble team) considered doing this at some point but never actually implemented the change. While I don't recall exactly why, the fact that there are many things to consider and address is probably part of the reason.

@@ -0,0 +1,130 @@
module github.com/cilium/cilium/pkg/k8s/apis/cilium.io/v2

replace github.com/cilium/cilium => ../../../../../
Copy link
Member

Choose a reason for hiding this comment

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

Whenever we tag a release, these replace directives would need to be removed and the cilium version below be updated to match the version about to be tagged. Failing to do so means it ill be impossible to import the go module individually which makes splitting into a separate module effectively useless.

While this is something that can be done, we also need to consider that one cannot import the module unless it matches a tagged version (so there's no importing that un-tagged version that may fix a bug you're facing for instance).

As a point of reference, this is a process we follow for the cilium/fake project, which consists of 3 modules with interdependencies. A release PR (example) contains 2 commits: the first one removes any replace directive and changes the version of the modules to the one that will be tagged (that's the commit we then tag) and a follow-up commit that adds back the replace directives so that development workflows aren't broken.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
dont-merge/discussion A discussion is ongoing and should be resolved before merging, regardless of reviews & tests status. dont-merge/needs-release-note-label The author needs to describe the release impact of these changes. kind/community-contribution This was a contribution made by a community member.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants