-
Notifications
You must be signed in to change notification settings - Fork 156
/
addon.go
181 lines (143 loc) · 4.54 KB
/
addon.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
/*
Copyright 2020 The Kubermatic Kubernetes Platform contributors.
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
http://www.apache.org/licenses/LICENSE-2.0
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.
*/
package addon
import (
"bytes"
"fmt"
"io/fs"
"os"
"path/filepath"
"strings"
"sync"
"text/template"
"github.com/Masterminds/sprig/v3"
"k8c.io/kubermatic/v2/pkg/resources/registry"
"k8c.io/kubermatic/v2/pkg/util/yaml"
"k8s.io/apimachinery/pkg/runtime"
)
// Addon is a single addon loaded from a single directory. Within this
// directory usually all .yaml files will have been loaded and concatenated
// into a single large template to render.
type Addon struct {
combinedTemplate *template.Template
lock *sync.Mutex
}
func addonFunctions(overwriteRegistry string) template.FuncMap {
funcs := sprig.TxtFuncMap()
// Registry is deprecated and should not be used anymore.
funcs["Registry"] = registry.GetOverwriteFunc(overwriteRegistry)
funcs["Image"] = registry.GetImageRewriterFunc(overwriteRegistry)
funcs["join"] = strings.Join
return funcs
}
func listAddonFilesRecursively(root string) ([]string, error) {
files := []string{}
root, err := filepath.Abs(root)
if err != nil {
return nil, err
}
err = filepath.WalkDir(root, func(path string, d fs.DirEntry, err error) error {
if err != nil {
return err
}
if d.IsDir() {
return nil
}
if !strings.HasSuffix(path, ".yaml") {
return nil
}
files = append(files, path)
return nil
})
if err != nil {
return nil, err
}
return files, nil
}
func LoadAddonsFromDirectory(directory string) (map[string]*Addon, error) {
directory, err := filepath.Abs(directory)
if err != nil {
return nil, err
}
fileInfos, err := os.ReadDir(directory)
if err != nil {
return nil, err
}
result := map[string]*Addon{}
for _, info := range fileInfos {
if !info.IsDir() {
continue
}
addonName := info.Name()
addon, err := LoadAddonFromDirectory(filepath.Join(directory, addonName))
if err != nil {
return nil, fmt.Errorf("failed to load %q addon: %w", addonName, err)
}
result[addonName] = addon
}
return result, nil
}
func LoadAddonFromDirectory(directory string) (*Addon, error) {
allAddonFiles, err := listAddonFilesRecursively(directory)
if err != nil {
return nil, err
}
// to provide more helpful error messages, we parse each addon file individually,
// even though later during runtime we deal with one big combined manifest
combined := strings.Builder{}
parser := template.New("temp").Funcs(addonFunctions("WILL_BE_INJECTED_LATER"))
for _, path := range allAddonFiles {
content, err := os.ReadFile(path)
if err != nil {
return nil, fmt.Errorf("failed to read file %s: %w", path, err)
}
if _, err := parser.Parse(string(content)); err != nil {
return nil, fmt.Errorf("failed to parse file %s: %w", path, err)
}
combined.Write(content)
combined.WriteString("\n\n---\n\n")
}
// parse the big combined manifest
parsed, err := parser.Parse(combined.String())
if err != nil {
return nil, err
}
return &Addon{
combinedTemplate: parsed,
lock: &sync.Mutex{},
}, nil
}
func (a *Addon) Render(overwriteRegistry string, data *TemplateData) ([]runtime.RawExtension, error) {
// Now that we know the overwrite registry (which can be cluster-dependent), we have
// to update the Registry/Image funcs, which rely on the registry name to be injected
// via a closure. Since both functions are part of the "addon interface" for addon
// authors, their signature cannot simply be changed to work based on the TemplateData.
// Because of this switcheroo we must ensure that this addon is not rendered in parallel
// by multiple goroutines.
a.lock.Lock()
defer a.lock.Unlock()
a.combinedTemplate.Funcs(addonFunctions(overwriteRegistry))
var buffer bytes.Buffer
if err := a.combinedTemplate.Execute(&buffer, data); err != nil {
return nil, fmt.Errorf("failed to render template: %w", err)
}
sd := strings.TrimSpace(buffer.String())
if len(sd) == 0 {
return nil, nil
}
manifests, err := yaml.ParseMultipleDocuments(&buffer)
if err != nil {
return nil, fmt.Errorf("failed to decode templated YAML: %w", err)
}
return manifests, nil
}