Repository navigation
feat: enable volume mounts for local func run - #3821
Ankitsinghsisodya wants to merge 11 commits into
Conversation
|
Hi @Ankitsinghsisodya. Thanks for your PR. I'm waiting for a knative member to verify that this patch is reasonable to test. If it is, they should reply with Tip We noticed you've done this a few times! Consider joining the org to skip this step and gain Once the patch is verified, the new status will be reflected by the I understand the commands that are listed here. DetailsInstructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes-sigs/prow repository. |
There was a problem hiding this comment.
Pull request overview
Note
Copilot was unable to run its full agentic suite in this review.
Adds Docker runner support for func.yaml volume definitions by translating them into Docker mount specs, along with unit tests covering mapping rules and error/skip behavior.
Changes:
- Add volume-to-mount translation (
toMount) and mount list construction (volumeMounts) during container creation. - Add validation to prevent path traversal for Secret/ConfigMap-backed local bind mounts.
- Introduce unit tests for volume mapping behavior and warning/skip semantics.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 6 comments.
| File | Description |
|---|---|
| pkg/docker/runner.go | Implements volume→mount conversion and wires mounts into container host config. |
| pkg/docker/runner_volumes_test.go | Adds tests for mount mapping, validation failures, and warning/skip behavior. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| return nil | ||
| } | ||
|
|
||
| func toMount(root string, vol fn.Volume) (mount.Mount, error) { |
| // safeLocalName rejects names that contain path separators or traversal | ||
| // sequences. Secret and ConfigMap names are used as file-path components; an | ||
| // adversarial name such as "../../etc" would otherwise escape the .func tree. | ||
| func safeLocalName(name string) error { | ||
| if strings.ContainsAny(name, "/\\") || strings.Contains(name, "..") { | ||
| return fmt.Errorf("name %q must not contain path separators or \"..\"", name) | ||
| } |
| func volumeMounts(root string, volumes []fn.Volume, out io.Writer) []mount.Mount { | ||
| var mounts []mount.Mount | ||
| for _, vol := range volumes { | ||
| if vol.Path == nil { | ||
| fmt.Fprintf(out, "warning: skipping volume %s: missing path\n", vol.String()) | ||
| continue | ||
| } |
| if err := os.MkdirAll(src, 0700); err != nil { | ||
| return mount.Mount{}, fmt.Errorf("cannot create local secret dir %q: %w", src, err) | ||
| } | ||
| return mount.Mount{Type: mount.TypeBind, Source: src, Target: target}, nil |
| if err := os.MkdirAll(src, 0750); err != nil { | ||
| return mount.Mount{}, fmt.Errorf("cannot create local configmap dir %q: %w", src, err) | ||
| } | ||
| return mount.Mount{Type: mount.TypeBind, Source: src, Target: target}, nil |
| want := filepath.Join(root, fn.RunDataDir, "run", "configmaps", "app-config") | ||
| if m.Source != want { | ||
| t.Errorf("source: got %q, want %q", m.Source, want) | ||
| } |
|
|
||
| if vol.EmptyDir != nil { | ||
| // Both mediums map to tmpfs locally: EmptyDir is ephemeral (pod-lifetime) | ||
| // and anonymous Docker volumes persist across runs, leaking storage. |
There was a problem hiding this comment.
Example of the body mismatch: this is tmpfs for EmptyDir (including default medium), not an anonymous volume as the PR table says.
| } | ||
|
|
||
| func newHostConfig(host, port string) (c container.HostConfig, err error) { | ||
| func newHostConfig(host, port string, f fn.Function, out io.Writer) (c container.HostConfig, err error) { |
There was a problem hiding this comment.
naming it out is misleading, as it's realy error out, and there's no comments. At a minimum it should be errOut
There was a problem hiding this comment.
The .func/-rooted secret/configMap binds and skip-and-warn on bad volumes look right. Pointer soup on fn.Volume is the existing YAML model; not something this PR invented.
The review table in the PR body does not match the code. Worth fixing so nobody reviews from the table:
emptyDir: {}is described as an anonymous volume; the code mounts tmpfs (same asmedium: Memory; the comment about avoiding leaks is the reason).- PVC is described as a bind of
.func/run/pvcs/<claim>; the code uses a named Docker volume keyed byclaimName.
A few Copilot notes I agree with:
- Secret and ConfigMap mounts in Kubernetes are read-only. Setting
ReadOnly: trueon those binds would match cluster semantics (and not mask "I wrote the secret file" surprises locally). safeLocalNamerejecting any".."substring is a bit eager (foo..baris not traversal). Rejecting path separators, empty names, and real..segments is enough.toMountassuming a non-nilPathis fine as long as onlyvolumeMountscalls it (it already skips nil). A one-line guard would make the helper reusable. Same idea ifoutcan be nil;io.Discardis cheap.
The ConfigMap test not asserting Target is tiny; nice if you are already touching tests.
Once the body matches the code (and ReadOnly if you agree), this looks good to me.
- Updated newContainer and newHostConfig functions to accept an output writer for logging. - Introduced volumeMounts and toMount functions to handle Docker volume specifications from function definitions. - Added support for Secrets, ConfigMaps, EmptyDir, and PVCs in volume mounts, improving flexibility for container configurations. - Implemented warnings for missing paths and errors during volume mapping, enhancing user feedback during execution.
- Introduced comprehensive unit tests for the volume mount functionality, covering various volume types including Secrets, ConfigMaps, EmptyDir, and PVCs. - Verified correct behavior for scenarios such as missing paths, read-only mounts, and unrecognized volume types. - Enhanced error handling and user feedback through warnings for skipped volumes and detailed error messages. - Ensured that the tests validate the expected mount configurations and behaviors, improving overall test coverage for the Docker runner.
- Adjusted comment formatting in the TestVolumeMounts_SkipsNilPath test case for improved readability. - Ensured consistency in comment style for better understanding of test scenarios.
- Introduced unit tests to ensure that secret and configMap names containing path traversal sequences are rejected, enhancing security by preventing directory traversal attacks. - Implemented a new function, safeLocalName, to validate names against path separators and traversal patterns, improving error handling in volume mounting. - Updated the toMount function to utilize the new validation, ensuring robust handling of volume specifications.
Secret and ConfigMap binds were writable and mode 0700, so a non-root container user could not read them and a local write could change the secret. Mount them read-only at 0755. safeLocalName rejected any ".." substring, so a legal name like foo..bar failed. Reject separators, empty names, and a real ".." segment only. The warning writer is errOut, and a nil writer is io.Discard. toMount guards a nil path. The ConfigMap test checks Target. An empty secret or configMap directory tells the user to put files there. The toMount comment had landed on safeLocalName.
A secret named run made the walk stop at the leaf, so the parent secrets directory could stay owner-only under a tight umask. Stop at the .func/run directory itself, and refuse a path outside it before changing any mode.
The looser check allowed ".", and filepath.Join collapses that to the parent directory, so one volume would mount every local secret. Secret and ConfigMap names are DNS-1123 subdomains on the cluster, which also rejects empty, "..", uppercase, and consecutive dots. Announce a secret or configMap directory only when this run creates it. An emptyDir sizeLimit becomes the tmpfs size. mount.Mount cannot set an SELinux :z relabel; that limit is stated on toMount.
The chmod commit picked up generate/zz_filesystem_generated.go while make test was rewriting it. That file matches eb77e83. Restore it so the branch does not carry a template embed. The engine bind-mounts the secret directory itself, so host parent modes do not matter to the container. chmod only a directory this run creates, and leave an existing mode alone.
Windows stores chmod 0700 as 0777, so the test failed even when the existing directory was left alone. Compare the mode the OS actually recorded.
Secret and ConfigMap mounts move to HostConfig.Binds as src:dst:ro,z so Docker and Podman relabel on SELinux. Non-SELinux hosts ignore the flag. emptyDir and PVC stay on Mounts. Name checks and the create-time hint are unchanged.
The Linux daemon splits a bind on colon. A drive letter or a colon in the function root would make container create fail. Those paths stay read-only Mounts and warn. Windows has no host SELinux, so :z buys nothing there.
a36f800 to
36095f8
Compare
|
Thanks @Ankitsinghsisodya. We rebased this and finished the review items on top of your commits: read-only secret and configMap binds with the SELinux relabel, DNS-1123 name checks, nil guards, /lgtm |
|
[APPROVALNOTIFIER] This PR is APPROVED This pull-request has been approved by: Ankitsinghsisodya, lkingland The full list of commands accepted by this bot can be found here. The pull request process is described here DetailsNeeds approval from an approver in each of these files:
Approvers can indicate their approval by writing |
Fixes #2940
Summary
func runnow mounts the volumes declared infunc.yaml(pkg/docker/runner.go→newHostConfig()), alongside the port bindings it already set up.volumeMountsandtoMountconvert eachfunc.yamlvolume into a Docker mount.Volume mapping
.func/run/secrets/<name>, on HostConfig.Binds assrc:dst:ro,z; the directory is created (mode 0755) if absent.func/run/configmaps/<name>, on HostConfig.Binds assrc:dst:ro,z; the directory is created (mode 0755) if absentsizeLimitsets the tmpfs sizeclaimName; not a bind under.func/run/pvcsSecret and configMap names must be DNS-1123 subdomains.
.is rejected becausefilepath.Joinwould collapse it onto the parent directory.:zis the SELinux relabel; hosts without SELinux ignore it, and a path containing:is bound without it (with a warning). An empty secret or configMap directory gets a hint to add one file per key.Every bind source is inside
.func/, which satisfies the issue's security constraint: no path outside.funcis ever mounted.Notes
f.Root, not the process working directory, which fixesfunc runinvoked from outside the function root.