Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
141 changes: 141 additions & 0 deletions cmdutils/switches/switches.go
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)
}
}
27 changes: 27 additions & 0 deletions cmdutils/switches/switches_suite_test.go
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")
}
138 changes: 138 additions & 0 deletions cmdutils/switches/switches_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
// 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 (
"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())
})
})
})
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ require (
github.com/google/addlicense v1.0.0
github.com/onsi/ginkgo v1.16.5
github.com/onsi/gomega v1.16.0
github.com/spf13/pflag v1.0.5
k8s.io/api v0.22.3
k8s.io/apimachinery v0.22.3
k8s.io/client-go v0.22.3
Expand Down