diff --git a/server/batteries/battery.go b/server/batteries/battery.go index 47035c5..76036d4 100644 --- a/server/batteries/battery.go +++ b/server/batteries/battery.go @@ -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 ( @@ -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", + }, } ) @@ -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 } } diff --git a/server/batteries/options.go b/server/batteries/options.go index 825e875..bbdb955 100644 --- a/server/batteries/options.go +++ b/server/batteries/options.go @@ -18,6 +18,7 @@ package batteries import ( "fmt" + "os" "strings" "github.com/spf13/pflag" @@ -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)) } }