This repository has been archived by the owner on May 31, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
a415f60
commit 0c7127c
Showing
10 changed files
with
302 additions
and
40 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
settings: | ||
cluster: | ||
name: "cluster-01" | ||
Context: {{- toYaml $ | nindent 4 }} |
17 changes: 17 additions & 0 deletions
17
examples/02-overlays/clusters/cluster-01/workers/subst.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
settings: | ||
cluster: | ||
name: "cluster-01" | ||
resources: | ||
- metdata: | ||
name: "test" | ||
- apiVersion: v1 | ||
kind: Pod | ||
metadata: | ||
name: nginx | ||
spec: | ||
containers: | ||
- name: nginx | ||
image: nginx:1.14.2 | ||
ports: | ||
- containerPort: 80 | ||
Context: {{- toYaml $ | nindent 4 }} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,150 @@ | ||
// Original source: | ||
// https://raw.githubusercontent.com/helm/helm/952708b436bb2c8d5ef0e2e9ef1d8aabe64aeae9/pkg/engine/funcs.go | ||
package utils | ||
|
||
import ( | ||
"bytes" | ||
"encoding/json" | ||
"strings" | ||
"text/template" | ||
|
||
"github.com/BurntSushi/toml" | ||
"github.com/Masterminds/sprig/v3" | ||
"sigs.k8s.io/yaml" | ||
) | ||
|
||
// funcMap returns a mapping of all of the functions that Engine has. | ||
// | ||
// Because some functions are late-bound (e.g. contain context-sensitive | ||
// data), the functions may not all perform identically outside of an Engine | ||
// as they will inside of an Engine. | ||
// | ||
// Known late-bound functions: | ||
// | ||
// - "include" | ||
// - "tpl" | ||
// | ||
// These are late-bound in Engine.Render(). The | ||
// version included in the FuncMap is a placeholder. | ||
func SprigFuncMap() template.FuncMap { | ||
f := sprig.TxtFuncMap() | ||
delete(f, "env") | ||
delete(f, "expandenv") | ||
|
||
// Add some extra functionality | ||
extra := template.FuncMap{ | ||
"toToml": toTOML, | ||
"toYaml": toYAML, | ||
"fromYaml": fromYAML, | ||
"fromYamlArray": fromYAMLArray, | ||
"toJson": toJSON, | ||
"fromJson": fromJSON, | ||
"fromJsonArray": fromJSONArray, | ||
} | ||
|
||
for k, v := range extra { | ||
f[k] = v | ||
} | ||
|
||
return f | ||
} | ||
|
||
// toYAML takes an interface, marshals it to yaml, and returns a string. It will | ||
// always return a string, even on marshal error (empty string). | ||
// | ||
// This is designed to be called from a template. | ||
func toYAML(v interface{}) string { | ||
data, err := yaml.Marshal(v) | ||
if err != nil { | ||
// Swallow errors inside of a template. | ||
return "" | ||
} | ||
return strings.TrimSuffix(string(data), "\n") | ||
} | ||
|
||
// fromYAML converts a YAML document into a map[string]interface{}. | ||
// | ||
// This is not a general-purpose YAML parser, and will not parse all valid | ||
// YAML documents. Additionally, because its intended use is within templates | ||
// it tolerates errors. It will insert the returned error message string into | ||
// m["Error"] in the returned map. | ||
func fromYAML(str string) map[string]interface{} { | ||
m := map[string]interface{}{} | ||
|
||
if err := yaml.Unmarshal([]byte(str), &m); err != nil { | ||
m["Error"] = err.Error() | ||
} | ||
return m | ||
} | ||
|
||
// fromYAMLArray converts a YAML array into a []interface{}. | ||
// | ||
// This is not a general-purpose YAML parser, and will not parse all valid | ||
// YAML documents. Additionally, because its intended use is within templates | ||
// it tolerates errors. It will insert the returned error message string as | ||
// the first and only item in the returned array. | ||
func fromYAMLArray(str string) []interface{} { | ||
a := []interface{}{} | ||
|
||
if err := yaml.Unmarshal([]byte(str), &a); err != nil { | ||
a = []interface{}{err.Error()} | ||
} | ||
return a | ||
} | ||
|
||
// toTOML takes an interface, marshals it to toml, and returns a string. It will | ||
// always return a string, even on marshal error (empty string). | ||
// | ||
// This is designed to be called from a template. | ||
func toTOML(v interface{}) string { | ||
b := bytes.NewBuffer(nil) | ||
e := toml.NewEncoder(b) | ||
err := e.Encode(v) | ||
if err != nil { | ||
return err.Error() | ||
} | ||
return b.String() | ||
} | ||
|
||
// toJSON takes an interface, marshals it to json, and returns a string. It will | ||
// always return a string, even on marshal error (empty string). | ||
// | ||
// This is designed to be called from a template. | ||
func toJSON(v interface{}) string { | ||
data, err := json.Marshal(v) | ||
if err != nil { | ||
// Swallow errors inside of a template. | ||
return "" | ||
} | ||
return string(data) | ||
} | ||
|
||
// fromJSON converts a JSON document into a map[string]interface{}. | ||
// | ||
// This is not a general-purpose JSON parser, and will not parse all valid | ||
// JSON documents. Additionally, because its intended use is within templates | ||
// it tolerates errors. It will insert the returned error message string into | ||
// m["Error"] in the returned map. | ||
func fromJSON(str string) map[string]interface{} { | ||
m := make(map[string]interface{}) | ||
|
||
if err := json.Unmarshal([]byte(str), &m); err != nil { | ||
m["Error"] = err.Error() | ||
} | ||
return m | ||
} | ||
|
||
// fromJSONArray converts a JSON array into a []interface{}. | ||
// | ||
// This is not a general-purpose JSON parser, and will not parse all valid | ||
// JSON documents. Additionally, because its intended use is within templates | ||
// it tolerates errors. It will insert the returned error message string as | ||
// the first and only item in the returned array. | ||
func fromJSONArray(str string) []interface{} { | ||
a := []interface{}{} | ||
|
||
if err := json.Unmarshal([]byte(str), &a); err != nil { | ||
a = []interface{}{err.Error()} | ||
} | ||
return a | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.