2016-07-28 02:04:11 +08:00
|
|
|
/*
|
2018-08-25 03:03:55 +08:00
|
|
|
Copyright The Helm Authors.
|
2019-03-07 07:45:52 +08:00
|
|
|
|
2016-07-28 02:04:11 +08:00
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
2019-03-07 07:45:52 +08:00
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
2016-07-28 02:04:11 +08:00
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2019-03-07 07:45:52 +08:00
|
|
|
package engine
|
2016-07-28 02:04:11 +08:00
|
|
|
|
|
|
|
import (
|
2016-12-10 03:53:53 +08:00
|
|
|
"encoding/base64"
|
|
|
|
"path"
|
2016-12-13 08:19:47 +08:00
|
|
|
"strings"
|
2016-12-10 03:53:53 +08:00
|
|
|
|
2016-12-02 00:09:46 +08:00
|
|
|
"github.com/gobwas/glob"
|
2018-04-19 07:28:50 +08:00
|
|
|
|
2025-09-02 05:46:14 +08:00
|
|
|
"helm.sh/helm/v4/pkg/chart/common"
|
2016-07-28 02:04:11 +08:00
|
|
|
)
|
|
|
|
|
2019-03-07 07:45:52 +08:00
|
|
|
// files is a map of files in a chart that can be accessed from a template.
|
|
|
|
type files map[string][]byte
|
2016-07-28 02:04:11 +08:00
|
|
|
|
2019-03-07 07:45:52 +08:00
|
|
|
// NewFiles creates a new files from chart files.
|
2020-02-07 00:45:15 +08:00
|
|
|
// Given an []*chart.File (the format for files in a chart.Chart), extract a map of files.
|
2025-09-02 05:46:14 +08:00
|
|
|
func newFiles(from []*common.File) files {
|
2018-08-25 02:28:29 +08:00
|
|
|
files := make(map[string][]byte)
|
2018-04-19 07:28:50 +08:00
|
|
|
for _, f := range from {
|
|
|
|
files[f.Name] = f.Data
|
2016-07-28 02:04:11 +08:00
|
|
|
}
|
|
|
|
return files
|
|
|
|
}
|
|
|
|
|
2016-08-04 03:01:13 +08:00
|
|
|
// GetBytes gets a file by path.
|
|
|
|
//
|
|
|
|
// The returned data is raw. In a template context, this is identical to calling
|
|
|
|
// {{index .Files $path}}.
|
2016-07-28 02:04:11 +08:00
|
|
|
//
|
|
|
|
// This is intended to be accessed from within a template, so a missed key returns
|
|
|
|
// an empty []byte.
|
2019-03-07 07:45:52 +08:00
|
|
|
func (f files) GetBytes(name string) []byte {
|
2018-08-25 02:28:29 +08:00
|
|
|
if v, ok := f[name]; ok {
|
|
|
|
return v
|
2016-07-28 02:04:11 +08:00
|
|
|
}
|
2018-08-25 02:28:29 +08:00
|
|
|
return []byte{}
|
2016-07-28 02:04:11 +08:00
|
|
|
}
|
|
|
|
|
2016-08-04 03:01:13 +08:00
|
|
|
// Get returns a string representation of the given file.
|
|
|
|
//
|
|
|
|
// Fetch the contents of a file as a string. It is designed to be called in a
|
|
|
|
// template.
|
2016-07-28 02:04:11 +08:00
|
|
|
//
|
2016-08-04 03:01:13 +08:00
|
|
|
// {{.Files.Get "foo"}}
|
2019-03-07 07:45:52 +08:00
|
|
|
func (f files) Get(name string) string {
|
2016-08-04 03:01:13 +08:00
|
|
|
return string(f.GetBytes(name))
|
2016-07-28 02:04:11 +08:00
|
|
|
}
|
2016-12-02 00:09:46 +08:00
|
|
|
|
|
|
|
// Glob takes a glob pattern and returns another files object only containing
|
|
|
|
// matched files.
|
|
|
|
//
|
|
|
|
// This is designed to be called from a template.
|
2016-12-02 02:07:08 +08:00
|
|
|
//
|
2016-12-02 00:09:46 +08:00
|
|
|
// {{ range $name, $content := .Files.Glob("foo/**") }}
|
|
|
|
// {{ $name }}: |
|
|
|
|
// {{ .Files.Get($name) | indent 4 }}{{ end }}
|
2019-03-07 07:45:52 +08:00
|
|
|
func (f files) Glob(pattern string) files {
|
2016-12-02 00:09:46 +08:00
|
|
|
g, err := glob.Compile(pattern, '/')
|
|
|
|
if err != nil {
|
|
|
|
g, _ = glob.Compile("**")
|
|
|
|
}
|
|
|
|
|
2019-03-07 07:45:52 +08:00
|
|
|
nf := newFiles(nil)
|
2016-12-02 00:09:46 +08:00
|
|
|
for name, contents := range f {
|
|
|
|
if g.Match(name) {
|
|
|
|
nf[name] = contents
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nf
|
|
|
|
}
|
2016-12-10 03:53:53 +08:00
|
|
|
|
|
|
|
// AsConfig turns a Files group and flattens it to a YAML map suitable for
|
2017-06-21 08:40:10 +08:00
|
|
|
// including in the 'data' section of a Kubernetes ConfigMap definition.
|
|
|
|
// Duplicate keys will be overwritten, so be aware that your file names
|
2016-12-10 03:53:53 +08:00
|
|
|
// (regardless of path) should be unique.
|
|
|
|
//
|
|
|
|
// This is designed to be called from a template, and will return empty string
|
2019-03-07 07:45:52 +08:00
|
|
|
// (via toYAML function) if it cannot be serialized to YAML, or if the Files
|
2016-12-10 03:53:53 +08:00
|
|
|
// object is nil.
|
|
|
|
//
|
|
|
|
// The output will not be indented, so you will want to pipe this to the
|
2017-06-21 08:40:10 +08:00
|
|
|
// 'indent' template function.
|
2016-12-10 03:53:53 +08:00
|
|
|
//
|
2023-03-10 02:27:22 +08:00
|
|
|
// data:
|
|
|
|
//
|
2016-12-10 03:53:53 +08:00
|
|
|
// {{ .Files.Glob("config/**").AsConfig() | indent 4 }}
|
2019-03-07 07:45:52 +08:00
|
|
|
func (f files) AsConfig() string {
|
2016-12-10 03:53:53 +08:00
|
|
|
if f == nil {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
2018-08-25 02:28:29 +08:00
|
|
|
m := make(map[string]string)
|
2016-12-10 03:53:53 +08:00
|
|
|
|
|
|
|
// Explicitly convert to strings, and file names
|
|
|
|
for k, v := range f {
|
|
|
|
m[path.Base(k)] = string(v)
|
|
|
|
}
|
|
|
|
|
2019-03-07 07:45:52 +08:00
|
|
|
return toYAML(m)
|
2016-12-10 03:53:53 +08:00
|
|
|
}
|
|
|
|
|
2017-06-21 08:40:10 +08:00
|
|
|
// AsSecrets returns the base64-encoded value of a Files object suitable for
|
|
|
|
// including in the 'data' section of a Kubernetes Secret definition.
|
|
|
|
// Duplicate keys will be overwritten, so be aware that your file names
|
2016-12-10 03:53:53 +08:00
|
|
|
// (regardless of path) should be unique.
|
|
|
|
//
|
|
|
|
// This is designed to be called from a template, and will return empty string
|
2019-03-07 07:45:52 +08:00
|
|
|
// (via toYAML function) if it cannot be serialized to YAML, or if the Files
|
2016-12-10 03:53:53 +08:00
|
|
|
// object is nil.
|
|
|
|
//
|
|
|
|
// The output will not be indented, so you will want to pipe this to the
|
2017-06-21 08:40:10 +08:00
|
|
|
// 'indent' template function.
|
2016-12-10 03:53:53 +08:00
|
|
|
//
|
2023-03-10 02:27:22 +08:00
|
|
|
// data:
|
|
|
|
//
|
2023-01-21 03:24:13 +08:00
|
|
|
// {{ .Files.Glob("secrets/*").AsSecrets() | indent 4 }}
|
2019-03-07 07:45:52 +08:00
|
|
|
func (f files) AsSecrets() string {
|
2016-12-10 03:53:53 +08:00
|
|
|
if f == nil {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
2018-08-25 02:28:29 +08:00
|
|
|
m := make(map[string]string)
|
2016-12-10 03:53:53 +08:00
|
|
|
|
|
|
|
for k, v := range f {
|
|
|
|
m[path.Base(k)] = base64.StdEncoding.EncodeToString(v)
|
|
|
|
}
|
|
|
|
|
2019-03-07 07:45:52 +08:00
|
|
|
return toYAML(m)
|
2016-12-10 03:53:53 +08:00
|
|
|
}
|
|
|
|
|
2016-12-13 09:53:32 +08:00
|
|
|
// Lines returns each line of a named file (split by "\n") as a slice, so it can
|
|
|
|
// be ranged over in your templates.
|
|
|
|
//
|
|
|
|
// This is designed to be called from a template.
|
|
|
|
//
|
|
|
|
// {{ range .Files.Lines "foo/bar.html" }}
|
|
|
|
// {{ . }}{{ end }}
|
2019-03-07 07:45:52 +08:00
|
|
|
func (f files) Lines(path string) []string {
|
2016-12-13 08:19:47 +08:00
|
|
|
if f == nil || f[path] == nil {
|
|
|
|
return []string{}
|
|
|
|
}
|
2023-01-21 03:24:13 +08:00
|
|
|
s := string(f[path])
|
|
|
|
if s[len(s)-1] == '\n' {
|
|
|
|
s = s[:len(s)-1]
|
|
|
|
}
|
|
|
|
return strings.Split(s, "\n")
|
2016-12-13 08:19:47 +08:00
|
|
|
}
|