24
"helm.sh/helm/v3/pkg/chart"
25
"helm.sh/helm/v3/pkg/chart/loader"
26
"k8s.io/apimachinery/pkg/version"
28
"istio.io/istio/manifests"
29
"istio.io/istio/operator/pkg/util"
34
DefaultProfileFilename = "default.yaml"
35
ChartsSubdirName = "charts"
36
profilesRoot = "profiles"
53
func NewGenericRenderer(files fs.FS, dir, componentName, namespace string, version *version.Info) *Renderer {
56
componentName: componentName,
64
func (h *Renderer) Run() error {
65
if err := h.loadChart(); err != nil {
74
func (h *Renderer) RenderManifest(values string) (string, error) {
76
return "", fmt.Errorf("fileTemplateRenderer for %s not started in renderChart", h.componentName)
78
return renderChart(h.namespace, values, h.chart, nil, h.version)
82
func (h *Renderer) RenderManifestFiltered(values string, filter TemplateFilterFunc) (string, error) {
84
return "", fmt.Errorf("fileTemplateRenderer for %s not started in renderChart", h.componentName)
86
return renderChart(h.namespace, values, h.chart, filter, h.version)
89
func GetFilesRecursive(f fs.FS, root string) ([]string, error) {
91
err := fs.WalkDir(f, root, func(path string, d fs.DirEntry, err error) error {
98
res = append(res, path)
105
func (h *Renderer) loadChart() error {
106
fnames, err := GetFilesRecursive(h.files, h.dir)
108
if os.IsNotExist(err) {
109
return fmt.Errorf("component %q does not exist", h.componentName)
111
return fmt.Errorf("list files: %v", err)
113
var bfs []*loader.BufferedFile
114
for _, fname := range fnames {
115
b, err := fs.ReadFile(h.files, fname)
117
return fmt.Errorf("read file: %v", err)
120
name := strings.ReplaceAll(stripPrefix(fname, h.dir), string(filepath.Separator), "/")
121
bf := &loader.BufferedFile{
125
bfs = append(bfs, bf)
126
scope.Debugf("Chart loaded: %s", bf.Name)
129
h.chart, err = loader.LoadFiles(bfs)
131
return fmt.Errorf("load files: %v", err)
136
func builtinProfileToFilename(name string) string {
138
return DefaultProfileFilename
140
return name + ".yaml"
143
func LoadValues(profileName string, chartsDir string) (string, error) {
144
path := strings.Join([]string{profilesRoot, builtinProfileToFilename(profileName)}, "/")
145
by, err := fs.ReadFile(manifests.BuiltinOrDir(chartsDir), path)
149
return string(by), nil
152
func readProfiles(chartsDir string) (map[string]bool, error) {
153
profiles := map[string]bool{}
154
f := manifests.BuiltinOrDir(chartsDir)
155
dir, err := fs.ReadDir(f, profilesRoot)
159
for _, f := range dir {
160
trimmedString := strings.TrimSuffix(f.Name(), ".yaml")
161
if f.Name() != trimmedString {
162
profiles[trimmedString] = true
169
func stripPrefix(path, prefix string) string {
170
pl := len(strings.Split(prefix, "/"))
171
pv := strings.Split(path, "/")
172
return strings.Join(pv[pl:], "/")
176
func ListProfiles(charts string) ([]string, error) {
177
profiles, err := readProfiles(charts)
181
return util.StringBoolMapToSlice(profiles), nil