-
Notifications
You must be signed in to change notification settings - Fork 1
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
✨ Add basic batteries #41
Merged
Merged
Changes from all commits
Commits
Show all changes
3 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 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
This file contains 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
This file contains 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,168 @@ | ||
package batteries | ||
|
||
import ( | ||
"golang.org/x/exp/slices" | ||
"k8s.io/apimachinery/pkg/util/sets" | ||
"k8s.io/apiserver/pkg/admission" | ||
"k8s.io/apiserver/pkg/admission/plugin/namespace/lifecycle" | ||
validatingadmissionpolicy "k8s.io/apiserver/pkg/admission/plugin/policy/validating" | ||
"k8s.io/apiserver/pkg/admission/plugin/resourcequota" | ||
mutatingwebhook "k8s.io/apiserver/pkg/admission/plugin/webhook/mutating" | ||
validatingwebhook "k8s.io/apiserver/pkg/admission/plugin/webhook/validating" | ||
controlplaneapiserver "k8s.io/kubernetes/pkg/controlplane/apiserver" | ||
"k8s.io/kubernetes/plugin/pkg/admission/admit" | ||
certapproval "k8s.io/kubernetes/plugin/pkg/admission/certificates/approval" | ||
"k8s.io/kubernetes/plugin/pkg/admission/certificates/ctbattest" | ||
|
||
certsigning "k8s.io/kubernetes/plugin/pkg/admission/certificates/signing" | ||
certsubjectrestriction "k8s.io/kubernetes/plugin/pkg/admission/certificates/subjectrestriction" | ||
"k8s.io/kubernetes/plugin/pkg/admission/defaulttolerationseconds" | ||
"k8s.io/kubernetes/plugin/pkg/admission/deny" | ||
"k8s.io/kubernetes/plugin/pkg/admission/eventratelimit" | ||
"k8s.io/kubernetes/plugin/pkg/admission/gc" | ||
"k8s.io/kubernetes/plugin/pkg/admission/namespace/autoprovision" | ||
"k8s.io/kubernetes/plugin/pkg/admission/namespace/exists" | ||
"k8s.io/kubernetes/plugin/pkg/admission/serviceaccount" | ||
// Admission policies | ||
) | ||
|
||
type Batteries struct { | ||
list BatteriesList | ||
|
||
BatteriesArgs []string | ||
} | ||
|
||
type Battery string | ||
|
||
type BatteriesList map[Battery]BatterySpec | ||
|
||
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. | ||
// If disabled, the battery will not be registered for these groups. | ||
GroupNames []string | ||
} | ||
|
||
const ( | ||
// BatteryLeases is the name of the lease battery. | ||
BatteryLeases Battery = "leases" | ||
// BatteryAuthentication is the name of the authentication battery. | ||
BatteryAuthentication Battery = "authentication" | ||
// BatteryAuthorization is the name of the authorization battery. | ||
BatteryAuthorization Battery = "authorization" | ||
// BatteryAdmission is the name of the admission battery. | ||
BatteryAdmission Battery = "admission" | ||
// BatteryFlowControl is the name of the flow control battery. | ||
BatteryFlowControl Battery = "flowcontrol" | ||
// BatteryCRDs is the name of the CRD battery. | ||
BatteryCRDs Battery = "crds" | ||
) | ||
|
||
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"}}, | ||
} | ||
) | ||
|
||
func (b Battery) String() string { | ||
return string(b) | ||
} | ||
|
||
func New() Batteries { | ||
b := Batteries{ | ||
list: make(BatteriesList, len(defaultBatteries)), | ||
} | ||
for name, spec := range defaultBatteries { | ||
b.list[name] = spec | ||
} | ||
return b | ||
} | ||
|
||
func (b Batteries) Enable(name Battery) { | ||
_b := b.list[name] | ||
_b.Enabled = true | ||
b.list[name] = _b | ||
} | ||
|
||
func (b Batteries) Disable(name Battery) { | ||
_b := b.list[name] | ||
_b.Enabled = false | ||
b.list[name] = _b | ||
} | ||
|
||
func (b Batteries) IsEnabled(name Battery) bool { | ||
spec, ok := b.list[name] | ||
return ok && spec.Enabled | ||
} | ||
|
||
// RegisterAllAdmissionPlugins registers all admission plugins based on the batteries configuration. | ||
func (b Batteries) RegisterAllAdmissionPlugins(plugins *admission.Plugins) { | ||
admit.Register(plugins) // DEPRECATED as no real meaning | ||
autoprovision.Register(plugins) | ||
lifecycle.Register(plugins) | ||
exists.Register(plugins) | ||
serviceaccount.Register(plugins) | ||
eventratelimit.Register(plugins) | ||
gc.Register(plugins) | ||
certapproval.Register(plugins) | ||
certsigning.Register(plugins) | ||
ctbattest.Register(plugins) | ||
certsubjectrestriction.Register(plugins) | ||
mutatingwebhook.Register(plugins) | ||
validatingadmissionpolicy.Register(plugins) | ||
validatingwebhook.Register(plugins) | ||
resourcequota.Register(plugins) | ||
deny.Register(plugins) | ||
} | ||
|
||
func (b Batteries) DefaultOffAdmissionPlugins() sets.Set[string] { | ||
defaultOnPlugins := sets.New( | ||
lifecycle.PluginName, // NamespaceLifecycle | ||
// limitranger.PluginName, // LimitRanger | ||
serviceaccount.PluginName, // ServiceAccount | ||
resourcequota.PluginName, // ResourceQuota | ||
certapproval.PluginName, // CertificateApproval | ||
certsigning.PluginName, // CertificateSigning | ||
ctbattest.PluginName, // ClusterTrustBundleAttest | ||
certsubjectrestriction.PluginName, // CertificateSubjectRestriction | ||
defaulttolerationseconds.PluginName, // DefaultTolerationSeconds | ||
) | ||
|
||
if b.IsEnabled(BatteryAdmission) { | ||
defaultOnPlugins.Insert( | ||
mutatingwebhook.PluginName, // MutatingAdmissionWebhook | ||
validatingwebhook.PluginName, // ValidatingAdmissionWebhook | ||
validatingadmissionpolicy.PluginName, // ValidatingAdmissionPolicy, only active when feature gate ValidatingAdmissionPolicy is enabled | ||
) | ||
} | ||
|
||
return sets.New[string](AllOrderedPlugins...).Difference(defaultOnPlugins) | ||
} | ||
|
||
func (b Batteries) containsAndDisabled(name string) bool { | ||
for _, spec := range b.list { | ||
if slices.Contains(spec.GroupNames, name) && !spec.Enabled { | ||
return true | ||
} | ||
} | ||
return false | ||
} | ||
|
||
func (b Batteries) FilterStorageProviders(input []controlplaneapiserver.RESTStorageProvider) []controlplaneapiserver.RESTStorageProvider { | ||
var result []controlplaneapiserver.RESTStorageProvider | ||
for _, rest := range input { | ||
if b.containsAndDisabled(rest.GroupName()) { | ||
continue | ||
} | ||
result = append(result, rest) | ||
} | ||
return result | ||
} |
This file contains 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,62 @@ | ||
/* | ||
Copyright 2024 The KCP 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 batteries | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/spf13/pflag" | ||
|
||
utilruntime "k8s.io/apimachinery/pkg/util/runtime" | ||
genericfeatures "k8s.io/apiserver/pkg/features" | ||
utilfeature "k8s.io/apiserver/pkg/util/feature" | ||
) | ||
|
||
// AddFlags adds the flags for the admin authentication to the given FlagSet. | ||
func (s *Batteries) AddFlags(fs *pflag.FlagSet) { | ||
if s == nil { | ||
return | ||
} | ||
|
||
fs.StringSliceVar(&s.BatteriesArgs, "batteries", []string{}, "The batteries to enable in the generic control-plane server.") | ||
} | ||
|
||
func (b Batteries) Complete() { | ||
// Ensure all some related configuration are configured | ||
|
||
for _, name := range b.BatteriesArgs { | ||
if _, ok := b.list[Battery(name)]; ok { | ||
b.Enable(Battery(name)) | ||
} | ||
} | ||
|
||
// If lease is disabled, we disable APIServerIdentity | ||
if !b.IsEnabled(BatteryLeases) { | ||
utilruntime.Must(utilfeature.DefaultMutableFeatureGate.Set(fmt.Sprintf("%s=false", genericfeatures.APIServerIdentity))) | ||
} | ||
} | ||
|
||
func (b Batteries) Validate() []error { | ||
var errs []error | ||
for _, name := range b.BatteriesArgs { | ||
fmt.Println(name) | ||
if _, ok := b.list[Battery(name)]; !ok { | ||
errs = append(errs, fmt.Errorf("invalid battery %q", name)) | ||
} | ||
} | ||
return errs | ||
} |
This file contains 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
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
what does in-memory storage mean? You mean you wire through no-op informers I assume?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
in-memory etcd - our usual standalone development mode.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ack