forked from kubernetes/kops
/
file_assets.go
114 lines (96 loc) · 3.26 KB
/
file_assets.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
/*
Copyright 2016 The Kubernetes Authors.
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 model
import (
"encoding/base64"
"fmt"
"path/filepath"
"strings"
"text/template"
"k8s.io/kops/pkg/apis/kops"
"k8s.io/kops/upup/pkg/fi"
"k8s.io/kops/upup/pkg/fi/nodeup/nodetasks"
)
// FileAssetsBuilder configures the hooks
type FileAssetsBuilder struct {
*NodeupModelContext
}
var _ fi.ModelBuilder = &FileAssetsBuilder{}
var templateFuncs = template.FuncMap{
"split": strings.Split,
"join": strings.Join,
}
// Build is responsible for writing out the file assets from cluster and instanceGroup
func (f *FileAssetsBuilder) Build(c *fi.ModelBuilderContext) error {
// used to keep track of previous file, so a instanceGroup can override a cluster wide one
tracker := make(map[string]bool, 0)
// ensure the default path exists
c.EnsureTask(&nodetasks.File{
Path: f.FileAssetsDefaultPath(),
Type: nodetasks.FileType_Directory,
Mode: s("0755"),
})
// do we have any instanceGroup file assets
if f.InstanceGroup.Spec.FileAssets != nil {
if err := f.buildFileAssets(c, f.InstanceGroup.Spec.FileAssets, tracker); err != nil {
return err
}
}
if f.Cluster.Spec.FileAssets != nil {
if err := f.buildFileAssets(c, f.Cluster.Spec.FileAssets, tracker); err != nil {
return err
}
}
return nil
}
// buildFileAssets is responsible for rendering the file assets to disk
func (f *FileAssetsBuilder) buildFileAssets(c *fi.ModelBuilderContext, assets []kops.FileAssetSpec, tracker map[string]bool) error {
for _, asset := range assets {
// @check if the file asset applys to us. If no roles applied we assume its applied to all roles
if len(asset.Roles) > 0 && !containsRole(f.InstanceGroup.Spec.Role, asset.Roles) {
continue
}
// @check if e have a path and if not use the default path
assetPath := asset.Path
if assetPath == "" {
assetPath = filepath.Join(f.FileAssetsDefaultPath(), asset.Name)
}
// @check if the file has already been done and skip
if _, found := tracker[assetPath]; found {
continue
}
tracker[assetPath] = true // update the tracker
// @check is the contents requires decoding
content := asset.Content
if asset.IsBase64 {
decoded, err := base64.RawStdEncoding.DecodeString(content)
if err != nil {
return fmt.Errorf("Failed on file asset: %s is invalid, unable to decode base64, error: %q", asset.Name, err)
}
content = string(decoded)
}
// We use EnsureTask so that we don't have to check if the asset directories have already been done
c.EnsureTask(&nodetasks.File{
Path: filepath.Dir(assetPath),
Type: nodetasks.FileType_Directory,
Mode: s("0755"),
})
c.AddTask(&nodetasks.File{
Contents: fi.NewStringResource(content),
Mode: s("0440"),
Path: assetPath,
Type: nodetasks.FileType_File,
})
}
return nil
}