-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmanifest.go
112 lines (97 loc) · 2.62 KB
/
manifest.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
// Copyright (c) 2023 Volvo Car Corporation
// SPDX-License-Identifier: Apache-2.0
package kubeutil
import (
"bufio"
"bytes"
"errors"
"fmt"
"io"
"os"
"path/filepath"
"strings"
"github.com/tidwall/gjson"
"github.com/tidwall/sjson"
kyaml "sigs.k8s.io/yaml"
)
// ManifestReadFile reads a YAML file and splits it into a list of YAML documents.
func ManifestReadFile(filePath string) ([]string, error) {
e := filepath.Ext(filePath)
if e != ".yaml" && e != ".yml" {
return nil, fmt.Errorf("not yaml file: %s", filePath)
}
yf, err := os.ReadFile(filePath)
if err != nil {
return nil, fmt.Errorf("read manifest %s: %w", filePath, err)
}
splitYaml, err := ManifestSplit(bytes.NewReader(yf))
if err != nil {
return nil, fmt.Errorf("splitting manifest: %s: %w", filePath, err)
}
return splitYaml, nil
}
// ManifestSplit splits a YAML manifest where each object is separated by '---'
// into a list of string containing YAML documents.
func ManifestSplit(r io.Reader) ([]string, error) {
scanner := bufio.NewScanner(r)
var content []string
var buf bytes.Buffer
for scanner.Scan() {
txt := scanner.Text()
tmp := strings.ReplaceAll(txt, " ", "")
switch {
// Skip empty lines, but not empty lines in the middle of a manifest.
case buf.Len() == 0 && len(tmp) == 0:
continue
// Skip top level comments
case strings.HasPrefix(txt, "#"):
continue
// Split by '---'
case strings.HasPrefix(txt, "---"):
if buf.Len() > 0 {
content = append(content, buf.String())
buf.Reset()
}
default:
buf.WriteString(txt + "\n")
}
}
s := buf.String()
if len(s) > 0 { // if a manifest ends with '---', don't add it
content = append(content, s)
}
if err := scanner.Err(); err != nil {
return nil, fmt.Errorf("spliting manifests: %w", err)
}
return content, nil
}
var unusedFields = []string{
"status",
// see https://github.com/tidwall/gjson/blob/master/SYNTAX.md
`metadata.annotations.kubectl\.kubernetes\.io\/last-applied-configuration`,
}
const metadataFields = "{metadata.name,metadata.namespace,metadata.labels,metadata.annotations}"
func CleanUpYAML(in []byte) ([]byte, error) {
j, err := kyaml.YAMLToJSON(in)
if err != nil {
return nil, err
}
return CleanUpJSON(j)
}
func CleanUpJSON(in []byte) ([]byte, error) {
out := in
var err error
newMeta := gjson.GetBytes(out, metadataFields)
out, err = sjson.SetBytes(out, "metadata", newMeta.Value())
if err != nil {
return in, fmt.Errorf("error setting new metadata : %v", err)
}
for _, field := range unusedFields {
var errD error
out, errD = sjson.DeleteBytes(out, field)
if errD != nil {
err = errors.Join(err, errD)
}
}
return out, err
}