-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
189 lines (158 loc) · 4.21 KB
/
main.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
package main
import (
"bytes"
"flag"
"fmt"
"os"
"os/exec"
"strconv"
"strings"
)
type RealKubectl struct{}
type StdoutWriter struct{}
func (sw StdoutWriter) Write(output string) {
fmt.Println(output)
}
func (r RealKubectl) getRolloutHistory(resourceType, resourceName, namespace string) (string, error) {
args := []string{"rollout", "history", fmt.Sprintf("%s/%s", resourceType, resourceName)}
if namespace != "" {
args = append(args, "-n", namespace)
}
return r.runKubectlCommand(args...)
}
func (r RealKubectl) getRolloutHistoryWithRevision(resourceType, resourceName string, revision int, namespace string) (string, error) {
args := []string{"rollout", "history", fmt.Sprintf("%s/%s", resourceType, resourceName), fmt.Sprintf("--revision=%d", revision)}
if namespace != "" {
args = append(args, "-n", namespace)
}
return r.runKubectlCommand(args...)
}
func (r RealKubectl) runKubectlCommand(args ...string) (string, error) {
cmd := exec.Command("kubectl", args...)
var out bytes.Buffer
cmd.Stdout = &out
cmd.Stderr = os.Stderr
err := cmd.Run()
if err != nil {
return "", err
}
return out.String(), nil
}
func getDiff(file1, file2 string) (string, error) {
cmd := exec.Command("diff", "-u", file1, file2)
var out bytes.Buffer
cmd.Stdout = &out
cmd.Stderr = os.Stderr
_ = cmd.Run()
return out.String(), nil
}
func writeTempFile(data string) (string, error) {
tempFile, err := os.CreateTemp("", "revision")
if err != nil {
return "", err
}
err = os.WriteFile(tempFile.Name(), []byte(data), 0644)
if err != nil {
return "", err
}
return tempFile.Name(), nil
}
func usage() string {
return "Usage: kubecompare [ <resource-type> <resource-name> | <resource-type>/<resource-name> ] [ <previous-revision> <next-revision> ]"
}
func mainLogic(k KubectlInterface, writer OutputWriter, namespace string, args []string) (int, error) {
if len(args) == 0 {
writer.Write(usage())
return 0, nil
}
var resourceType, resourceName, previousRevisionArg, nextRevisionArg string
if strings.Contains(args[0], "/") {
parts := strings.SplitN(args[0], "/", 2)
resourceType, resourceName = parts[0], parts[1]
args = args[1:]
} else {
if len(args) < 2 {
return 2, fmt.Errorf("invalid number of arguments")
}
resourceType, resourceName = args[0], args[1]
args = args[2:]
}
if len(args) == 0 {
history, err := k.getRolloutHistory(resourceType, resourceName, namespace)
if err != nil {
return 1, err
}
writer.Write(history)
return 0, nil
} else if len(args) == 2 {
previousRevisionArg, nextRevisionArg = args[0], args[1]
} else {
return 2, fmt.Errorf("invalid number of arguments")
}
previousRevision, err := strconv.Atoi(previousRevisionArg)
if err != nil {
return 2, err
}
nextRevision, err := strconv.Atoi(nextRevisionArg)
if err != nil {
return 2, err
}
previousData, err := k.getRolloutHistoryWithRevision(resourceType, resourceName, previousRevision, namespace)
if err != nil {
return 1, err
}
nextData, err := k.getRolloutHistoryWithRevision(resourceType, resourceName, nextRevision, namespace)
if err != nil {
return 1, err
}
previousFile, err := writeTempFile(previousData)
if err != nil {
return 1, err
}
defer os.Remove(previousFile)
nextFile, err := writeTempFile(nextData)
if err != nil {
return 1, err
}
defer os.Remove(nextFile)
diff, err := getDiff(previousFile, nextFile)
if err != nil {
return 1, err
}
writer.Write(diff)
return 0, nil
}
func parseFlags() (bool, string, []string) {
helpFlag := flag.Bool("h", false, "Show usage information")
var namespace string
flag.StringVar(&namespace, "n", "", "Specify namespace")
flag.StringVar(&namespace, "namespace", "", "Specify namespace")
flag.Parse()
args := flag.Args()
for _, arg := range []string{"-n", "--namespace"} {
for i, a := range args {
if a == arg {
args = append(args[:i], args[i+2:]...)
break
}
}
}
return *helpFlag, namespace, args
}
func main() {
helpFlag, namespace, args := parseFlags()
if helpFlag {
fmt.Println(usage())
os.Exit(0)
}
kubectl := RealKubectl{}
writer := StdoutWriter{}
status, err := mainLogic(kubectl, writer, namespace, args)
if err != nil {
fmt.Println("Error:", err)
if status == 2 {
fmt.Println(usage())
}
}
os.Exit(status)
}