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

Add the ability to specify Zarf variables as filepaths #1906

Merged
merged 11 commits into from
Jul 19, 2023
4 changes: 2 additions & 2 deletions docs/3-create-a-zarf-package/4-zarf-schema.md
Original file line number Diff line number Diff line change
Expand Up @@ -2209,7 +2209,7 @@ Must be one of:
 
<blockquote>

**Description:** Changes the handling of a variable to load contents differently (i.e. from a file rather than as a raw variable)
**Description:** Changes the handling of a variable to load contents differently (i.e. from a file rather than as a raw variable - templated files should be kept below 1 MiB)

| | |
| -------- | ------------------ |
Expand Down Expand Up @@ -2840,7 +2840,7 @@ Must be one of:
&nbsp;
<blockquote>

**Description:** Changes the handling of a variable to load contents differently (i.e. from a file rather than as a raw variable)
**Description:** Changes the handling of a variable to load contents differently (i.e. from a file rather than as a raw variable - templated files should be kept below 1 MiB)

| | |
| -------- | ------------------ |
Expand Down
2 changes: 1 addition & 1 deletion src/pkg/packager/variables.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ func (p *Packager) setVariableMapInConfig() error {
return nil
}

func (p *Packager) setVariableInConfig(name, value string, sensitive bool, autoIndent bool, varType string) {
func (p *Packager) setVariableInConfig(name, value string, sensitive bool, autoIndent bool, varType types.VariableType) {
p.cfg.SetVariableMap[name] = &types.ZarfSetVariable{
Name: name,
Value: value,
Expand Down
7 changes: 4 additions & 3 deletions src/pkg/utils/io.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (

"github.com/defenseunicorns/zarf/src/pkg/message"
"github.com/defenseunicorns/zarf/src/pkg/utils/helpers"
"github.com/defenseunicorns/zarf/src/types"
"github.com/otiai10/copy"
)

Expand Down Expand Up @@ -54,7 +55,7 @@ func GetCryptoHashFromFile(path string, hashName crypto.Hash) (string, error) {
type TextTemplate struct {
Sensitive bool
AutoIndent bool
Type string
Type types.VariableType
Value string
}

Expand Down Expand Up @@ -193,9 +194,9 @@ func ReplaceTextTemplate(path string, mappings map[string]*TextTemplate, depreca
value = template.Value

// Check if the value is a file type and load the value contents from the file
if template.Type == "file" {
if template.Type == types.FileVariableType {
if isText, err := IsTextFile(value); err != nil || !isText {
message.Warn("Refusing to load a non-text file for templating")
message.Warnf("Refusing to load a non-text file for templating %s", templateKey)
line = matches[regexTemplateLine.SubexpIndex("postTemplate")]
continue
}
Expand Down
8 changes: 4 additions & 4 deletions src/types/component.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,10 +169,10 @@ type ZarfComponentAction struct {

// ZarfComponentActionSetVariable represents a variable that is to be set via an action
type ZarfComponentActionSetVariable struct {
Name string `json:"name" jsonschema:"description=The name to be used for the variable,pattern=^[A-Z0-9_]+$"`
Sensitive bool `json:"sensitive,omitempty" jsonschema:"description=Whether to mark this variable as sensitive to not print it in the Zarf log"`
AutoIndent bool `json:"autoIndent,omitempty" jsonschema:"description=Whether to automatically indent the variable's value (if multiline) when templating. Based on the number of chars before the start of ###ZARF_VAR_."`
Type string `json:"type,omitempty" jsonschema:"description=Changes the handling of a variable to load contents differently (i.e. from a file rather than as a raw variable),enum=raw,enum=file"`
Name string `json:"name" jsonschema:"description=The name to be used for the variable,pattern=^[A-Z0-9_]+$"`
Sensitive bool `json:"sensitive,omitempty" jsonschema:"description=Whether to mark this variable as sensitive to not print it in the Zarf log"`
AutoIndent bool `json:"autoIndent,omitempty" jsonschema:"description=Whether to automatically indent the variable's value (if multiline) when templating. Based on the number of chars before the start of ###ZARF_VAR_."`
Type VariableType `json:"type,omitempty" jsonschema:"description=Changes the handling of a variable to load contents differently (i.e. from a file rather than as a raw variable - templated files should be kept below 1 MiB),enum=raw,enum=file"`
}

// ZarfComponentActionWait specifies a condition to wait for before continuing
Expand Down
14 changes: 7 additions & 7 deletions src/types/package.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,13 @@ type ZarfBuildData struct {

// ZarfPackageVariable are variables that can be used to dynamically template K8s resources.
type ZarfPackageVariable struct {
Name string `json:"name" jsonschema:"description=The name to be used for the variable,pattern=^[A-Z0-9_]+$"`
Description string `json:"description,omitempty" jsonschema:"description=A description of the variable to be used when prompting the user a value"`
Default string `json:"default,omitempty" jsonschema:"description=The default value to use for the variable"`
Prompt bool `json:"prompt,omitempty" jsonschema:"description=Whether to prompt the user for input for this variable"`
Sensitive bool `json:"sensitive,omitempty" jsonschema:"description=Whether to mark this variable as sensitive to not print it in the Zarf log"`
AutoIndent bool `json:"autoIndent,omitempty" jsonschema:"description=Whether to automatically indent the variable's value (if multiline) when templating. Based on the number of chars before the start of ###ZARF_VAR_."`
Type string `json:"type,omitempty" jsonschema:"description=Changes the handling of a variable to load contents differently (i.e. from a file rather than as a raw variable),enum=raw,enum=file"`
Name string `json:"name" jsonschema:"description=The name to be used for the variable,pattern=^[A-Z0-9_]+$"`
Description string `json:"description,omitempty" jsonschema:"description=A description of the variable to be used when prompting the user a value"`
Default string `json:"default,omitempty" jsonschema:"description=The default value to use for the variable"`
Prompt bool `json:"prompt,omitempty" jsonschema:"description=Whether to prompt the user for input for this variable"`
Sensitive bool `json:"sensitive,omitempty" jsonschema:"description=Whether to mark this variable as sensitive to not print it in the Zarf log"`
AutoIndent bool `json:"autoIndent,omitempty" jsonschema:"description=Whether to automatically indent the variable's value (if multiline) when templating. Based on the number of chars before the start of ###ZARF_VAR_."`
Type VariableType `json:"type,omitempty" jsonschema:"description=Changes the handling of a variable to load contents differently (i.e. from a file rather than as a raw variable - templated files should be kept below 1 MiB),enum=raw,enum=file"`
}

// ZarfPackageConstant are constants that can be used to dynamically template K8s resources.
Expand Down
15 changes: 10 additions & 5 deletions src/types/runtime.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,13 @@ const (
ManifestsFolder = "manifests"
DataInjectionsFolder = "data"
ValuesFolder = "values"

RawVariableType VariableType = "raw"
FileVariableType VariableType = "file"
)

type VariableType string
Racer159 marked this conversation as resolved.
Show resolved Hide resolved

// ZarfCommonOptions tracks the user-defined preferences used across commands.
type ZarfCommonOptions struct {
Confirm bool `json:"confirm" jsonschema:"description=Verify that Zarf should perform an action"`
Expand Down Expand Up @@ -86,11 +91,11 @@ type ZarfPartialPackageData struct {

// ZarfSetVariable tracks internal variables that have been set during this run of Zarf
type ZarfSetVariable struct {
Name string `json:"name" jsonschema:"description=The name to be used for the variable,pattern=^[A-Z0-9_]+$"`
Sensitive bool `json:"sensitive,omitempty" jsonschema:"description=Whether to mark this variable as sensitive to not print it in the Zarf log"`
AutoIndent bool `json:"autoIndent,omitempty" jsonschema:"description=Whether to automatically indent the variable's value (if multiline) when templating. Based on the number of chars before the start of ###ZARF_VAR_."`
Value string `json:"value" jsonschema:"description=The value the variable is currently set with"`
Type string `json:"type,omitempty" jsonschema:"description=Changes the handling of a variable to load contents differently (i.e. from a file rather than as a raw variable),enum=raw,enum=file"`
Name string `json:"name" jsonschema:"description=The name to be used for the variable,pattern=^[A-Z0-9_]+$"`
Sensitive bool `json:"sensitive,omitempty" jsonschema:"description=Whether to mark this variable as sensitive to not print it in the Zarf log"`
AutoIndent bool `json:"autoIndent,omitempty" jsonschema:"description=Whether to automatically indent the variable's value (if multiline) when templating. Based on the number of chars before the start of ###ZARF_VAR_."`
Value string `json:"value" jsonschema:"description=The value the variable is currently set with"`
Type VariableType `json:"type,omitempty" jsonschema:"description=Changes the handling of a variable to load contents differently (i.e. from a file rather than as a raw variable - templated files should be kept below 1 MiB),enum=raw,enum=file"`
}

// ConnectString contains information about a connection made with Zarf connect.
Expand Down
6 changes: 3 additions & 3 deletions src/ui/lib/api-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -463,14 +463,14 @@ export interface ZarfComponentActionSetVariable {
sensitive?: boolean;
/**
* Changes the handling of a variable to load contents differently (i.e. from a file rather
* than as a raw variable)
* than as a raw variable - templated files should be kept below 1 MiB)
*/
type?: Type;
}

/**
* Changes the handling of a variable to load contents differently (i.e. from a file rather
* than as a raw variable)
* than as a raw variable - templated files should be kept below 1 MiB)
*/
export enum Type {
File = "file",
Expand Down Expand Up @@ -978,7 +978,7 @@ export interface ZarfPackageVariable {
sensitive?: boolean;
/**
* Changes the handling of a variable to load contents differently (i.e. from a file rather
* than as a raw variable)
* than as a raw variable - templated files should be kept below 1 MiB)
*/
type?: Type;
}
Expand Down
4 changes: 2 additions & 2 deletions zarf.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -482,7 +482,7 @@
"file"
],
"type": "string",
"description": "Changes the handling of a variable to load contents differently (i.e. from a file rather than as a raw variable)"
"description": "Changes the handling of a variable to load contents differently (i.e. from a file rather than as a raw variable - templated files should be kept below 1 MiB)"
}
},
"additionalProperties": false,
Expand Down Expand Up @@ -1019,7 +1019,7 @@
"file"
],
"type": "string",
"description": "Changes the handling of a variable to load contents differently (i.e. from a file rather than as a raw variable)"
"description": "Changes the handling of a variable to load contents differently (i.e. from a file rather than as a raw variable - templated files should be kept below 1 MiB)"
}
},
"additionalProperties": false,
Expand Down