Skip to content

Commit 5e67eb1

Browse files
committed
Update switches, remove set
`set` is removed due to official support in k8s' `sets` package. Fix the `switches` package and add `AnyEnabled`, `AllEnabled` and `Values`.
1 parent 504e6d6 commit 5e67eb1

File tree

7 files changed

+196
-479
lines changed

7 files changed

+196
-479
lines changed

cmdutils/switches/switches.go

Lines changed: 38 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ import (
2222
"sort"
2323
"strings"
2424

25-
"github.com/onmetal/controller-utils/set"
25+
"k8s.io/apimachinery/pkg/util/sets"
2626
)
2727

2828
const (
@@ -118,14 +118,34 @@ func (s *Switches) Enabled(name string) bool {
118118
return s.settings[name]
119119
}
120120

121+
// AllEnabled checks whether all switches with the given names are enabled.
122+
func (s *Switches) AllEnabled(names ...string) bool {
123+
for _, name := range names {
124+
if !s.settings[name] {
125+
return false
126+
}
127+
}
128+
return true
129+
}
130+
131+
// AnyEnabled checks whether any switch of the given names is enabled.
132+
func (s *Switches) AnyEnabled(names ...string) bool {
133+
for _, name := range names {
134+
if s.settings[name] {
135+
return true
136+
}
137+
}
138+
return false
139+
}
140+
121141
// All returns names of all items set in settings
122-
func (s *Switches) All() set.Set[string] {
123-
return set.Keys(s.defaults)
142+
func (s *Switches) All() sets.Set[string] {
143+
return sets.KeySet(s.defaults)
124144
}
125145

126146
// Active returns names of all active items
127-
func (s *Switches) Active() set.Set[string] {
128-
names := set.New[string]()
147+
func (s *Switches) Active() sets.Set[string] {
148+
names := sets.New[string]()
129149
for k, enabled := range s.settings {
130150
if enabled {
131151
names.Insert(k)
@@ -135,9 +155,18 @@ func (s *Switches) Active() set.Set[string] {
135155
return names
136156
}
137157

158+
// Values returns the switches with their values.
159+
func (s *Switches) Values() map[string]bool {
160+
res := make(map[string]bool, len(s.defaults))
161+
for key := range s.defaults {
162+
res[key] = s.settings[key]
163+
}
164+
return res
165+
}
166+
138167
// EnabledByDefault returns names of all enabled items
139-
func (s *Switches) EnabledByDefault() set.Set[string] {
140-
names := set.New[string]()
168+
func (s *Switches) EnabledByDefault() sets.Set[string] {
169+
names := sets.New[string]()
141170
for k, enabled := range s.defaults {
142171
if enabled {
143172
names.Insert(k)
@@ -148,8 +177,8 @@ func (s *Switches) EnabledByDefault() set.Set[string] {
148177
}
149178

150179
// DisabledByDefault returns names of all disabled items
151-
func (s *Switches) DisabledByDefault() set.Set[string] {
152-
names := set.New[string]()
180+
func (s *Switches) DisabledByDefault() sets.Set[string] {
181+
names := sets.New[string]()
153182
for k, enabled := range s.defaults {
154183
if !enabled {
155184
names.Insert(k)

cmdutils/switches/switches_test.go

Lines changed: 157 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -16,136 +16,239 @@ package switches
1616
import (
1717
"flag"
1818

19-
"github.com/onmetal/controller-utils/set"
2019
. "github.com/onsi/ginkgo/v2"
2120
. "github.com/onsi/gomega"
2221
"github.com/spf13/pflag"
22+
"k8s.io/apimachinery/pkg/util/sets"
2323
)
2424

2525
var _ = Describe("CMD Switches", func() {
2626
Context("Testing Switches interface", func() {
27-
It("should disable runner", func() {
28-
s := New("runner-a", "runner-b")
29-
Expect(s.Set("*,-runner-b")).ToNot(HaveOccurred())
30-
Expect(s.Enabled("runner-a")).To(BeTrue())
31-
Expect(s.Enabled("runner-b")).To(BeFalse())
27+
Describe("Enabled", func() {
28+
It("should return whether an item is enabled or not", func() {
29+
s := New("runner-a", "runner-b")
30+
Expect(s.Set("*,-runner-b")).ToNot(HaveOccurred())
31+
32+
Expect(s.Enabled("runner-a")).To(BeTrue())
33+
Expect(s.Enabled("runner-b")).To(BeFalse())
34+
})
3235
})
33-
It("should return all items", func() {
34-
s := New("runner-a", "runner-b", Disable("runner-c"))
35-
Expect(s.Set("*,-runner-b")).ToNot(HaveOccurred())
3636

37-
expected := set.New("runner-a", "runner-b", "runner-c")
38-
Expect(s.All()).To(Equal(expected))
37+
Describe("AllEnabled", func() {
38+
It("should return true when all given switches are enabled", func() {
39+
s := New("runner-a", Disable("runner-b"), "runner-c")
40+
Expect(s.Set("*")).NotTo(HaveOccurred())
41+
42+
Expect(s.AllEnabled("runner-a", "runner-c")).To(BeTrue())
43+
})
44+
45+
It("should return false if any of the given switches is not enabled", func() {
46+
s := New("runner-a", Disable("runner-b"), "runner-c")
47+
Expect(s.Set("*")).NotTo(HaveOccurred())
48+
49+
Expect(s.AllEnabled("runner-a", "runner-b")).To(BeFalse())
50+
})
51+
52+
It("should return true on empty arguments", func() {
53+
s := New("runner-a", Disable("runner-b"), "runner-c")
54+
Expect(s.Set("*")).NotTo(HaveOccurred())
55+
56+
Expect(s.AllEnabled()).To(BeTrue())
57+
})
3958
})
40-
It("should return only active items", func() {
41-
s := New("runner-a", "runner-b", Disable("runner-c"))
42-
Expect(s.Set("*,-runner-b")).ToNot(HaveOccurred())
4359

44-
expected := set.New("runner-a")
45-
Expect(s.Active()).To(Equal(expected))
60+
Describe("AnyEnabled", func() {
61+
It("should return true when any of the given switches is enabled", func() {
62+
s := New("runner-a", Disable("runner-b"), "runner-c")
63+
Expect(s.Set("*")).NotTo(HaveOccurred())
64+
65+
Expect(s.AnyEnabled("runner-a", "runner-b")).To(BeTrue())
66+
})
67+
68+
It("should return false if all of the given switches are not enabled", func() {
69+
s := New("runner-a", Disable("runner-b"), "runner-c")
70+
Expect(s.Set("*")).NotTo(HaveOccurred())
71+
72+
Expect(s.AnyEnabled("runner-b")).To(BeFalse())
73+
})
74+
75+
It("should return false on empty arguments", func() {
76+
s := New("runner-a", Disable("runner-b"), "runner-c")
77+
Expect(s.Set("*")).NotTo(HaveOccurred())
78+
79+
Expect(s.AnyEnabled()).To(BeFalse())
80+
})
4681
})
47-
It("should return all enabled items", func() {
48-
s := New("runner-a", Disable("runner-b"))
4982

50-
expected := set.New("runner-a")
51-
Expect(s.EnabledByDefault()).To(Equal(expected))
83+
Describe("Values", func() {
84+
It("should return the switches with their values", func() {
85+
s := New("runner-a", "runner-b")
86+
Expect(s.Set("*,-runner-b")).ToNot(HaveOccurred())
87+
88+
Expect(s.Values()).To(Equal(map[string]bool{
89+
"runner-a": true,
90+
"runner-b": false,
91+
}))
92+
})
5293
})
53-
It("should return all disabled items", func() {
54-
s := New("runner-a", Disable("runner-b"))
5594

56-
expected := set.New("runner-b")
57-
Expect(s.DisabledByDefault()).To(Equal(expected))
95+
Describe("All", func() {
96+
It("should return all items", func() {
97+
s := New("runner-a", "runner-b", Disable("runner-c"))
98+
Expect(s.Set("*,-runner-b")).ToNot(HaveOccurred())
99+
100+
Expect(s.All()).To(Equal(sets.New("runner-a", "runner-b", "runner-c")))
101+
})
58102
})
59-
It("should return string", func() {
60-
s := New("runner-a", "runner-b")
61-
Expect(s.Set("*,-runner-b")).ToNot(HaveOccurred())
62-
Expect(s.String()).To(Equal("runner-a,-runner-b"))
103+
104+
Describe("Active", func() {
105+
It("should return all enabled items", func() {
106+
s := New("runner-a", "runner-b", Disable("runner-c"))
107+
Expect(s.Set("*,-runner-b")).ToNot(HaveOccurred())
108+
109+
Expect(s.Active()).To(Equal(sets.New("runner-a")))
110+
})
111+
})
112+
113+
Describe("EnabledByDefault", func() {
114+
It("should return all items that are enabled by default", func() {
115+
s := New("runner-a", Disable("runner-b"))
116+
117+
Expect(s.EnabledByDefault()).To(Equal(sets.New("runner-a")))
118+
})
119+
})
120+
121+
Describe("DisabledByDefault", func() {
122+
It("should return all items disabled by default", func() {
123+
s := New("runner-a", Disable("runner-b"))
124+
125+
Expect(s.DisabledByDefault()).To(Equal(sets.New("runner-b")))
126+
})
127+
})
128+
129+
Describe("String", func() {
130+
It("should return a string repressentation of the switches", func() {
131+
s := New("runner-a", "runner-b")
132+
Expect(s.Set("*,-runner-b")).ToNot(HaveOccurred())
133+
134+
Expect(s.String()).To(Equal("runner-a,-runner-b"))
135+
})
63136
})
64137
})
65138

66-
Context("Testing flag package behavior", func() {
139+
Describe("goflag.Parse", func() {
67140
It("should disable all controllers when no flag is passed", func() {
68141
fs := flag.NewFlagSet("", flag.ExitOnError)
69142
controllers := New("runner-a", Disable("runner-b"), "runner-c")
70143
fs.Var(controllers, "controllers", "")
71144

72145
Expect(fs.Parse([]string{})).NotTo(HaveOccurred())
73-
Expect(controllers.Enabled("runner-a")).To(BeFalse())
74-
Expect(controllers.Enabled("runner-b")).To(BeFalse())
75-
Expect(controllers.Enabled("runner-c")).To(BeFalse())
146+
147+
Expect(controllers.Values()).To(Equal(map[string]bool{
148+
"runner-a": false,
149+
"runner-b": false,
150+
"runner-c": false,
151+
}))
76152
})
153+
77154
It("should keep default settings when * is passed", func() {
78155
fs := flag.NewFlagSet("", flag.ExitOnError)
79156
controllers := New("runner-a", Disable("runner-b"), "runner-c")
80157
fs.Var(controllers, "controllers", "")
81158

82159
Expect(fs.Parse([]string{"--controllers=*"})).NotTo(HaveOccurred())
83-
Expect(controllers.Enabled("runner-a")).To(BeTrue())
84-
Expect(controllers.Enabled("runner-b")).To(BeFalse())
85-
Expect(controllers.Enabled("runner-c")).To(BeTrue())
160+
161+
Expect(controllers.Values()).To(Equal(map[string]bool{
162+
"runner-a": true,
163+
"runner-b": false,
164+
"runner-c": true,
165+
}))
86166
})
167+
87168
It("should override default settings", func() {
88169
fs := flag.NewFlagSet("", flag.ExitOnError)
89170
controllers := New("runner-a", Disable("runner-b"), "runner-c")
90171
fs.Var(controllers, "controllers", "")
91172

92173
Expect(fs.Parse([]string{"--controllers=runner-a,-runner-c"})).NotTo(HaveOccurred())
93-
Expect(controllers.Enabled("runner-a")).To(BeTrue())
94-
Expect(controllers.Enabled("runner-b")).To(BeFalse())
95-
Expect(controllers.Enabled("runner-c")).To(BeFalse())
174+
175+
Expect(controllers.Values()).To(Equal(map[string]bool{
176+
"runner-a": true,
177+
"runner-b": false,
178+
"runner-c": false,
179+
}))
96180
})
181+
97182
It("should override some of default settings", func() {
98183
fs := flag.NewFlagSet("", flag.ExitOnError)
99184
controllers := New("runner-a", Disable("runner-b"), "runner-c")
100185
fs.Var(controllers, "controllers", "")
101186

102187
Expect(fs.Parse([]string{"--controllers=*,-runner-a"})).NotTo(HaveOccurred())
103-
Expect(controllers.Enabled("runner-a")).To(BeFalse())
104-
Expect(controllers.Enabled("runner-b")).To(BeFalse())
105-
Expect(controllers.Enabled("runner-c")).To(BeTrue())
188+
189+
Expect(controllers.Values()).To(Equal(map[string]bool{
190+
"runner-a": false,
191+
"runner-b": false,
192+
"runner-c": true,
193+
}))
106194
})
107195
})
108196

109-
Context("Testing pflag package behavior", func() {
197+
Describe("pflag.parse", func() {
110198
It("should disable all controllers when no flag is passed", func() {
111199
fs := pflag.NewFlagSet("", pflag.ExitOnError)
112200
controllers := New("runner-a", Disable("runner-b"), "runner-c")
113201
fs.Var(controllers, "controllers", "")
114202

115203
Expect(fs.Parse([]string{})).NotTo(HaveOccurred())
116-
Expect(controllers.Enabled("runner-a")).To(BeFalse())
117-
Expect(controllers.Enabled("runner-b")).To(BeFalse())
118-
Expect(controllers.Enabled("runner-c")).To(BeFalse())
204+
205+
Expect(controllers.Values()).To(Equal(map[string]bool{
206+
"runner-a": false,
207+
"runner-b": false,
208+
"runner-c": false,
209+
}))
119210
})
211+
120212
It("should keep default settings when * is passed", func() {
121213
fs := pflag.NewFlagSet("", pflag.ExitOnError)
122214
controllers := New("runner-a", Disable("runner-b"), "runner-c")
123215
fs.Var(controllers, "controllers", "")
124216

125217
Expect(fs.Parse([]string{"--controllers=*"})).NotTo(HaveOccurred())
126-
Expect(controllers.Enabled("runner-a")).To(BeTrue())
127-
Expect(controllers.Enabled("runner-b")).To(BeFalse())
128-
Expect(controllers.Enabled("runner-c")).To(BeTrue())
218+
219+
Expect(controllers.Values()).To(Equal(map[string]bool{
220+
"runner-a": true,
221+
"runner-b": false,
222+
"runner-c": true,
223+
}))
129224
})
225+
130226
It("should override default settings", func() {
131227
fs := pflag.NewFlagSet("", pflag.ExitOnError)
132228
controllers := New("runner-a", Disable("runner-b"), "runner-c")
133229
fs.Var(controllers, "controllers", "")
134230

135231
Expect(fs.Parse([]string{"--controllers=runner-a,-runner-c"})).NotTo(HaveOccurred())
136-
Expect(controllers.Enabled("runner-a")).To(BeTrue())
137-
Expect(controllers.Enabled("runner-b")).To(BeFalse())
138-
Expect(controllers.Enabled("runner-c")).To(BeFalse())
232+
233+
Expect(controllers.Values()).To(Equal(map[string]bool{
234+
"runner-a": true,
235+
"runner-b": false,
236+
"runner-c": false,
237+
}))
139238
})
239+
140240
It("should override some of default settings", func() {
141241
fs := pflag.NewFlagSet("", pflag.ExitOnError)
142242
controllers := New("runner-a", Disable("runner-b"), "runner-c")
143243
fs.Var(controllers, "controllers", "")
144244

145245
Expect(fs.Parse([]string{"--controllers=*,-runner-a"})).NotTo(HaveOccurred())
146-
Expect(controllers.Enabled("runner-a")).To(BeFalse())
147-
Expect(controllers.Enabled("runner-b")).To(BeFalse())
148-
Expect(controllers.Enabled("runner-c")).To(BeTrue())
246+
247+
Expect(controllers.Values()).To(Equal(map[string]bool{
248+
"runner-a": false,
249+
"runner-b": false,
250+
"runner-c": true,
251+
}))
149252
})
150253
})
151254
})

go.mod

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
module github.com/onmetal/controller-utils
22

3-
go 1.19
3+
go 1.20
44

55
require (
66
github.com/golang/mock v1.6.0
@@ -9,7 +9,6 @@ require (
99
github.com/onsi/gomega v1.26.0
1010
github.com/spf13/pflag v1.0.5
1111
github.com/stretchr/testify v1.8.1
12-
golang.org/x/exp v0.0.0-20221006183845-316c7553db56
1312
k8s.io/api v0.26.1
1413
k8s.io/apiextensions-apiserver v0.26.1
1514
k8s.io/apimachinery v0.26.1

go.sum

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -416,8 +416,6 @@ golang.org/x/exp v0.0.0-20191227195350-da58074b4299/go.mod h1:2RIsYlXP63K8oxa1u0
416416
golang.org/x/exp v0.0.0-20200119233911-0405dc783f0a/go.mod h1:2RIsYlXP63K8oxa1u096TMicItID8zy7Y6sNkU49FU4=
417417
golang.org/x/exp v0.0.0-20200207192155-f17229e696bd/go.mod h1:J/WKrq2StrnmMY6+EHIKF9dgMWnmCNThgcyBT1FY9mM=
418418
golang.org/x/exp v0.0.0-20200224162631-6cc2880d07d6/go.mod h1:3jZMyOhIsHpP37uCMkUooju7aAi5cS1Q23tOzKc+0MU=
419-
golang.org/x/exp v0.0.0-20221006183845-316c7553db56 h1:BrYbdKcCNjLyrN6aKqXy4hPw9qGI8IATkj4EWv9Q+kQ=
420-
golang.org/x/exp v0.0.0-20221006183845-316c7553db56/go.mod h1:cyybsKvd6eL0RnXn6p/Grxp8F5bW7iYuBgsNCOHpMYE=
421419
golang.org/x/image v0.0.0-20190227222117-0694c2d4d067/go.mod h1:kZ7UVZpmo3dzQBMxlp+ypCbDeSB+sBbTgSJuh5dn5js=
422420
golang.org/x/image v0.0.0-20190802002840-cff245a6509b/go.mod h1:FeLwcggjj3mMvU+oOTbSwawSJRM1uh48EjtB4UJZlP0=
423421
golang.org/x/lint v0.0.0-20181026193005-c67002cb31c3/go.mod h1:UVdnD1Gm6xHRNCYTkRU2/jEulfH38KcIWyp/GAMgvoE=

0 commit comments

Comments
 (0)