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

Prefetching dependencies with Cachi2 #285

Merged
merged 13 commits into from
Feb 4, 2025
Merged

Prefetching dependencies with Cachi2 #285

merged 13 commits into from
Feb 4, 2025

Conversation

ccronca
Copy link
Collaborator

@ccronca ccronca commented Jan 27, 2025

This is an initial PR to get feedback on prefetching dependencies using Cachi2. Since Rapidast uses Konflux as the build system, this relies on Cachi2 support in Konflux

The konflux buildah-oci-ta.yaml v0.2 task, used to build the Rapidast image, mounts the prefetched dependencies into /cachi2. So, the Containerfile has been updated to retrieve dependencies from /cachi2 when prefetching is enabled.

To keep the Containerfile working for non-Konflux builds, I’ve added a build-time variable called PREFETCH. If PREFETCH=true, the build will try to use /cachi2 for dependencies. If not, it’ll just pull dependencies the same way it did before.

If this approach makes sense, I’ll add prefetching support for other dependencies

EDIT:
Dependencies covered by this PR:

  • Generic binaries: ZAP, Trivy, Kubecl, and Firefox
  • NPM dependencies

Dependencies not covered by this PR:

  • Pip dependencies: Prefetching these dependencies involves multiple steps, so I’d like to handle them in a separate PR
  • Zap addons: I'm not sure how to implement this yet, so it will be covered in a different PR if necessary
  • Rpm dependencies: Prefetching these dependencies involves multiple steps, so I’d like to handle them in a separate PR

@sfowl
Copy link
Collaborator

sfowl commented Jan 27, 2025

This is disappointing, especially since the docs make it seem like the feature should be available already:

task prefetch-dependencies has the status "Failed":

2025-01-27 15:12:26,470 ERROR UnsupportedFeature: Package manager(s) not yet supported: generic
Error: UnsupportedFeature: Package manager(s) not yet supported: generic
  But the good news is that we're already working on it!

The general approach of using a PREFETCH arg seems to fine to me though, FWIW.

@ccronca
Copy link
Collaborator Author

ccronca commented Jan 28, 2025

This is disappointing, especially since the docs make it seem like the feature should be available already:

task prefetch-dependencies has the status "Failed":

2025-01-27 15:12:26,470 ERROR UnsupportedFeature: Package manager(s) not yet supported: generic
Error: UnsupportedFeature: Package manager(s) not yet supported: generic
  But the good news is that we're already working on it!

The general approach of using a PREFETCH arg seems to fine to me though, FWIW.

Looks like this is fixed now! The main issue was that the task-prefetch-dependencies-oci-ta in the original bundle (task-prefetch-dependencies-oci-ta:0.1@sha256:3c11f5de6a0281bf93857f0c85bbbdfeda4cc118337da273fef0c138bda5eebb) was running cachi2 version 0.13, which doesn’t support the generic package manager. I updated it to the latest version of the image, and it’s working as expected now.

@sfowl since you're on board with this approach, I’ll go ahead and expand it to include prefetching support for other dependencies


## redocly (https://github.com/Redocly/redocly-cli)
RUN mkdir -p /tmp/redocly/node_modules && npm install --prefix /tmp/redocly @redocly/[email protected]
COPY package.json package-lock.json /tmp/redocly/
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Cachi2 relies on package.json and package-lock.json files to manage the prefetching of npm dependencies. So, instead of explicitly installing the redocly/cli package, it has been added to the package.json for automatic handling.

RUN mkdir -p /tmp/redocly/node_modules && npm install --prefix /tmp/redocly @redocly/[email protected]
COPY package.json package-lock.json /tmp/redocly/
RUN mkdir -p /tmp/redocly/node_modules && if [ "$PREFETCH" == "true" ]; then \
npm install --offline --prefix /tmp/redocly; \
Copy link
Collaborator Author

@ccronca ccronca Jan 28, 2025

Choose a reason for hiding this comment

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

The offline argument has been added to block any external access when installing npm dependencies, making sure everything comes from Cachi2. Essentially, Konflux will update the remote dependencies in the package-lock.json to point to the local file system using Cachi2’s inject-files feature


# These versions should be consistent with the listed in the artifacts.lock.yaml file
ARG ZAP_VERSION=2.15.0
ARG FF_VERSION=128.6.0esr
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Firefox is now locked to a specific version. Previously, it was downloading the latest one, but in order to validate the checksum with Cachi2, we need to specify a fixed version. We could continue fetching the latest version when binaries aren’t pre-fetched, but I wanted to maintain consistency across both approaches.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Sounds good to me

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

This file, along with the package.json, was generated by simply running npm install @redocly/[email protected]. This file includes a modification compared to the one generated directly, I've added the version key, as Cachi2 requires it. Without this key, it will fail to download dependencies:

2025-01-28 16:17:34,068 ERROR KeyError: 'version'                                                                                                                                                                    

@ccronca ccronca marked this pull request as ready for review January 28, 2025 16:27
@ccronca ccronca requested a review from a team as a code owner January 28, 2025 16:27
@ccronca
Copy link
Collaborator Author

ccronca commented Jan 30, 2025

This PR is ready for review. Other dependencies will be handled in separate PRs.

@ccronca ccronca self-assigned this Feb 3, 2025
Copy link
Collaborator

@jeremychoi jeremychoi left a comment

Choose a reason for hiding this comment

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

lgtm

@ccronca ccronca merged commit a4984c5 into development Feb 4, 2025
5 checks passed
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