forked from openshift/origin
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathconfig.go
282 lines (254 loc) · 12.3 KB
/
config.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
package openshiftapiserver
import (
"bytes"
"fmt"
"io/ioutil"
"net/http"
"time"
"github.com/golang/glog"
"k8s.io/apiserver/pkg/admission"
admissionmetrics "k8s.io/apiserver/pkg/admission/metrics"
genericapiserver "k8s.io/apiserver/pkg/server"
genericapiserveroptions "k8s.io/apiserver/pkg/server/options"
cacheddiscovery "k8s.io/client-go/discovery/cached"
"k8s.io/client-go/informers"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/restmapper"
"k8s.io/client-go/tools/cache"
"k8s.io/kubernetes/pkg/api/legacyscheme"
configv1 "github.com/openshift/api/config/v1"
openshiftcontrolplanev1 "github.com/openshift/api/openshiftcontrolplane/v1"
"github.com/openshift/library-go/pkg/config/helpers"
"github.com/openshift/origin/pkg/admission/namespaceconditions"
originadmission "github.com/openshift/origin/pkg/apiserver/admission"
"github.com/openshift/origin/pkg/cmd/openshift-apiserver/openshiftapiserver/configprocessing"
configlatest "github.com/openshift/origin/pkg/cmd/server/apis/config/latest"
"github.com/openshift/origin/pkg/image/apiserver/registryhostname"
usercache "github.com/openshift/origin/pkg/user/cache"
"github.com/openshift/origin/pkg/version"
)
func NewOpenshiftAPIConfig(config *openshiftcontrolplanev1.OpenShiftAPIServerConfig) (*OpenshiftAPIConfig, error) {
kubeClientConfig, err := helpers.GetKubeClientConfig(config.KubeClientConfig)
if err != nil {
return nil, err
}
kubeClient, err := kubernetes.NewForConfig(kubeClientConfig)
if err != nil {
return nil, err
}
kubeInformers := informers.NewSharedInformerFactory(kubeClient, 10*time.Minute)
openshiftVersion := version.Get()
backend, policyChecker, err := configprocessing.GetAuditConfig(config.AuditConfig)
if err != nil {
return nil, err
}
restOptsGetter, err := NewRESTOptionsGetter(config.APIServerArguments, config.StorageConfig)
if err != nil {
return nil, err
}
genericConfig := genericapiserver.NewRecommendedConfig(legacyscheme.Codecs)
// Current default values
//Serializer: codecs,
//ReadWritePort: 443,
//BuildHandlerChainFunc: DefaultBuildHandlerChain,
//HandlerChainWaitGroup: new(utilwaitgroup.SafeWaitGroup),
//LegacyAPIGroupPrefixes: sets.NewString(DefaultLegacyAPIPrefix),
//DisabledPostStartHooks: sets.NewString(),
//HealthzChecks: []healthz.HealthzChecker{healthz.PingHealthz, healthz.LogHealthz},
//EnableIndex: true,
//EnableDiscovery: true,
//EnableProfiling: true,
//EnableMetrics: true,
//MaxRequestsInFlight: 400,
//MaxMutatingRequestsInFlight: 200,
//RequestTimeout: time.Duration(60) * time.Second,
//MinRequestTimeout: 1800,
//EnableAPIResponseCompression: utilfeature.DefaultFeatureGate.Enabled(features.APIResponseCompression),
//LongRunningFunc: genericfilters.BasicLongRunningRequestCheck(sets.NewString("watch"), sets.NewString()),
// TODO this is actually specific to the kubeapiserver
//RuleResolver authorizer.RuleResolver
genericConfig.SharedInformerFactory = kubeInformers
genericConfig.ClientConfig = kubeClientConfig
// these are set via options
//SecureServing *SecureServingInfo
//Authentication AuthenticationInfo
//Authorization AuthorizationInfo
//LoopbackClientConfig *restclient.Config
// this is set after the options are overlayed to get the authorizer we need.
//AdmissionControl admission.Interface
//ReadWritePort int
//PublicAddress net.IP
// these are defaulted sanely during complete
//DiscoveryAddresses discovery.Addresses
genericConfig.CorsAllowedOriginList = config.CORSAllowedOrigins
genericConfig.Version = &openshiftVersion
// we don't use legacy audit anymore
genericConfig.AuditBackend = backend
genericConfig.AuditPolicyChecker = policyChecker
genericConfig.ExternalAddress = "apiserver.openshift-apiserver.svc"
genericConfig.BuildHandlerChainFunc = OpenshiftHandlerChain
genericConfig.RequestInfoResolver = configprocessing.OpenshiftRequestInfoResolver()
genericConfig.OpenAPIConfig = configprocessing.DefaultOpenAPIConfig(nil)
genericConfig.RESTOptionsGetter = restOptsGetter
// previously overwritten. I don't know why
genericConfig.RequestTimeout = time.Duration(60) * time.Second
genericConfig.MinRequestTimeout = int(config.ServingInfo.RequestTimeoutSeconds)
genericConfig.MaxRequestsInFlight = int(config.ServingInfo.MaxRequestsInFlight)
genericConfig.MaxMutatingRequestsInFlight = int(config.ServingInfo.MaxRequestsInFlight / 2)
genericConfig.LongRunningFunc = configprocessing.IsLongRunningRequest
// I'm just hoping this works. I don't think we use it.
//MergedResourceConfig *serverstore.ResourceConfig
servingOptions, err := configprocessing.ToServingOptions(config.ServingInfo)
if err != nil {
return nil, err
}
if err := servingOptions.ApplyTo(&genericConfig.Config.SecureServing, &genericConfig.Config.LoopbackClientConfig); err != nil {
return nil, err
}
authenticationOptions := genericapiserveroptions.NewDelegatingAuthenticationOptions()
// keep working for integration tests
if len(config.AggregatorConfig.ClientCA) > 0 {
authenticationOptions.ClientCert.ClientCA = config.ServingInfo.ClientCA
authenticationOptions.RequestHeader.ClientCAFile = config.AggregatorConfig.ClientCA
authenticationOptions.RequestHeader.AllowedNames = config.AggregatorConfig.AllowedNames
authenticationOptions.RequestHeader.UsernameHeaders = config.AggregatorConfig.UsernameHeaders
authenticationOptions.RequestHeader.GroupHeaders = config.AggregatorConfig.GroupHeaders
authenticationOptions.RequestHeader.ExtraHeaderPrefixes = config.AggregatorConfig.ExtraHeaderPrefixes
}
authenticationOptions.RemoteKubeConfigFile = config.KubeClientConfig.KubeConfig
if err := authenticationOptions.ApplyTo(&genericConfig.Authentication, genericConfig.SecureServing, genericConfig.OpenAPIConfig); err != nil {
return nil, err
}
authorizationOptions := genericapiserveroptions.NewDelegatingAuthorizationOptions().WithAlwaysAllowPaths("/healthz", "/healthz/").WithAlwaysAllowGroups("system:masters")
authorizationOptions.RemoteKubeConfigFile = config.KubeClientConfig.KubeConfig
if err := authorizationOptions.ApplyTo(&genericConfig.Authorization); err != nil {
return nil, err
}
informers, err := NewInformers(kubeInformers, kubeClientConfig, genericConfig.LoopbackClientConfig)
if err != nil {
return nil, err
}
if err := informers.GetOpenshiftUserInformers().User().V1().Groups().Informer().AddIndexers(cache.Indexers{
usercache.ByUserIndexName: usercache.ByUserIndexKeys,
}); err != nil {
return nil, err
}
projectCache, err := NewProjectCache(informers.kubernetesInformers.Core().V1().Namespaces(), kubeClientConfig, config.ProjectConfig.DefaultNodeSelector)
if err != nil {
return nil, err
}
clusterQuotaMappingController := NewClusterQuotaMappingController(informers.kubernetesInformers.Core().V1().Namespaces(), informers.quotaInformers.Quota().InternalVersion().ClusterResourceQuotas())
discoveryClient := cacheddiscovery.NewMemCacheClient(kubeClient.Discovery())
restMapper := restmapper.NewDeferredDiscoveryRESTMapper(discoveryClient)
admissionInitializer, err := originadmission.NewPluginInitializer(config.ImagePolicyConfig.ExternalRegistryHostnames, config.ImagePolicyConfig.InternalRegistryHostname, config.CloudProviderFile, kubeClientConfig, informers, genericConfig.Authorization.Authorizer, projectCache, restMapper, clusterQuotaMappingController)
if err != nil {
return nil, err
}
namespaceLabelDecorator := namespaceconditions.NamespaceLabelConditions{
NamespaceClient: kubeClient.CoreV1(),
NamespaceLister: informers.GetKubernetesInformers().Core().V1().Namespaces().Lister(),
SkipLevelZeroNames: originadmission.SkipRunLevelZeroPlugins,
SkipLevelOneNames: originadmission.SkipRunLevelOnePlugins,
}
admissionDecorators := admission.Decorators{
admission.DecoratorFunc(namespaceLabelDecorator.WithNamespaceLabelConditions),
admission.DecoratorFunc(admissionmetrics.WithControllerMetrics),
}
explicitOn := []string{}
explicitOff := []string{}
for plugin, config := range config.AdmissionPluginConfig {
enabled, err := isAdmissionPluginActivated(config)
if err != nil {
return nil, err
}
if enabled {
glog.V(2).Infof("Enabling %s", plugin)
explicitOn = append(explicitOn, plugin)
} else {
glog.V(2).Infof("Disabling %s", plugin)
explicitOff = append(explicitOff, plugin)
}
}
genericConfig.AdmissionControl, err = originadmission.NewAdmissionChains([]string{}, explicitOn, explicitOff, config.AdmissionPluginConfig, admissionInitializer, admissionDecorators)
if err != nil {
return nil, err
}
var externalRegistryHostname string
if len(config.ImagePolicyConfig.ExternalRegistryHostnames) > 0 {
externalRegistryHostname = config.ImagePolicyConfig.ExternalRegistryHostnames[0]
}
registryHostnameRetriever, err := registryhostname.DefaultRegistryHostnameRetriever(kubeClientConfig, externalRegistryHostname, config.ImagePolicyConfig.InternalRegistryHostname)
if err != nil {
return nil, err
}
imageLimitVerifier := ImageLimitVerifier(informers.internalKubernetesInformers.Core().InternalVersion().LimitRanges())
var caData []byte
if len(config.ImagePolicyConfig.AdditionalTrustedCA) != 0 {
glog.V(2).Infof("Image import using additional CA path: %s", config.ImagePolicyConfig.AdditionalTrustedCA)
var err error
caData, err = ioutil.ReadFile(config.ImagePolicyConfig.AdditionalTrustedCA)
if err != nil {
return nil, fmt.Errorf("failed to read CA bundle %s for image importing: %v", config.ImagePolicyConfig.AdditionalTrustedCA, err)
}
}
subjectLocator := NewSubjectLocator(informers.GetKubernetesInformers().Rbac().V1())
projectAuthorizationCache := NewProjectAuthorizationCache(
subjectLocator,
informers.GetInternalKubernetesInformers().Core().InternalVersion().Namespaces().Informer(),
informers.GetKubernetesInformers().Rbac().V1(),
)
routeAllocator, err := configprocessing.RouteAllocator(config.RoutingConfig.Subdomain)
if err != nil {
return nil, err
}
ruleResolver := NewRuleResolver(informers.kubernetesInformers.Rbac().V1())
ret := &OpenshiftAPIConfig{
GenericConfig: genericConfig,
ExtraConfig: OpenshiftAPIExtraConfig{
InformerStart: informers.Start,
KubeAPIServerClientConfig: kubeClientConfig,
KubeInternalInformers: informers.internalKubernetesInformers,
KubeInformers: kubeInformers, // TODO remove this and use the one from the genericconfig
QuotaInformers: informers.quotaInformers,
SecurityInformers: informers.securityInformers,
RuleResolver: ruleResolver,
SubjectLocator: subjectLocator,
LimitVerifier: imageLimitVerifier,
RegistryHostnameRetriever: registryHostnameRetriever,
AllowedRegistriesForImport: config.ImagePolicyConfig.AllowedRegistriesForImport,
MaxImagesBulkImportedPerRepository: config.ImagePolicyConfig.MaxImagesBulkImportedPerRepository,
AdditionalTrustedCA: caData,
RouteAllocator: routeAllocator,
ProjectAuthorizationCache: projectAuthorizationCache,
ProjectCache: projectCache,
ProjectRequestTemplate: config.ProjectConfig.ProjectRequestTemplate,
ProjectRequestMessage: config.ProjectConfig.ProjectRequestMessage,
ClusterQuotaMappingController: clusterQuotaMappingController,
RESTMapper: restMapper,
ServiceAccountMethod: string(config.ServiceAccountOAuthGrantMethod),
},
}
return ret, ret.ExtraConfig.Validate()
}
func OpenshiftHandlerChain(apiHandler http.Handler, genericConfig *genericapiserver.Config) http.Handler {
// this is the normal kube handler chain
handler := genericapiserver.DefaultBuildHandlerChain(apiHandler, genericConfig)
handler = configprocessing.WithCacheControl(handler, "no-store") // protected endpoints should not be cached
return handler
}
func isAdmissionPluginActivated(config configv1.AdmissionPluginConfig) (bool, error) {
var (
data []byte
err error
)
switch {
case len(config.Configuration.Raw) == 0:
data, err = ioutil.ReadFile(config.Location)
default:
data = config.Configuration.Raw
}
if err != nil {
return false, err
}
return configlatest.IsAdmissionPluginActivated(bytes.NewReader(data), true)
}