Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: rename action to actions in template context #4194

Merged
merged 9 commits into from
May 9, 2023
20 changes: 13 additions & 7 deletions core/src/config/template-contexts/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -203,13 +203,12 @@ export interface ActionSpecContextParams {
* Used to resolve action spec and variables.
*/
export class ActionSpecContext extends OutputConfigContext {
// TODO-0.13.0: rename to actions (to allow using action singular in certain contexts + match the modules field)
@schema(
ActionReferencesContext.getSchema().description(
"Runtime outputs and information from other actions (only resolved at runtime when executing actions)."
)
)
public action: ActionReferencesContext
public actions: ActionReferencesContext

@schema(ActionReferencesContext.getSchema().description("Alias for `action`."))
public runtime: ActionReferencesContext
Expand Down Expand Up @@ -239,8 +238,15 @@ export class ActionSpecContext extends OutputConfigContext {
public this: ActionReferenceContext

constructor(params: ActionSpecContextParams) {
const { action, garden, partialRuntimeResolution, variables, inputs, resolvedDependencies, executedDependencies } =
params
const {
action,
Copy link
Contributor

Choose a reason for hiding this comment

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

just double checking: we're intentionally passing in a singular action here to the constructor?

Copy link
Collaborator Author

@vvagaytsev vvagaytsev May 9, 2023

Choose a reason for hiding this comment

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

I think so. The assignment looks like this:

this.actions = new ActionReferencesContext(this, partialRuntimeResolution, [
      ...resolvedDependencies,
      ...executedDependencies,
    ])

The action is not used directly there. And the original TODO comment was about changing the template context syntax for better clarity.

garden,
partialRuntimeResolution,
variables,
inputs,
resolvedDependencies,
executedDependencies,
} = params

const internal = action.getInternal()

Expand All @@ -260,13 +266,13 @@ export class ActionSpecContext extends OutputConfigContext {
const parentName = internal?.parentName
const templateName = internal?.templateName

this.action = new ActionReferencesContext(this, partialRuntimeResolution, [
this.actions = new ActionReferencesContext(this, partialRuntimeResolution, [
...resolvedDependencies,
...executedDependencies,
])

// Throw specific error when attempting to resolve self
this.action[action.kind.toLowerCase()].set(
this.actions[action.kind.toLowerCase()].set(
name,
new ErrorContext(`Action ${chalk.white.bold(action.key())} cannot reference itself.`)
)
Expand All @@ -277,7 +283,7 @@ export class ActionSpecContext extends OutputConfigContext {
}
this.inputs = inputs

this.runtime = this.action
this.runtime = this.actions

this.this = new ActionReferenceContext({
root: this,
Expand Down
2 changes: 1 addition & 1 deletion core/src/outputs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ export async function resolveProjectOutputs(garden: Garden, log: Log): Promise<O
} else if (ref[1] === "tasks") {
needActions.push({ kind: "Run", name: ref[2] as string })
}
} else if (ref[0] === "action" && ref[1] && ref[2]) {
} else if (ref[0] === "actions" && ref[1] && ref[2]) {
needActions.push({ kind: <ActionKind>ref[1], name: ref[2] as string })
}
}
Expand Down
2 changes: 1 addition & 1 deletion core/src/plugins/kubernetes/sync.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ export interface KubernetesDeployDevModeSyncSpec extends DevModeSyncOptions {
containerName?: string
}

const exampleActionRef = templateStringLiteral("action.build.my-container-image.sourcePath")
const exampleActionRef = templateStringLiteral("actions.build.my-container-image.sourcePath")

export const kubernetesDeploySyncPathSchema = () =>
syncDefaultsSchema()
Expand Down
2 changes: 1 addition & 1 deletion core/src/template-string/template-string.ts
Original file line number Diff line number Diff line change
Expand Up @@ -494,7 +494,7 @@ export function getActionTemplateReferences<T extends object>(config: T): Action

// ${action.*}
const refs: ActionTemplateReference[] = rawRefs
.filter((ref) => ref[0] === "action")
.filter((ref) => ref[0] === "actions")
.map((ref) => {
if (!ref[1]) {
throw new ConfigurationError("Found invalid action reference (missing kind)", { config, ref })
Expand Down
2 changes: 1 addition & 1 deletion core/test/data/test-projects/helm/api/garden.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ spec:
values:
image:
repository: api-image
tag: ${action.build.api-image.version}
tag: ${actions.build.api-image.version}
ingress:
enabled: true
paths: [/]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ spec:
spec:
containers:
- name: api
image: ${action.build.api-image.outputs.deployment-image-id}
image: ${actions.build.api-image.outputs.deployment-image-id}
args: [python, app.py]
ports:
- containerPort: 80
Expand Down
8 changes: 4 additions & 4 deletions core/test/unit/src/actions/action-configs-to-graph.ts
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,7 @@ describe("actionConfigsToGraph", () => {
basePath: tmpDir.path,
},
spec: {
command: ["echo", "${action.build.foo.version}"],
command: ["echo", "${actions.build.foo.version}"],
},
},
],
Expand All @@ -313,7 +313,7 @@ describe("actionConfigsToGraph", () => {
explicit: false,
kind: "Build",
name: "foo",
fullRef: ["action", "build", "foo", "version"],
fullRef: ["actions", "build", "foo", "version"],
needsExecutedOutputs: false,
needsStaticOutputs: true,
},
Expand Down Expand Up @@ -343,7 +343,7 @@ describe("actionConfigsToGraph", () => {
basePath: tmpDir.path,
},
spec: {
command: ["echo", "${action.build.foo.outputs.bar}"],
command: ["echo", "${actions.build.foo.outputs.bar}"],
},
},
],
Expand All @@ -360,7 +360,7 @@ describe("actionConfigsToGraph", () => {
explicit: false,
kind: "Build",
name: "foo",
fullRef: ["action", "build", "foo", "outputs", "bar"],
fullRef: ["actions", "build", "foo", "outputs", "bar"],
needsExecutedOutputs: true,
needsStaticOutputs: false,
},
Expand Down
10 changes: 5 additions & 5 deletions core/test/unit/src/tasks/resolve-action.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ describe("ResolveActionTask", () => {
type: "test",
name: "foo",
spec: {
foo: "${action.build.foo.outputs.something}",
foo: "${actions.build.foo.outputs.something}",
},
},
])
Expand All @@ -116,7 +116,7 @@ describe("ResolveActionTask", () => {
type: "test",
name: "foo",
spec: {
foo: "${action.build.foo.version}",
foo: "${actions.build.foo.version}",
},
},
])
Expand Down Expand Up @@ -258,7 +258,7 @@ describe("ResolveActionTask", () => {
type: "test",
name: "foo",
spec: {
deployCommand: ["echo", "${action.build.foo.version}"],
deployCommand: ["echo", "${actions.build.foo.version}"],
},
},
])
Expand Down Expand Up @@ -290,7 +290,7 @@ describe("ResolveActionTask", () => {
type: "test",
name: "foo",
spec: {
foo: "${action.build.foo.version}",
foo: "${actions.build.foo.version}",
},
},
])
Expand Down Expand Up @@ -419,7 +419,7 @@ describe("ResolveActionTask", () => {
type: "test",
name: "foo",
spec: {
deployCommand: ["echo", "${action.run.foo.outputs.log}"],
deployCommand: ["echo", "${actions.run.foo.outputs.log}"],
},
},
])
Expand Down
30 changes: 15 additions & 15 deletions core/test/unit/src/template-string.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1683,42 +1683,42 @@ describe("collectTemplateReferences", () => {
})

describe("getActionTemplateReferences", () => {
context("action.*", () => {
context("actions.*", () => {
it("returns valid action references", () => {
const config = {
build: '${action["build"].build-a}',
deploy: '${action["deploy"].deploy-a}',
run: '${action["run"].run-a}',
test: '${action["test"].test-a}',
build: '${actions["build"].build-a}',
deploy: '${actions["deploy"].deploy-a}',
run: '${actions["run"].run-a}',
test: '${actions["test"].test-a}',
}
const actionTemplateReferences = getActionTemplateReferences(config)
expect(actionTemplateReferences).to.eql([
{
kind: "Build",
name: "build-a",
fullRef: ["action", "build", "build-a"],
fullRef: ["actions", "build", "build-a"],
},
{
kind: "Deploy",
name: "deploy-a",
fullRef: ["action", "deploy", "deploy-a"],
fullRef: ["actions", "deploy", "deploy-a"],
},
{
kind: "Run",
name: "run-a",
fullRef: ["action", "run", "run-a"],
fullRef: ["actions", "run", "run-a"],
},
{
kind: "Test",
name: "test-a",
fullRef: ["action", "test", "test-a"],
fullRef: ["actions", "test", "test-a"],
},
])
})

it("throws if action ref has no kind", () => {
const config = {
foo: "${action}",
foo: "${actions}",
}
void expectError(() => getActionTemplateReferences(config), {
contains: "Found invalid action reference (missing kind)",
Expand All @@ -1727,7 +1727,7 @@ describe("getActionTemplateReferences", () => {

it("throws if action ref has invalid kind", () => {
const config = {
foo: '${action["badkind"].some-name}',
foo: '${actions["badkind"].some-name}',
}
void expectError(() => getActionTemplateReferences(config), {
contains: "Found invalid action reference (invalid kind 'badkind')",
Expand All @@ -1736,7 +1736,7 @@ describe("getActionTemplateReferences", () => {

it("throws if action kind is not a string", () => {
const config = {
foo: "${action[123]}",
foo: "${actions[123]}",
}
void expectError(() => getActionTemplateReferences(config), {
contains: "Found invalid action reference (kind is not a string)",
Expand All @@ -1745,7 +1745,7 @@ describe("getActionTemplateReferences", () => {

it("throws if action kind is not resolvable", () => {
const config = {
foo: "${action[foo.bar].some-name}",
foo: "${actions[foo.bar].some-name}",
}
void expectError(() => getActionTemplateReferences(config), {
contains: "Found invalid action reference (invalid kind '${foo.bar}')",
Expand All @@ -1754,7 +1754,7 @@ describe("getActionTemplateReferences", () => {

it("throws if action ref has no name", () => {
const config = {
foo: '${action["build"]}',
foo: '${actions["build"]}',
}
void expectError(() => getActionTemplateReferences(config), {
contains: "Found invalid action reference (missing name)",
Expand All @@ -1763,7 +1763,7 @@ describe("getActionTemplateReferences", () => {

it("throws if action name is not a string", () => {
const config = {
foo: '${action["build"].123}',
foo: '${actions["build"].123}',
}
void expectError(() => getActionTemplateReferences(config), {
contains: "Found invalid action reference (name is not a string)",
Expand Down
10 changes: 5 additions & 5 deletions docs/k8s-plugins/action-types/helm.md
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ type: helm-pod
dependencies:
- deploy.api
variables:
hostname: vote.${var.baseHostname}
hostname: vote.${var.baseHostname}
timeout: 60
spec:
resource:
Expand Down Expand Up @@ -167,8 +167,8 @@ dependencies: [build.worker-image]
spec:
values:
image:
repository: ${action.build.worker-image.outputs.deployment-image-name}
tag: ${action.build.worker-image.version}
repository: ${actions.build.worker-image.outputs.deployment-image-name}
tag: ${actions.build.worker-image.version}

```

Expand All @@ -195,7 +195,7 @@ spec:
sync:
paths:
- containerPath: /app/src
sourcePath: ${action.build.vote-image.sourcePath}/src
sourcePath: ${actions.build.vote-image.sourcePath}/src
mode: two-way

```
Expand All @@ -216,7 +216,7 @@ You can define a remote environment as a `production` environment by setting the

## Next steps

Check out the full [action reference](../../reference/action-types/README.md) for more details
Check out the full [action reference](../../reference/action-types/README.md) for more details
and the [vote-helm](../../../examples/vote-helm/README.md) example project for a full project
that showcases Garden's Helm support.

Expand Down
8 changes: 4 additions & 4 deletions docs/k8s-plugins/action-types/kubernetes.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ you'll find a lot common between the two guides.

See the full spec for the `kubernetes` deploy action in our [reference docs](../../reference/action-types/Deploy/kubernetes.md).

[`kubernetes-pod` run](../../reference/action-types/Run/kubernetes-pod.md), [`kubernetes-pod` test](../../reference/action-types/Test/kubernetes-pod.md) and `kubernetes-exec`
[`kubernetes-pod` run](../../reference/action-types/Run/kubernetes-pod.md), [`kubernetes-pod` test](../../reference/action-types/Test/kubernetes-pod.md) and `kubernetes-exec`
actions can be used for testing and task porposes.

## Referencing manifests
Expand Down Expand Up @@ -169,7 +169,7 @@ type: kubernetes-pod
dependencies:
- deploy.api
variables:
hostname: vote.${var.baseHostname}
hostname: vote.${var.baseHostname}
timeout: 60
spec:
resource:
Expand Down Expand Up @@ -208,7 +208,7 @@ spec:
spec:
containers:
- name: worker
image: ${action.build.worker-image.outputs.deployment-image-id} # <--- Here we're referencing the output from the api-image module. This will also work in manifest files.
image: ${actions.build.worker-image.outputs.deployment-image-id} # <--- Here we're referencing the output from the api-image module. This will also work in manifest files.
# ...
```

Expand All @@ -232,7 +232,7 @@ spec:
sync:
paths:
- containerPath: /app/src
sourcePath: ${action.build.vote-image.sourcePath}/src
sourcePath: ${actions.build.vote-image.sourcePath}/src
mode: two-way

```
Expand Down
6 changes: 3 additions & 3 deletions docs/reference/action-types/Deploy/helm.md
Original file line number Diff line number Diff line change
Expand Up @@ -336,8 +336,8 @@ spec:
# defined.
#
# This should generally be a templated path to another action's source path (e.g.
# `${action.build.my-container-image.sourcePath}`), or a relative path. If a path is hard-coded, you must make
# sure the path exists, and that it is reliably the correct path for every user.
# `${actions.build.my-container-image.sourcePath}`), or a relative path. If a path is hard-coded, you must
# make sure the path exists, and that it is reliably the correct path for every user.
sourcePath: .

# POSIX-style absolute path to sync to inside the container. The root path (i.e. "/") is not allowed.
Expand Down Expand Up @@ -1126,7 +1126,7 @@ The name of a container in the target. Specify this if the target contains more

The local path to sync from, either absolute or relative to the source directory where the Deploy action is defined.

This should generally be a templated path to another action's source path (e.g. `${action.build.my-container-image.sourcePath}`), or a relative path. If a path is hard-coded, you must make sure the path exists, and that it is reliably the correct path for every user.
This should generally be a templated path to another action's source path (e.g. `${actions.build.my-container-image.sourcePath}`), or a relative path. If a path is hard-coded, you must make sure the path exists, and that it is reliably the correct path for every user.

| Type | Default | Required |
| ----------- | ------- | -------- |
Expand Down
6 changes: 3 additions & 3 deletions docs/reference/action-types/Deploy/kubernetes.md
Original file line number Diff line number Diff line change
Expand Up @@ -318,8 +318,8 @@ spec:
# defined.
#
# This should generally be a templated path to another action's source path (e.g.
# `${action.build.my-container-image.sourcePath}`), or a relative path. If a path is hard-coded, you must make
# sure the path exists, and that it is reliably the correct path for every user.
# `${actions.build.my-container-image.sourcePath}`), or a relative path. If a path is hard-coded, you must
# make sure the path exists, and that it is reliably the correct path for every user.
sourcePath: .

# POSIX-style absolute path to sync to inside the container. The root path (i.e. "/") is not allowed.
Expand Down Expand Up @@ -1070,7 +1070,7 @@ The name of a container in the target. Specify this if the target contains more

The local path to sync from, either absolute or relative to the source directory where the Deploy action is defined.

This should generally be a templated path to another action's source path (e.g. `${action.build.my-container-image.sourcePath}`), or a relative path. If a path is hard-coded, you must make sure the path exists, and that it is reliably the correct path for every user.
This should generally be a templated path to another action's source path (e.g. `${actions.build.my-container-image.sourcePath}`), or a relative path. If a path is hard-coded, you must make sure the path exists, and that it is reliably the correct path for every user.

| Type | Default | Required |
| ----------- | ------- | -------- |
Expand Down
Loading