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
44 changes: 35 additions & 9 deletions server/batteries/battery.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,12 @@ type BatterySpec struct {
// Enabled indicates whether the battery is enabled.
Enabled bool

// GroupNames is the list of group names that the battery is responsible for.
// Description is a human-readable description of the battery.
Description string

// Groups is the list of group names that the battery is responsible for.
// If disabled, the battery will not be registered for these groups.
GroupNames []string
Groups []string
}

const (
Expand All @@ -57,12 +60,35 @@ const (
var (
// The generic features.
defaultBatteries = map[Battery]BatterySpec{
BatteryLeases: {Enabled: false, GroupNames: []string{"coordination.k8s.io"}},
BatteryAuthentication: {Enabled: false, GroupNames: []string{"authentication.k8s.io", "rbac.authentication.k8s.io"}},
BatteryAuthorization: {Enabled: false, GroupNames: []string{"authorization.k8s.io", "rbac.authorization.k8s.io"}},
BatteryAdmission: {Enabled: false, GroupNames: []string{"admissionregistration.k8s.io"}},
BatteryFlowControl: {Enabled: false, GroupNames: []string{"flowcontrol.apiserver.k8s.io"}},
BatteryCRDs: {Enabled: false, GroupNames: []string{"apiextensions.k8s.io"}},
BatteryLeases: {
Enabled: false,
Groups: []string{"coordination.k8s.io"},
Description: "Leases are used to coordinate some operations between Kubernetes components"},
BatteryAuthentication: {
Enabled: false,
Groups: []string{"authentication.k8s.io"},
Description: "Authentication verifies the identity of the user",
},
BatteryAuthorization: {
Enabled: false,
Groups: []string{"authorization.k8s.io", "rbac.authorization.k8s.io"},
Description: "Authorization decides whether a request is allowed",
},
BatteryAdmission: {
Enabled: false,
Groups: []string{"admissionregistration.k8s.io"},
Description: "Admission controllers validate and mutate requests",
},
BatteryFlowControl: {
Enabled: false,
Groups: []string{"flowcontrol.apiserver.k8s.io"},
Description: "Flow control limits number of requests processed at a time",
},
BatteryCRDs: {
Enabled: false,
Groups: []string{"apiextensions.k8s.io"},
Description: "CustomResourceDefinitions (CRDs) allow definition of custom resources",
},
}
)

Expand Down Expand Up @@ -143,7 +169,7 @@ func (b CompletedOptions) DefaultOffAdmissionPlugins() sets.Set[string] {

func (b CompletedOptions) containsAndDisabled(name string) bool {
for _, spec := range b.batteries {
if slices.Contains(spec.GroupNames, name) && !spec.Enabled {
if slices.Contains(spec.Groups, name) && !spec.Enabled {
return true
}
}
Expand Down
45 changes: 40 additions & 5 deletions server/batteries/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package batteries

import (
"fmt"
"os"
"strings"

"github.com/spf13/pflag"
Expand Down Expand Up @@ -50,18 +51,52 @@ func (s *Options) AddFlags(fs *pflag.FlagSet) {
return
}

bats := sets.NewString()
for b := range defaultBatteries {
bats = bats.Insert(string(b))
all := sets.NewString()
enabled := sets.NewString()
var maxLen int
for name := range defaultBatteries {
if len(name) > maxLen {
maxLen = len(name)
}
}
fs.StringSliceVar(&s.Enabled, "batteries", []string{}, "The batteries to enable in the generic control-plane server. Possible values: "+strings.Join(bats.List(), ", "))
for name, bat := range defaultBatteries {
if bat.Groups == nil {
all = all.Insert(fmt.Sprintf("%-*s: %s", maxLen, name, bat.Description))
}
all = all.Insert(fmt.Sprintf("%-*s %s [%s]", maxLen+1, name+":", bat.Description, strings.Join(bat.Groups, ", ")))
if bat.Enabled {
enabled.Insert(string(name))
}
}
fs.StringSliceVar(&s.Enabled, "batteries", []string{}, fmt.Sprintf(
"The batteries to enable in the generic control-plane server ('-battery' to disable, '+battery' or 'battery' to enable).\n\nPossible values:\n- %s\n\nEnabled batteries: %s",
strings.Join(all.List(), "\n- "),
strings.Join(enabled.List(), ", "),
))
}

// Complete defaults fields that have not set by the consumer of this package.
func (b Options) Complete() CompletedOptions {
// Ensure all related configurations are configured
for _, name := range b.Enabled {
if _, ok := b.batteries[Battery(name)]; ok {
if len(name) == 0 {
continue
}
switch name[0] {
case '-':
if _, ok := b.batteries[Battery(name[1:])]; !ok {
fmt.Fprintf(os.Stderr, "Warning: unknown battery %q\n", name[1:])
}
b.Disable(Battery(name[1:]))
case '+':
if _, ok := b.batteries[Battery(name[1:])]; !ok {
fmt.Fprintf(os.Stderr, "Warning: unknown battery %q\n", name[1:])
}
b.Enable(Battery(name[1:]))
default:
if _, ok := b.batteries[Battery(name[1:])]; !ok {
fmt.Fprintf(os.Stderr, "Warning: unknown battery %q\n", name)
}
b.Enable(Battery(name))
}
}
Expand Down