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

Renaming --rego-v1 cmd flag to --v0-v1 #7225

Merged

Conversation

johanfylling
Copy link
Contributor

Affects opa fmt and opa check.

opa fmt command is updated to expect incoming rego to be v0.

Affects `opa fmt` and `opa check`.

`opa fmt` command is updated to expect incoming rego to be v0.

Signed-off-by: Johan Fylling <[email protected]>
@@ -153,8 +153,12 @@ func addStrictFlag(fs *pflag.FlagSet, strict *bool, value bool) {
fs.BoolVarP(strict, "strict", "S", value, "enable compiler strict mode")
}

func addRegoV1FlagWithDescription(fs *pflag.FlagSet, regoV1 *bool, value bool, description string) {
func addRegoV0V1FlagWithDescription(fs *pflag.FlagSet, regoV1 *bool, value bool, description string) {
fs.BoolVar(regoV1, "v0-v1", value, description)
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Not sure if --v0-v1 is the best name, but I can't come up with anything better.

Could call it --v0-v1-compatible to be more similar to --v0-compatible; but that command covers a slightly different use-case, so could be more confusing than helpful.

Suggestions are welcome :).

Copy link

netlify bot commented Dec 18, 2024

Deploy Preview for openpolicyagent ready!

Name Link
🔨 Latest commit 5895796
🔍 Latest deploy log https://app.netlify.com/sites/openpolicyagent/deploys/67628f4a5d902800080ea3dc
😎 Deploy Preview https://deploy-preview-7225--openpolicyagent.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 site configuration.

@@ -138,6 +138,10 @@ func formatFile(params *fmtCommandParams, out io.Writer, filename string, info o
DropV0Imports: params.dropV0Imports,
}

if params.regoV1 {
opts.ParserOptions = &ast.ParserOptions{RegoVersion: ast.RegoV0}
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Since we expect incoming policies to be v0, we'll error if the relevant imports aren't present; which isn't ideal.

If we drop this, then we'll default to v1, and the user can override to with --v0-compatible (now they can actually override to v1 with the hidden --v1-compatible flag).

Alternatively, we could try parsing with one version, and if it fails, parse with the other; but that's hard to make fool-proof.

Copy link
Contributor Author

@johanfylling johanfylling Dec 18, 2024

Choose a reason for hiding this comment

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

To clarify the current behaviour:

  • opa fmt:
    • v1 rego is formatted to v1
    • rego.v1/future.keywords imports are NOT removed
    • rego.v1/future.keywords imports are NOT added if missing
    • v0 rego is rejected
  • opa fmt --v0-compatible:
    • v0 rego is formatted to v0
    • v1 rego is rejected
  • opa fmt --v0-v1:
    • v0 rego is formatted to be compatible with v0 AND v1
    • v1 rego is rejected
  • opa fmt --v0-v1 --v1-compatible:
    • v1 rego is formatted to be compatible with v0 AND v1
    • v0 rego is rejected
    • --v1-compatible flag is hidden in opa fmt --help

A mix of v0/v1 modules is NOT supported.

In the above, a v0 module can have rego.v1 and future.keywords imports and be v1 compliant.

Signed-off-by: Johan Fylling <[email protected]>
p contains x if {
some x in ["a", "b", "c"]
}
`,
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This test case illustrates a scenario that can be solved with setting the --v1-compatible flag. This flag is however hidden, so not ideal.

converting v0->v1 is likely the more common use case, but we can't guarantee this won't happen; and will probably become more likely as time pass ..

}

for _, tc := range tests {
t.Run(tc.note, func(t *testing.T) {
params := fmtCommandParams{
// Locking rego-version to v0, as it's only then the --rego-v1 flag is relevant
v0Compatible: true,
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is why the tests didn't fail on main when we moved to v1 as default.

anderseknert
anderseknert previously approved these changes Dec 18, 2024
Signed-off-by: Johan Fylling <[email protected]>
@johanfylling johanfylling merged commit 24c45fc into open-policy-agent:main Dec 18, 2024
28 checks passed
@johanfylling johanfylling deleted the rename_rego_v1_cmd_flag branch December 18, 2024 13:10
charlieegan3 added a commit to charlieegan3/opa that referenced this pull request Dec 18, 2024
charlieegan3 added a commit to charlieegan3/opa that referenced this pull request Dec 18, 2024
charlieegan3 added a commit to charlieegan3/opa that referenced this pull request Dec 18, 2024
charlieegan3 added a commit to charlieegan3/opa that referenced this pull request Dec 18, 2024
ashutosh-narkar pushed a commit to ashutosh-narkar/opa that referenced this pull request Dec 18, 2024
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.

2 participants