/
main.go
186 lines (170 loc) · 5.43 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
// badref - Find invalid ownerReferences in a Kubernetes cluster
// Copyright (C) 2020 John D. Strunk
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published
// by the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
package main
import (
"context"
"flag"
"fmt"
"os"
"strings"
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/apimachinery/pkg/types"
"k8s.io/client-go/kubernetes"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/log/zap"
)
type ObjectDescription struct {
APIVersion string
Kind string
Name string
Namespace string
types.UID
OwnerReferences []v1.OwnerReference
IsNamespaced bool
}
func (d ObjectDescription) KindNamespaceName() string {
if d.IsNamespaced {
return fmt.Sprintf("%v %v/%v", d.Namespace, d.Kind, d.Name)
}
return fmt.Sprintf("%v/%v", d.Kind, d.Name)
}
func newObjectDescription(uo unstructured.Unstructured, namespaced bool) ObjectDescription {
return ObjectDescription{
APIVersion: uo.GetAPIVersion(),
Kind: uo.GetKind(),
Name: uo.GetName(),
Namespace: uo.GetNamespace(),
UID: uo.GetUID(),
OwnerReferences: uo.GetOwnerReferences(),
IsNamespaced: namespaced,
}
}
type ObjectCatalog map[types.UID]ObjectDescription
func main() {
flag.Parse()
ctrl.SetLogger(zap.New(zap.UseDevMode(true)))
config, err := ctrl.GetConfig()
if err != nil {
panic(err.Error())
}
clientset, err := kubernetes.NewForConfig(config)
if err != nil {
panic(err.Error())
}
cl, err := client.New(config, client.Options{})
if err != nil {
panic(err.Error())
}
// Load all the Kubernetes objects
oc := ObjectCatalog{}
numResources := 0
resources, err := clientset.ServerPreferredResources()
if err != nil {
panic(err.Error())
}
for _, resourceList := range resources {
// fmt.Printf("Loading %v... ", resourceList.GroupVersion)
gv, err := schema.ParseGroupVersion(resourceList.GroupVersion)
if err != nil {
panic(err.Error())
}
for _, r := range resourceList.APIResources {
hasList := false
for _, v := range r.Verbs {
if v == "list" {
hasList = true
}
}
if hasList {
ul := &unstructured.UnstructuredList{}
ul.SetGroupVersionKind(schema.GroupVersionKind{
Group: gv.Group,
Version: gv.Version,
Kind: r.Kind,
})
err = cl.List(context.TODO(), ul)
if err != nil {
fmt.Printf("Error during list of %v: %v\n", ul.GroupVersionKind(), err)
}
for _, uo := range ul.Items {
numResources++
oc[uo.GetUID()] = newObjectDescription(uo, r.Namespaced)
}
}
}
}
fmt.Printf("Discovered %v resources\n", numResources)
// Validate owner references
foundErrors := false
checkedObj := 0
checkedOwners := 0
for _, obj := range oc {
checkedObj++
// fmt.Printf("Checking: %v\n", obj.KindNamespaceName())
hasController := false
for _, ref := range obj.OwnerReferences {
owner, found := oc[ref.UID]
if !found {
// The owner doesn't exist, so nothing to check
fmt.Printf("Warning: Couldn't find owner for %v/%v\n", obj.Namespace, obj.Name)
continue
}
checkedOwners++
if ref.Controller != nil && *ref.Controller {
if !hasController {
hasController = true
} else {
foundErrors = true
fmt.Printf("ERROR: Object %v has more than 1 controller\n", obj.KindNamespaceName())
}
}
// Check the rules
if !obj.IsNamespaced && owner.IsNamespaced {
foundErrors = true
fmt.Printf("ERROR: Non-namespaced %v is owned by namespaced %v\n",
obj.KindNamespaceName(), owner.KindNamespaceName())
}
if obj.IsNamespaced && owner.IsNamespaced && obj.Namespace != owner.Namespace {
foundErrors = true
fmt.Printf("ERROR: namespaced %v is owned by object in another namespace %v\n",
obj.KindNamespaceName(), owner.KindNamespaceName())
}
if !strings.EqualFold(ref.Kind, owner.Kind) {
foundErrors = true
fmt.Printf("Warning: In object %v, owner ref kind (%v) does not match owner %v (%v).\n",
obj.KindNamespaceName(), ref.Kind, owner.KindNamespaceName(), owner.Kind)
}
if !strings.EqualFold(ref.Name, owner.Name) {
foundErrors = true
fmt.Printf("Warning: In object %v, owner ref name (%v) does not match owner %v (%v).\n",
obj.KindNamespaceName(), ref.Name, owner.KindNamespaceName(), owner.Name)
}
if !strings.EqualFold(ref.APIVersion, owner.APIVersion) {
foundErrors = true
fmt.Printf("Warning: In object %v, owner ref APIVersion (%v) does not match owner %v (%v).\n",
obj.KindNamespaceName(), ref.APIVersion, owner.KindNamespaceName(), owner.APIVersion)
}
}
}
fmt.Printf("Scanned %v objects\n", checkedObj)
fmt.Printf("Checked %v owner references\n", checkedOwners)
if foundErrors {
fmt.Printf("=== ERRORS FOUND ===\n")
os.Exit(1)
} else {
fmt.Printf("All OK!\n")
}
}