This repository has been archived by the owner on Jun 13, 2023. It is now read-only.
/
util.go
206 lines (177 loc) · 5.67 KB
/
util.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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
package internal
import (
"bufio"
"bytes"
"encoding/json"
"errors"
"fmt"
"hash/fnv"
"io"
"io/fs"
"os"
"path"
"path/filepath"
"strings"
opLabels "github.com/kyma-project/module-manager/pkg/labels"
"github.com/kyma-project/module-manager/pkg/types"
v1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/labels"
yamlUtil "k8s.io/apimachinery/pkg/util/yaml"
"sigs.k8s.io/controller-runtime/pkg/cache"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/yaml"
)
const (
YamlDecodeBufferSize = 2048
OthersReadExecuteFilePermission = 0o755
DebugLogLevel = 2
TraceLogLevel = 3
configFileName = "installConfig.yaml"
configsFolder = "configs"
)
func CleanFilePathJoin(root, destDir string) (string, error) {
// On Windows, this is a drive separator. On UNIX-like, this is the path list separator.
// In neither case do we want to trust a TAR that contains these.
if strings.Contains(destDir, ":") {
return "", errors.New("path contains ':', which is illegal")
}
// The Go tar library does not convert separators for us.
// We assume here, as we do elsewhere, that `\\` means a Windows path.
destDir = strings.ReplaceAll(destDir, "\\", "/")
// We want to alert the user that something bad was attempted. Cleaning it
// is not a good practice.
for _, part := range strings.Split(destDir, "/") {
if part == ".." {
return "", errors.New("path contains '..', which is illegal")
}
}
// If a path is absolute, the creator of the TAR is doing something shady.
if path.IsAbs(destDir) {
return "", errors.New("path is absolute, which is illegal")
}
newPath := filepath.Join(root, filepath.Clean(destDir))
return filepath.ToSlash(newPath), nil
}
func ParseManifestStringToObjects(manifest string) (*types.ManifestResources, error) {
objects := &types.ManifestResources{}
reader := yamlUtil.NewYAMLReader(bufio.NewReader(strings.NewReader(manifest)))
for {
rawBytes, err := reader.Read()
if err != nil {
if errors.Is(err, io.EOF) {
return objects, nil
}
return nil, fmt.Errorf("invalid YAML doc: %w", err)
}
rawBytes = bytes.TrimSpace(rawBytes)
unstructuredObj := unstructured.Unstructured{}
if err := yaml.Unmarshal(rawBytes, &unstructuredObj); err != nil {
objects.Blobs = append(objects.Blobs, append(bytes.TrimPrefix(rawBytes, []byte("---\n")), '\n'))
}
if len(rawBytes) == 0 || bytes.Equal(rawBytes, []byte("null")) || len(unstructuredObj.Object) == 0 {
continue
}
objects.Items = append(objects.Items, &unstructuredObj)
}
}
func GetFsChartPath(imageSpec types.ImageSpec) string {
return filepath.Join(os.TempDir(), fmt.Sprintf("%s-%s", imageSpec.Name, imageSpec.Ref))
}
func GetConfigFilePath(config types.ImageSpec) string {
return filepath.Join(os.TempDir(), configsFolder, config.Ref, configFileName)
}
func GetYamlFileContent(filePath string) (interface{}, error) {
var fileContent interface{}
file, err := os.Open(filePath)
if err != nil {
return nil, err
}
if file != nil {
if err = yamlUtil.NewYAMLOrJSONDecoder(file, YamlDecodeBufferSize).Decode(&fileContent); err != nil {
return nil, fmt.Errorf("reading content from file path %s: %w", filePath, err)
}
err = file.Close()
}
return fileContent, err
}
func WriteToFile(filePath string, bytes []byte) error {
// create directory
if err := os.MkdirAll(filepath.Dir(filePath), fs.ModePerm); err != nil {
return err
}
// create file
file, err := os.Create(filePath)
if err != nil {
return fmt.Errorf("file creation at path %s caused an error: %w", filePath, err)
}
// write to file
if _, err = file.Write(bytes); err != nil {
return fmt.Errorf("writing file to path %s caused an error: %w", filePath, err)
}
return file.Close()
}
func GetResourceLabel(resource client.Object, labelName string) (string, error) {
labels := resource.GetLabels()
label, ok := labels[labelName]
if !ok {
return "", &types.LabelNotFoundError{
Resource: resource,
LabelName: label,
}
}
return label, nil
}
func GetStringifiedYamlFromFilePath(filePath string) (string, error) {
file, err := os.ReadFile(filePath)
if err != nil {
return "", err
}
return string(file), err
}
// CalculateHash returns hash for interfaceToBeHashed.
func CalculateHash(interfaceToBeHashed any) (uint32, error) {
data, err := json.Marshal(interfaceToBeHashed)
if err != nil {
return 0, err
}
h := fnv.New32a()
h.Write(data)
return h.Sum32(), nil
}
func GetCacheFunc() cache.NewCacheFunc {
return cache.BuilderWithOptions(
cache.Options{
SelectorsByObject: cache.SelectorsByObject{
&v1.Secret{}: {
Label: labels.SelectorFromSet(
labels.Set{opLabels.ManagedBy: opLabels.LifecycleManager},
),
},
},
},
)
}
// JoinYAMLDocuments joins provided documents by replacing any leading/trailing markers and whitespaces
// with a single YAML marker between any two documents.
func JoinYAMLDocuments(yamlDocs [][]byte) string {
if len(yamlDocs) == 0 {
return ""
}
var res bytes.Buffer
for i, ydoc := range yamlDocs {
if len(ydoc) == 0 {
continue
}
if i > 0 {
//Previous document ends with a newline!
res.Write([]byte("---\n"))
}
trimmed := bytes.TrimSpace(ydoc) // get rid of all the surrounding whitespaces
trimmed = bytes.TrimPrefix(trimmed, []byte("---\n")) // get rid of the leading marker, if any
trimmed = bytes.TrimSuffix(trimmed, []byte("---")) // get rid of the trailing marker, if any
trimmed = bytes.TrimSpace(trimmed) // get rid of any remaining surrounding whitespaces
res.Write(append(trimmed, []byte("\n")...)) // ensure single newline at the end
}
return res.String()
}