forked from openshift/origin
/
run_server.go
159 lines (135 loc) · 4.92 KB
/
run_server.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
/*
Copyright 2017 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package server
import (
"fmt"
genericapiserverstorage "k8s.io/apiserver/pkg/server/storage"
clientset "k8s.io/client-go/kubernetes"
"k8s.io/client-go/pkg/api"
"github.com/golang/glog"
"github.com/kubernetes-incubator/service-catalog/pkg/apiserver"
"github.com/kubernetes-incubator/service-catalog/pkg/apiserver/options"
"github.com/kubernetes-incubator/service-catalog/pkg/registry/servicecatalog/server"
"github.com/kubernetes-incubator/service-catalog/pkg/storage/tpr"
)
// RunServer runs an API server with configuration according to opts
func RunServer(opts *ServiceCatalogServerOptions) error {
storageType, err := opts.StorageType()
if err != nil {
return err
}
if opts.StopCh == nil {
/* the caller of RunServer should generate the stop channel
if there is a need to stop the API server */
opts.StopCh = make(chan struct{})
}
if storageType == server.StorageTypeTPR {
return runTPRServer(opts)
}
return runEtcdServer(opts)
}
func installTPRsToCore(cl clientset.Interface) func() error {
return func() error {
if err := tpr.InstallTypes(cl.Extensions().ThirdPartyResources()); err != nil {
glog.Errorf("Failed to install TPR types (%s)", err)
return err
}
return nil
}
}
func runTPRServer(opts *ServiceCatalogServerOptions) error {
tprOpts := opts.TPROptions
glog.Infoln("Installing TPR types to the cluster")
if err := tprOpts.InstallTPRsFunc(); err != nil {
glog.V(4).Infof("Installing TPR types failed, continuing anyway (%s)", err)
return err
}
glog.V(4).Infoln("Preparing to run API server")
genericConfig, scConfig, err := buildGenericConfig(opts)
if err != nil {
return err
}
config := apiserver.NewTPRConfig(
tprOpts.RESTClient,
genericConfig,
tprOpts.GlobalNamespace,
tprOpts.storageFactory(),
)
completed := config.Complete()
// make the server
glog.V(4).Infoln("Completing API server configuration")
server, err := completed.NewServer()
if err != nil {
return fmt.Errorf("error completing API server configuration: %v", err)
}
addPostStartHooks(server.GenericAPIServer, scConfig, opts.StopCh)
glog.Infoln("Running the API server")
server.GenericAPIServer.PrepareRun().Run(opts.StopCh)
return nil
}
func runEtcdServer(opts *ServiceCatalogServerOptions) error {
etcdOpts := opts.EtcdOptions
glog.V(4).Infoln("Preparing to run API server")
genericConfig, scConfig, err := buildGenericConfig(opts)
if err != nil {
return err
}
// etcd options
if errs := etcdOpts.Validate(); len(errs) > 0 {
glog.Errorln("Error validating etcd options, do you have `--etcd-servers localhost` set?")
return errs[0]
}
glog.V(4).Infoln("Creating storage factory")
// The API server stores objects using a particular API version for each
// group, regardless of API version of the object when it was created.
//
// storageGroupsToEncodingVersion holds a map of API group to version that
// the API server uses to store that group.
storageGroupsToEncodingVersion, err := options.NewStorageSerializationOptions().StorageGroupsToEncodingVersion()
if err != nil {
return fmt.Errorf("error generating storage version map: %s", err)
}
// Build the default storage factory.
//
// The default storage factory returns the storage interface for a
// particular GroupResource (an (api-group, resource) tuple).
storageFactory, err := apiserver.NewStorageFactory(
etcdOpts.StorageConfig,
etcdOpts.DefaultStorageMediaType,
api.Codecs,
genericapiserverstorage.NewDefaultResourceEncodingConfig(api.Registry),
storageGroupsToEncodingVersion,
nil, /* group storage version overrides */
apiserver.DefaultAPIResourceConfigSource(),
nil, /* resource config overrides */
)
if err != nil {
glog.Errorf("error creating storage factory: %v", err)
return err
}
// // Set the finalized generic and storage configs
config := apiserver.NewEtcdConfig(genericConfig, 0 /* deleteCollectionWorkers */, storageFactory)
// Fill in defaults not already set in the config
completed := config.Complete()
// make the server
glog.V(4).Infoln("Completing API server configuration")
server, err := completed.NewServer()
if err != nil {
return fmt.Errorf("error completing API server configuration: %v", err)
}
addPostStartHooks(server.GenericAPIServer, scConfig, opts.StopCh)
// do we need to do any post api installation setup? We should have set up the api already?
glog.Infoln("Running the API server")
server.PrepareRun().Run(opts.StopCh)
return nil
}