-
Notifications
You must be signed in to change notification settings - Fork 90
/
gather_namespaces_with_overlapping_uids.go
187 lines (169 loc) · 4.86 KB
/
gather_namespaces_with_overlapping_uids.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
package clusterconfig
import (
"context"
"encoding/json"
"fmt"
"strconv"
"strings"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"
corev1client "k8s.io/client-go/kubernetes/typed/core/v1"
"github.com/openshift/insights-operator/pkg/record"
)
type uidRange struct {
starting int64
length int64
}
type namespaceWithRange struct {
name string
uidRange
}
// GatherNamespacesWithOverlappingUIDs Collects namespaces with overlapping UID ranges.
//
// ### API Reference
// - https://github.com/kubernetes/client-go/blob/master/kubernetes/typed/core/v1/namespace.go
// - Response is an array of arrays of namespaces with overlapping UIDs. Each namespace is represented by its name
// and the UID range value from the `openshift.io/sa.scc.uid-range` annotation
//
// ### Sample data
// - docs/insights-archive-sample/config/namespaces_with_overlapping_uids.json
//
// ### Location in archive
// - `config/namespaces_with_overlapping_uids.json`
//
// ### Config ID
// `clusterconfig/overlapping_namespace_uids`
//
// ### Released version
// - 4.11.0
//
// ### Backported versions
// - 4.8.41+
// - 4.9.31+
// - 4.10.12+
//
// ### Changes
// None
func (g *Gatherer) GatherNamespacesWithOverlappingUIDs(ctx context.Context) ([]record.Record, []error) {
gatherKubeClient, err := kubernetes.NewForConfig(g.gatherProtoKubeConfig)
if err != nil {
return nil, []error{err}
}
return gatherNamespacesWithOverlappingUIDs(ctx, gatherKubeClient.CoreV1())
}
func gatherNamespacesWithOverlappingUIDs(ctx context.Context, coreClient corev1client.CoreV1Interface) ([]record.Record, []error) {
nsList, err := coreClient.Namespaces().List(ctx, metav1.ListOptions{})
if err != nil {
return nil, []error{err}
}
var namespaces []namespaceWithRange
var errs []error
for i := range nsList.Items {
ns := nsList.Items[i]
uidRangeString := ns.Annotations["openshift.io/sa.scc.uid-range"]
r, err := uidStringToRange(uidRangeString)
if err != nil {
errs = append(errs, fmt.Errorf("can't read uid range of the %s namespace", ns.Name))
continue
}
namespaces = append(namespaces, namespaceWithRange{ns.Name, *r})
}
var resultSet SetOfNamespaceSets
for i := range namespaces {
n1 := namespaces[i]
// remove first i+1 elements from the slice so that we don't iterate over them again
remainingNs := namespaces[i+1:]
for j := range remainingNs {
n2 := remainingNs[j]
if n1.IsOverlappingWith(n2.uidRange) {
if es, ok := resultSet.BothOverlap(n1, n2); ok {
es.Insert(n1, n2)
} else {
s := NewSet()
s.Insert(n1, n2)
resultSet = append(resultSet, s)
}
}
}
}
return []record.Record{{
Name: "config/namespaces_with_overlapping_uids",
Item: resultSet,
}}, errs
}
// uidStringToRange converts string UID range to `UIDRange` type
func uidStringToRange(s string) (*uidRange, error) {
values := strings.Split(s, "/")
starting, err := strconv.Atoi(values[0])
if err != nil {
return nil, err
}
rge, err := strconv.Atoi(values[1])
if err != nil {
return nil, err
}
return &uidRange{
starting: int64(starting),
length: int64(rge),
}, nil
}
type NamespaceSet map[namespaceWithRange]struct{}
// NewSet creates a set of namesapces from a list of values.
func NewSet(namespaces ...namespaceWithRange) NamespaceSet {
ns := NamespaceSet{}
ns.Insert(namespaces...)
return ns
}
// Insert adds namespaces to the set.
func (ns NamespaceSet) Insert(namespaces ...namespaceWithRange) NamespaceSet {
for _, n := range namespaces {
ns[n] = struct{}{}
}
return ns
}
// BothOverlap checks if the namespaces n1 and n2 are overlapping
// with all ranges in the set
func (ns NamespaceSet) BothOverlap(n1, n2 namespaceWithRange) bool {
if len(ns) == 0 {
return false
}
for k := range ns {
if !n1.IsOverlappingWith(k.uidRange) || !n2.IsOverlappingWith(k.uidRange) {
return false
}
}
return true
}
type SetOfNamespaceSets []NamespaceSet
// BothOverlap tries to find a NamespaceSet where all the members overlap with n1 and n2
func (ss SetOfNamespaceSets) BothOverlap(n1, n2 namespaceWithRange) (NamespaceSet, bool) {
for _, set := range ss {
if set.BothOverlap(n1, n2) {
return set, true
}
}
return nil, false
}
func (ss SetOfNamespaceSets) Marshal() ([]byte, error) {
result := make([][]string, 0, len(ss))
for _, set := range ss {
var overlapping []string
for s := range set {
overlapping = append(overlapping, s.name)
}
result = append(result, overlapping)
}
return json.Marshal(result)
}
func (ss SetOfNamespaceSets) GetExtension() string {
return "json"
}
// IsOverlappingWith checks if the UIDRange is overlapping with the provided one
func (u uidRange) IsOverlappingWith(r uidRange) bool {
uSum := u.starting + u.length
rSum := r.starting + r.length
return (uSum > r.starting && uSum <= rSum) || (rSum > u.starting && rSum <= uSum)
}
func (u uidRange) String() string {
return fmt.Sprintf("%d/%d", u.starting, u.length)
}