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

feat(templates): allow templating in template path #6736

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions core/src/config/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ export interface GardenResourceInternalFields {
inputs?: DeepPrimitiveMap
parentName?: string
templateName?: string
templatePath?: string
// Used to map fields to specific doc and location
yamlDoc?: YamlDocumentWithSource
}
Expand Down
1 change: 1 addition & 0 deletions core/src/config/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -297,6 +297,7 @@ export interface ModuleConfig<M extends {} = any, S extends {} = any, T extends
// set by ModuleTemplates for templated modules
parentName?: string
templateName?: string
templatePath?: string
inputs?: DeepPrimitiveMap

// Plugins can add custom fields that are kept here
Expand Down
1 change: 1 addition & 0 deletions core/src/config/render-template.ts
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,7 @@ export async function renderConfigTemplate({
enterpriseDomain,
parentName: resolved.name,
templateName: template.name,
templatePath: template.internal.basePath,
inputs: partiallyResolvedInputs,
})

Expand Down
11 changes: 9 additions & 2 deletions core/src/config/template-contexts/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import { exampleVersion, OutputConfigContext } from "./module.js"
import { TemplatableConfigContext } from "./project.js"
import { DOCS_BASE_URL } from "../../constants.js"
import { styles } from "../../logger/styles.js"
import { InternalError } from "../../exceptions.js"

function mergeVariables({ garden, variables }: { garden: Garden; variables: DeepPrimitiveMap }): DeepPrimitiveMap {
const mergedVariables: DeepPrimitiveMap = {}
Expand Down Expand Up @@ -296,6 +297,7 @@ export class ActionSpecContext extends OutputConfigContext {
const sourcePath = action.sourcePath()
const parentName = internal?.parentName
const templateName = internal?.templateName
const templatePath = internal?.templatePath

this.actions = new ActionReferencesContext(this, partialRuntimeResolution, [
...resolvedDependencies,
Expand All @@ -308,9 +310,14 @@ export class ActionSpecContext extends OutputConfigContext {
new ErrorContext(`Action ${styles.highlight.bold(action.key())} cannot reference itself.`)
)

if (parentName && templateName) {
if (parentName) {
if (!templateName || !templatePath) {
throw new InternalError({
message: `Missing template name and path when rendering ${parentName}. This is a bug, please report to the Garden team.`,
})
}
this.parent = new ParentContext(this, parentName)
this.template = new TemplateContext(this, templateName)
this.template = new TemplateContext({ root: this, name: templateName, path: templatePath })
}
this.inputs = inputs

Expand Down
10 changes: 7 additions & 3 deletions core/src/config/template-contexts/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -316,9 +316,13 @@ export class TemplateContext extends ConfigContext {
@schema(joiIdentifier().description(`The name of the template.`))
public name: string

constructor(root: ConfigContext, name: string) {
super(root)
this.name = name
@schema(joi.string().description(`The absolute path to the directory containing the ConfigTemplate being rendered.`))
public path: string
Comment on lines +319 to +320
Copy link
Member

Choose a reason for hiding this comment

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

Wouldn't this cause an issue where the version calculation is different depending on the directory where the git repository has been checked out in?
Also, most of the time we do not accept absolute paths in the action specs.
Maybe we need to calculate the relative path to the ConfigTemplate from the location of the RenderConfigTemplate.

See also https://discord.com/channels/817392104711651328/1318995192501764151/1319639199036735500

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Ah yeah, that's a good point. Relative paths would avoid the versioning problem.


constructor(params: { root: ConfigContext; name: string; path: string }) {
super(params.root)
this.name = params.name
this.path = params.path
}
}

Expand Down
13 changes: 10 additions & 3 deletions core/src/config/template-contexts/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import type { DeployTask } from "../../tasks/deploy.js"
import type { RunTask } from "../../tasks/run.js"
import { DOCS_BASE_URL } from "../../constants.js"
import { styles } from "../../logger/styles.js"
import { InternalError } from "../../exceptions.js"

export const exampleVersion = "v-17ad4cb3fd"

Expand Down Expand Up @@ -252,6 +253,7 @@ export interface ModuleConfigContextParams extends OutputConfigContextParams {
// Template attributes
parentName: string | undefined
templateName: string | undefined
templatePath: string | undefined
inputs: DeepPrimitiveMap | undefined
}

Expand Down Expand Up @@ -286,14 +288,19 @@ export class ModuleConfigContext extends OutputConfigContext {
constructor(params: ModuleConfigContextParams) {
super(params)

const { name, path, inputs, parentName, templateName, buildPath } = params
const { name, path, inputs, parentName, templateName, templatePath, buildPath } = params

// Throw specific error when attempting to resolve self
this.modules.set(name, new ErrorContext(`Config ${styles.highlight.bold(name)} cannot reference itself.`))

if (parentName && templateName) {
if (parentName) {
if (!templateName || !templatePath) {
throw new InternalError({
message: `Missing template name and path when rendering ${parentName}. This is a bug, please report to the Garden team.`,
})
}
this.parent = new ParentContext(this, parentName)
this.template = new TemplateContext(this, templateName)
this.template = new TemplateContext({ root: this, name: templateName, path: templatePath })
}
this.inputs = inputs || {}

Expand Down
10 changes: 7 additions & 3 deletions core/src/config/template-contexts/project.ts
Original file line number Diff line number Diff line change
Expand Up @@ -440,8 +440,12 @@ export class TemplatableConfigContext extends RemoteSourceConfigContext {

constructor(garden: Garden, config: ActionConfig | WorkflowConfig) {
super(garden, garden.variables)
this.inputs = config.internal.inputs || {}
this.parent = config.internal.parentName ? new ParentContext(this, config.internal.parentName) : undefined
this.template = config.internal.templateName ? new TemplateContext(this, config.internal.templateName) : undefined
const { inputs, parentName, templateName, templatePath } = config.internal
this.inputs = inputs || {}
this.parent = parentName ? new ParentContext(this, parentName) : undefined
this.template =
templateName && templatePath
? new TemplateContext({ root: this, name: templateName, path: templatePath })
: undefined
}
}
9 changes: 7 additions & 2 deletions core/src/config/template-contexts/render.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,16 @@ export class RenderTemplateConfigContext extends ProjectConfigContext {
public inputs: DeepPrimitiveMap

constructor(
params: { parentName: string; templateName: string; inputs: DeepPrimitiveMap } & ProjectConfigContextParams
params: {
parentName: string
templatePath: string
templateName: string
inputs: DeepPrimitiveMap
} & ProjectConfigContextParams
) {
super(params)
this.parent = new ParentContext(this, params.parentName)
this.template = new TemplateContext(this, params.templateName)
this.template = new TemplateContext({ root: this, path: params.templatePath, name: params.templateName })
this.inputs = params.inputs
}
}
3 changes: 3 additions & 0 deletions core/src/resolve-module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -518,6 +518,7 @@ export class ModuleResolver {
buildPath,
parentName: rawConfig.parentName,
templateName: rawConfig.templateName,
templatePath: rawConfig.path,
inputs: {},
modules: [],
graphResults: this.graphResults,
Expand Down Expand Up @@ -606,6 +607,7 @@ export class ModuleResolver {
buildPath,
parentName: config.parentName,
templateName: config.templateName,
templatePath: config.path,
inputs,
graphResults: this.graphResults,
partialRuntimeResolution: true,
Expand Down Expand Up @@ -812,6 +814,7 @@ export class ModuleResolver {
buildPath,
parentName: resolvedConfig.parentName,
templateName: resolvedConfig.templateName,
templatePath: resolvedConfig.path,
inputs: resolvedConfig.inputs,
modules: dependencies,
graphResults: this.graphResults,
Expand Down
2 changes: 2 additions & 0 deletions core/test/unit/src/config/render-template.ts
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,7 @@ describe("config templates", () => {
{
type: "test",
name: "${parent.name}-${template.name}-${inputs.foo}",
variables: { templatePath: "${template.path}" },
build: {
dependencies: [{ name: "${parent.name}-${template.name}-foo", copy: [] }],
timeout: DEFAULT_BUILD_TIMEOUT_SEC,
Expand All @@ -205,6 +206,7 @@ describe("config templates", () => {
expect(module.name).to.equal("test-test-bar")
expect(module.build.dependencies).to.eql([{ name: "test-test-foo", copy: [] }])
expect(module.spec.image).to.equal("${modules.foo.outputs.bar || inputs.foo}")
expect(module.variables!["templatePath"]).to.equal(garden.projectRoot)
})

it("throws if config is invalid", async () => {
Expand Down
1 change: 1 addition & 0 deletions core/test/unit/src/config/template-contexts/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ describe("ModuleConfigContext", () => {
parentName: module.parentName,
inputs: module.inputs,
templateName: module.templateName,
templatePath: module.templatePath,
})
})

Expand Down