| 1 |
/* |
| 2 |
Copyright 2016 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 |
"github.com/spf13/pflag" |
| 21 |
"k8s.io/apimachinery/pkg/runtime" |
| 22 |
"k8s.io/apiserver/pkg/admission" |
| 23 |
"k8s.io/apiserver/pkg/server" |
| 24 |
"k8s.io/apiserver/pkg/storage/storagebackend" |
| 25 |
"k8s.io/apiserver/pkg/util/feature" |
| 26 |
"k8s.io/client-go/dynamic" |
| 27 |
"k8s.io/client-go/kubernetes" |
| 28 |
"k8s.io/component-base/featuregate" |
| 29 |
) |
| 30 |
|
| 31 |
// RecommendedOptions contains the recommended options for running an API server. |
| 32 |
// If you add something to this list, it should be in a logical grouping. |
| 33 |
// Each of them can be nil to leave the feature unconfigured on ApplyTo. |
| 34 |
type RecommendedOptions struct { |
| 35 |
Etcd *EtcdOptions |
| 36 |
SecureServing *SecureServingOptionsWithLoopback |
| 37 |
Authentication *DelegatingAuthenticationOptions |
| 38 |
Authorization *DelegatingAuthorizationOptions |
| 39 |
Audit *AuditOptions |
| 40 |
Features *FeatureOptions |
| 41 |
CoreAPI *CoreAPIOptions |
| 42 |
|
| 43 |
// FeatureGate is a way to plumb feature gate through if you have them. |
| 44 |
FeatureGate featuregate.FeatureGate |
| 45 |
// ExtraAdmissionInitializers is called once after all ApplyTo from the options above, to pass the returned |
| 46 |
// admission plugin initializers to Admission.ApplyTo. |
| 47 |
ExtraAdmissionInitializers func(c *server.RecommendedConfig) ([]admission.PluginInitializer, error) |
| 48 |
Admission *AdmissionOptions |
| 49 |
// API Server Egress Selector is used to control outbound traffic from the API Server |
| 50 |
EgressSelector *EgressSelectorOptions |
| 51 |
// Traces contains options to control distributed request tracing. |
| 52 |
Traces *TracingOptions |
| 53 |
} |
| 54 |
|
| 55 |
func NewRecommendedOptions(prefix string, codec runtime.Codec) *RecommendedOptions { |
| 56 |
sso := NewSecureServingOptions() |
| 57 |
|
| 58 |
// We are composing recommended options for an aggregated api-server, |
| 59 |
// whose client is typically a proxy multiplexing many operations --- |
| 60 |
// notably including long-running ones --- into one HTTP/2 connection |
| 61 |
// into this server. So allow many concurrent operations. |
| 62 |
sso.HTTP2MaxStreamsPerConnection = 1000 |
| 63 |
|
| 64 |
return &RecommendedOptions{ |
| 65 |
Etcd: NewEtcdOptions(storagebackend.NewDefaultConfig(prefix, codec)), |
| 66 |
SecureServing: sso.WithLoopback(), |
| 67 |
Authentication: NewDelegatingAuthenticationOptions(), |
| 68 |
Authorization: NewDelegatingAuthorizationOptions(), |
| 69 |
Audit: NewAuditOptions(), |
| 70 |
Features: NewFeatureOptions(), |
| 71 |
CoreAPI: NewCoreAPIOptions(), |
| 72 |
// Wired a global by default that sadly people will abuse to have different meanings in different repos. |
| 73 |
// Please consider creating your own FeatureGate so you can have a consistent meaning for what a variable contains |
| 74 |
// across different repos. Future you will thank you. |
| 75 |
FeatureGate: feature.DefaultFeatureGate, |
| 76 |
ExtraAdmissionInitializers: func(c *server.RecommendedConfig) ([]admission.PluginInitializer, error) { return nil, nil }, |
| 77 |
Admission: NewAdmissionOptions(), |
| 78 |
EgressSelector: NewEgressSelectorOptions(), |
| 79 |
Traces: NewTracingOptions(), |
| 80 |
} |
| 81 |
} |
| 82 |
|
| 83 |
func (o *RecommendedOptions) AddFlags(fs *pflag.FlagSet) { |
| 84 |
o.Etcd.AddFlags(fs) |
| 85 |
o.SecureServing.AddFlags(fs) |
| 86 |
o.Authentication.AddFlags(fs) |
| 87 |
o.Authorization.AddFlags(fs) |
| 88 |
o.Audit.AddFlags(fs) |
| 89 |
o.Features.AddFlags(fs) |
| 90 |
o.CoreAPI.AddFlags(fs) |
| 91 |
o.Admission.AddFlags(fs) |
| 92 |
o.EgressSelector.AddFlags(fs) |
| 93 |
o.Traces.AddFlags(fs) |
| 94 |
} |
| 95 |
|
| 96 |
// ApplyTo adds RecommendedOptions to the server configuration. |
| 97 |
// pluginInitializers can be empty, it is only need for additional initializers. |
| 98 |
func (o *RecommendedOptions) ApplyTo(config *server.RecommendedConfig) error { |
| 99 |
if err := o.Etcd.ApplyTo(&config.Config); err != nil { |
| 100 |
return err |
| 101 |
} |
| 102 |
if err := o.EgressSelector.ApplyTo(&config.Config); err != nil { |
| 103 |
return err |
| 104 |
} |
| 105 |
if err := o.Traces.ApplyTo(config.Config.EgressSelector, &config.Config); err != nil { |
| 106 |
return err |
| 107 |
} |
| 108 |
if err := o.SecureServing.ApplyTo(&config.Config.SecureServing, &config.Config.LoopbackClientConfig); err != nil { |
| 109 |
return err |
| 110 |
} |
| 111 |
if err := o.Authentication.ApplyTo(&config.Config.Authentication, config.SecureServing, config.OpenAPIConfig); err != nil { |
| 112 |
return err |
| 113 |
} |
| 114 |
if err := o.Authorization.ApplyTo(&config.Config.Authorization); err != nil { |
| 115 |
return err |
| 116 |
} |
| 117 |
if err := o.Audit.ApplyTo(&config.Config); err != nil { |
| 118 |
return err |
| 119 |
} |
| 120 |
if err := o.CoreAPI.ApplyTo(config); err != nil { |
| 121 |
return err |
| 122 |
} |
| 123 |
var kubeClient kubernetes.Interface |
| 124 |
var dynamicClient dynamic.Interface |
| 125 |
if config.ClientConfig != nil { |
| 126 |
var err error |
| 127 |
kubeClient, err = kubernetes.NewForConfig(config.ClientConfig) |
| 128 |
if err != nil { |
| 129 |
return err |
| 130 |
} |
| 131 |
dynamicClient, err = dynamic.NewForConfig(config.ClientConfig) |
| 132 |
if err != nil { |
| 133 |
return err |
| 134 |
} |
| 135 |
} |
| 136 |
if err := o.Features.ApplyTo(&config.Config, kubeClient, config.SharedInformerFactory); err != nil { |
| 137 |
return err |
| 138 |
} |
| 139 |
initializers, err := o.ExtraAdmissionInitializers(config) |
| 140 |
if err != nil { |
| 141 |
return err |
| 142 |
} |
| 143 |
if err := o.Admission.ApplyTo(&config.Config, config.SharedInformerFactory, kubeClient, dynamicClient, o.FeatureGate, |
| 144 |
initializers...); err != nil { |
| 145 |
return err |
| 146 |
} |
| 147 |
return nil |
| 148 |
} |
| 149 |
|
| 150 |
func (o *RecommendedOptions) Validate() []error { |
| 151 |
errors := []error{} |
| 152 |
errors = append(errors, o.Etcd.Validate()...) |
| 153 |
errors = append(errors, o.SecureServing.Validate()...) |
| 154 |
errors = append(errors, o.Authentication.Validate()...) |
| 155 |
errors = append(errors, o.Authorization.Validate()...) |
| 156 |
errors = append(errors, o.Audit.Validate()...) |
| 157 |
errors = append(errors, o.Features.Validate()...) |
| 158 |
errors = append(errors, o.CoreAPI.Validate()...) |
| 159 |
errors = append(errors, o.Admission.Validate()...) |
| 160 |
errors = append(errors, o.EgressSelector.Validate()...) |
| 161 |
errors = append(errors, o.Traces.Validate()...) |
| 162 |
|
| 163 |
return errors |
| 164 |
} |