Skip to content

Commit

Permalink
cmd/build: expose new configurable via --prune-unused
Browse files Browse the repository at this point in the history
Signed-off-by: Stephan Renatus <stephan.renatus@gmail.com>
  • Loading branch information
srenatus committed Aug 23, 2022
1 parent a35ac17 commit af5de64
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 1 deletion.
10 changes: 9 additions & 1 deletion cmd/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ type buildParams struct {
capabilities *capabilitiesFlag
target *util.EnumFlag
bundleMode bool
pruneUnused bool
optimizationLevel int
entrypoints repeatedStringFlag
outputFile string
Expand Down Expand Up @@ -114,6 +115,11 @@ The 'build' command supports targets (specified by -t):
the input files for each specified entrypoint. The bundle may contain the
original policy or data files.
plan The plan target emits a bundle containing a plan, i.e., an intermediate
representation compiled from the input files for each specified entrypoint.
This is for further processing, OPA cannot evaluate a "plan bundle" like it
can evaluate a wasm or rego bundle.
The -e flag tells the 'build' command which documents will be queried by the software
asking for policy decisions, so that it can focus optimization efforts and ensure
that document is not eliminated by the optimizer.
Expand Down Expand Up @@ -218,7 +224,8 @@ against OPA v0.22.0:
}

buildCommand.Flags().VarP(buildParams.target, "target", "t", "set the output bundle target type")
buildCommand.Flags().BoolVarP(&buildParams.debug, "debug", "", false, "enable debug output")
buildCommand.Flags().BoolVar(&buildParams.pruneUnused, "prune-unused", false, "exclude dependents of entrypoints")
buildCommand.Flags().BoolVar(&buildParams.debug, "debug", false, "enable debug output")
buildCommand.Flags().IntVarP(&buildParams.optimizationLevel, "optimize", "O", 0, "set optimization level")
buildCommand.Flags().VarP(&buildParams.entrypoints, "entrypoint", "e", "set slash separated entrypoint path")
buildCommand.Flags().VarP(&buildParams.revision, "revision", "r", "set output bundle revision")
Expand Down Expand Up @@ -273,6 +280,7 @@ func dobuild(params buildParams, args []string) error {
WithCapabilities(capabilities).
WithTarget(params.target.String()).
WithAsBundle(params.bundleMode).
WithPruneUnused(params.pruneUnused).
WithOptimizationLevel(params.optimizationLevel).
WithOutput(buf).
WithEntrypoints(params.entrypoints.v...).
Expand Down
68 changes: 68 additions & 0 deletions cmd/build_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -220,3 +220,71 @@ func TestBuildVerificationConfigError(t *testing.T) {
}
})
}

func TestBuildPlanWithPruneUnused(t *testing.T) {

files := map[string]string{
"test.rego": `
package test
p[1]
f(x) { p[x] }
`,
}

test.WithTempFS(files, func(root string) {
params := newBuildParams()
if err := params.target.Set("plan"); err != nil {
t.Fatal(err)
}
params.pruneUnused = true
params.entrypoints.v = []string{"test"}
params.outputFile = path.Join(root, "bundle.tar.gz")

err := dobuild(params, []string{root})
if err != nil {
t.Fatal(err)
}

_, err = loader.NewFileLoader().AsBundle(params.outputFile)
if err != nil {
t.Fatal(err)
}

// Check that manifest is not written given no input manifest and no other flags
f, err := os.Open(params.outputFile)
if err != nil {
t.Fatal(err)
}
defer f.Close()

gr, err := gzip.NewReader(f)
if err != nil {
t.Fatal(err)
}

tr := tar.NewReader(gr)

found := false // for plan.json

for {
f, err := tr.Next()
if err == io.EOF {
break
} else if err != nil {
t.Fatal(err)
}
switch {
case f.Name == "/plan.json":
found = true
case f.Name == "/data.json" || strings.HasSuffix(f.Name, "/test.rego"): // expected
default:
t.Errorf("unexpected file: %s", f.Name)
}
}
if !found {
t.Error("plan.json not found")
}
})
}

0 comments on commit af5de64

Please sign in to comment.