-
Notifications
You must be signed in to change notification settings - Fork 2
added switches package #26
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,141 @@ | ||
// Copyright 2021 OnMetal authors | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
// Package switches provides new type that implements flag.Value interface -- Switches. | ||
// It can be used for enabling/disabling controllers/webhooks in your controller manager. | ||
package switches | ||
|
||
import ( | ||
"encoding/csv" | ||
"fmt" | ||
"strings" | ||
|
||
"sigs.k8s.io/kustomize/kyaml/sets" | ||
) | ||
|
||
const ( | ||
All = "*" | ||
|
||
disablePrefix = "-" | ||
) | ||
|
||
type Switches struct { | ||
settings map[string]bool | ||
} | ||
|
||
// New creates an instance of Switches | ||
func New(settings []string) *Switches { | ||
s := &Switches{ | ||
settings: make(map[string]bool), | ||
} | ||
s.setSettings(settings) | ||
return s | ||
} | ||
|
||
// Disable prepends disablePrefix prefix to an item name | ||
func Disable(name string) string { | ||
return disablePrefix + name | ||
} | ||
|
||
func (s *Switches) String() string { | ||
return fmt.Sprintf("%v", s.settings) | ||
} | ||
|
||
func (s *Switches) Set(val string) error { | ||
var ( | ||
err error | ||
settings []string | ||
) | ||
|
||
if val != "" { | ||
stringReader := strings.NewReader(val) | ||
csvReader := csv.NewReader(stringReader) | ||
|
||
settings, err = csvReader.Read() | ||
if err != nil { | ||
return fmt.Errorf("failed to set switches value: %w", err) | ||
} | ||
|
||
// Validate that all specified controllers are known | ||
for _, v := range settings { | ||
trimmed := strings.TrimPrefix(v, disablePrefix) | ||
if _, ok := s.settings[trimmed]; trimmed != All && !ok { | ||
return fmt.Errorf("unknown item: %s", trimmed) | ||
} | ||
} | ||
} else { | ||
settings = []string{""} | ||
} | ||
|
||
s.setSettings(settings) | ||
return nil | ||
} | ||
|
||
// Enabled checks if item is enabled | ||
func (s *Switches) Enabled(name string) bool { | ||
return s.settings[name] | ||
} | ||
|
||
// All returns names of all items set in settings | ||
func (s *Switches) All() sets.String { | ||
names := make(sets.String, len(s.settings)) | ||
for k := range s.settings { | ||
names.Insert(k) | ||
} | ||
|
||
return names | ||
} | ||
|
||
// DisabledByDefault returns names of all disabled items | ||
func (s *Switches) DisabledByDefault() sets.String { | ||
names := make(sets.String) | ||
for k, enabled := range s.settings { | ||
if !enabled { | ||
names.Insert(k) | ||
} | ||
} | ||
|
||
return names | ||
} | ||
|
||
func (s *Switches) Type() string { | ||
return "Switches" | ||
} | ||
|
||
func (s *Switches) setSettings(settings []string) { | ||
if len(settings) == 1 && settings[0] == "" { | ||
return | ||
} | ||
|
||
var isDefault bool | ||
for _, v := range settings { | ||
if v == All { | ||
isDefault = true | ||
break | ||
} | ||
} | ||
|
||
if !isDefault { | ||
for k := range s.settings { | ||
s.settings[k] = false | ||
} | ||
} | ||
|
||
for _, v := range settings { | ||
if v == All { | ||
continue | ||
} | ||
s.settings[strings.TrimPrefix(v, disablePrefix)] = !strings.HasPrefix(v, disablePrefix) | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
// Copyright 2021 OnMetal authors | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package switches | ||
|
||
import ( | ||
"testing" | ||
|
||
. "github.com/onsi/ginkgo" | ||
. "github.com/onsi/gomega" | ||
) | ||
|
||
func TestCondition(t *testing.T) { | ||
RegisterFailHandler(Fail) | ||
RunSpecs(t, "Condition Suite") | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,138 @@ | ||
// Copyright 2021 OnMetal authors | ||
GrigoriyMikhalkin marked this conversation as resolved.
Show resolved
Hide resolved
|
||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
package switches | ||
|
||
import ( | ||
"flag" | ||
|
||
. "github.com/onsi/ginkgo" | ||
. "github.com/onsi/gomega" | ||
"github.com/spf13/pflag" | ||
"sigs.k8s.io/kustomize/kyaml/sets" | ||
) | ||
|
||
var _ = Describe("CMD Switches", func() { | ||
Context("Testing Switches interface", func() { | ||
It("should disable runner", func() { | ||
s := New([]string{"runner-a", Disable("runner-b")}) | ||
Expect(s.Enabled("runner-a")).To(BeTrue()) | ||
Expect(s.Enabled("runner-b")).To(BeFalse()) | ||
}) | ||
It("should return all items", func() { | ||
s := New([]string{"runner-a", Disable("runner-b")}) | ||
|
||
expected := make(sets.String) | ||
expected.Insert("runner-a", "runner-b") | ||
Expect(s.All()).To(Equal(expected)) | ||
}) | ||
It("should return all disabled items", func() { | ||
s := New([]string{"runner-a", Disable("runner-b")}) | ||
|
||
expected := make(sets.String) | ||
expected.Insert("runner-b") | ||
Expect(s.DisabledByDefault()).To(Equal(expected)) | ||
}) | ||
It("should return string", func() { | ||
s := New([]string{"runner-a", Disable("runner-b")}) | ||
|
||
Expect(s.String()).To(Equal("map[runner-a:true runner-b:false]")) | ||
}) | ||
}) | ||
|
||
Context("Testing flag package behavior", func() { | ||
It("should keep default settings when no flag is passed", func() { | ||
fs := flag.NewFlagSet("", flag.ExitOnError) | ||
controllers := New([]string{"runner-a", Disable("runner-b"), "runner-c"}) | ||
fs.Var(controllers, "controllers", "") | ||
|
||
Expect(fs.Parse([]string{})).NotTo(HaveOccurred()) | ||
Expect(controllers.Enabled("runner-a")).To(BeTrue()) | ||
Expect(controllers.Enabled("runner-b")).To(BeFalse()) | ||
Expect(controllers.Enabled("runner-c")).To(BeTrue()) | ||
}) | ||
It("should keep default settings when * is passed", func() { | ||
fs := flag.NewFlagSet("", flag.ExitOnError) | ||
controllers := New([]string{"runner-a", Disable("runner-b"), "runner-c"}) | ||
fs.Var(controllers, "controllers", "") | ||
|
||
Expect(fs.Parse([]string{"--controllers=*"})).NotTo(HaveOccurred()) | ||
Expect(controllers.Enabled("runner-a")).To(BeTrue()) | ||
Expect(controllers.Enabled("runner-b")).To(BeFalse()) | ||
Expect(controllers.Enabled("runner-c")).To(BeTrue()) | ||
}) | ||
It("should override default settings", func() { | ||
fs := flag.NewFlagSet("", flag.ExitOnError) | ||
controllers := New([]string{"runner-a", Disable("runner-b"), "runner-c"}) | ||
fs.Var(controllers, "controllers", "") | ||
|
||
Expect(fs.Parse([]string{"--controllers=runner-a,-runner-c"})).NotTo(HaveOccurred()) | ||
Expect(controllers.Enabled("runner-a")).To(BeTrue()) | ||
Expect(controllers.Enabled("runner-b")).To(BeFalse()) | ||
Expect(controllers.Enabled("runner-c")).To(BeFalse()) | ||
}) | ||
It("should override some of default settings", func() { | ||
fs := flag.NewFlagSet("", flag.ExitOnError) | ||
controllers := New([]string{"runner-a", Disable("runner-b"), "runner-c"}) | ||
fs.Var(controllers, "controllers", "") | ||
|
||
Expect(fs.Parse([]string{"--controllers=*,-runner-a"})).NotTo(HaveOccurred()) | ||
Expect(controllers.Enabled("runner-a")).To(BeFalse()) | ||
Expect(controllers.Enabled("runner-b")).To(BeFalse()) | ||
Expect(controllers.Enabled("runner-c")).To(BeTrue()) | ||
}) | ||
}) | ||
|
||
Context("Testing pflag package behavior", func() { | ||
It("should keep default settings when no flag is passed", func() { | ||
fs := pflag.NewFlagSet("", pflag.ExitOnError) | ||
controllers := New([]string{"runner-a", Disable("runner-b"), "runner-c"}) | ||
fs.Var(controllers, "controllers", "") | ||
|
||
Expect(fs.Parse([]string{})).NotTo(HaveOccurred()) | ||
Expect(controllers.Enabled("runner-a")).To(BeTrue()) | ||
Expect(controllers.Enabled("runner-b")).To(BeFalse()) | ||
Expect(controllers.Enabled("runner-c")).To(BeTrue()) | ||
}) | ||
It("should keep default settings when * is passed", func() { | ||
fs := pflag.NewFlagSet("", pflag.ExitOnError) | ||
controllers := New([]string{"runner-a", Disable("runner-b"), "runner-c"}) | ||
fs.Var(controllers, "controllers", "") | ||
|
||
Expect(fs.Parse([]string{"--controllers=*"})).NotTo(HaveOccurred()) | ||
Expect(controllers.Enabled("runner-a")).To(BeTrue()) | ||
Expect(controllers.Enabled("runner-b")).To(BeFalse()) | ||
Expect(controllers.Enabled("runner-c")).To(BeTrue()) | ||
}) | ||
It("should override default settings", func() { | ||
fs := pflag.NewFlagSet("", pflag.ExitOnError) | ||
controllers := New([]string{"runner-a", Disable("runner-b"), "runner-c"}) | ||
fs.Var(controllers, "controllers", "") | ||
|
||
Expect(fs.Parse([]string{"--controllers=runner-a,-runner-c"})).NotTo(HaveOccurred()) | ||
Expect(controllers.Enabled("runner-a")).To(BeTrue()) | ||
Expect(controllers.Enabled("runner-b")).To(BeFalse()) | ||
Expect(controllers.Enabled("runner-c")).To(BeFalse()) | ||
}) | ||
It("should override some of default settings", func() { | ||
fs := pflag.NewFlagSet("", pflag.ExitOnError) | ||
controllers := New([]string{"runner-a", Disable("runner-b"), "runner-c"}) | ||
fs.Var(controllers, "controllers", "") | ||
|
||
Expect(fs.Parse([]string{"--controllers=*,-runner-a"})).NotTo(HaveOccurred()) | ||
Expect(controllers.Enabled("runner-a")).To(BeFalse()) | ||
Expect(controllers.Enabled("runner-b")).To(BeFalse()) | ||
Expect(controllers.Enabled("runner-c")).To(BeTrue()) | ||
}) | ||
}) | ||
}) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.