This repository has been archived by the owner on Apr 8, 2021. It is now read-only.
generated from giantswarm/template-operator
-
Notifications
You must be signed in to change notification settings - Fork 1
/
key.go
98 lines (78 loc) · 2.21 KB
/
key.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
package key
import (
"bytes"
"encoding/base64"
"fmt"
"io"
"os"
"path/filepath"
"text/template"
"github.com/Masterminds/sprig"
"github.com/giantswarm/apiextensions/pkg/apis/core/v1alpha1"
"github.com/giantswarm/microerror"
"github.com/shurcooL/httpfs/vfsutil"
"github.com/giantswarm/ignition-operator/pkg/asset"
"github.com/giantswarm/ignition-operator/pkg/label"
)
const (
FilePath = "/files"
UnitPath = "/units"
MasterTemplatePath = "/ignition/master_template.yaml"
WorkerTemplatePath = "/ignition/worker_template.yaml"
)
func OperatorVersion(getter LabelsGetter) string {
return getter.GetLabels()[label.OperatorVersion]
}
func Render(values interface{}, filesdir string, b64 bool) (map[string]string, error) {
files := make(map[string]string)
walkFunction := func(path string, f os.FileInfo, rs io.ReadSeeker, err error) error {
if err != nil {
return microerror.Mask(err)
}
if !f.Mode().IsRegular() {
return nil
}
file, err := vfsutil.ReadFile(asset.Assets, path)
if err != nil {
return microerror.Mask(err)
}
tmpl, err := template.New(path).Funcs(sprig.TxtFuncMap()).Parse(string(file))
if err != nil {
return microerror.Mask(err)
}
var rendered bytes.Buffer
err = tmpl.Execute(&rendered, values)
if err != nil {
return microerror.Mask(err)
}
relativePath, err := filepath.Rel(filesdir, path)
if err != nil {
return microerror.Mask(err)
}
if b64 {
files[relativePath] = base64.StdEncoding.EncodeToString(rendered.Bytes())
} else {
files[relativePath] = rendered.String()
}
return nil
}
err := vfsutil.WalkFiles(asset.Assets, filesdir, walkFunction)
if err != nil {
return nil, microerror.Mask(err)
}
return files, nil
}
func StatusSecretName(clusterID string, ignitionName string) string {
return fmt.Sprintf("ignition-%s-%s", clusterID, ignitionName)
}
func ToIgnition(v interface{}) (v1alpha1.Ignition, error) {
if v == nil {
return v1alpha1.Ignition{}, microerror.Maskf(wrongTypeError, "expected '%T', got '%T'", &v1alpha1.Ignition{}, v)
}
p, ok := v.(*v1alpha1.Ignition)
if !ok {
return v1alpha1.Ignition{}, microerror.Maskf(wrongTypeError, "expected '%T', got '%T'", &v1alpha1.Ignition{}, v)
}
c := p.DeepCopy()
return *c, nil
}