/
file_assets.go
96 lines (79 loc) · 2.79 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
/*
Copyright 2019 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"
"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.NodeupModelBuilder = &FileAssetsBuilder{}
// Build is responsible for writing out the file assets from cluster and instanceGroup
func (f *FileAssetsBuilder) Build(c *fi.NodeupModelBuilderContext) error {
// used to keep track of previous file, so a instanceGroup can override a cluster wide one
tracker := make(map[string]bool)
// ensure the default path exists
c.EnsureTask(&nodetasks.File{
Path: f.FileAssetsDefaultPath(),
Type: nodetasks.FileType_Directory,
Mode: s("0755"),
})
return f.buildFileAssets(c, f.NodeupConfig.FileAssets, tracker)
}
// buildFileAssets is responsible for rendering the file assets to disk
func (f *FileAssetsBuilder) buildFileAssets(c *fi.NodeupModelBuilderContext, assets []kops.FileAssetSpec, tracker map[string]bool) error {
for _, asset := range assets {
// @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)
}
// If not specified, the default Mode is 0440
if asset.Mode == "" {
asset.Mode = "0440"
}
// 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(asset.Mode),
Path: assetPath,
Type: nodetasks.FileType_File,
})
}
return nil
}