Skip to content

Commit 829ba10

Browse files
added switches package
1 parent 0a02961 commit 829ba10

File tree

3 files changed

+203
-0
lines changed

3 files changed

+203
-0
lines changed

cmdutils/switches/switches.go

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
// Copyright 2021 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 switches provides new type that implements flag.Value interface -- Switches.
16+
// It can be used for enabling/disabling controllers/webhooks in your controller manager.
17+
package switches
18+
19+
import (
20+
"fmt"
21+
"strings"
22+
)
23+
24+
const (
25+
defaultValue = "*"
26+
disablePrefix = "-"
27+
)
28+
29+
type Switches struct {
30+
settings map[string]bool
31+
}
32+
33+
// New creates an instance of Switches
34+
func New(settings []string) *Switches {
35+
s := &Switches{
36+
settings: make(map[string]bool),
37+
}
38+
s.setSettings(settings)
39+
return s
40+
}
41+
42+
// Disable prepends disablePrefix prefix to a controller name
43+
func Disable(name string) string {
44+
return disablePrefix + name
45+
}
46+
47+
func (s *Switches) String() string {
48+
return fmt.Sprintf("%v", s.settings)
49+
}
50+
51+
func (s *Switches) Set(val string) error {
52+
s.setSettings(strings.Split(val, ","))
53+
return nil
54+
}
55+
56+
// Enabled checks if controller is enabled
57+
func (s *Switches) Enabled(name string) bool {
58+
return s.settings[name]
59+
}
60+
61+
// All returns names of all controllers set in settings
62+
func (s *Switches) All() (names []string) {
63+
for k := range s.settings {
64+
names = append(names, k)
65+
}
66+
67+
return
68+
}
69+
70+
// DisabledByDefault returns names of all disabled controllers
71+
func (s *Switches) DisabledByDefault() (names []string) {
72+
for k, enabled := range s.settings {
73+
if !enabled {
74+
names = append(names, k)
75+
}
76+
}
77+
78+
return
79+
}
80+
81+
func (s *Switches) Type() string {
82+
return "Switches"
83+
}
84+
85+
func (s *Switches) setSettings(settings []string) {
86+
if len(settings) == 1 && settings[0] == "" {
87+
return
88+
}
89+
90+
var isDefault bool
91+
for _, v := range settings {
92+
if v == defaultValue {
93+
isDefault = true
94+
break
95+
}
96+
}
97+
98+
if !isDefault {
99+
s.settings = make(map[string]bool)
100+
}
101+
102+
for _, v := range settings {
103+
if v == defaultValue {
104+
continue
105+
}
106+
s.settings[strings.TrimPrefix(v, disablePrefix)] = !strings.HasPrefix(v, disablePrefix)
107+
}
108+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// Copyright 2021 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 switches
16+
17+
import (
18+
"testing"
19+
20+
. "github.com/onsi/ginkgo"
21+
. "github.com/onsi/gomega"
22+
)
23+
24+
func TestCondition(t *testing.T) {
25+
RegisterFailHandler(Fail)
26+
RunSpecs(t, "Condition Suite")
27+
}

cmdutils/switches/switches_test.go

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
// Copyright 2021 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+
package switches
15+
16+
import (
17+
. "github.com/onsi/ginkgo"
18+
. "github.com/onsi/gomega"
19+
)
20+
21+
var _ = Describe("CMD Switches", func() {
22+
Context("Setting switches values", func() {
23+
It("should disable runner", func() {
24+
s := New([]string{"runner-a", Disable("runner-b")})
25+
Expect(s.Enabled("runner-a")).To(BeTrue())
26+
Expect(s.Enabled("runner-b")).To(BeFalse())
27+
})
28+
It("should reuse default settings", func() {
29+
s := New([]string{"runner-a", Disable("runner-b")})
30+
defaults := make(map[string]bool, len(s.settings))
31+
for k, v := range s.settings {
32+
defaults[k] = v
33+
}
34+
35+
By("updating settings with empty value")
36+
Expect(s.Set("")).NotTo(HaveOccurred())
37+
Expect(s.settings).To(Equal(defaults))
38+
39+
By("updating settings with *")
40+
Expect(s.Set("*")).NotTo(HaveOccurred())
41+
Expect(s.settings).To(Equal(defaults))
42+
})
43+
It("shouldn't reuse default settings", func() {
44+
s := New([]string{"runner-a", Disable("runner-b")})
45+
defaults := make(map[string]bool, len(s.settings))
46+
for k, v := range s.settings {
47+
defaults[k] = v
48+
}
49+
50+
By("updating settings with new values")
51+
Expect(s.Set("-runner-a,runner-b")).NotTo(HaveOccurred())
52+
Expect(s.settings).ToNot(Equal(defaults))
53+
})
54+
It("overriding existing settings", func() {
55+
s := New([]string{"runner-a", Disable("runner-b")})
56+
defaults := make(map[string]bool, len(s.settings))
57+
for k, v := range s.settings {
58+
defaults[k] = v
59+
}
60+
61+
By("overriding settings")
62+
Expect(s.Set("*,runner-b")).NotTo(HaveOccurred())
63+
Expect(s.settings).ToNot(Equal(defaults))
64+
defaults["runner-b"] = true
65+
Expect(s.settings).To(Equal(defaults))
66+
})
67+
})
68+
})

0 commit comments

Comments
 (0)