mirror of
https://github.com/coder/coder.git
synced 2026-09-24 15:04:27 +08:00
feat: add base template variables to API (#26425)
This commit is contained in:
@@ -41,6 +41,7 @@ type BaseManifest struct {
|
||||
DisplayName string `json:"display_name"`
|
||||
OS string `json:"os"`
|
||||
DefaultContext BaseDefaultContext `json:"default_context"`
|
||||
Variables []ModuleVariable `json:"variables"`
|
||||
}
|
||||
|
||||
// BaseDefaultContext holds default render values stored in base.json.
|
||||
@@ -192,6 +193,17 @@ func BaseTemplateIDs() []string {
|
||||
return ids
|
||||
}
|
||||
|
||||
// BaseVariables returns the user-facing variables for a given base
|
||||
// template ID. Computed variables are excluded. Returns nil if the
|
||||
// base is unknown or has no variables.
|
||||
func BaseVariables(exampleID string) []ModuleVariable {
|
||||
bases, err := loadBases()
|
||||
if err != nil || bases[exampleID] == nil {
|
||||
return nil
|
||||
}
|
||||
return bases[exampleID].Manifest.Variables
|
||||
}
|
||||
|
||||
// BaseTemplateFS returns a filesystem rooted at the given base template
|
||||
// directory within the embedded bases catalog. Returns an error if
|
||||
// exampleID is not a known base template.
|
||||
|
||||
@@ -4,5 +4,24 @@
|
||||
"os": "linux",
|
||||
"default_context": {
|
||||
"container_image": "codercom/enterprise-base:ubuntu"
|
||||
}
|
||||
},
|
||||
"variables": [
|
||||
{
|
||||
"name": "use_kubeconfig",
|
||||
"type": "bool",
|
||||
"description": "Use host kubeconfig. Set to true if the Coder host is running outside the Kubernetes cluster for workspaces.",
|
||||
"default": false,
|
||||
"required": false,
|
||||
"sensitive": false,
|
||||
"computed": false
|
||||
},
|
||||
{
|
||||
"name": "namespace",
|
||||
"type": "string",
|
||||
"description": "The Kubernetes namespace to create workspaces in. Must exist prior to creating workspaces.",
|
||||
"required": true,
|
||||
"sensitive": false,
|
||||
"computed": false
|
||||
}
|
||||
]
|
||||
}
|
||||
|
||||
@@ -4,6 +4,7 @@ import (
|
||||
"archive/tar"
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"maps"
|
||||
"regexp"
|
||||
"strings"
|
||||
"time"
|
||||
@@ -21,6 +22,9 @@ var numberPattern = regexp.MustCompile(`^-?[0-9]+(\.[0-9]+)?$`)
|
||||
// ComposeRequest describes which base template and modules to render.
|
||||
type ComposeRequest struct {
|
||||
BaseTemplateID string
|
||||
// BaseVariableValues maps base template variable names to their
|
||||
// user-supplied values.
|
||||
BaseVariableValues map[string]string
|
||||
// RegistryURL is the module registry base URL from the deployment
|
||||
// config (CODER_TEMPLATE_BUILDER_REGISTRY_URL).
|
||||
RegistryURL string
|
||||
@@ -49,7 +53,7 @@ type ComposeResult struct {
|
||||
// source files. It extracts the coder_agent resource name from the
|
||||
// rendered base HCL and wires it into each module block.
|
||||
func Compose(req ComposeRequest) (*ComposeResult, error) {
|
||||
mainTF, err := renderBase(req.BaseTemplateID)
|
||||
mainTF, err := renderBase(req.BaseTemplateID, req.BaseVariableValues)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -93,9 +97,14 @@ func formatHCL(src []byte) []byte {
|
||||
return hclwrite.Format(src)
|
||||
}
|
||||
|
||||
// renderBase renders the base template for the given example ID.
|
||||
func renderBase(baseTemplateID string) ([]byte, error) {
|
||||
// renderBase renders the base template for the given example ID,
|
||||
// merging any user-supplied variable values into the render context.
|
||||
func renderBase(baseTemplateID string, baseVars map[string]string) ([]byte, error) {
|
||||
renderCtx := DefaultBaseRenderContext(baseTemplateID)
|
||||
if renderCtx.Variables == nil {
|
||||
renderCtx.Variables = make(map[string]string)
|
||||
}
|
||||
maps.Copy(renderCtx.Variables, baseVars)
|
||||
mainTF, err := RenderBaseTemplate(baseTemplateID, "main.tf.tmpl", renderCtx)
|
||||
if err != nil {
|
||||
return nil, xerrors.Errorf("render base template: %w", err)
|
||||
|
||||
Reference in New Issue
Block a user