forked from Shopify/kubeaudit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fix.go
135 lines (112 loc) · 4.25 KB
/
fix.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
package kubeaudit
import (
"bytes"
"github.com/Shopify/kubeaudit/internal/k8s"
"github.com/Shopify/kubeaudit/internal/yaml"
"github.com/Shopify/kubeaudit/k8stypes"
)
func fix(results []Result) ([]byte, error) {
var outputBytes [][]byte
var newResources []k8stypes.Resource
if len(results) == 0 {
return []byte{}, nil
}
// Fix all the resources
for _, result := range results {
for _, auditResult := range result.GetAuditResults() {
newResources = append(newResources, auditResult.Fix(result.GetResource().Object())...)
}
}
// Convert all the resources to bytes
for _, result := range results {
if result.GetResource().Object() == nil {
outputBytes = append(outputBytes, result.GetResource().Bytes())
continue
}
fixedresourceBytes, err := resourceToBytes(result.GetResource().Object(), result.GetResource().Bytes())
if err != nil {
return nil, err
}
outputBytes = append(outputBytes, fixedresourceBytes)
}
// Convert all the new resources to bytes
for _, newResource := range newResources {
fixedresourceBytes, err := resourceToBytes(newResource, nil)
if err != nil {
return nil, err
}
outputBytes = append(outputBytes, fixedresourceBytes)
}
fixedManifest := bytes.Join(outputBytes, []byte("---"))
return fixedManifest, nil
}
func resourceToBytes(fixedResource k8stypes.Resource, origResourceBytes []byte) ([]byte, error) {
fixedresourceBytes, err := k8s.EncodeResource(fixedResource)
if err != nil {
return nil, err
}
if origResourceBytes == nil {
// This is a new resource (not in the original manifest)
// Add a leading newline
fixedresourceBytes = append([]byte{'\n'}, fixedresourceBytes...)
} else {
fixedresourceBytes, err = yaml.Merge(origResourceBytes, fixedresourceBytes)
if err != nil {
return nil, err
}
// Add any leading and trailing whitespace that was present in the original
fixedresourceBytes = bytes.Replace(origResourceBytes, bytes.TrimSpace(origResourceBytes), fixedresourceBytes, 1)
// Remove the redundant trailing newline
if fixedresourceBytes[len(fixedresourceBytes)-1] == '\n' {
fixedresourceBytes = fixedresourceBytes[:len(fixedresourceBytes)-1]
}
}
fixedresourceBytes, err = cleanupManifest(origResourceBytes, fixedresourceBytes)
if err != nil {
return nil, err
}
return fixedresourceBytes, nil
}
// TODO do this better??
func cleanupManifest(origData, finalData []byte) ([]byte, error) {
objectMetacreationTs := []byte("\n creationTimestamp: null\n")
specTemplatecreationTs := []byte("\n creationTimestamp: null\n")
jobSpecTemplatecreationTs := []byte("\n creationTimestamp: null\n")
nullStatus := []byte("\nstatus: {}\n")
nullReplicaStatus := []byte("status:\n replicas: 0\n")
nullLBStatus := []byte("status:\n loadBalancer: {}\n")
nullMetaStatus := []byte("\n status: {}\n")
var hasObjectMetacreationTs, hasSpecTemplatecreationTs, hasJobSpecTemplatecreationTs, hasNullStatus,
hasNullReplicaStatus, hasNullLBStatus, hasNullMetaStatus bool
if origData != nil {
hasObjectMetacreationTs = bytes.Contains(origData, objectMetacreationTs)
hasSpecTemplatecreationTs = bytes.Contains(origData, specTemplatecreationTs)
hasJobSpecTemplatecreationTs = bytes.Contains(origData, jobSpecTemplatecreationTs)
hasNullStatus = bytes.Contains(origData, nullStatus)
hasNullReplicaStatus = bytes.Contains(origData, nullReplicaStatus)
hasNullLBStatus = bytes.Contains(origData, nullLBStatus)
hasNullMetaStatus = bytes.Contains(origData, nullMetaStatus)
} // null value is false in case of origFile
if !hasObjectMetacreationTs {
finalData = bytes.Replace(finalData, objectMetacreationTs, []byte("\n"), -1)
}
if !hasSpecTemplatecreationTs {
finalData = bytes.Replace(finalData, specTemplatecreationTs, []byte("\n"), -1)
}
if !hasJobSpecTemplatecreationTs {
finalData = bytes.Replace(finalData, jobSpecTemplatecreationTs, []byte("\n"), -1)
}
if !hasNullStatus {
finalData = bytes.Replace(finalData, nullStatus, []byte("\n"), -1)
}
if !hasNullReplicaStatus {
finalData = bytes.Replace(finalData, nullReplicaStatus, []byte("\n"), -1)
}
if !hasNullLBStatus {
finalData = bytes.Replace(finalData, nullLBStatus, []byte("\n"), -1)
}
if !hasNullMetaStatus {
finalData = bytes.Replace(finalData, nullMetaStatus, []byte("\n"), -1)
}
return finalData, nil
}