-
Notifications
You must be signed in to change notification settings - Fork 12
/
main.go
169 lines (147 loc) · 4.1 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
package main
import (
"bytes"
"flag"
"io"
"log"
"os"
"os/exec"
"sync"
"github.com/yanc0/untrak/outputs"
"github.com/yanc0/untrak/utils"
yaml "gopkg.in/yaml.v2"
"github.com/yanc0/untrak/kubernetes"
"github.com/yanc0/untrak/config"
)
func main() {
// Flags, command line parameters
var cfgPathOpt = flag.String("config", "./untrak.yaml", "untrak Config Path")
var outputOpt = flag.String("o", "text", "Output format")
var failOpt = flag.Bool("fail", false, "Fail on untracked resources")
flag.Parse()
var wg sync.WaitGroup
var resourcesIn []*kubernetes.Resource
var resourcesOut []*kubernetes.Resource
// Config Load
cfg, err := config.Load(*cfgPathOpt)
if err != nil {
log.Printf("[ERR] Cannot load %s file: %v\n", *cfgPathOpt, err)
os.Exit(1)
}
cfg.NonNamespaced = append(cfg.NonNamespaced, kubernetes.DefaultNonNamespacedResources...)
wg.Add(1)
go func() {
defer wg.Done()
resourcesIn, err = getKubernetesResources(cfg.In)
if err != nil {
log.Printf("[ERR] Failed to get Kubernetes resources (in): %v\n", err)
os.Exit(1)
}
}()
wg.Add(1)
go func() {
defer wg.Done()
resourcesOut, err = getKubernetesResources(cfg.Out)
if err != nil {
log.Printf("[ERR] Failed to get Kubernetes resources (out): %v\n", err)
os.Exit(1)
}
}()
wg.Wait()
untrackedResources := listUntrackedResources(resourcesIn, resourcesOut, cfg.Exclude, cfg.NonNamespaced)
switch {
case *outputOpt == "text":
outputs.Text(untrackedResources)
case *outputOpt == "yaml":
outputs.YAML(untrackedResources)
default:
outputs.Text(untrackedResources)
}
if len(untrackedResources) > 0 && *failOpt {
os.Exit(1)
}
}
func getKubernetesResources(cfgs []*config.CommandConfig) ([]*kubernetes.Resource, error) {
const yamlSeparator = "---\n"
var resources []*kubernetes.Resource
var wg sync.WaitGroup
var mutex = &sync.Mutex{}
for _, cfg := range cfgs {
wg.Add(1)
go func(cmd string, args ...string) {
defer wg.Done()
// substitute env variables if any has been set
for i, _ := range args {
args[i] = os.ExpandEnv(args[i])
}
c := exec.Command(cmd, args...)
var outb, errb bytes.Buffer
c.Stdout = &outb
c.Stderr = &errb
err := c.Run()
if err != nil {
log.Fatal(err, errb.String())
}
stdoutDec := yaml.NewDecoder(&outb)
for {
tempResource := &kubernetes.Resource{}
err := stdoutDec.Decode(tempResource)
if err != nil && err != io.EOF {
log.Printf("[ERR] Failed to decode yaml stream: %s\n", err.Error())
os.Exit(1)
}
if err == io.EOF {
break
}
if tempResource.Kind == "List" {
mutex.Lock()
resources = append(resources, tempResource.Items...)
mutex.Unlock()
continue
}
// Resource can be empty if yaml file has return lines, separators or comments
// for example:
// # empty resource
// ---
// ---
// YAML decoder consider these lines valid but resource will be uninitialized
if !tempResource.Empty() {
mutex.Lock()
resources = append(resources, tempResource)
mutex.Unlock()
}
}
}(cfg.Cmd, cfg.Args...)
}
wg.Wait()
return resources, nil
}
func listUntrackedResources(in []*kubernetes.Resource, out []*kubernetes.Resource, kindExclude []string, nonNamespaced []string) []*kubernetes.Resource {
var untrackedResources []*kubernetes.Resource
for _, resourceOut := range out {
// Resource is in the exlude list, skip it
if utils.StringInListCaseInsensitive(kindExclude, resourceOut.Kind) {
continue
}
found := false
for _, resourceIn := range in {
// If input resource is not namespaced, compare only kind and Name
if utils.StringInListCaseInsensitive(nonNamespaced, resourceIn.Kind) {
if resourceOut.Kind == resourceIn.Kind && resourceOut.Metadata.Name == resourceIn.Metadata.Name {
found = true
break
}
}
// If resource has been found in both IN an OUT, there is nothing to do
if resourceOut.ID() == resourceIn.ID() {
found = true
break
}
}
// If resource OUT is not found in IN, it is untracked
if !found {
untrackedResources = append(untrackedResources, resourceOut)
}
}
return untrackedResources
}