mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-08-03 09:22:44 +00:00
Merge pull request #120908 from sttts/sttts-optional-authz
controlplane/apiserver: don't crash if authz or other options are explicitly disabled in options
This commit is contained in:
commit
1020678366
@ -152,7 +152,7 @@ func BuildGenericConfig(
|
|||||||
lastErr = fmt.Errorf("invalid authorization config: %v", err)
|
lastErr = fmt.Errorf("invalid authorization config: %v", err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
if !sets.NewString(s.Authorization.Modes...).Has(modes.ModeRBAC) {
|
if s.Authorization != nil && !sets.NewString(s.Authorization.Modes...).Has(modes.ModeRBAC) {
|
||||||
genericConfig.DisabledPostStartHooks.Insert(rbacrest.PostStartHookName)
|
genericConfig.DisabledPostStartHooks.Insert(rbacrest.PostStartHookName)
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -172,12 +172,15 @@ func BuildGenericConfig(
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// BuildAuthorizer constructs the authorizer
|
// BuildAuthorizer constructs the authorizer. If authorization is not set in s, it returns nil, nil, nil
|
||||||
func BuildAuthorizer(s controlplaneapiserver.CompletedOptions, EgressSelector *egressselector.EgressSelector, versionedInformers clientgoinformers.SharedInformerFactory) (authorizer.Authorizer, authorizer.RuleResolver, error) {
|
func BuildAuthorizer(s controlplaneapiserver.CompletedOptions, EgressSelector *egressselector.EgressSelector, versionedInformers clientgoinformers.SharedInformerFactory) (authorizer.Authorizer, authorizer.RuleResolver, error) {
|
||||||
authorizationConfig, err := s.Authorization.ToAuthorizationConfig(versionedInformers)
|
authorizationConfig, err := s.Authorization.ToAuthorizationConfig(versionedInformers)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, nil, err
|
return nil, nil, err
|
||||||
}
|
}
|
||||||
|
if authorizationConfig == nil {
|
||||||
|
return nil, nil, nil
|
||||||
|
}
|
||||||
|
|
||||||
if EgressSelector != nil {
|
if EgressSelector != nil {
|
||||||
egressDialer, err := EgressSelector.Lookup(egressselector.ControlPlane.AsNetworkContext())
|
egressDialer, err := EgressSelector.Lookup(egressselector.ControlPlane.AsNetworkContext())
|
||||||
|
@ -42,6 +42,8 @@ import (
|
|||||||
"k8s.io/kubernetes/pkg/serviceaccount"
|
"k8s.io/kubernetes/pkg/serviceaccount"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// Options define the flags and validation for a generic controlplane. If the
|
||||||
|
// structs are nil, the options are not added to the command line and not validated.
|
||||||
type Options struct {
|
type Options struct {
|
||||||
GenericServerRunOptions *genericoptions.ServerRunOptions
|
GenericServerRunOptions *genericoptions.ServerRunOptions
|
||||||
Etcd *genericoptions.EtcdOptions
|
Etcd *genericoptions.EtcdOptions
|
||||||
|
@ -67,6 +67,9 @@ func NewAdmissionOptions() *AdmissionOptions {
|
|||||||
|
|
||||||
// AddFlags adds flags related to admission for kube-apiserver to the specified FlagSet
|
// AddFlags adds flags related to admission for kube-apiserver to the specified FlagSet
|
||||||
func (a *AdmissionOptions) AddFlags(fs *pflag.FlagSet) {
|
func (a *AdmissionOptions) AddFlags(fs *pflag.FlagSet) {
|
||||||
|
if a == nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
fs.StringSliceVar(&a.PluginNames, "admission-control", a.PluginNames, ""+
|
fs.StringSliceVar(&a.PluginNames, "admission-control", a.PluginNames, ""+
|
||||||
"Admission is divided into two phases. "+
|
"Admission is divided into two phases. "+
|
||||||
"In the first phase, only mutating admission plugins run. "+
|
"In the first phase, only mutating admission plugins run. "+
|
||||||
|
@ -210,6 +210,10 @@ func (o *BuiltInAuthenticationOptions) WithWebHook() *BuiltInAuthenticationOptio
|
|||||||
|
|
||||||
// Validate checks invalid config combination
|
// Validate checks invalid config combination
|
||||||
func (o *BuiltInAuthenticationOptions) Validate() []error {
|
func (o *BuiltInAuthenticationOptions) Validate() []error {
|
||||||
|
if o == nil {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
var allErrors []error
|
var allErrors []error
|
||||||
|
|
||||||
allErrors = append(allErrors, o.validateOIDCOptions()...)
|
allErrors = append(allErrors, o.validateOIDCOptions()...)
|
||||||
@ -270,6 +274,10 @@ func (o *BuiltInAuthenticationOptions) Validate() []error {
|
|||||||
|
|
||||||
// AddFlags returns flags of authentication for a API Server
|
// AddFlags returns flags of authentication for a API Server
|
||||||
func (o *BuiltInAuthenticationOptions) AddFlags(fs *pflag.FlagSet) {
|
func (o *BuiltInAuthenticationOptions) AddFlags(fs *pflag.FlagSet) {
|
||||||
|
if o == nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
fs.StringSliceVar(&o.APIAudiences, "api-audiences", o.APIAudiences, ""+
|
fs.StringSliceVar(&o.APIAudiences, "api-audiences", o.APIAudiences, ""+
|
||||||
"Identifiers of the API. The service account token authenticator will validate that "+
|
"Identifiers of the API. The service account token authenticator will validate that "+
|
||||||
"tokens used against the API are bound to at least one of these audiences. If the "+
|
"tokens used against the API are bound to at least one of these audiences. If the "+
|
||||||
@ -416,8 +424,13 @@ func (o *BuiltInAuthenticationOptions) AddFlags(fs *pflag.FlagSet) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// ToAuthenticationConfig convert BuiltInAuthenticationOptions to kubeauthenticator.Config
|
// ToAuthenticationConfig convert BuiltInAuthenticationOptions to kubeauthenticator.Config. Returns
|
||||||
|
// an empty config if o is nil.
|
||||||
func (o *BuiltInAuthenticationOptions) ToAuthenticationConfig() (kubeauthenticator.Config, error) {
|
func (o *BuiltInAuthenticationOptions) ToAuthenticationConfig() (kubeauthenticator.Config, error) {
|
||||||
|
if o == nil {
|
||||||
|
return kubeauthenticator.Config{}, nil
|
||||||
|
}
|
||||||
|
|
||||||
ret := kubeauthenticator.Config{
|
ret := kubeauthenticator.Config{
|
||||||
TokenSuccessCacheTTL: o.TokenSuccessCacheTTL,
|
TokenSuccessCacheTTL: o.TokenSuccessCacheTTL,
|
||||||
TokenFailureCacheTTL: o.TokenFailureCacheTTL,
|
TokenFailureCacheTTL: o.TokenFailureCacheTTL,
|
||||||
|
@ -29,6 +29,7 @@ import (
|
|||||||
authzconfig "k8s.io/apiserver/pkg/apis/apiserver"
|
authzconfig "k8s.io/apiserver/pkg/apis/apiserver"
|
||||||
genericoptions "k8s.io/apiserver/pkg/server/options"
|
genericoptions "k8s.io/apiserver/pkg/server/options"
|
||||||
versionedinformers "k8s.io/client-go/informers"
|
versionedinformers "k8s.io/client-go/informers"
|
||||||
|
|
||||||
"k8s.io/kubernetes/pkg/kubeapiserver/authorizer"
|
"k8s.io/kubernetes/pkg/kubeapiserver/authorizer"
|
||||||
authzmodes "k8s.io/kubernetes/pkg/kubeapiserver/authorizer/modes"
|
authzmodes "k8s.io/kubernetes/pkg/kubeapiserver/authorizer/modes"
|
||||||
)
|
)
|
||||||
@ -106,6 +107,10 @@ func (o *BuiltInAuthorizationOptions) Validate() []error {
|
|||||||
|
|
||||||
// AddFlags returns flags of authorization for a API Server
|
// AddFlags returns flags of authorization for a API Server
|
||||||
func (o *BuiltInAuthorizationOptions) AddFlags(fs *pflag.FlagSet) {
|
func (o *BuiltInAuthorizationOptions) AddFlags(fs *pflag.FlagSet) {
|
||||||
|
if o == nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
fs.StringSliceVar(&o.Modes, "authorization-mode", o.Modes, ""+
|
fs.StringSliceVar(&o.Modes, "authorization-mode", o.Modes, ""+
|
||||||
"Ordered list of plug-ins to do authorization on secure port. Comma-delimited list of: "+
|
"Ordered list of plug-ins to do authorization on secure port. Comma-delimited list of: "+
|
||||||
strings.Join(authzmodes.AuthorizationModeChoices, ",")+".")
|
strings.Join(authzmodes.AuthorizationModeChoices, ",")+".")
|
||||||
@ -130,14 +135,17 @@ func (o *BuiltInAuthorizationOptions) AddFlags(fs *pflag.FlagSet) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// ToAuthorizationConfig convert BuiltInAuthorizationOptions to authorizer.Config
|
// ToAuthorizationConfig convert BuiltInAuthorizationOptions to authorizer.Config
|
||||||
func (o *BuiltInAuthorizationOptions) ToAuthorizationConfig(versionedInformerFactory versionedinformers.SharedInformerFactory) (authorizer.Config, error) {
|
func (o *BuiltInAuthorizationOptions) ToAuthorizationConfig(versionedInformerFactory versionedinformers.SharedInformerFactory) (*authorizer.Config, error) {
|
||||||
|
if o == nil {
|
||||||
|
return nil, nil
|
||||||
|
}
|
||||||
|
|
||||||
authzConfiguration, err := o.buildAuthorizationConfiguration()
|
authzConfiguration, err := o.buildAuthorizationConfiguration()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return authorizer.Config{}, fmt.Errorf("failed to build authorization config: %s", err)
|
return nil, fmt.Errorf("failed to build authorization config: %s", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
return authorizer.Config{
|
return &authorizer.Config{
|
||||||
PolicyFile: o.PolicyFile,
|
PolicyFile: o.PolicyFile,
|
||||||
VersionedInformerFactory: versionedInformerFactory,
|
VersionedInformerFactory: versionedInformerFactory,
|
||||||
WebhookRetryBackoff: o.WebhookRetryBackoff,
|
WebhookRetryBackoff: o.WebhookRetryBackoff,
|
||||||
|
@ -42,6 +42,9 @@ func NewAPIEnablementOptions() *APIEnablementOptions {
|
|||||||
|
|
||||||
// AddFlags adds flags for a specific APIServer to the specified FlagSet
|
// AddFlags adds flags for a specific APIServer to the specified FlagSet
|
||||||
func (s *APIEnablementOptions) AddFlags(fs *pflag.FlagSet) {
|
func (s *APIEnablementOptions) AddFlags(fs *pflag.FlagSet) {
|
||||||
|
if s == nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
fs.Var(&s.RuntimeConfig, "runtime-config", ""+
|
fs.Var(&s.RuntimeConfig, "runtime-config", ""+
|
||||||
"A set of key=value pairs that enable or disable built-in APIs. Supported options are:\n"+
|
"A set of key=value pairs that enable or disable built-in APIs. Supported options are:\n"+
|
||||||
"v1=true|false for the core API group\n"+
|
"v1=true|false for the core API group\n"+
|
||||||
@ -87,7 +90,6 @@ func (s *APIEnablementOptions) Validate(registries ...GroupRegistry) []error {
|
|||||||
|
|
||||||
// ApplyTo override MergedResourceConfig with defaults and registry
|
// ApplyTo override MergedResourceConfig with defaults and registry
|
||||||
func (s *APIEnablementOptions) ApplyTo(c *server.Config, defaultResourceConfig *serverstore.ResourceConfig, registry resourceconfig.GroupVersionRegistry) error {
|
func (s *APIEnablementOptions) ApplyTo(c *server.Config, defaultResourceConfig *serverstore.ResourceConfig, registry resourceconfig.GroupVersionRegistry) error {
|
||||||
|
|
||||||
if s == nil {
|
if s == nil {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
@ -40,6 +40,10 @@ func NewOptions() *Options {
|
|||||||
|
|
||||||
// Validate validates metrics flags options.
|
// Validate validates metrics flags options.
|
||||||
func (o *Options) Validate() []error {
|
func (o *Options) Validate() []error {
|
||||||
|
if o == nil {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
var errs []error
|
var errs []error
|
||||||
err := validateShowHiddenMetricsVersion(parseVersion(version.Get()), o.ShowHiddenMetricsForVersion)
|
err := validateShowHiddenMetricsVersion(parseVersion(version.Get()), o.ShowHiddenMetricsForVersion)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
Loading…
Reference in New Issue
Block a user