Skip to content

feat: enable volume mounts for local func run#3821

Open
Ankitsinghsisodya wants to merge 4 commits into
knative:mainfrom
Ankitsinghsisodya:feat/volume-mounts-local-run-2940
Open

feat: enable volume mounts for local func run#3821
Ankitsinghsisodya wants to merge 4 commits into
knative:mainfrom
Ankitsinghsisodya:feat/volume-mounts-local-run-2940

Conversation

@Ankitsinghsisodya
Copy link
Copy Markdown
Contributor

@Ankitsinghsisodya Ankitsinghsisodya commented May 21, 2026

Fixes #2940

Note: Re-submission of #3776, accidentally closed when the head repository was deleted.

Summary

  • Updated pkg/docker/runner.go -> newHostConfig() to configure volume mounts in addition to port bindings when running functions locally with func run
  • Added volumeMounts and toMount helpers to convert func.yaml volume definitions into Docker mount specs
  • Volumes that cannot be mapped locally emit a warning to stderr and are skipped; the run is not aborted

Volume mapping

func.yaml type Docker mount Local path
secret: <name> bind .func/run/secrets/<name> (created if absent)
configMap: <name> bind .func/run/configmaps/<name> (created if absent)
emptyDir: {medium: Memory} tmpfs
emptyDir: {} anonymous volume
persistentVolumeClaim bind .func/run/pvcs/<claimName> (created if absent)

All bind-mount sources are rooted inside .func/, satisfying the security constraint in the issue (no paths outside .func are ever mounted).

Notes

- 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.
Copilot AI review requested due to automatic review settings May 21, 2026 11:18
@knative-prow knative-prow Bot requested review from dsimansk and jrangelramos May 21, 2026 11:18
@knative-prow
Copy link
Copy Markdown

knative-prow Bot commented May 21, 2026

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by: Ankitsinghsisodya
Once this PR has been reviewed and has the lgtm label, please assign dsimansk for approval. For more information see the Code Review Process.

The full list of commands accepted by this bot can be found here.

Details Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@knative-prow knative-prow Bot added size/L 🤖 PR changes 100-499 lines, ignoring generated files. needs-ok-to-test 🤖 Needs an org member to approve testing labels May 21, 2026
@knative-prow
Copy link
Copy Markdown

knative-prow Bot commented May 21, 2026

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 /ok-to-test on its own line. Until that is done, I will not automatically test new commits in this PR, but the usual testing commands by org members will still work.

Tip

We noticed you've done this a few times! Consider joining the org to skip this step and gain /lgtm and other bot rights. We recommend asking approvers on your previous PRs to sponsor you.

Once the patch is verified, the new status will be reflected by the ok-to-test label.

I understand the commands that are listed here.

Details

Instructions 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.

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

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.

Comment thread pkg/docker/runner.go
return nil
}

func toMount(root string, vol fn.Volume) (mount.Mount, error) {
Comment thread pkg/docker/runner.go
Comment on lines +358 to +364
// 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)
}
Comment thread pkg/docker/runner.go
Comment on lines +330 to +336
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
}
Comment thread pkg/docker/runner.go
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
Comment thread pkg/docker/runner.go
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)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

needs-ok-to-test 🤖 Needs an org member to approve testing size/L 🤖 PR changes 100-499 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Volume Mounts for Local Runs

2 participants