Skip to content

Commit 50b2aff

Browse files
authored
Merge pull request #40 from sttts/sttts-split-generic-kube-apiserver-config-23
1.23: genericcontrolplane: split generic config construction from kube-apiserver and apiextensions
2 parents cfb7ceb + 39d9dac commit 50b2aff

File tree

6 files changed

+247
-116
lines changed

6 files changed

+247
-116
lines changed

pkg/genericcontrolplane/admission/initializer.go

-7
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,6 @@ import (
2323
quota "k8s.io/apiserver/pkg/quota/v1"
2424
)
2525

26-
// TODO add a `WantsToRun` which takes a stopCh. Might make it generic.
27-
28-
// WantsCloudConfig defines a function which sets CloudConfig for admission plugins that need it.
29-
type WantsCloudConfig interface {
30-
SetCloudConfig([]byte)
31-
}
32-
3326
// WantsRESTMapper defines a function which sets RESTMapper for admission plugins that need it.
3427
type WantsRESTMapper interface {
3528
SetRESTMapper(meta.RESTMapper)

pkg/genericcontrolplane/apiextensions.go

+2-4
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,6 @@ See the License for the specific language governing permissions and
1414
limitations under the License.
1515
*/
1616

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

2219
import (
@@ -34,14 +31,15 @@ import (
3431
utilfeature "k8s.io/apiserver/pkg/util/feature"
3532
"k8s.io/apiserver/pkg/util/webhook"
3633
kubeexternalinformers "k8s.io/client-go/informers"
34+
3735
"k8s.io/kubernetes/pkg/genericcontrolplane/options"
3836
)
3937

4038
func CreateAPIExtensionsConfig(
4139
kubeAPIServerConfig genericapiserver.Config,
4240
externalInformers kubeexternalinformers.SharedInformerFactory,
4341
pluginInitializers []admission.PluginInitializer,
44-
commandOptions *options.ServerRunOptions,
42+
commandOptions options.CompletedServerRunOptions,
4543
serviceResolver webhook.ServiceResolver,
4644
authResolverWrapper webhook.AuthenticationInfoResolverWrapper,
4745
) (*apiextensionsapiserver.Config, error) {
+72
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
/*
2+
Copyright 2022 The Kubernetes Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package options
18+
19+
import (
20+
cliflag "k8s.io/component-base/cli/flag"
21+
)
22+
23+
// Flags returns flags for a specific APIServer by section name
24+
func (s *ServerRunOptions) Flags() (fss cliflag.NamedFlagSets) {
25+
s.GenericServerRunOptions.AddUniversalFlags(fss.FlagSet("generic"))
26+
s.Etcd.AddFlags(fss.FlagSet("etcd"))
27+
s.SecureServing.AddFlags(fss.FlagSet("secure serving"))
28+
s.Audit.AddFlags(fss.FlagSet("auditing"))
29+
s.Features.AddFlags(fss.FlagSet("features"))
30+
s.Authentication.AddFlags(fss.FlagSet("authentication"))
31+
32+
s.APIEnablement.AddFlags(fss.FlagSet("API enablement"))
33+
s.EgressSelector.AddFlags(fss.FlagSet("egress selector"))
34+
s.Admission.AddFlags(fss.FlagSet("admission"))
35+
36+
s.Metrics.AddFlags(fss.FlagSet("metrics"))
37+
s.Logs.AddFlags(fss.FlagSet("logs"))
38+
s.Traces.AddFlags(fss.FlagSet("traces"))
39+
40+
fs := fss.FlagSet("misc")
41+
fs.DurationVar(&s.EventTTL, "event-ttl", s.EventTTL,
42+
"Amount of time to retain events.")
43+
44+
fs.BoolVar(&s.EnableLogsHandler, "enable-logs-handler", s.EnableLogsHandler,
45+
"If true, install a /logs handler for the apiserver logs.")
46+
fs.MarkDeprecated("enable-logs-handler", "This flag will be removed in v1.19") //nolint:golint,errcheck
47+
48+
fs.Int64Var(&s.MaxConnectionBytesPerSec, "max-connection-bytes-per-sec", s.MaxConnectionBytesPerSec, ""+
49+
"If non-zero, throttle each user connection to this number of bytes/sec. "+
50+
"Currently only applies to long-running requests.")
51+
52+
fs.IntVar(&s.IdentityLeaseDurationSeconds, "identity-lease-duration-seconds", s.IdentityLeaseDurationSeconds,
53+
"The duration of kube-apiserver lease in seconds, must be a positive number. (In use when the APIServerIdentity feature gate is enabled.)")
54+
55+
fs.IntVar(&s.IdentityLeaseRenewIntervalSeconds, "identity-lease-renew-interval-seconds", s.IdentityLeaseRenewIntervalSeconds,
56+
"The interval of kube-apiserver renewing its lease in seconds, must be a positive number. (In use when the APIServerIdentity feature gate is enabled.)")
57+
58+
fs.StringVar(&s.ProxyClientCertFile, "proxy-client-cert-file", s.ProxyClientCertFile, ""+
59+
"Client certificate used to prove the identity of the aggregator or kube-apiserver "+
60+
"when it must call out during a request. This includes proxying requests to a user "+
61+
"api-server and calling out to webhook admission plugins. It is expected that this "+
62+
"cert includes a signature from the CA in the --requestheader-client-ca-file flag. "+
63+
"That CA is published in the 'extension-apiserver-authentication' configmap in "+
64+
"the kube-system namespace. Components receiving calls from kube-aggregator should "+
65+
"use that CA to perform their half of the mutual TLS verification.")
66+
fs.StringVar(&s.ProxyClientKeyFile, "proxy-client-key-file", s.ProxyClientKeyFile, ""+
67+
"Private key for the client certificate used to prove the identity of the aggregator or kube-apiserver "+
68+
"when it must call out during a request. This includes proxying requests to a user "+
69+
"api-server and calling out to webhook admission plugins.")
70+
71+
return fss
72+
}

pkg/genericcontrolplane/options/options.go

+55
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,10 @@ limitations under the License.
1717
package options
1818

1919
import (
20+
"fmt"
2021
"net/http"
22+
"os"
23+
"strings"
2124
"time"
2225

2326
"k8s.io/apiserver/pkg/admission/plugin/webhook/mutating"
@@ -27,6 +30,7 @@ import (
2730
"k8s.io/apiserver/pkg/storage/storagebackend"
2831
"k8s.io/component-base/logs"
2932
"k8s.io/component-base/metrics"
33+
"k8s.io/klog/v2"
3034

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

80+
// completedServerRunOptions is a private wrapper that enforces a call of Complete() before Run can be invoked.
81+
type completedServerRunOptions struct {
82+
ServerRunOptions
83+
}
84+
85+
type CompletedServerRunOptions struct {
86+
// Embed a private pointer that cannot be instantiated outside of this package.
87+
*completedServerRunOptions
88+
}
89+
7690
// NewServerRunOptions creates a new ServerRunOptions object with default parameters
7791
func NewServerRunOptions() *ServerRunOptions {
7892
s := ServerRunOptions{
@@ -107,3 +121,44 @@ func NewServerRunOptions() *ServerRunOptions {
107121

108122
return &s
109123
}
124+
125+
// Complete defaults missing field values. It mutates the receiver.
126+
func (o *ServerRunOptions) Complete() (CompletedServerRunOptions, error) {
127+
if err := o.GenericServerRunOptions.DefaultAdvertiseAddress(o.SecureServing.SecureServingOptions); err != nil {
128+
return CompletedServerRunOptions{}, err
129+
}
130+
131+
if err := o.SecureServing.MaybeDefaultWithSelfSignedCerts(o.GenericServerRunOptions.AdvertiseAddress.String(), nil, nil); err != nil {
132+
return CompletedServerRunOptions{}, fmt.Errorf("error creating self-signed certificates: %v", err)
133+
}
134+
135+
if len(o.GenericServerRunOptions.ExternalHost) == 0 {
136+
if len(o.GenericServerRunOptions.AdvertiseAddress) > 0 {
137+
o.GenericServerRunOptions.ExternalHost = o.GenericServerRunOptions.AdvertiseAddress.String()
138+
} else {
139+
if hostname, err := os.Hostname(); err == nil {
140+
o.GenericServerRunOptions.ExternalHost = hostname
141+
} else {
142+
return CompletedServerRunOptions{}, fmt.Errorf("error finding host name: %v", err)
143+
}
144+
}
145+
klog.Infof("external host was not specified, using %v", o.GenericServerRunOptions.ExternalHost)
146+
}
147+
148+
for key, value := range o.APIEnablement.RuntimeConfig {
149+
if key == "v1" || strings.HasPrefix(key, "v1/") ||
150+
key == "api/v1" || strings.HasPrefix(key, "api/v1/") {
151+
delete(o.APIEnablement.RuntimeConfig, key)
152+
o.APIEnablement.RuntimeConfig["/v1"] = value
153+
}
154+
if key == "api/legacy" {
155+
delete(o.APIEnablement.RuntimeConfig, key)
156+
}
157+
}
158+
159+
return CompletedServerRunOptions{
160+
&completedServerRunOptions{
161+
ServerRunOptions: *o,
162+
},
163+
}, nil
164+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/*
2+
Copyright 2022 The Kubernetes Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package options
18+
19+
import (
20+
"fmt"
21+
22+
apiextensionsapiserver "k8s.io/apiextensions-apiserver/pkg/apiserver"
23+
24+
"k8s.io/kubernetes/pkg/api/genericcontrolplanescheme"
25+
)
26+
27+
func validateAPIServerIdentity(options *CompletedServerRunOptions) []error {
28+
var errs []error
29+
if options.IdentityLeaseDurationSeconds <= 0 {
30+
errs = append(errs, fmt.Errorf("--identity-lease-duration-seconds should be a positive number, but value '%d' provided", options.IdentityLeaseDurationSeconds))
31+
}
32+
if options.IdentityLeaseRenewIntervalSeconds <= 0 {
33+
errs = append(errs, fmt.Errorf("--identity-lease-renew-interval-seconds should be a positive number, but value '%d' provided", options.IdentityLeaseRenewIntervalSeconds))
34+
}
35+
return errs
36+
}
37+
38+
// Validate checks Options and return a slice of found errs.
39+
func (s *CompletedServerRunOptions) Validate() []error {
40+
var errs []error
41+
errs = append(errs, s.Etcd.Validate()...)
42+
errs = append(errs, s.SecureServing.Validate()...)
43+
errs = append(errs, s.Authentication.Validate()...)
44+
errs = append(errs, s.Audit.Validate()...)
45+
errs = append(errs, s.Admission.Validate()...)
46+
errs = append(errs, s.APIEnablement.Validate(genericcontrolplanescheme.Scheme, apiextensionsapiserver.Scheme)...)
47+
errs = append(errs, s.Metrics.Validate()...)
48+
errs = append(errs, validateAPIServerIdentity(s)...)
49+
50+
return errs
51+
}

0 commit comments

Comments
 (0)