-
Notifications
You must be signed in to change notification settings - Fork 9
/
change.go
98 lines (88 loc) · 2.7 KB
/
change.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
package openshift
import (
"github.com/pmezard/go-difflib/difflib"
)
var (
kindToShortMapping = map[string]string{
"Service": "svc",
"Route": "route",
"DeploymentConfig": "dc",
"Deployment": "deployment",
"BuildConfig": "bc",
"ImageStream": "is",
"PersistentVolumeClaim": "pvc",
"Template": "template",
"ConfigMap": "cm",
"Secret": "secret",
"RoleBinding": "rolebinding",
"ServiceAccount": "serviceaccount",
"CronJob": "cronjob",
"Job": "job",
"LimitRange": "limitrange",
"ResourceQuota": "quota",
"HorizontalPodAutoscaler": "hpa",
}
)
// Change is a description of a drift between current and desired state, and
// the required patches to bring them back in sync.
type Change struct {
Action string
Kind string
Name string
CurrentState string
DesiredState string
}
// NewChange creates a new change for given template/platform item.
func NewChange(templateItem *ResourceItem, platformItem *ResourceItem) *Change {
c := &Change{
Kind: templateItem.Kind,
Name: templateItem.Name,
CurrentState: platformItem.YamlConfig(),
DesiredState: templateItem.YamlConfig(),
}
if platformItem.YamlConfig() != templateItem.YamlConfig() {
c.Action = "Update"
} else {
c.Action = "Noop"
}
return c
}
// ItemName returns the kind/name of the resource the change relates to.
func (c *Change) ItemName() string {
return kindToShortMapping[c.Kind] + "/" + c.Name
}
// Diff returns a unified diff text for the change.
func (c *Change) Diff(revealSecrets bool) string {
if c.isSecret() && !revealSecrets {
return "Secret drift is hidden. Use --reveal-secrets to see details.\n"
}
diff := difflib.UnifiedDiff{
A: difflib.SplitLines(c.CurrentState),
B: difflib.SplitLines(c.DesiredState),
FromFile: "Current State (OpenShift cluster)",
ToFile: "Desired State (Processed template)",
Context: 3,
}
text, _ := difflib.GetUnifiedDiffString(diff)
return text
}
func (c *Change) isSecret() bool {
return kindToShortMapping[c.Kind] == "secret"
}
func recreateChanges(templateItem, platformItem *ResourceItem) []*Change {
deleteChange := &Change{
Action: "Delete",
Kind: templateItem.Kind,
Name: templateItem.Name,
CurrentState: platformItem.YamlConfig(),
DesiredState: "",
}
createChange := &Change{
Action: "Create",
Kind: templateItem.Kind,
Name: templateItem.Name,
CurrentState: "",
DesiredState: templateItem.YamlConfig(),
}
return []*Change{deleteChange, createChange}
}