Skip to content

Commit

Permalink
Merge pull request #40 from sttts/sttts-split-generic-kube-apiserver-…
Browse files Browse the repository at this point in the history
…config-23

1.23: genericcontrolplane: split generic config construction from kube-apiserver and apiextensions
  • Loading branch information
sttts authored Feb 10, 2022
2 parents cfb7ceb + 39d9dac commit 50b2aff
Show file tree
Hide file tree
Showing 6 changed files with 247 additions and 116 deletions.
7 changes: 0 additions & 7 deletions pkg/genericcontrolplane/admission/initializer.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,6 @@ import (
quota "k8s.io/apiserver/pkg/quota/v1"
)

// TODO add a `WantsToRun` which takes a stopCh. Might make it generic.

// WantsCloudConfig defines a function which sets CloudConfig for admission plugins that need it.
type WantsCloudConfig interface {
SetCloudConfig([]byte)
}

// WantsRESTMapper defines a function which sets RESTMapper for admission plugins that need it.
type WantsRESTMapper interface {
SetRESTMapper(meta.RESTMapper)
Expand Down
6 changes: 2 additions & 4 deletions pkg/genericcontrolplane/apiextensions.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,6 @@ See the License for the specific language governing permissions and
limitations under the License.
*/

// Package app does all of the work necessary to create a Kubernetes
// APIServer by binding together the API, master and APIServer infrastructure.
// It can be configured and called directly or via the hyperkube framework.
package genericcontrolplane

import (
Expand All @@ -34,14 +31,15 @@ import (
utilfeature "k8s.io/apiserver/pkg/util/feature"
"k8s.io/apiserver/pkg/util/webhook"
kubeexternalinformers "k8s.io/client-go/informers"

"k8s.io/kubernetes/pkg/genericcontrolplane/options"
)

func CreateAPIExtensionsConfig(
kubeAPIServerConfig genericapiserver.Config,
externalInformers kubeexternalinformers.SharedInformerFactory,
pluginInitializers []admission.PluginInitializer,
commandOptions *options.ServerRunOptions,
commandOptions options.CompletedServerRunOptions,
serviceResolver webhook.ServiceResolver,
authResolverWrapper webhook.AuthenticationInfoResolverWrapper,
) (*apiextensionsapiserver.Config, error) {
Expand Down
72 changes: 72 additions & 0 deletions pkg/genericcontrolplane/options/flags.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
/*
Copyright 2022 The Kubernetes 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 options

import (
cliflag "k8s.io/component-base/cli/flag"
)

// Flags returns flags for a specific APIServer by section name
func (s *ServerRunOptions) Flags() (fss cliflag.NamedFlagSets) {
s.GenericServerRunOptions.AddUniversalFlags(fss.FlagSet("generic"))
s.Etcd.AddFlags(fss.FlagSet("etcd"))
s.SecureServing.AddFlags(fss.FlagSet("secure serving"))
s.Audit.AddFlags(fss.FlagSet("auditing"))
s.Features.AddFlags(fss.FlagSet("features"))
s.Authentication.AddFlags(fss.FlagSet("authentication"))

s.APIEnablement.AddFlags(fss.FlagSet("API enablement"))
s.EgressSelector.AddFlags(fss.FlagSet("egress selector"))
s.Admission.AddFlags(fss.FlagSet("admission"))

s.Metrics.AddFlags(fss.FlagSet("metrics"))
s.Logs.AddFlags(fss.FlagSet("logs"))
s.Traces.AddFlags(fss.FlagSet("traces"))

fs := fss.FlagSet("misc")
fs.DurationVar(&s.EventTTL, "event-ttl", s.EventTTL,
"Amount of time to retain events.")

fs.BoolVar(&s.EnableLogsHandler, "enable-logs-handler", s.EnableLogsHandler,
"If true, install a /logs handler for the apiserver logs.")
fs.MarkDeprecated("enable-logs-handler", "This flag will be removed in v1.19") //nolint:golint,errcheck

fs.Int64Var(&s.MaxConnectionBytesPerSec, "max-connection-bytes-per-sec", s.MaxConnectionBytesPerSec, ""+
"If non-zero, throttle each user connection to this number of bytes/sec. "+
"Currently only applies to long-running requests.")

fs.IntVar(&s.IdentityLeaseDurationSeconds, "identity-lease-duration-seconds", s.IdentityLeaseDurationSeconds,
"The duration of kube-apiserver lease in seconds, must be a positive number. (In use when the APIServerIdentity feature gate is enabled.)")

fs.IntVar(&s.IdentityLeaseRenewIntervalSeconds, "identity-lease-renew-interval-seconds", s.IdentityLeaseRenewIntervalSeconds,
"The interval of kube-apiserver renewing its lease in seconds, must be a positive number. (In use when the APIServerIdentity feature gate is enabled.)")

fs.StringVar(&s.ProxyClientCertFile, "proxy-client-cert-file", s.ProxyClientCertFile, ""+
"Client certificate used to prove the identity of the aggregator or kube-apiserver "+
"when it must call out during a request. This includes proxying requests to a user "+
"api-server and calling out to webhook admission plugins. It is expected that this "+
"cert includes a signature from the CA in the --requestheader-client-ca-file flag. "+
"That CA is published in the 'extension-apiserver-authentication' configmap in "+
"the kube-system namespace. Components receiving calls from kube-aggregator should "+
"use that CA to perform their half of the mutual TLS verification.")
fs.StringVar(&s.ProxyClientKeyFile, "proxy-client-key-file", s.ProxyClientKeyFile, ""+
"Private key for the client certificate used to prove the identity of the aggregator or kube-apiserver "+
"when it must call out during a request. This includes proxying requests to a user "+
"api-server and calling out to webhook admission plugins.")

return fss
}
55 changes: 55 additions & 0 deletions pkg/genericcontrolplane/options/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,10 @@ limitations under the License.
package options

import (
"fmt"
"net/http"
"os"
"strings"
"time"

"k8s.io/apiserver/pkg/admission/plugin/webhook/mutating"
Expand All @@ -27,6 +30,7 @@ import (
"k8s.io/apiserver/pkg/storage/storagebackend"
"k8s.io/component-base/logs"
"k8s.io/component-base/metrics"
"k8s.io/klog/v2"

kubeoptions "k8s.io/kubernetes/pkg/kubeapiserver/options"
"k8s.io/kubernetes/pkg/serviceaccount"
Expand Down Expand Up @@ -73,6 +77,16 @@ type ServerRunOptions struct {
BuildHandlerChainFunc func(apiHandler http.Handler, c *genericapiserver.Config) (secure http.Handler)
}

// completedServerRunOptions is a private wrapper that enforces a call of Complete() before Run can be invoked.
type completedServerRunOptions struct {
ServerRunOptions
}

type CompletedServerRunOptions struct {
// Embed a private pointer that cannot be instantiated outside of this package.
*completedServerRunOptions
}

// NewServerRunOptions creates a new ServerRunOptions object with default parameters
func NewServerRunOptions() *ServerRunOptions {
s := ServerRunOptions{
Expand Down Expand Up @@ -107,3 +121,44 @@ func NewServerRunOptions() *ServerRunOptions {

return &s
}

// Complete defaults missing field values. It mutates the receiver.
func (o *ServerRunOptions) Complete() (CompletedServerRunOptions, error) {
if err := o.GenericServerRunOptions.DefaultAdvertiseAddress(o.SecureServing.SecureServingOptions); err != nil {
return CompletedServerRunOptions{}, err
}

if err := o.SecureServing.MaybeDefaultWithSelfSignedCerts(o.GenericServerRunOptions.AdvertiseAddress.String(), nil, nil); err != nil {
return CompletedServerRunOptions{}, fmt.Errorf("error creating self-signed certificates: %v", err)
}

if len(o.GenericServerRunOptions.ExternalHost) == 0 {
if len(o.GenericServerRunOptions.AdvertiseAddress) > 0 {
o.GenericServerRunOptions.ExternalHost = o.GenericServerRunOptions.AdvertiseAddress.String()
} else {
if hostname, err := os.Hostname(); err == nil {
o.GenericServerRunOptions.ExternalHost = hostname
} else {
return CompletedServerRunOptions{}, fmt.Errorf("error finding host name: %v", err)
}
}
klog.Infof("external host was not specified, using %v", o.GenericServerRunOptions.ExternalHost)
}

for key, value := range o.APIEnablement.RuntimeConfig {
if key == "v1" || strings.HasPrefix(key, "v1/") ||
key == "api/v1" || strings.HasPrefix(key, "api/v1/") {
delete(o.APIEnablement.RuntimeConfig, key)
o.APIEnablement.RuntimeConfig["/v1"] = value
}
if key == "api/legacy" {
delete(o.APIEnablement.RuntimeConfig, key)
}
}

return CompletedServerRunOptions{
&completedServerRunOptions{
ServerRunOptions: *o,
},
}, nil
}
51 changes: 51 additions & 0 deletions pkg/genericcontrolplane/options/validation.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*
Copyright 2022 The Kubernetes 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 options

import (
"fmt"

apiextensionsapiserver "k8s.io/apiextensions-apiserver/pkg/apiserver"

"k8s.io/kubernetes/pkg/api/genericcontrolplanescheme"
)

func validateAPIServerIdentity(options *CompletedServerRunOptions) []error {
var errs []error
if options.IdentityLeaseDurationSeconds <= 0 {
errs = append(errs, fmt.Errorf("--identity-lease-duration-seconds should be a positive number, but value '%d' provided", options.IdentityLeaseDurationSeconds))
}
if options.IdentityLeaseRenewIntervalSeconds <= 0 {
errs = append(errs, fmt.Errorf("--identity-lease-renew-interval-seconds should be a positive number, but value '%d' provided", options.IdentityLeaseRenewIntervalSeconds))
}
return errs
}

// Validate checks Options and return a slice of found errs.
func (s *CompletedServerRunOptions) Validate() []error {
var errs []error
errs = append(errs, s.Etcd.Validate()...)
errs = append(errs, s.SecureServing.Validate()...)
errs = append(errs, s.Authentication.Validate()...)
errs = append(errs, s.Audit.Validate()...)
errs = append(errs, s.Admission.Validate()...)
errs = append(errs, s.APIEnablement.Validate(genericcontrolplanescheme.Scheme, apiextensionsapiserver.Scheme)...)
errs = append(errs, s.Metrics.Validate()...)
errs = append(errs, validateAPIServerIdentity(s)...)

return errs
}
Loading

0 comments on commit 50b2aff

Please sign in to comment.