Skip to content

Client API dynamic paths #2196

Closed
Closed

Description

@helderco

Overview

Sometimes, when using client: filesystem: or client: network:, the path needs to be configurable (usually from an env var) but we can't have the action's CUE path set at runtime, or dependent on another action. That's because we don't support dynamic actions yet, so actions need to be known at load time.

Details
dagger.#Plan & {
	actions: {
		test: [string]: core.#Nop & {
			input: "world"
		}

		// action with dynamic path here
		test: (gen.output): _

		// run action which depends on a dynamic path
		run: core.#Nop & {
			input: test[gen.output].output
		}

		// #GenString just produces a string in `output` 
		// from a core action
		gen: #GenString
	}
}

#GenString: {
	_img: core.#Pull & {
		source: "alpine"
	}
	_gen: core.#Exec & {
		input: _img.output
		args: ["sh", "-c", "echo hello > /out"]
	}
	_read: core.#ReadFile & {
		input: _gen.output
		path: "/out"
	}
	output: _read.contents
}

This is what we're doing with Client API, and it doesn't work. actions.test isn't run:

➜  dagger do run
[✔] actions.gen
[✔] actions.run

Of course it works if the value is known at compile time:

-		gen: #GenString
+		gen: output: "hello"
➜  dagger do run
[✔] actions.test.hello
[✔] actions.run

What users want to do?

client: {
	env: {
		KUBECONFIG: string
		OCP_PATH: string
	}
	filesystem: {
		(env.KUBECONFIG): read: contents: dagger.#Secret
		(env.OCP_PATH): write: contents: actions.loginOCP.output
	}
}

Related:

Current workarounds

Use a command:

client: {
	env: {
		KUBECONFIG: string
		OCP_PATH: string
	}
	commands: {
		kubeconfig: {
			name: "cat"
			args: [env.KUBECONFIG]
			stdout: dagger.#Secret
		}
		ocp: {
			name: "sh"
			args: ["-c", "cat > \(env.OCP_PATH)"]
			stdin: actions.loginOCP.output
		}
	}
}

Proposal

While we wait for dynamic actions, there's a backwards compatible way to tweak the API. Just use the fs path in the CUE path as a default, but allow setting a different one inside de action.

This allows you to:

client: {
	env: {
		KUBECONFIG: string
		OCP_PATH: string
	}
	filesystem: {
		kubeconfig: read: {
			path: env.KUBECONFIG
			contents: dagger.#Secret
		}
		ocp: write: {
			path: env.OCP_PATH
			contents: actions.loginOCP.output
		}
	}
}

Same with client: network.

Pros:

  • Still compatible with current API if you have a fixed path and want to use that;
  • Frees you from having to maintain the path in all references, or to keep references shorter for long paths;
  • Solves current issues by having the option of having a custom name instead of the fs path in the action CUE path;

Cons:

  • Commit to new design even if issues are resolved (not an issue for me);

Implementations

/cc @shykes @aluzzardi @gerhard

Activity

  1. helderco commented on Apr 14, 2022

    @helderco
    Author
  2. helderco commented on Apr 21, 2022

    @helderco
    Author
  3. helderco commented on Apr 21, 2022

    @helderco
    ContributorAuthor

    There's two more cases the proposal would unlock. They're two sides of the same coin, but both have been requested.

    Reading multiple times from the same dir

    client: filesystem: {
    	goFiles: read: {
    		path: "."
    		contents: dagger.#FS
    		include: ["**/*.go"]
    	}
    	cueFiles: read: {
    		path: "."
    		contents: dagger.#FS
    		include: ["**/*.cue"]
    	}
    }

    Writing multiple times to the same dir

    client: filesystem: {
    	lintedGo: write: {
    		path: "."
    		contents: actions.lint.go.output
    	}
    	lintedCue: write: {
    		path: "."
    		contents: actions.lint.cue.output
    	}
    }
  4. helderco commented on May 2, 2022

    @helderco
    ContributorAuthor

    In #2385 I found a limitation in implementing dynamic paths when reading directories, i.e., client: filesystem: ...: read: contents: dagger.#FS: We need to know the path before running the plan to setup the BuildKit client (LocalDirs).

    To be clear, this only happens when reading directories. Not files, secrets nor when writing.

    As of this moment there's no clear path forward.

  5. changed the title [-]Client API path issues[/-] [+]Client API dynamic paths[/+] on May 2, 2022
  6. self-assigned this
    on May 7, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions