Skip to content

feat: Gatekeeper policy migration guide. #627

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 6 commits into
base: main
Choose a base branch
from

Conversation

jvanz
Copy link
Member

@jvanz jvanz commented Jun 18, 2025

Description

Adds a how to guide showing how users can migrate a Gatekeeper policy into Kubewarden policy.

Fix #621

Adds a how to guide showing how users can migrate a Gatekeeper policy
into Kubewarden policy.

Signed-off-by: José Guilherme Vanz <[email protected]>
@jvanz jvanz self-assigned this Jun 18, 2025
@jvanz jvanz requested a review from a team as a code owner June 18, 2025 20:47
@github-project-automation github-project-automation bot moved this to Pending review in Kubewarden Jun 18, 2025
Copy link

netlify bot commented Jun 18, 2025

Deploy Preview for docs-kubewarden-io ready!

Name Link
🔨 Latest commit 4bda25c
🔍 Latest deploy log https://app.netlify.com/projects/docs-kubewarden-io/deploys/68559122d9f4aa000897dc1d
😎 Deploy Preview https://deploy-preview-627--docs-kubewarden-io.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify project configuration.

@jvanz jvanz removed this from Kubewarden Jun 18, 2025
Copy link
Member

@flavio flavio left a comment

Choose a reason for hiding this comment

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

Looks good, but I think we should come up with a better approach (something automated).

I would try to clean this page as much as possible and publish that. This is better than nothing and, most important of all, provides clear instructions to whoever is going to create some automation around that.

Comment on lines 34 to 36
[catalog](https://artifacthub.io/packages/search?kind=13). Some of the
[policies](https://github.com/kubewarden/rego-policies-library) are public
available OPA and Gatekeeper policies migrated migrated to Kubewarden
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
[catalog](https://artifacthub.io/packages/search?kind=13). Some of the
[policies](https://github.com/kubewarden/rego-policies-library) are public
available OPA and Gatekeeper policies migrated migrated to Kubewarden
[catalog](https://artifacthub.io/packages/search?kind=13).

I think that's enough, all the policies are already exposed in a nice way by ArtifactHub

Copy link
Member Author

Choose a reason for hiding this comment

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

I've added the link to the repository to give users examples on how they can migrate their gatekeeper policies. I think is always nice to see other examples.

Comment on lines 149 to 174
Attempting to build the code after this change might reveal new compilation errors:

```
opa build -t wasm -e policy/violation -o bundle.tar.gz policy.rego
error: load error: 2 errors occurred during loading:
policy.rego:3: rego_parse_error: `if` keyword is required before rule body
policy.rego:3: rego_parse_error: `contains` keyword is required for partial set rules
make: *** [Makefile:4: policy.wasm] Error 1
```

The policy author must fix these errors to allow the `opa` CLI to build the code
successfully. The specific changes may vary depending on the `opa` version and
the original policy code. For this example, the final `policy.rego` code looks
like this:

```rego
package policy

violation contains {"msg": msg, "details": {"missing_labels": missing}} if {
provided := {label | input.review.object.metadata.labels[label]}
required := {label | label := input.parameters.labels[_]}
missing := required - provided
count(missing) > 0
msg = sprintf("you must provide labels: %v", [missing])
}
```
Copy link
Member

Choose a reason for hiding this comment

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

I would rewrite this section and put that into a warning box. We also have to update the README of our templates (both Gatekeeper and OPA).

This is the information we need to distill, I did some research.

OPA graduated to version 1.0.0 on Dec 2024. Link. The 1.0.0 introduces a potentially breaking change:

Using if for all rule definitions and contains for multi-value rules is now mandatory, not just when using the rego.v1 import.

This is breaks the majority of the old policies.

It's still possible to build old policies that do not use this syntax by providing the --v0-compatible flag to the opa build command. (I've tested it against our template policy).

Gatekeeper has updated to OPA v1.0.0 with the v3.19.0 release.
From the release notes, the link this section of their docs.

Based on what is written there, and looking deep into the code, Gatekeeper assumes v0 is used unless the template is explicit about that:

...
  targets:
    - target: admission.k8s.gatekeeper.sh
      code:
        - engine: Rego
          source:
            version: "v1"
            rego: |
              <v1-rego-code>
...

Hence, to summarize:

  • The user has to check which version of Rego is mentioned
  • If no version is mentioned -> v0 is used -> the opa build must use the special "--v0-compatible" flag OR, they must use an old version of OPA (a pre 1.0.0 - but I would not mention that)
  • If v1 is mentioned -> they must have a recent version of OPA, no special build flag has to be passed to the opa build command

I'm starting to wonder if we shouldn't create a new kwctl scaffold program that deals with this conversion... there are many steps and things to take into account 🤔

Copy link
Member Author

Choose a reason for hiding this comment

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

I've added a new section about compatibility that users may face. ;)

:::

## Step 4: Prepare `metadata.yml` for Distribution

Copy link
Member

Choose a reason for hiding this comment

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

Also for these steps... we could automate them with kwctl scaffold

Copy link
Member Author

@jvanz jvanz Jun 20, 2025

Choose a reason for hiding this comment

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

Yes, we can. I agree.However, there is no such automation now. Thus, the documentation needs to be written. ;)

Updates the Gatekeeper migration documentationg adding missing links,
fixing typos and adding usefull command to copy rego code.

Co-authored-by: Flavio Castelli <[email protected]>
Signed-off-by: José Guilherme Vanz <[email protected]>
@jvanz jvanz force-pushed the gatekeeper-migration branch from 0232c0b to b057603 Compare June 20, 2025 12:44
jvanz added 3 commits June 20, 2025 10:18
Adds a documentation section about compatibility issues that users may
face when migrating policies.

Signed-off-by: José Guilherme Vanz <[email protected]>
Updates the Gatekeeper migration documentation to make clear that the
test updates are an optional step. And, the user can reuse the tests
from the migrated Gatekeeper policy, if they exist.

Signed-off-by: José Guilherme Vanz <[email protected]>
Updates the Gatekeeper migration docs removing the long AdmissionRequest
json used in the tests and point to users that they can use kwctl
scaffold command to generate those file for tests.

Signed-off-by: José Guilherme Vanz <[email protected]>
@jvanz jvanz requested a review from flavio June 20, 2025 14:07
Updates the Gatekeeper migration documentation to consider the
OPA_V0_COMPATIBLE variable when build the policy. This variable allow
users to compile policy with written before opa v1.0.0.

Signed-off-by: José Guilherme Vanz <[email protected]>
Comment on lines +225 to +228
- If your Rego policy template does NOT specify a `version` (or implies `v0`): You
must either call the `Makefile` target with the `OPA_V0_COMPATIBLE=true` variable
(e.g., `make OPA_V0_COMPATIBLE=true`) to ensure `opa` commands are called with the
`--v0-compatible` flag, or update your policy to the new `v1.0.0` syntax.
Copy link
Member Author

Choose a reason for hiding this comment

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

This consider that we apply the changes from this PR

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.

Doc: write howto about migrating a Gatekeeper policy to Kubewarden
2 participants