forked from projectatomic/atomic-enterprise
-
Notifications
You must be signed in to change notification settings - Fork 0
/
master.go
971 lines (842 loc) · 38.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
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
package origin
import (
"bytes"
"crypto/tls"
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"net"
"net/http"
"os"
"regexp"
"strings"
"time"
"bitbucket.org/ww/goautoneg"
etcdclient "github.com/coreos/go-etcd/etcd"
restful "github.com/emicklei/go-restful"
"github.com/emicklei/go-restful/swagger"
"github.com/golang/glog"
"github.com/GoogleCloudPlatform/kubernetes/pkg/api"
kapi "github.com/GoogleCloudPlatform/kubernetes/pkg/api"
kapierror "github.com/GoogleCloudPlatform/kubernetes/pkg/api/errors"
klatest "github.com/GoogleCloudPlatform/kubernetes/pkg/api/latest"
"github.com/GoogleCloudPlatform/kubernetes/pkg/api/rest"
"github.com/GoogleCloudPlatform/kubernetes/pkg/apiserver"
kclient "github.com/GoogleCloudPlatform/kubernetes/pkg/client"
kmaster "github.com/GoogleCloudPlatform/kubernetes/pkg/master"
"github.com/GoogleCloudPlatform/kubernetes/pkg/util"
"github.com/openshift/origin/pkg/api/latest"
"github.com/openshift/origin/pkg/api/v1beta1"
"github.com/openshift/origin/pkg/api/v1beta3"
buildclient "github.com/openshift/origin/pkg/build/client"
buildcontrollerfactory "github.com/openshift/origin/pkg/build/controller/factory"
buildstrategy "github.com/openshift/origin/pkg/build/controller/strategy"
buildgenerator "github.com/openshift/origin/pkg/build/generator"
buildregistry "github.com/openshift/origin/pkg/build/registry/build"
buildetcd "github.com/openshift/origin/pkg/build/registry/build/etcd"
buildconfigregistry "github.com/openshift/origin/pkg/build/registry/buildconfig"
buildconfigetcd "github.com/openshift/origin/pkg/build/registry/buildconfig/etcd"
buildlogregistry "github.com/openshift/origin/pkg/build/registry/buildlog"
"github.com/openshift/origin/pkg/build/webhook"
"github.com/openshift/origin/pkg/build/webhook/generic"
"github.com/openshift/origin/pkg/build/webhook/github"
cmdutil "github.com/openshift/origin/pkg/cmd/util"
"github.com/openshift/origin/pkg/cmd/util/clientcmd"
configchangecontroller "github.com/openshift/origin/pkg/deploy/controller/configchange"
deployerpodcontroller "github.com/openshift/origin/pkg/deploy/controller/deployerpod"
deploycontroller "github.com/openshift/origin/pkg/deploy/controller/deployment"
deployconfigcontroller "github.com/openshift/origin/pkg/deploy/controller/deploymentconfig"
imagechangecontroller "github.com/openshift/origin/pkg/deploy/controller/imagechange"
deployconfiggenerator "github.com/openshift/origin/pkg/deploy/generator"
deployregistry "github.com/openshift/origin/pkg/deploy/registry/deploy"
deployconfigregistry "github.com/openshift/origin/pkg/deploy/registry/deployconfig"
deployetcd "github.com/openshift/origin/pkg/deploy/registry/etcd"
deployrollback "github.com/openshift/origin/pkg/deploy/rollback"
"github.com/openshift/origin/pkg/dns"
imagecontroller "github.com/openshift/origin/pkg/image/controller"
"github.com/openshift/origin/pkg/image/registry/image"
imageetcd "github.com/openshift/origin/pkg/image/registry/image/etcd"
"github.com/openshift/origin/pkg/image/registry/imagerepository"
"github.com/openshift/origin/pkg/image/registry/imagerepositorymapping"
"github.com/openshift/origin/pkg/image/registry/imagerepositorytag"
"github.com/openshift/origin/pkg/image/registry/imagestream"
imagestreametcd "github.com/openshift/origin/pkg/image/registry/imagestream/etcd"
"github.com/openshift/origin/pkg/image/registry/imagestreamimage"
"github.com/openshift/origin/pkg/image/registry/imagestreammapping"
"github.com/openshift/origin/pkg/image/registry/imagestreamtag"
accesstokenetcd "github.com/openshift/origin/pkg/oauth/registry/oauthaccesstoken/etcd"
authorizetokenetcd "github.com/openshift/origin/pkg/oauth/registry/oauthauthorizetoken/etcd"
clientetcd "github.com/openshift/origin/pkg/oauth/registry/oauthclient/etcd"
clientauthetcd "github.com/openshift/origin/pkg/oauth/registry/oauthclientauthorization/etcd"
projectapi "github.com/openshift/origin/pkg/project/api"
projectcache "github.com/openshift/origin/pkg/project/cache"
projectcontroller "github.com/openshift/origin/pkg/project/controller"
projectproxy "github.com/openshift/origin/pkg/project/registry/project/proxy"
projectrequeststorage "github.com/openshift/origin/pkg/project/registry/projectrequest/delegated"
routeallocationcontroller "github.com/openshift/origin/pkg/route/controller/allocation"
routeetcd "github.com/openshift/origin/pkg/route/registry/etcd"
routeregistry "github.com/openshift/origin/pkg/route/registry/route"
clusternetworketcd "github.com/openshift/origin/pkg/sdn/registry/clusternetwork/etcd"
hostsubnetetcd "github.com/openshift/origin/pkg/sdn/registry/hostsubnet/etcd"
"github.com/openshift/origin/pkg/service"
templateregistry "github.com/openshift/origin/pkg/template/registry"
templateetcd "github.com/openshift/origin/pkg/template/registry/etcd"
identityregistry "github.com/openshift/origin/pkg/user/registry/identity"
identityetcd "github.com/openshift/origin/pkg/user/registry/identity/etcd"
userregistry "github.com/openshift/origin/pkg/user/registry/user"
useretcd "github.com/openshift/origin/pkg/user/registry/user/etcd"
"github.com/openshift/origin/pkg/user/registry/useridentitymapping"
authorizationapi "github.com/openshift/origin/pkg/authorization/api"
clusterpolicyregistry "github.com/openshift/origin/pkg/authorization/registry/clusterpolicy"
clusterpolicystorage "github.com/openshift/origin/pkg/authorization/registry/clusterpolicy/etcd"
clusterpolicybindingregistry "github.com/openshift/origin/pkg/authorization/registry/clusterpolicybinding"
clusterpolicybindingstorage "github.com/openshift/origin/pkg/authorization/registry/clusterpolicybinding/etcd"
clusterrolestorage "github.com/openshift/origin/pkg/authorization/registry/clusterrole/proxy"
clusterrolebindingstorage "github.com/openshift/origin/pkg/authorization/registry/clusterrolebinding/proxy"
policyregistry "github.com/openshift/origin/pkg/authorization/registry/policy"
policyetcd "github.com/openshift/origin/pkg/authorization/registry/policy/etcd"
policybindingregistry "github.com/openshift/origin/pkg/authorization/registry/policybinding"
policybindingetcd "github.com/openshift/origin/pkg/authorization/registry/policybinding/etcd"
resourceaccessreviewregistry "github.com/openshift/origin/pkg/authorization/registry/resourceaccessreview"
rolestorage "github.com/openshift/origin/pkg/authorization/registry/role/policybased"
rolebindingstorage "github.com/openshift/origin/pkg/authorization/registry/rolebinding/policybased"
"github.com/openshift/origin/pkg/authorization/registry/subjectaccessreview"
"github.com/openshift/origin/pkg/cmd/server/admin"
configapi "github.com/openshift/origin/pkg/cmd/server/api"
"github.com/openshift/origin/plugins/osdn"
routeplugin "github.com/openshift/origin/plugins/route/allocation/simple"
)
const (
OpenShiftAPIPrefix = "/osapi" // TODO: make configurable
KubernetesAPIPrefix = "/api" // TODO: make configurable
OpenShiftAPIV1Beta1 = "v1beta1"
OpenShiftAPIV1Beta3 = "v1beta3"
OpenShiftAPIPrefixV1Beta1 = OpenShiftAPIPrefix + "/" + OpenShiftAPIV1Beta1
OpenShiftAPIPrefixV1Beta3 = OpenShiftAPIPrefix + "/" + OpenShiftAPIV1Beta3
OpenShiftRouteSubdomain = "router.default.local"
swaggerAPIPrefix = "/swaggerapi/"
)
var excludedV1Beta3Types = util.NewStringSet(
"templateConfigs", "deployments", "buildLogs",
"imageRepositories", "imageRepositories/status", "imageRepositoryMappings", "imageRepositoryTags",
)
// APIInstaller installs additional API components into this server
type APIInstaller interface {
// Returns an array of strings describing what was installed
InstallAPI(*restful.Container) []string
}
// APIInstallFunc is a function for installing APIs
type APIInstallFunc func(*restful.Container) []string
// InstallAPI implements APIInstaller
func (fn APIInstallFunc) InstallAPI(container *restful.Container) []string {
return fn(container)
}
func (c *MasterConfig) InstallProtectedAPI(container *restful.Container) []string {
defaultRegistry := env("OPENSHIFT_DEFAULT_REGISTRY", "${DOCKER_REGISTRY_SERVICE_HOST}:${DOCKER_REGISTRY_SERVICE_PORT}")
svcCache := service.NewServiceResolverCache(c.KubeClient().Services(api.NamespaceDefault).Get)
defaultRegistryFunc, err := svcCache.Defer(defaultRegistry)
if err != nil {
glog.Fatalf("OPENSHIFT_DEFAULT_REGISTRY variable is invalid %q: %v", defaultRegistry, err)
}
kubeletClient, err := kclient.NewKubeletClient(c.KubeletClientConfig)
if err != nil {
glog.Fatalf("Unable to configure Kubelet client: %v", err)
}
buildStorage := buildetcd.NewStorage(c.EtcdHelper)
buildRegistry := buildregistry.NewRegistry(buildStorage)
buildConfigStorage := buildconfigetcd.NewStorage(c.EtcdHelper)
buildConfigRegistry := buildconfigregistry.NewRegistry(buildConfigStorage)
deployEtcd := deployetcd.New(c.EtcdHelper)
routeEtcd := routeetcd.New(c.EtcdHelper)
hostSubnetStorage := hostsubnetetcd.NewREST(c.EtcdHelper)
clusterNetworkStorage := clusternetworketcd.NewREST(c.EtcdHelper)
userStorage := useretcd.NewREST(c.EtcdHelper)
userRegistry := userregistry.NewRegistry(userStorage)
identityStorage := identityetcd.NewREST(c.EtcdHelper)
identityRegistry := identityregistry.NewRegistry(identityStorage)
userIdentityMappingStorage := useridentitymapping.NewREST(userRegistry, identityRegistry)
policyStorage := policyetcd.NewStorage(c.EtcdHelper)
policyRegistry := policyregistry.NewRegistry(policyStorage)
policyBindingStorage := policybindingetcd.NewStorage(c.EtcdHelper)
policyBindingRegistry := policybindingregistry.NewRegistry(policyBindingStorage)
clusterPolicyStorage := clusterpolicystorage.NewStorage(c.EtcdHelper)
clusterPolicyRegistry := clusterpolicyregistry.NewRegistry(clusterPolicyStorage)
clusterPolicyBindingStorage := clusterpolicybindingstorage.NewStorage(c.EtcdHelper)
clusterPolicyBindingRegistry := clusterpolicybindingregistry.NewRegistry(clusterPolicyBindingStorage)
roleStorage := rolestorage.NewVirtualStorage(policyRegistry)
roleBindingStorage := rolebindingstorage.NewVirtualStorage(policyRegistry, policyBindingRegistry, clusterPolicyRegistry, clusterPolicyBindingRegistry)
clusterRoleStorage := clusterrolestorage.NewClusterRoleStorage(clusterPolicyRegistry)
clusterRoleBindingStorage := clusterrolebindingstorage.NewClusterRoleBindingStorage(clusterPolicyRegistry, clusterPolicyBindingRegistry)
subjectAccessReviewStorage := subjectaccessreview.NewREST(c.Authorizer)
subjectAccessReviewRegistry := subjectaccessreview.NewRegistry(subjectAccessReviewStorage)
imageStorage := imageetcd.NewREST(c.EtcdHelper)
imageRegistry := image.NewRegistry(imageStorage)
imageStreamStorage, imageStreamStatusStorage := imagestreametcd.NewREST(c.EtcdHelper, imagestream.DefaultRegistryFunc(defaultRegistryFunc), subjectAccessReviewRegistry)
imageStreamRegistry := imagestream.NewRegistry(imageStreamStorage, imageStreamStatusStorage)
imageStreamMappingStorage := imagestreammapping.NewREST(imageRegistry, imageStreamRegistry)
imageStreamMappingRegistry := imagestreammapping.NewRegistry(imageStreamMappingStorage)
imageStreamTagStorage := imagestreamtag.NewREST(imageRegistry, imageStreamRegistry)
imageStreamTagRegistry := imagestreamtag.NewRegistry(imageStreamTagStorage)
imageStreamImageStorage := imagestreamimage.NewREST(imageRegistry, imageStreamRegistry)
imageStreamImageRegistry := imagestreamimage.NewRegistry(imageStreamImageStorage)
imageRepositoryStorage, imageRepositoryStatusStorage := imagerepository.NewREST(imageStreamRegistry)
imageRepositoryMappingStorage := imagerepositorymapping.NewREST(imageStreamMappingRegistry)
imageRepositoryTagStorage := imagerepositorytag.NewREST(imageStreamTagRegistry)
routeAllocator := c.RouteAllocator()
buildGenerator := &buildgenerator.BuildGenerator{
Client: buildgenerator.Client{
GetBuildConfigFunc: buildConfigRegistry.GetBuildConfig,
UpdateBuildConfigFunc: buildConfigRegistry.UpdateBuildConfig,
GetBuildFunc: buildRegistry.GetBuild,
CreateBuildFunc: buildRegistry.CreateBuild,
GetImageStreamFunc: imageStreamRegistry.GetImageStream,
GetImageStreamImageFunc: imageStreamImageRegistry.GetImageStreamImage,
GetImageStreamTagFunc: imageStreamTagRegistry.GetImageStreamTag,
},
}
buildClone, buildConfigInstantiate := buildgenerator.NewREST(buildGenerator)
// TODO: with sharding, this needs to be changed
deployConfigGenerator := &deployconfiggenerator.DeploymentConfigGenerator{
Client: deployconfiggenerator.Client{
DCFn: deployEtcd.GetDeploymentConfig,
ISFn: imageStreamRegistry.GetImageStream,
LISFn2: imageStreamRegistry.ListImageStreams,
},
}
_, kclient := c.DeploymentConfigControllerClients()
deployRollback := &deployrollback.RollbackGenerator{}
deployRollbackClient := deployrollback.Client{
DCFn: deployEtcd.GetDeploymentConfig,
RCFn: clientDeploymentInterface{kclient}.GetDeployment,
GRFn: deployRollback.GenerateRollback,
}
projectStorage := projectproxy.NewREST(kclient.Namespaces(), c.ProjectAuthorizationCache)
namespace, templateName, err := configapi.ParseNamespaceAndName(c.Options.ProjectRequestConfig.ProjectRequestTemplate)
if err != nil {
glog.Errorf("Error parsing project request template value: %v", err)
// we can continue on, the storage that gets created will be valid, it simply won't work properly. There's no reason to kill the master
}
projectRequestStorage := projectrequeststorage.NewREST(c.Options.ProjectRequestConfig.ProjectRequestMessage, namespace, templateName, c.PrivilegedLoopbackOpenShiftClient)
bcClient, _ := c.BuildControllerClients()
buildConfigWebHooks := buildconfigregistry.NewWebHookREST(
buildConfigRegistry,
buildclient.NewOSClientBuildConfigInstantiatorClient(bcClient),
map[string]webhook.Plugin{
"generic": generic.New(),
"github": github.New(),
},
)
// initialize OpenShift API
storage := map[string]rest.Storage{
"builds": buildStorage,
"builds/clone": buildClone,
"buildConfigs": buildConfigStorage,
"buildConfigs/webhooks": buildConfigWebHooks,
"buildConfigs/instantiate": buildConfigInstantiate,
"buildLogs": buildlogregistry.NewREST(buildRegistry, c.BuildLogClient(), kubeletClient),
"builds/log": buildlogregistry.NewREST(buildRegistry, c.BuildLogClient(), kubeletClient),
"images": imageStorage,
"imageStreams": imageStreamStorage,
"imageStreams/status": imageStreamStatusStorage,
"imageStreamImages": imageStreamImageStorage,
"imageStreamMappings": imageStreamMappingStorage,
"imageStreamTags": imageStreamTagStorage,
"imageRepositories": imageRepositoryStorage,
"imageRepositories/status": imageRepositoryStatusStorage,
"imageRepositoryMappings": imageRepositoryMappingStorage,
"imageRepositoryTags": imageRepositoryTagStorage,
"deployments": deployregistry.NewREST(deployEtcd),
"deploymentConfigs": deployconfigregistry.NewREST(deployEtcd),
"generateDeploymentConfigs": deployconfiggenerator.NewREST(deployConfigGenerator, latest.Codec),
"deploymentConfigRollbacks": deployrollback.NewREST(deployRollbackClient, latest.Codec),
"processedTemplates": templateregistry.NewREST(false),
"templates": templateetcd.NewREST(c.EtcdHelper),
// DEPRECATED: remove with v1beta1
"templateConfigs": templateregistry.NewREST(true),
"routes": routeregistry.NewREST(routeEtcd, routeAllocator),
"projects": projectStorage,
"projectRequests": projectRequestStorage,
"hostSubnets": hostSubnetStorage,
"clusterNetworks": clusterNetworkStorage,
"users": userStorage,
"identities": identityStorage,
"userIdentityMappings": userIdentityMappingStorage,
"oAuthAuthorizeTokens": authorizetokenetcd.NewREST(c.EtcdHelper),
"oAuthAccessTokens": accesstokenetcd.NewREST(c.EtcdHelper),
"oAuthClients": clientetcd.NewREST(c.EtcdHelper),
"oAuthClientAuthorizations": clientauthetcd.NewREST(c.EtcdHelper),
"resourceAccessReviews": resourceaccessreviewregistry.NewREST(c.Authorizer),
"subjectAccessReviews": subjectAccessReviewStorage,
"policies": policyStorage,
"policyBindings": policyBindingStorage,
"roles": roleStorage,
"roleBindings": roleBindingStorage,
"clusterPolicies": clusterPolicyStorage,
"clusterPolicyBindings": clusterPolicyBindingStorage,
"clusterRoleBindings": clusterRoleBindingStorage,
"clusterRoles": clusterRoleStorage,
}
if err := c.api_v1beta1(storage).InstallREST(container); err != nil {
glog.Fatalf("Unable to initialize v1beta1 API: %v", err)
}
if err := c.api_v1beta3(storage).InstallREST(container); err != nil {
/*// TODO: remove this check once v1beta3 is complete
if utilerrs.FilterOut(err, func(err error) bool {
return strings.Contains(err.Error(), "is registered for version")
}) != nil {*/
glog.Fatalf("Unable to initialize v1beta3 API: %v", err)
//}
}
var root *restful.WebService
for _, svc := range container.RegisteredWebServices() {
switch svc.RootPath() {
case "/":
root = svc
case OpenShiftAPIPrefixV1Beta1:
svc.Doc("OpenShift REST API, version v1beta1").ApiVersion("v1beta1")
case OpenShiftAPIPrefixV1Beta3:
svc.Doc("OpenShift REST API, version v1beta3").ApiVersion("v1beta3")
}
}
if root == nil {
root = new(restful.WebService)
container.Add(root)
}
initAPIVersionRoute(root, "v1beta1", "v1beta3")
return []string{
fmt.Sprintf("Started OpenShift API at %%s%s", OpenShiftAPIPrefixV1Beta1),
fmt.Sprintf("Started OpenShift API at %%s%s (experimental)", OpenShiftAPIPrefixV1Beta3),
}
}
func (c *MasterConfig) InstallUnprotectedAPI(container *restful.Container) []string {
bcClient, _ := c.BuildControllerClients()
bcGetterUpdater := buildclient.NewOSClientBuildConfigClient(bcClient)
handler := webhook.NewController(
bcGetterUpdater,
buildclient.NewOSClientBuildConfigInstantiatorClient(bcClient),
map[string]webhook.Plugin{
"generic": generic.New(),
"github": github.New(),
})
// TODO: deprecated, remove when v1beta1 is dropped
prefix := OpenShiftAPIPrefixV1Beta1 + "/buildConfigHooks/"
container.Handle(prefix, http.StripPrefix(prefix, handler))
return []string{}
}
//initAPIVersionRoute initializes the osapi endpoint to behave similar to the upstream api endpoint
func initAPIVersionRoute(root *restful.WebService, versions ...string) {
versionHandler := apiserver.APIVersionHandler(versions...)
root.Route(root.GET(OpenShiftAPIPrefix).To(versionHandler).
Doc("list supported server API versions").
Produces(restful.MIME_JSON).
Consumes(restful.MIME_JSON))
}
// If we know the location of the asset server, redirect to it when / is requested
// and the Accept header supports text/html
func assetServerRedirect(handler http.Handler, assetPublicURL string) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
if req.URL.Path == "/" {
accepts := goautoneg.ParseAccept(req.Header.Get("Accept"))
for _, accept := range accepts {
if accept.Type == "text" && accept.SubType == "html" {
http.Redirect(w, req, assetPublicURL, http.StatusFound)
return
}
}
}
// Dispatch to the next handler
handler.ServeHTTP(w, req)
})
}
// TODO We would like to use the IndexHandler from k8s but we do not yet have a
// MuxHelper to track all registered paths
func indexAPIPaths(handler http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
if req.URL.Path == "/" {
// TODO once we have a MuxHelper we will not need to hardcode this list of paths
object := api.RootPaths{Paths: []string{
"/api",
"/api/v1beta1",
"/api/v1beta3",
"/api/v1beta3",
"/healthz",
"/healthz/ping",
"/logs/",
"/metrics",
"/osapi",
"/osapi/v1beta1",
"/swaggerapi/",
}}
// TODO it would be nice if apiserver.writeRawJSON was not private
output, err := json.MarshalIndent(object, "", " ")
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
w.Write(output)
} else {
// Dispatch to the next handler
handler.ServeHTTP(w, req)
}
})
}
// Run launches the OpenShift master. It takes optional installers that may install additional endpoints into the server.
// All endpoints get configured CORS behavior
// Protected installers' endpoints are protected by API authentication and authorization.
// Unprotected installers' endpoints do not have any additional protection added.
func (c *MasterConfig) Run(protected []APIInstaller, unprotected []APIInstaller) {
var extra []string
safe := kmaster.NewHandlerContainer(http.NewServeMux())
open := kmaster.NewHandlerContainer(http.NewServeMux())
// enforce authentication on protected endpoints
protected = append(protected, APIInstallFunc(c.InstallProtectedAPI))
for _, i := range protected {
extra = append(extra, i.InstallAPI(safe)...)
}
handler := c.authorizationFilter(safe)
handler = authenticationHandlerFilter(handler, c.Authenticator, c.getRequestContextMapper())
handler = namespacingFilter(handler, c.getRequestContextMapper())
// unprotected resources
unprotected = append(unprotected, APIInstallFunc(c.InstallUnprotectedAPI))
for _, i := range unprotected {
extra = append(extra, i.InstallAPI(open)...)
}
handler = indexAPIPaths(handler)
open.Handle("/", handler)
// install swagger
swaggerConfig := swagger.Config{
WebServicesUrl: c.Options.MasterPublicURL,
WebServices: append(safe.RegisteredWebServices(), open.RegisteredWebServices()...),
ApiPath: swaggerAPIPrefix,
}
// log nothing from swagger
swagger.LogInfo = func(format string, v ...interface{}) {}
swagger.RegisterSwaggerService(swaggerConfig, open)
extra = append(extra, fmt.Sprintf("Started Swagger Schema API at %%s%s", swaggerAPIPrefix))
handler = open
// add CORS support
if origins := c.ensureCORSAllowedOrigins(); len(origins) != 0 {
handler = apiserver.CORS(handler, origins, nil, nil, "true")
}
if c.Options.AssetConfig != nil {
handler = assetServerRedirect(handler, c.Options.AssetConfig.PublicURL)
}
// Make the outermost filter the requestContextMapper to ensure all components share the same context
if contextHandler, err := kapi.NewRequestContextFilter(c.getRequestContextMapper(), handler); err != nil {
glog.Fatalf("Error setting up request context filter: %v", err)
} else {
handler = contextHandler
}
server := &http.Server{
Addr: c.Options.ServingInfo.BindAddress,
Handler: handler,
ReadTimeout: 5 * time.Minute,
WriteTimeout: 5 * time.Minute,
MaxHeaderBytes: 1 << 20,
}
go util.Forever(func() {
for _, s := range extra {
glog.Infof(s, c.Options.ServingInfo.BindAddress)
}
if c.TLS {
server.TLSConfig = &tls.Config{
// Change default from SSLv3 to TLSv1.0 (because of POODLE vulnerability)
MinVersion: tls.VersionTLS10,
// Populate PeerCertificates in requests, but don't reject connections without certificates
// This allows certificates to be validated by authenticators, while still allowing other auth types
ClientAuth: tls.RequestClientCert,
ClientCAs: c.ClientCAs,
}
glog.Fatal(server.ListenAndServeTLS(c.Options.ServingInfo.ServerCert.CertFile, c.Options.ServingInfo.ServerCert.KeyFile))
} else {
glog.Fatal(server.ListenAndServe())
}
}, 0)
// Attempt to verify the server came up for 20 seconds (100 tries * 100ms, 100ms timeout per try)
cmdutil.WaitForSuccessfulDial(c.TLS, "tcp", c.Options.ServingInfo.BindAddress, 100*time.Millisecond, 100*time.Millisecond, 100)
// Attempt to create the required policy rules now, and then stick in a forever loop to make sure they are always available
c.ensureComponentAuthorizationRules()
c.ensureOpenShiftSharedResourcesNamespace()
go util.Forever(func() {
c.ensureOpenShiftSharedResourcesNamespace()
}, 10*time.Second)
}
func (c *MasterConfig) defaultAPIGroupVersion() *apiserver.APIGroupVersion {
return &apiserver.APIGroupVersion{
Root: OpenShiftAPIPrefix,
Mapper: latest.RESTMapper,
Creater: kapi.Scheme,
Typer: kapi.Scheme,
Convertor: kapi.Scheme,
Linker: latest.SelfLinker,
Admit: c.AdmissionControl,
Context: c.getRequestContextMapper(),
}
}
// api_v1beta1 returns the resources and codec for API version v1beta1.
func (c *MasterConfig) api_v1beta1(all map[string]rest.Storage) *apiserver.APIGroupVersion {
storage := make(map[string]rest.Storage)
for k, v := range all {
storage[strings.ToLower(k)] = v
storage[k] = v
}
version := c.defaultAPIGroupVersion()
version.Storage = storage
version.Version = OpenShiftAPIV1Beta1
version.Codec = v1beta1.Codec
return version
}
// api_v1beta3 returns the resources and codec for API version v1beta3.
func (c *MasterConfig) api_v1beta3(all map[string]rest.Storage) *apiserver.APIGroupVersion {
storage := make(map[string]rest.Storage)
for k, v := range all {
if excludedV1Beta3Types.Has(k) {
continue
}
storage[strings.ToLower(k)] = v
}
version := c.defaultAPIGroupVersion()
version.Storage = storage
version.Version = OpenShiftAPIV1Beta3
version.Codec = v1beta3.Codec
return version
}
// getRequestContextMapper returns a mapper from requests to contexts, initializing it if needed
func (c *MasterConfig) getRequestContextMapper() kapi.RequestContextMapper {
if c.RequestContextMapper == nil {
c.RequestContextMapper = kapi.NewRequestContextMapper()
}
return c.RequestContextMapper
}
// ensureOpenShiftSharedResourcesNamespace is called as part of global policy initialization to ensure shared namespace exists
func (c *MasterConfig) ensureOpenShiftSharedResourcesNamespace() {
namespace, err := c.KubeClient().Namespaces().Get(c.Options.PolicyConfig.OpenShiftSharedResourcesNamespace)
if err != nil {
namespace = &kapi.Namespace{
ObjectMeta: kapi.ObjectMeta{Name: c.Options.PolicyConfig.OpenShiftSharedResourcesNamespace},
Spec: kapi.NamespaceSpec{
Finalizers: []kapi.FinalizerName{projectapi.FinalizerProject},
},
}
kapi.FillObjectMetaSystemFields(api.NewContext(), &namespace.ObjectMeta)
_, err = c.KubeClient().Namespaces().Create(namespace)
if err != nil {
glog.Errorf("Error creating namespace: %v due to %v\n", namespace, err)
}
}
}
// ensureComponentAuthorizationRules initializes the cluster policies
func (c *MasterConfig) ensureComponentAuthorizationRules() {
clusterPolicyRegistry := clusterpolicyregistry.NewRegistry(clusterpolicystorage.NewStorage(c.EtcdHelper))
ctx := kapi.WithNamespace(kapi.NewContext(), "")
if _, err := clusterPolicyRegistry.GetClusterPolicy(ctx, authorizationapi.PolicyName); kapierror.IsNotFound(err) {
glog.Infof("No cluster policy found. Creating bootstrap policy based on: %v", c.Options.PolicyConfig.BootstrapPolicyFile)
if err := admin.OverwriteBootstrapPolicy(c.EtcdHelper, c.Options.PolicyConfig.BootstrapPolicyFile, admin.CreateBootstrapPolicyFileFullCommand, true, ioutil.Discard); err != nil {
glog.Errorf("Error creating bootstrap policy: %v", err)
}
} else {
glog.V(2).Infof("Ignoring bootstrap policy file because cluster policy found")
}
}
func (c *MasterConfig) authorizationFilter(handler http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
attributes, err := c.AuthorizationAttributeBuilder.GetAttributes(req)
if err != nil {
forbidden(err.Error(), "", w, req)
return
}
if attributes == nil {
forbidden("No attributes", "", w, req)
return
}
ctx, exists := c.RequestContextMapper.Get(req)
if !exists {
forbidden("context not found", attributes.GetAPIVersion(), w, req)
return
}
allowed, reason, err := c.Authorizer.Authorize(ctx, attributes)
if err != nil {
forbidden(err.Error(), attributes.GetAPIVersion(), w, req)
return
}
if !allowed {
forbidden(reason, attributes.GetAPIVersion(), w, req)
return
}
handler.ServeHTTP(w, req)
})
}
// forbidden renders a simple forbidden error
func forbidden(reason, apiVersion string, w http.ResponseWriter, req *http.Request) {
// the api version can be empty for two basic reasons:
// 1. malformed API request
// 2. not an API request at all
// In these cases, just assume the latest version that will work better than nothing
if len(apiVersion) == 0 {
apiVersion = klatest.Version
}
// Reason is an opaque string that describes why access is allowed or forbidden (forbidden by the time we reach here).
// We don't have direct access to kind or name (not that those apply either in the general case)
// We create a NewForbidden to stay close the API, but then we override the message to get a serialization
// that makes sense when a human reads it.
forbiddenError, _ := kapierror.NewForbidden("", "", errors.New("")).(*kapierror.StatusError)
forbiddenError.ErrStatus.Message = fmt.Sprintf("%q is forbidden because %s", req.RequestURI, reason)
// Not all API versions in valid API requests will have a matching codec in kubernetes. If we can't find one,
// just default to the latest kube codec.
codec := klatest.Codec
if requestedCodec, err := klatest.InterfacesFor(apiVersion); err == nil {
codec = requestedCodec
}
formatted := &bytes.Buffer{}
output, err := codec.Encode(&forbiddenError.ErrStatus)
if err != nil {
fmt.Fprintf(formatted, "%s", forbiddenError.Error())
} else {
_ = json.Indent(formatted, output, "", " ")
}
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusForbidden)
w.Write(formatted.Bytes())
}
// RunProjectAuthorizationCache starts the project authorization cache
func (c *MasterConfig) RunProjectAuthorizationCache() {
// TODO: look at exposing a configuration option in future to control how often we run this loop
period := 1 * time.Second
c.ProjectAuthorizationCache.Run(period)
}
// RunOriginNamespaceController starts the controller that takes part in namespace termination of openshift content
func (c *MasterConfig) RunOriginNamespaceController() {
osclient, kclient := c.OriginNamespaceControllerClients()
factory := projectcontroller.NamespaceControllerFactory{
Client: osclient,
KubeClient: kclient,
}
controller := factory.Create()
controller.Run()
}
// RunPolicyCache starts the policy cache
func (c *MasterConfig) RunPolicyCache() {
c.PolicyCache.Run()
}
// RunAssetServer starts the asset server for the OpenShift UI.
func (c *MasterConfig) RunAssetServer() {
}
func (c *MasterConfig) RunDNSServer() {
config, err := dns.NewServerDefaults()
if err != nil {
glog.Fatalf("Could not start DNS: %v", err)
}
config.DnsAddr = c.Options.DNSConfig.BindAddress
_, port, err := net.SplitHostPort(c.Options.DNSConfig.BindAddress)
if err != nil {
glog.Fatalf("Could not start DNS: %v", err)
}
if port != "53" {
glog.Warningf("Binding DNS on port %v instead of 53 (you may need to run as root and update your config), using %s which will not resolve from all locations", port, c.Options.DNSConfig.BindAddress)
}
if ok, err := cmdutil.TryListen(c.Options.DNSConfig.BindAddress); !ok {
glog.Warningf("Could not start DNS: %v", err)
return
}
go func() {
err := dns.ListenAndServe(config, c.DNSServerClient(), c.EtcdHelper.Client.(*etcdclient.Client))
glog.Fatalf("Could not start DNS: %v", err)
}()
cmdutil.WaitForSuccessfulDial(false, "tcp", c.Options.DNSConfig.BindAddress, 100*time.Millisecond, 100*time.Millisecond, 100)
glog.Infof("OpenShift DNS listening at %s", c.Options.DNSConfig.BindAddress)
}
// RunProjectCache populates project cache, used by scheduler and project admission controller.
func (c *MasterConfig) RunProjectCache() {
glog.Infof("Using default project node label selector: %s", c.Options.ProjectNodeSelector)
projectcache.RunProjectCache(c.PrivilegedLoopbackKubernetesClient, c.Options.ProjectNodeSelector)
}
// RunBuildController starts the build sync loop for builds and buildConfig processing.
func (c *MasterConfig) RunBuildController() {
// initialize build controller
dockerImage := c.ImageFor("docker-builder")
stiImage := c.ImageFor("sti-builder")
osclient, kclient := c.BuildControllerClients()
factory := buildcontrollerfactory.BuildControllerFactory{
OSClient: osclient,
KubeClient: kclient,
BuildUpdater: buildclient.NewOSClientBuildClient(osclient),
DockerBuildStrategy: &buildstrategy.DockerBuildStrategy{
Image: dockerImage,
// TODO: this will be set to --storage-version (the internal schema we use)
Codec: v1beta1.Codec,
},
SourceBuildStrategy: &buildstrategy.SourceBuildStrategy{
Image: stiImage,
TempDirectoryCreator: buildstrategy.STITempDirectoryCreator,
// TODO: this will be set to --storage-version (the internal schema we use)
Codec: v1beta1.Codec,
},
CustomBuildStrategy: &buildstrategy.CustomBuildStrategy{
// TODO: this will be set to --storage-version (the internal schema we use)
Codec: v1beta1.Codec,
},
}
controller := factory.Create()
controller.Run()
}
// RunBuildPodController starts the build/pod status sync loop for build status
func (c *MasterConfig) RunBuildPodController() {
osclient, kclient := c.BuildControllerClients()
factory := buildcontrollerfactory.BuildPodControllerFactory{
OSClient: osclient,
KubeClient: kclient,
BuildUpdater: buildclient.NewOSClientBuildClient(osclient),
}
controller := factory.Create()
controller.Run()
}
// RunBuildImageChangeTriggerController starts the build image change trigger controller process.
func (c *MasterConfig) RunBuildImageChangeTriggerController() {
bcClient, _ := c.BuildControllerClients()
bcUpdater := buildclient.NewOSClientBuildConfigClient(bcClient)
bcInstantiator := buildclient.NewOSClientBuildConfigInstantiatorClient(bcClient)
factory := buildcontrollerfactory.ImageChangeControllerFactory{Client: bcClient, BuildConfigInstantiator: bcInstantiator, BuildConfigUpdater: bcUpdater}
factory.Create().Run()
}
// RunDeploymentController starts the deployment controller process.
func (c *MasterConfig) RunDeploymentController() error {
_, kclient := c.DeploymentControllerClients()
_, kclientConfig, err := configapi.GetKubeClient(c.Options.MasterClients.OpenShiftLoopbackKubeConfig)
if err != nil {
return err
}
// TODO eliminate these environment variables once we figure out what they do
env := []api.EnvVar{
{Name: "KUBERNETES_MASTER", Value: kclientConfig.Host},
{Name: "OPENSHIFT_MASTER", Value: kclientConfig.Host},
}
env = append(env, clientcmd.EnvVarsFromConfig(c.DeployerClientConfig())...)
factory := deploycontroller.DeploymentControllerFactory{
KubeClient: kclient,
Codec: latest.Codec,
Environment: env,
DeployerImage: c.ImageFor("deployer"),
}
controller := factory.Create()
controller.Run()
return nil
}
// RunDeployerPodController starts the deployer pod controller process.
func (c *MasterConfig) RunDeployerPodController() {
_, kclient := c.DeploymentControllerClients()
factory := deployerpodcontroller.DeployerPodControllerFactory{
KubeClient: kclient,
}
controller := factory.Create()
controller.Run()
}
func (c *MasterConfig) RunDeploymentConfigController() {
osclient, kclient := c.DeploymentConfigControllerClients()
factory := deployconfigcontroller.DeploymentConfigControllerFactory{
Client: osclient,
KubeClient: kclient,
Codec: latest.Codec,
}
controller := factory.Create()
controller.Run()
}
func (c *MasterConfig) RunDeploymentConfigChangeController() {
osclient, kclient := c.DeploymentConfigChangeControllerClients()
factory := configchangecontroller.DeploymentConfigChangeControllerFactory{
Client: osclient,
KubeClient: kclient,
Codec: latest.Codec,
}
controller := factory.Create()
controller.Run()
}
func (c *MasterConfig) RunDeploymentImageChangeTriggerController() {
osclient := c.DeploymentImageChangeControllerClient()
factory := imagechangecontroller.ImageChangeControllerFactory{Client: osclient}
controller := factory.Create()
controller.Run()
}
// SDN controller runs openshift-sdn if the said network plugin is provided
func (c *MasterConfig) RunSDNController() {
if c.Options.NetworkConfig.NetworkPluginName == osdn.NetworkPluginName() {
osdn.Master(*c.SdnClient(), *c.KubeClient(), c.Options.NetworkConfig.ClusterNetworkCIDR, c.Options.NetworkConfig.HostSubnetLength)
}
}
// RouteAllocator returns a route allocation controller.
func (c *MasterConfig) RouteAllocator() *routeallocationcontroller.RouteAllocationController {
factory := routeallocationcontroller.RouteAllocationControllerFactory{
OSClient: c.PrivilegedLoopbackOpenShiftClient,
KubeClient: c.KubeClient(),
}
subdomain := env("OPENSHIFT_ROUTE_SUBDOMAIN", OpenShiftRouteSubdomain)
plugin, err := routeplugin.NewSimpleAllocationPlugin(subdomain)
if err != nil {
glog.Fatalf("Route plugin initialization failed: %v", err)
}
return factory.Create(plugin)
}
func (c *MasterConfig) RunImageImportController() {
osclient := c.ImageImportControllerClient()
factory := imagecontroller.ImportControllerFactory{
Client: osclient,
}
controller := factory.Create()
controller.Run()
}
// ensureCORSAllowedOrigins takes a string list of origins and attempts to covert them to CORS origin
// regexes, or exits if it cannot.
func (c *MasterConfig) ensureCORSAllowedOrigins() []*regexp.Regexp {
if len(c.Options.CORSAllowedOrigins) == 0 {
return []*regexp.Regexp{}
}
allowedOriginRegexps, err := util.CompileRegexps(util.StringList(c.Options.CORSAllowedOrigins))
if err != nil {
glog.Fatalf("Invalid --cors-allowed-origins: %v", err)
}
return allowedOriginRegexps
}
// 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
}
type clientDeploymentInterface struct {
KubeClient kclient.Interface
}
func (c clientDeploymentInterface) GetDeployment(ctx api.Context, name string) (*api.ReplicationController, error) {
return c.KubeClient.ReplicationControllers(api.NamespaceValue(ctx)).Get(name)
}
// namespacingFilter adds a filter that adds the namespace of the request to the context. Not all requests will have namespaces,
// but any that do will have the appropriate value added.
func namespacingFilter(handler http.Handler, contextMapper kapi.RequestContextMapper) http.Handler {
infoResolver := &apiserver.APIRequestInfoResolver{util.NewStringSet("api", "osapi"), latest.RESTMapper}
return http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
ctx, ok := contextMapper.Get(req)
if !ok {
http.Error(w, "Unable to find request context", http.StatusInternalServerError)
return
}
if _, exists := kapi.NamespaceFrom(ctx); !exists {
if requestInfo, err := infoResolver.GetAPIRequestInfo(req); err == nil {
// only set the namespace if the apiRequestInfo was resolved
// keep in mind that GetAPIRequestInfo will fail on non-api requests, so don't fail the entire http request on that
// kind of failure.
// TODO reconsider special casing this. Having the special case hereallow us to fully share the kube
// APIRequestInfoResolver without any modification or customization.
namespace := requestInfo.Namespace
if (requestInfo.Resource == "projects") && (len(requestInfo.Name) > 0) {
namespace = requestInfo.Name
}
ctx = kapi.WithNamespace(ctx, namespace)
contextMapper.Update(req, ctx)
}
}
handler.ServeHTTP(w, req)
})
}