@@ -17,31 +17,25 @@ limitations under the License.
1717package templates
1818
1919import (
20- "bytes"
2120 "context"
2221 "fmt"
23- "io"
2422 "os"
2523 "strings"
26- "text/template"
2724
2825 "k8s.io/klog/v2"
29- "k8s.io/kops/pkg/apis/kops"
3026 "k8s.io/kops/upup/pkg/fi"
3127 "k8s.io/kops/util/pkg/vfs"
3228)
3329
3430type Templates struct {
35- cluster * kops.Cluster
36- resources map [string ]fi.Resource
37- TemplateFunctions template.FuncMap
31+ resources map [string ]fi.Resource
32+ isTemplate map [string ]bool
3833}
3934
40- func LoadTemplates (ctx context.Context , cluster * kops. Cluster , base vfs.Path ) (* Templates , error ) {
35+ func LoadTemplates (ctx context.Context , base vfs.Path ) (* Templates , error ) {
4136 t := & Templates {
42- cluster : cluster ,
43- resources : make (map [string ]fi.Resource ),
44- TemplateFunctions : make (template.FuncMap ),
37+ resources : make (map [string ]fi.Resource ),
38+ isTemplate : make (map [string ]bool ),
4539 }
4640 err := t .loadFrom (ctx , base )
4741 if err != nil {
@@ -54,6 +48,11 @@ func (t *Templates) Find(key string) fi.Resource {
5448 return t .resources [key ]
5549}
5650
51+ // IsTemplate reports whether key was loaded from a file ending in .template.
52+ func (t * Templates ) IsTemplate (key string ) bool {
53+ return t .isTemplate [key ]
54+ }
55+
5756func (t * Templates ) loadFrom (ctx context.Context , base vfs.Path ) error {
5857 files , err := base .ReadTree (ctx )
5958 if err != nil {
@@ -75,74 +74,11 @@ func (t *Templates) loadFrom(ctx context.Context, base vfs.Path) error {
7574 return fmt .Errorf ("error getting relative path for %s" , f )
7675 }
7776
78- var resource fi.Resource
79- if strings .HasSuffix (key , ".template" ) {
80- key = strings .TrimSuffix (key , ".template" )
81- klog .V (6 ).Infof ("loading (templated) resource %q" , key )
82-
83- resource = & templateResource {
84- template : string (contents ),
85- loader : t ,
86- key : key ,
87- }
88- } else {
89- klog .V (6 ).Infof ("loading resource %q" , key )
90- resource = fi .NewBytesResource (contents )
91-
92- }
93-
94- t .resources [key ] = resource
77+ isTemplate := strings .HasSuffix (key , ".template" )
78+ key = strings .TrimSuffix (key , ".template" )
79+ klog .V (6 ).Infof ("loading resource %q" , key )
80+ t .resources [key ] = fi .NewBytesResource (contents )
81+ t .isTemplate [key ] = isTemplate
9582 }
9683 return nil
9784}
98-
99- func (l * Templates ) executeTemplate (key string , d string ) (string , error ) {
100- t := template .New (key )
101-
102- funcMap := make (template.FuncMap )
103- // funcMap["Args"] = func() []string {
104- // return args
105- //}
106- //funcMap["RenderResource"] = func(resourceName string, args []string) (string, error) {
107- // return l.renderResource(resourceName, args)
108- //}
109- for k , fn := range l .TemplateFunctions {
110- funcMap [k ] = fn
111- }
112- t .Funcs (funcMap )
113-
114- t .Option ("missingkey=zero" )
115-
116- spec := l .cluster .Spec
117-
118- _ , err := t .Parse (d )
119- if err != nil {
120- return "" , fmt .Errorf ("error parsing template %q: %v" , key , err )
121- }
122-
123- var buffer bytes.Buffer
124- err = t .ExecuteTemplate (& buffer , key , spec )
125- if err != nil {
126- return "" , fmt .Errorf ("error executing template %q: %v" , key , err )
127- }
128-
129- return buffer .String (), nil
130- }
131-
132- type templateResource struct {
133- key string
134- loader * Templates
135- template string
136- }
137-
138- var _ fi.Resource = & templateResource {}
139-
140- func (a * templateResource ) Open () (io.Reader , error ) {
141- var err error
142- result , err := a .loader .executeTemplate (a .key , a .template )
143- if err != nil {
144- return nil , fmt .Errorf ("error executing resource template %q: %v" , a .key , err )
145- }
146- reader := bytes .NewReader ([]byte (result ))
147- return reader , nil
148- }
0 commit comments