/
master.go
372 lines (324 loc) · 14.9 KB
/
master.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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
package origin
import (
"fmt"
"net/http"
"net/url"
"os"
"strings"
"time"
"github.com/golang/glog"
apiextensionsinformers "k8s.io/apiextensions-apiserver/pkg/client/informers/internalversion"
apiserver "k8s.io/apiserver/pkg/server"
aggregatorapiserver "k8s.io/kube-aggregator/pkg/apiserver"
kubeapiserver "k8s.io/kubernetes/pkg/master"
kcorestorage "k8s.io/kubernetes/pkg/registry/core/rest"
assetapiserver "github.com/openshift/origin/pkg/assets/apiserver"
configapi "github.com/openshift/origin/pkg/cmd/server/api"
"github.com/openshift/origin/pkg/cmd/server/bootstrappolicy"
serverhandlers "github.com/openshift/origin/pkg/cmd/server/handlers"
kubernetes "github.com/openshift/origin/pkg/cmd/server/kubernetes/master"
cmdutil "github.com/openshift/origin/pkg/cmd/util"
"github.com/openshift/origin/pkg/cmd/util/plug"
oauthutil "github.com/openshift/origin/pkg/oauth/util"
routeplugin "github.com/openshift/origin/pkg/route/allocation/simple"
routeallocationcontroller "github.com/openshift/origin/pkg/route/controller/allocation"
sccstorage "github.com/openshift/origin/pkg/security/registry/securitycontextconstraints/etcd"
)
const (
openShiftOAuthAPIPrefix = "/oauth"
openShiftLoginPrefix = "/login"
openShiftOAuthCallbackPrefix = "/oauth2callback"
)
func (c *MasterConfig) newOpenshiftAPIConfig(kubeAPIServerConfig apiserver.Config) (*OpenshiftAPIConfig, error) {
// sccStorage must use the upstream RESTOptionsGetter to be in the correct location
// this probably creates a duplicate cache, but there are not very many SCCs, so live with it to avoid further linkage
sccStorage := sccstorage.NewREST(kubeAPIServerConfig.RESTOptionsGetter)
// make a shallow copy to let us twiddle a few things
// most of the config actually remains the same. We only need to mess with a couple items
genericConfig := kubeAPIServerConfig
// TODO try to stop special casing these. We should all agree on them.
genericConfig.RESTOptionsGetter = c.RESTOptionsGetter
ret := &OpenshiftAPIConfig{
GenericConfig: &apiserver.RecommendedConfig{Config: genericConfig},
ExtraConfig: OpenshiftAPIExtraConfig{
KubeClientInternal: c.PrivilegedLoopbackKubernetesClientsetInternal,
KubeletClientConfig: c.KubeletClientConfig,
KubeInternalInformers: c.InternalKubeInformers,
QuotaInformers: c.QuotaInformers,
SecurityInformers: c.SecurityInformers,
RuleResolver: c.RuleResolver,
SubjectLocator: c.SubjectLocator,
LimitVerifier: c.LimitVerifier,
RegistryHostnameRetriever: c.RegistryHostnameRetriever,
AllowedRegistriesForImport: c.Options.ImagePolicyConfig.AllowedRegistriesForImport,
MaxImagesBulkImportedPerRepository: c.Options.ImagePolicyConfig.MaxImagesBulkImportedPerRepository,
RouteAllocator: c.RouteAllocator(),
ProjectAuthorizationCache: c.ProjectAuthorizationCache,
ProjectCache: c.ProjectCache,
ProjectRequestTemplate: c.Options.ProjectConfig.ProjectRequestTemplate,
ProjectRequestMessage: c.Options.ProjectConfig.ProjectRequestMessage,
EnableBuilds: configapi.IsBuildEnabled(&c.Options),
ClusterQuotaMappingController: c.ClusterQuotaMappingController,
SCCStorage: sccStorage,
},
}
if c.Options.OAuthConfig != nil {
ret.ExtraConfig.ServiceAccountMethod = c.Options.OAuthConfig.GrantConfig.ServiceAccountMethod
}
return ret, ret.ExtraConfig.Validate()
}
func (c *MasterConfig) newOpenshiftNonAPIConfig(kubeAPIServerConfig apiserver.Config, controllerPlug plug.Plug) *OpenshiftNonAPIConfig {
ret := &OpenshiftNonAPIConfig{
GenericConfig: &apiserver.RecommendedConfig{
Config: kubeAPIServerConfig,
SharedInformerFactory: c.ClientGoKubeInformers,
},
ExtraConfig: NonAPIExtraConfig{
ControllerPlug: controllerPlug,
EnableOAuth: c.Options.OAuthConfig != nil,
},
}
if c.Options.OAuthConfig != nil {
ret.ExtraConfig.MasterPublicURL = c.Options.OAuthConfig.MasterPublicURL
}
return ret
}
func (c *MasterConfig) withAPIExtensions(delegateAPIServer apiserver.DelegationTarget, kubeAPIServerConfig apiserver.Config) (apiserver.DelegationTarget, apiextensionsinformers.SharedInformerFactory, error) {
kubeAPIServerOptions, err := kubernetes.BuildKubeAPIserverOptions(c.Options)
if err != nil {
return nil, nil, err
}
apiExtensionsConfig, err := createAPIExtensionsConfig(kubeAPIServerConfig, kubeAPIServerOptions.Etcd)
if err != nil {
return nil, nil, err
}
apiExtensionsServer, err := createAPIExtensionsServer(apiExtensionsConfig, delegateAPIServer)
if err != nil {
return nil, nil, err
}
return apiExtensionsServer.GenericAPIServer, apiExtensionsServer.Informers, nil
}
func (c *MasterConfig) withNonAPIRoutes(delegateAPIServer apiserver.DelegationTarget, kubeAPIServerConfig apiserver.Config, controllerPlug plug.Plug) (apiserver.DelegationTarget, error) {
openshiftNonAPIConfig := c.newOpenshiftNonAPIConfig(kubeAPIServerConfig, controllerPlug)
openshiftNonAPIServer, err := openshiftNonAPIConfig.Complete().New(delegateAPIServer)
if err != nil {
return nil, err
}
return openshiftNonAPIServer.GenericAPIServer, nil
}
func (c *MasterConfig) withOpenshiftAPI(delegateAPIServer apiserver.DelegationTarget, kubeAPIServerConfig apiserver.Config) (apiserver.DelegationTarget, error) {
openshiftAPIServerConfig, err := c.newOpenshiftAPIConfig(kubeAPIServerConfig)
if err != nil {
return nil, err
}
// We need to add an openshift type to the kube's core storage until at least 3.8. This does that by using a patch we carry.
kcorestorage.LegacyStorageMutatorFn = sccstorage.AddSCC(openshiftAPIServerConfig.ExtraConfig.SCCStorage)
openshiftAPIServer, err := openshiftAPIServerConfig.Complete().New(delegateAPIServer)
if err != nil {
return nil, err
}
// this sets up the openapi endpoints
preparedOpenshiftAPIServer := openshiftAPIServer.GenericAPIServer.PrepareRun()
return preparedOpenshiftAPIServer.GenericAPIServer, nil
}
func (c *MasterConfig) withKubeAPI(delegateAPIServer apiserver.DelegationTarget, kubeAPIServerConfig kubeapiserver.Config) (apiserver.DelegationTarget, error) {
var err error
if err != nil {
return nil, err
}
kubeAPIServer, err := kubeAPIServerConfig.Complete(c.ClientGoKubeInformers).New(delegateAPIServer)
if err != nil {
return nil, err
}
// this sets up the openapi endpoints
preparedKubeAPIServer := kubeAPIServer.GenericAPIServer.PrepareRun()
return preparedKubeAPIServer.GenericAPIServer, nil
}
func (c *MasterConfig) newAssetServerHandler(genericConfig *apiserver.Config) (http.Handler, error) {
if !c.WebConsoleEnabled() || c.WebConsoleStandalone() {
return http.NotFoundHandler(), nil
}
config, err := assetapiserver.NewAssetServerConfig(*c.Options.AssetConfig, genericConfig.SecureServingInfo.Listener)
if err != nil {
return nil, err
}
config.GenericConfig.AuditBackend = genericConfig.AuditBackend
config.GenericConfig.AuditPolicyChecker = genericConfig.AuditPolicyChecker
assetServer, err := config.Complete().New(apiserver.EmptyDelegate)
if err != nil {
return nil, err
}
return assetServer.GenericAPIServer.PrepareRun().GenericAPIServer.Handler.FullHandlerChain, nil
}
func (c *MasterConfig) newOAuthServerHandler(genericConfig *apiserver.Config) (http.Handler, map[string]apiserver.PostStartHookFunc, error) {
if c.Options.OAuthConfig == nil {
return http.NotFoundHandler(), nil, nil
}
config, err := NewOAuthServerConfigFromMasterConfig(c, genericConfig.SecureServingInfo.Listener)
if err != nil {
return nil, nil, err
}
config.GenericConfig.AuditBackend = genericConfig.AuditBackend
config.GenericConfig.AuditPolicyChecker = genericConfig.AuditPolicyChecker
oauthServer, err := config.Complete().New(apiserver.EmptyDelegate)
if err != nil {
return nil, nil, err
}
return oauthServer.GenericAPIServer.PrepareRun().GenericAPIServer.Handler.FullHandlerChain,
map[string]apiserver.PostStartHookFunc{
"oauth.openshift.io-EnsureBootstrapOAuthClients": config.EnsureBootstrapOAuthClients,
},
nil
}
func (c *MasterConfig) withAggregator(delegateAPIServer apiserver.DelegationTarget, kubeAPIServerConfig apiserver.Config, apiExtensionsInformers apiextensionsinformers.SharedInformerFactory) (*aggregatorapiserver.APIAggregator, error) {
aggregatorConfig, err := c.createAggregatorConfig(kubeAPIServerConfig)
if err != nil {
return nil, err
}
aggregatorServer, err := createAggregatorServer(aggregatorConfig, delegateAPIServer, c.InternalKubeInformers, apiExtensionsInformers)
if err != nil {
// we don't need special handling for innerStopCh because the aggregator server doesn't create any go routines
return nil, err
}
return aggregatorServer, nil
}
// Run launches the OpenShift master by creating a kubernetes master, installing
// OpenShift APIs into it and then running it.
func (c *MasterConfig) Run(controllerPlug plug.Plug, stopCh <-chan struct{}) error {
var err error
var apiExtensionsInformers apiextensionsinformers.SharedInformerFactory
var delegateAPIServer apiserver.DelegationTarget
var extraPostStartHooks map[string]apiserver.PostStartHookFunc
c.kubeAPIServerConfig.GenericConfig.BuildHandlerChainFunc, extraPostStartHooks, err = c.buildHandlerChain(c.kubeAPIServerConfig.GenericConfig)
if err != nil {
return err
}
delegateAPIServer = apiserver.EmptyDelegate
delegateAPIServer, apiExtensionsInformers, err = c.withAPIExtensions(delegateAPIServer, *c.kubeAPIServerConfig.GenericConfig)
if err != nil {
return err
}
delegateAPIServer, err = c.withNonAPIRoutes(delegateAPIServer, *c.kubeAPIServerConfig.GenericConfig, controllerPlug)
if err != nil {
return err
}
delegateAPIServer, err = c.withOpenshiftAPI(delegateAPIServer, *c.kubeAPIServerConfig.GenericConfig)
if err != nil {
return err
}
delegateAPIServer, err = c.withKubeAPI(delegateAPIServer, *c.kubeAPIServerConfig)
if err != nil {
return err
}
aggregatedAPIServer, err := c.withAggregator(delegateAPIServer, *c.kubeAPIServerConfig.GenericConfig, apiExtensionsInformers)
if err != nil {
return err
}
// Start the audit backend before any request comes in. This means we cannot turn it into a
// post start hook because without calling Backend.Run the Backend.ProcessEvents call might block.
if c.AuditBackend != nil {
if err := c.AuditBackend.Run(stopCh); err != nil {
return fmt.Errorf("failed to run the audit backend: %v", err)
}
}
// add post-start hooks
aggregatedAPIServer.GenericAPIServer.AddPostStartHookOrDie("template.openshift.io-sharednamespace", c.ensureOpenShiftSharedResourcesNamespace)
aggregatedAPIServer.GenericAPIServer.AddPostStartHookOrDie("authorization.openshift.io-bootstrapclusterroles", bootstrappolicy.Policy().EnsureRBACPolicy())
for name, fn := range c.additionalPostStartHooks {
aggregatedAPIServer.GenericAPIServer.AddPostStartHookOrDie(name, fn)
}
for name, fn := range extraPostStartHooks {
aggregatedAPIServer.GenericAPIServer.AddPostStartHookOrDie(name, fn)
}
go aggregatedAPIServer.GenericAPIServer.PrepareRun().Run(stopCh)
// Attempt to verify the server came up for 20 seconds (100 tries * 100ms, 100ms timeout per try)
return cmdutil.WaitForSuccessfulDial(true, c.Options.ServingInfo.BindNetwork, c.Options.ServingInfo.BindAddress, 100*time.Millisecond, 100*time.Millisecond, 100)
}
func (c *MasterConfig) buildHandlerChain(genericConfig *apiserver.Config) (func(apiHandler http.Handler, kc *apiserver.Config) http.Handler, map[string]apiserver.PostStartHookFunc, error) {
assetServerHandler, err := c.newAssetServerHandler(genericConfig)
if err != nil {
return nil, nil, err
}
oauthServerHandler, extraPostStartHooks, err := c.newOAuthServerHandler(genericConfig)
if err != nil {
return nil, nil, err
}
return func(apiHandler http.Handler, genericConfig *apiserver.Config) http.Handler {
// these are after the kube handler
handler := c.versionSkewFilter(apiHandler, genericConfig.RequestContextMapper)
handler = namespacingFilter(handler, genericConfig.RequestContextMapper)
// this is the normal kube handler chain
handler = apiserver.DefaultBuildHandlerChain(handler, genericConfig)
// these handlers are all before the normal kube chain
handler = serverhandlers.TranslateLegacyScopeImpersonation(handler)
handler = cacheControlFilter(handler, "no-store") // protected endpoints should not be cached
if c.WebConsoleEnabled() {
handler = assetapiserver.WithAssetServerRedirect(handler, c.Options.AssetConfig.PublicURL)
}
// these handlers are actually separate API servers which have their own handler chains.
// our server embeds these
handler = c.withConsoleRedirection(handler, assetServerHandler, c.Options.AssetConfig)
handler = c.withOAuthRedirection(handler, oauthServerHandler)
return handler
},
extraPostStartHooks,
nil
}
func (c *MasterConfig) withConsoleRedirection(handler, assetServerHandler http.Handler, assetConfig *configapi.AssetConfig) http.Handler {
if assetConfig == nil {
return handler
}
if !c.WebConsoleEnabled() || c.WebConsoleStandalone() {
return handler
}
publicURL, err := url.Parse(assetConfig.PublicURL)
if err != nil {
// fails validation before here
glog.Fatal(err)
}
// path always ends in a slash or the
prefix := publicURL.Path
lastIndex := len(publicURL.Path) - 1
if publicURL.Path[lastIndex] == '/' {
prefix = publicURL.Path[0:lastIndex]
}
glog.Infof("Starting Web Console %s", assetConfig.PublicURL)
return WithPatternPrefixHandler(handler, assetServerHandler, prefix)
}
func (c *MasterConfig) withOAuthRedirection(handler, oauthServerHandler http.Handler) http.Handler {
if c.Options.OAuthConfig == nil {
return handler
}
glog.Infof("Starting OAuth2 API at %s", oauthutil.OpenShiftOAuthAPIPrefix)
return WithPatternPrefixHandler(handler, oauthServerHandler, openShiftOAuthAPIPrefix, openShiftLoginPrefix, openShiftOAuthCallbackPrefix)
}
// RouteAllocator returns a route allocation controller.
func (c *MasterConfig) RouteAllocator() *routeallocationcontroller.RouteAllocationController {
factory := routeallocationcontroller.RouteAllocationControllerFactory{
KubeClient: c.PrivilegedLoopbackKubernetesClientsetInternal,
}
plugin, err := routeplugin.NewSimpleAllocationPlugin(c.Options.RoutingConfig.Subdomain)
if err != nil {
glog.Fatalf("Route plugin initialization failed: %v", err)
}
return factory.Create(plugin)
}
// env returns an environment variable, or the defaultValue if it is not set.
func env(key string, defaultValue string) string {
val := os.Getenv(key)
if len(val) == 0 {
return defaultValue
}
return val
}
func WithPatternPrefixHandler(handler http.Handler, patternHandler http.Handler, prefixes ...string) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
for _, p := range prefixes {
if strings.HasPrefix(req.URL.Path, p) {
patternHandler.ServeHTTP(w, req)
return
}
}
handler.ServeHTTP(w, req)
})
}