policy-test: use local registry - #2583
Conversation
fc7a027 to
1f0f237
Compare
667f516 to
ecd4a47
Compare
ecd4a47 to
7c9e278
Compare
1f0f237 to
15decf2
Compare
|
Update: Instead of the weird proxy setup, we can dynamically overwrite the pause image in the genpolicy settings file before running Also, with the rebase, the rego evaluation timing goes down to <10ms (~10-20ms max for |
There was a problem hiding this comment.
Thanks! I have a bunch of small comments, but generally, this LGTM. Tested it out locally and seems to work.
- One thing I didn't catch on the last PR - maybe the
policy-testshould be moved from top-level intotools/...? Tools also doesn't feel quite right though. IDK. - I still don't really like having to run two terminals/just commands.
craneuses https://github.com/google/go-containerregistry, so why not do this ourselves? Super rough sketch:whereimport ( "net" "net/http" "github.com/google/go-containerregistry/pkg/registry" "github.com/google/go-containerregistry/pkg/crane" "github.com/google/go-containerregistry/pkg/v1/layout" ) ln, err := net.Listen("tcp", "127.0.0.1:0") if err != nil { return fmt.Errorf("listen: %w", err) } srv := &http.Server{Handler: registry.New(registry.Logger(log.New(io.Discard, "", 0)))} go srv.Serve(ln) defer srv.Close() addr := ln.Addr().String() for _, img := range []string{busyboxRef, pauseRef} { if err := crane.Copy(img, addr+"/"+strings.SplitN(img, "/", 2)[1], crane.Insecure); err != nil {...} } idx, err := layout.ImageIndexFromPath(initializerOCIDir) crane.PushIndex(idx, addr+"/contrast/initializer:latest", crane.Insecure)
initializerOCIDircomes from nix, e.g. through anotherinstall -Dinassets/. We could even pull busybox and pause images in the nix build and persist them, so no re-download between runs. The only not-so-nice thing about this would be to have to either write the image-replacements from go, or (probably better), allowgeneratePoliciesto optionally take a map of image replacements directly.
The second point should probably be decided on before implementing the review comments (@burgerdev), since going the go-containerregistry route would make a lot of them irrelevant.
|
|
||
| // Patch the pause image in genpolicy-settings.json to use the insecure registry if specified. | ||
| if flags.insecureRegistry != "" { | ||
| genpolicySettings = bytes.ReplaceAll(genpolicySettings, []byte("ghcr.io/edgelesssys/kubernetes/pause"), []byte(flags.insecureRegistry+"/kubernetes/pause")) |
There was a problem hiding this comment.
This fails silently if no match is found. Maybe add a check to ensure the new pause image is written to the settings.
| default_deploy_target := "openssl" | ||
| default_platform := "${default_platform}" | ||
| default_set := "${set}" | ||
| default_registry := "${container_registry}" |
There was a problem hiding this comment.
Is there a reason to add the new default_registry here, or could we just do
push target set=default_set registry=container_registry:below?
| "--settings=" + filepath.Join(workDir, "genpolicy-settings.json"), | ||
| "--genpolicy-cache-path=" + filepath.Join(workDir, "layers-cache.json"), | ||
| "--image-replacements=" + flags.imageReplacementsFile, | ||
| "--insecure-registry=" + flags.insecureRegistry, |
There was a problem hiding this comment.
I think cobra's StringArray turns the empty default into [""], so something is always passed here. Maybe
if flags.insecureRegistry != "" {
args = append(args, "--insecure-registry="+flags.insecureRegistry)
}or similar?
| tail -n 0 --pid $PID -f ./{{ workspace_dir }}/registry.log | ||
|
|
||
| # Run the policy test suite. | ||
| policy: initializer |
There was a problem hiding this comment.
Shouldn't we still push the initializer here? If it didn't change, it's close to a no-op, but pushing it only in policy-registry means needing to restart the registry when we made a change and want to re-test.
| fi | ||
| mkdir -p ./{{ workspace_dir }} | ||
| echo "Setting up registry..." | ||
| crane registry serve --address localhost:5000 > ./{{ workspace_dir }}/registry.log 2>&1 & |
There was a problem hiding this comment.
Probably need to wait (shortly) for readiness
There was a problem hiding this comment.
The short delay until crane starts serving has been a recurring pain point in Constellation's Bazel builds. If possible, I'd really prefer to run the registry from Go code, as @charludo suggested.
| set -euo pipefail | ||
| if curl -sf localhost:5000/v2/ > /dev/null; then | ||
| echo "Registry already running on port 5000." | ||
| exit 1 |
There was a problem hiding this comment.
This should be exit 0, we're basically saying "nothing to do, already up!".
Alternatively, exit 1 and "port 5000 in use, can't start".
| fi | ||
| mkdir -p ./{{ workspace_dir }} | ||
| echo "Setting up registry..." | ||
| crane registry serve --address localhost:5000 > ./{{ workspace_dir }}/registry.log 2>&1 & |
There was a problem hiding this comment.
Lots of places with the 5000 now, how about policy_registry := "localhost:5000" at the top of the file?
| printf "$busybox=localhost:5000/$busybox\n" >> ./{{ workspace_dir }}/just.containerlookup | ||
| crane copy ghcr.io/edgelesssys/kubernetes/pause:3.6 localhost:5000/kubernetes/pause:3.6 | ||
| echo "Registry is ready." | ||
| tail -n 0 --pid $PID -f ./{{ workspace_dir }}/registry.log |
There was a problem hiding this comment.
I think this won't work on darwin...? I have no way to check though.
There was a problem hiding this comment.
Correct, this would need to use gtail via Nix.
| actualModRoot=$modRoot | ||
| modRoot=. | ||
| ${contrast.cli.preConfigure} | ||
| install -D ${kata.genpolicy.settings-dev}/genpolicy-settings.json policy-test/assets/genpolicy-settings-kata.json |
There was a problem hiding this comment.
Interestingly, this now leaves the CLI's asset at the THIS FILE IS REPLACED... state. It currently does not matter, because you always pass --settings= in policy-test/main.go, but maybe worth a comment here.
7c9e278 to
e6ce525
Compare
This uses the
--insecure-registryflag from #2580 to configure a local registry viacrane registry serveduring the policy test suite evaluation. To use this, first runjust policy-registryand wait until it saysRegistry is ready.. Then runjust policy. The first target will start a local registry viacrane registry serveand push the needed images into it while also writing to the image replacements file.One problem is: genpolicy also pulls the configured pause image (ghcr.io/edgelesssys/kuberenetes/pause:3.6 in our case), which cannot be replaced by the image replacements file, since it isn't present in any of the deployment YAMLs. We configure
ghcr.ioas an insecure registry and set up an HTTP proxy that reroutes requests toghcr.ioto the local registry. For this to work, we of course push the pause image to the local registry beforehand.Doing some testing, I get the following results:
Fixes CON-251
This PR is currently rebased on both #2580 and #2582, so only the most recent commit is relevant.