Skip to content

Commit 0f1d2b5

Browse files
committed
batteries: print description and group names in --help
Signed-off-by: Dr. Stefan Schimanski <stefan.schimanski@gmail.com>
1 parent 7008409 commit 0f1d2b5

File tree

2 files changed

+75
-14
lines changed

2 files changed

+75
-14
lines changed

server/batteries/battery.go

Lines changed: 35 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,12 @@ type BatterySpec struct {
3434
// Enabled indicates whether the battery is enabled.
3535
Enabled bool
3636

37-
// GroupNames is the list of group names that the battery is responsible for.
37+
// Description is a human-readable description of the battery.
38+
Description string
39+
40+
// Groups is the list of group names that the battery is responsible for.
3841
// If disabled, the battery will not be registered for these groups.
39-
GroupNames []string
42+
Groups []string
4043
}
4144

4245
const (
@@ -57,12 +60,35 @@ const (
5760
var (
5861
// The generic features.
5962
defaultBatteries = map[Battery]BatterySpec{
60-
BatteryLeases: {Enabled: false, GroupNames: []string{"coordination.k8s.io"}},
61-
BatteryAuthentication: {Enabled: false, GroupNames: []string{"authentication.k8s.io", "rbac.authentication.k8s.io"}},
62-
BatteryAuthorization: {Enabled: false, GroupNames: []string{"authorization.k8s.io", "rbac.authorization.k8s.io"}},
63-
BatteryAdmission: {Enabled: false, GroupNames: []string{"admissionregistration.k8s.io"}},
64-
BatteryFlowControl: {Enabled: false, GroupNames: []string{"flowcontrol.apiserver.k8s.io"}},
65-
BatteryCRDs: {Enabled: false, GroupNames: []string{"apiextensions.k8s.io"}},
63+
BatteryLeases: {
64+
Enabled: false,
65+
Groups: []string{"coordination.k8s.io"},
66+
Description: "Leases are used to coordinate some operations between Kubernetes components"},
67+
BatteryAuthentication: {
68+
Enabled: false,
69+
Groups: []string{"authentication.k8s.io"},
70+
Description: "Authentication verifies the identity of the user",
71+
},
72+
BatteryAuthorization: {
73+
Enabled: false,
74+
Groups: []string{"authorization.k8s.io", "rbac.authorization.k8s.io"},
75+
Description: "Authorization decides whether a request is allowed",
76+
},
77+
BatteryAdmission: {
78+
Enabled: false,
79+
Groups: []string{"admissionregistration.k8s.io"},
80+
Description: "Admission controllers validate and mutate requests",
81+
},
82+
BatteryFlowControl: {
83+
Enabled: false,
84+
Groups: []string{"flowcontrol.apiserver.k8s.io"},
85+
Description: "Flow control limits number of requests processed at a time",
86+
},
87+
BatteryCRDs: {
88+
Enabled: false,
89+
Groups: []string{"apiextensions.k8s.io"},
90+
Description: "CustomResourceDefinitions (CRDs) allow definition of custom resources",
91+
},
6692
}
6793
)
6894

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

144170
func (b CompletedOptions) containsAndDisabled(name string) bool {
145171
for _, spec := range b.batteries {
146-
if slices.Contains(spec.GroupNames, name) && !spec.Enabled {
172+
if slices.Contains(spec.Groups, name) && !spec.Enabled {
147173
return true
148174
}
149175
}

server/batteries/options.go

Lines changed: 40 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ package batteries
1818

1919
import (
2020
"fmt"
21+
"os"
2122
"strings"
2223

2324
"github.com/spf13/pflag"
@@ -50,18 +51,52 @@ func (s *Options) AddFlags(fs *pflag.FlagSet) {
5051
return
5152
}
5253

53-
bats := sets.NewString()
54-
for b := range defaultBatteries {
55-
bats = bats.Insert(string(b))
54+
all := sets.NewString()
55+
enabled := sets.NewString()
56+
var maxLen int
57+
for name := range defaultBatteries {
58+
if len(name) > maxLen {
59+
maxLen = len(name)
60+
}
5661
}
57-
fs.StringSliceVar(&s.Enabled, "batteries", []string{}, "The batteries to enable in the generic control-plane server. Possible values: "+strings.Join(bats.List(), ", "))
62+
for name, bat := range defaultBatteries {
63+
if bat.Groups == nil {
64+
all = all.Insert(fmt.Sprintf("%-*s: %s", maxLen, name, bat.Description))
65+
}
66+
all = all.Insert(fmt.Sprintf("%-*s %s [%s]", maxLen+1, name+":", bat.Description, strings.Join(bat.Groups, ", ")))
67+
if bat.Enabled {
68+
enabled.Insert(string(name))
69+
}
70+
}
71+
fs.StringSliceVar(&s.Enabled, "batteries", []string{}, fmt.Sprintf(
72+
"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",
73+
strings.Join(all.List(), "\n- "),
74+
strings.Join(enabled.List(), ", "),
75+
))
5876
}
5977

6078
// Complete defaults fields that have not set by the consumer of this package.
6179
func (b Options) Complete() CompletedOptions {
6280
// Ensure all related configurations are configured
6381
for _, name := range b.Enabled {
64-
if _, ok := b.batteries[Battery(name)]; ok {
82+
if len(name) == 0 {
83+
continue
84+
}
85+
switch name[0] {
86+
case '-':
87+
if _, ok := b.batteries[Battery(name[1:])]; !ok {
88+
fmt.Fprintf(os.Stderr, "Warning: unknown battery %q\n", name[1:])
89+
}
90+
b.Disable(Battery(name[1:]))
91+
case '+':
92+
if _, ok := b.batteries[Battery(name[1:])]; !ok {
93+
fmt.Fprintf(os.Stderr, "Warning: unknown battery %q\n", name[1:])
94+
}
95+
b.Enable(Battery(name[1:]))
96+
default:
97+
if _, ok := b.batteries[Battery(name[1:])]; !ok {
98+
fmt.Fprintf(os.Stderr, "Warning: unknown battery %q\n", name)
99+
}
65100
b.Enable(Battery(name))
66101
}
67102
}

0 commit comments

Comments
 (0)