Skip to content

Enable hermetic build for operator #619

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
wants to merge 4 commits into
base: devel
Choose a base branch
from

Conversation

littlejawa
Copy link
Contributor

@littlejawa littlejawa commented Apr 24, 2025

- Description of the problem which is fixed/What is the use case
Enable hermetic build for the controller manager.

- What I did
gomod dependencies are managed by cachi2/Hermeto, enabling it is pretty simple.
I had to work around the controller-gen problem (again). Taking inspiration from the way we do it in CPaaS, but trying to avoid using different Dockerfile/Makefile for local and CI builds.

I'm adding a new script that make will use in place of the regular controller-gen. This script uses "go run" to run the tool.
In the context of a local build, this results in downloading/building the file as usual.
In the context of a hermetic build, with the dependencies pre-fetched, it will build the tool based on the prefetched sources.

- How to verify it
For a local build: run podman build .
For CI: the Konflux task in this PR must pass.
You can look at the logs too:

  • the "prefetch" step must pass and show that go dependencies were fetched
  • the "build" step must have the line:
Build will be executed with network isolation

Fixes: KATA-3796

@openshift-ci openshift-ci bot requested review from jensfr and snir911 April 24, 2025 12:43
When running in a hermetic environment (no access to the network), the build
was failing because of our Makefile trying to install controller-gen at build
time.

As this line was added by the operator-sdk, we would like to avoid modifying
it. It's actually useful in local builds, to make sure we're using the right
tools.
We also want to avoid using a different Makefile or Dockerfile for Konflux.

This commit provides a way to allow using the same process for building
locally and in our CI. It mimics the behavior we have in CPaaS, relying on
having controller-gen defined as a dependency in our go.mod file.

I'm adding a script that runs "go run" for controller-gen, and place it where
the Makefile expects to find the actual controller-gen binary
'make' will find that and skip the install of the tool, then call that script
in place of the actual tool whenever needed.

- in the context of a local build, "go run" will download and build the tool,
  just like it would if you used "go install", then run it as expected.
- in the context of Konflux in a hermetic environment, "go run" will find the
  controller-gen sources pre-fetched by cachi2/Hermeto, and build it from there.

Signed-off-by: Julien Ropé <[email protected]>
They fails when running in hermetic builds where there is no network.
The build command will actually perform the download, if needed,
when running outside of the CI, so those lines don't seem to be
necessary anyway.

Signed-off-by: Julien Ropé <[email protected]>
Set the flag and enable the gomod manager for prefetching.
As no RPM is installed during build, this is all we need to the controller.

Signed-off-by: Julien Ropé <[email protected]>
@littlejawa littlejawa force-pushed the enable_hermetic_build_for_operator branch from cdf50ac to 52d0ce4 Compare April 24, 2025 13:19
Copy link
Member

@beraldoleal beraldoleal left a comment

Choose a reason for hiding this comment

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

LGTM, thanks.

Dockerfile Outdated
@@ -13,6 +13,10 @@ COPY api api/
COPY config config/
COPY controllers controllers/

# Copy our controller-gen script to work around hermetic build issues
# See comments in the script itself for more details.
COPY controller-gen bin/controller-gen-v0.17.2
Copy link
Contributor

Choose a reason for hiding this comment

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

When does that version number change? My understanding is that this needs to be kept consistent with go.mod, how do we document that? Also asking because controller-tools release v0.17.3 has been out for three weeks now.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Updating it is part of the work we do on each release cycle. It's been kind of a manual process up to now.

With Konflux, we are offered an automated dependency check with PRs that update our go.mod dependencies (that's from Mintmaker), so we might get it like that.
Updating it requires changing both go.mod and Makefile. And now this Dockerfile.

There's no clear documentation about it, maybe there is room for improvement...
At the very least, I can add a comment here.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@c3d - I have added a commit (affae46) that tries to remove the need to update all those files with controller-gen version.
Is it overkill?

It can build both on my laptop and in Konflux apparently.

Copy link
Contributor

@c3d c3d left a comment

Choose a reason for hiding this comment

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

For what it works, builds OK on macOS.

Also, I tried building with controller-tools v0.17.3, and it seems to work as well.

When we update the controller-tools version, we need to update
go.mod, Makefile and Dockerfile, making sure they're all in sync.

This commit tries to automate that for container builds:
the Dockerfile will create an environment variable with the version
found in go.mod, and use that for setting up the controller-gen script,
and calling 'make' with that version.

For local builds, where only the Makefile is used, the default version
found in the Makefile still needs to be updated. But for container builds,
the value in the Makefile will be overriden, and always in sync with go.mod.

Signed-off-by: Julien Ropé <[email protected]>
Copy link

openshift-ci bot commented Apr 25, 2025

@littlejawa: all tests passed!

Full PR test history. Your PR dashboard.

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes-sigs/prow repository. I understand the commands that are listed here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants