Skip to content

Commit 332157d

Browse files
committed
Implement object key utilities
1 parent 3a67a51 commit 332157d

File tree

2 files changed

+179
-0
lines changed

2 files changed

+179
-0
lines changed

clientutils/objectkey.go

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
// Copyright 2022 OnMetal authors
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package clientutils
16+
17+
import "sigs.k8s.io/controller-runtime/pkg/client"
18+
19+
// ObjectKeySet set is a set of client.ObjectKey.
20+
type ObjectKeySet map[client.ObjectKey]struct{}
21+
22+
// Insert inserts the given items into the ObjectKeySet.
23+
// The ObjectKeySet has to be non-nil for this operation.
24+
func (s ObjectKeySet) Insert(items ...client.ObjectKey) {
25+
for _, item := range items {
26+
s[item] = struct{}{}
27+
}
28+
}
29+
30+
// Has checks if the given item is in the set.
31+
func (s ObjectKeySet) Has(item client.ObjectKey) bool {
32+
_, ok := s[item]
33+
return ok
34+
}
35+
36+
// Delete removes the given items from the ObjectKeySet.
37+
// The ObjectKeySet has to be non-nil for this operation.
38+
func (s ObjectKeySet) Delete(items ...client.ObjectKey) {
39+
for _, item := range items {
40+
delete(s, item)
41+
}
42+
}
43+
44+
// Len returns the length of the ObjectKeySet.
45+
func (s ObjectKeySet) Len() int {
46+
return len(s)
47+
}
48+
49+
// NewObjectKeySet creates a new ObjectKeySet and initializes it with the given items.
50+
func NewObjectKeySet(items ...client.ObjectKey) ObjectKeySet {
51+
s := make(ObjectKeySet)
52+
s.Insert(items...)
53+
return s
54+
}

clientutils/objectkey_test.go

Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
// Copyright 2022 OnMetal authors
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package clientutils_test
16+
17+
import (
18+
. "github.com/onmetal/controller-utils/clientutils"
19+
. "github.com/onsi/ginkgo"
20+
. "github.com/onsi/gomega"
21+
"sigs.k8s.io/controller-runtime/pkg/client"
22+
)
23+
24+
var _ = Describe("ObjectKey", func() {
25+
Context("ObjectKeySet", func() {
26+
var (
27+
k1, k2, k3, k4, k5, k6 client.ObjectKey
28+
)
29+
BeforeEach(func() {
30+
k1 = client.ObjectKey{
31+
Namespace: "n1",
32+
Name: "foo",
33+
}
34+
k2 = client.ObjectKey{
35+
Namespace: "n1",
36+
Name: "bar",
37+
}
38+
k3 = client.ObjectKey{
39+
Namespace: "n2",
40+
Name: "foo",
41+
}
42+
k4 = client.ObjectKey{
43+
Namespace: "n2",
44+
Name: "bar",
45+
}
46+
k5 = client.ObjectKey{
47+
Name: "cluster1",
48+
}
49+
k6 = client.ObjectKey{
50+
Name: "cluster2",
51+
}
52+
})
53+
54+
Describe("New", func() {
55+
It("should initialize a new object key set with the given elements", func() {
56+
s := NewObjectKeySet(k1, k2, k3, k4, k5, k6)
57+
Expect(s).To(Equal(ObjectKeySet{
58+
k1: struct{}{},
59+
k2: struct{}{},
60+
k3: struct{}{},
61+
k4: struct{}{},
62+
k5: struct{}{},
63+
k6: struct{}{},
64+
}))
65+
})
66+
})
67+
68+
Describe("Insert", func() {
69+
It("should insert the given items", func() {
70+
s := NewObjectKeySet()
71+
s.Insert(k1, k2, k3)
72+
Expect(s).To(Equal(ObjectKeySet{
73+
k1: struct{}{},
74+
k2: struct{}{},
75+
k3: struct{}{},
76+
}))
77+
78+
s.Insert(k4, k5, k6)
79+
Expect(s).To(Equal(ObjectKeySet{
80+
k1: struct{}{},
81+
k2: struct{}{},
82+
k3: struct{}{},
83+
k4: struct{}{},
84+
k5: struct{}{},
85+
k6: struct{}{},
86+
}))
87+
})
88+
})
89+
90+
Describe("Delete", func() {
91+
It("should delete the given items from the set", func() {
92+
s := NewObjectKeySet(k1, k2, k3, k4, k5, k6)
93+
94+
s.Delete(k2, k4, k6)
95+
96+
Expect(s).To(Equal(ObjectKeySet{
97+
k1: struct{}{},
98+
k3: struct{}{},
99+
k5: struct{}{},
100+
}))
101+
})
102+
})
103+
104+
Describe("Has", func() {
105+
It("should return whether the given item is present in the set", func() {
106+
s := NewObjectKeySet(k1, k2, k3)
107+
108+
Expect(s.Has(k1)).To(BeTrue(), "set should have key: set %#v, key %s", s, k1)
109+
Expect(s.Has(k2)).To(BeTrue(), "set should have key: set %#v, key %s", s, k2)
110+
Expect(s.Has(k3)).To(BeTrue(), "set should have key: set %#v, key %s", s, k3)
111+
Expect(s.Has(k4)).To(BeFalse(), "set should not have key: set %#v, key %s", s, k4)
112+
Expect(s.Has(k5)).To(BeFalse(), "set should not have key: set %#v, key %s", s, k5)
113+
Expect(s.Has(k6)).To(BeFalse(), "set should not have key: set %#v, key %s", s, k6)
114+
})
115+
})
116+
117+
Describe("Len", func() {
118+
It("should return the length of the set", func() {
119+
Expect(NewObjectKeySet(k1, k2, k3).Len()).To(Equal(3))
120+
Expect((ObjectKeySet)(nil).Len()).To(Equal(0))
121+
Expect(NewObjectKeySet().Len()).To(Equal(0))
122+
})
123+
})
124+
})
125+
})

0 commit comments

Comments
 (0)