forked from kubernetes-retired/service-catalog
/
etcd_config.go
135 lines (119 loc) · 4.64 KB
/
etcd_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
/*
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 apiserver
import (
"github.com/golang/glog"
"github.com/kubernetes-incubator/service-catalog/pkg/registry/servicecatalog/server"
"k8s.io/apiserver/pkg/registry/generic"
"k8s.io/apiserver/pkg/registry/generic/registry"
"k8s.io/apiserver/pkg/registry/rest"
genericapiserver "k8s.io/apiserver/pkg/server"
"k8s.io/apiserver/pkg/server/storage"
)
// EtcdConfig contains a generic API server Config along with config specific to
// the service catalog API server.
type etcdConfig struct {
genericConfig *genericapiserver.RecommendedConfig
extraConfig *extraConfig
}
// extraConfig contains all additional configuration parameters for etcdConfig
type extraConfig struct {
// BABYNETES: cargo culted from master.go
deleteCollectionWorkers int
storageFactory storage.StorageFactory
}
// NewEtcdConfig returns a new server config to describe an etcd-backed API server
func NewEtcdConfig(
genCfg *genericapiserver.RecommendedConfig,
deleteCollWorkers int,
factory storage.StorageFactory,
) Config {
return &etcdConfig{
genericConfig: genCfg,
extraConfig: &extraConfig{
deleteCollectionWorkers: deleteCollWorkers,
storageFactory: factory,
},
}
}
// Complete fills in any fields not set that are required to have valid data
// and can be derived from other fields.
func (c *etcdConfig) Complete() CompletedConfig {
completedGenericConfig := completeGenericConfig(c.genericConfig)
return completedEtcdConfig{
genericConfig: completedGenericConfig,
extraConfig: c.extraConfig,
// Not every API group compiled in is necessarily enabled by the operator
// at runtime.
//
// Install the API resource config source, which describes versions of
// which API groups are enabled.
apiResourceConfigSource: DefaultAPIResourceConfigSource(),
}
}
// CompletedEtcdConfig is an internal type to take advantage of typechecking in
// the type system.
type completedEtcdConfig struct {
genericConfig genericapiserver.CompletedConfig
extraConfig *extraConfig
apiResourceConfigSource storage.APIResourceConfigSource
}
// NewServer creates a new server that can be run. Returns a non-nil error if the server couldn't
// be created
func (c completedEtcdConfig) NewServer(stopCh <-chan struct{}) (*ServiceCatalogAPIServer, error) {
s, err := createSkeletonServer(c.genericConfig)
if err != nil {
return nil, err
}
glog.V(4).Infoln("Created skeleton API server")
roFactory := etcdRESTOptionsFactory{
deleteCollectionWorkers: c.extraConfig.deleteCollectionWorkers,
// TODO https://github.com/kubernetes/kubernetes/issues/44507
// we still need to enable it so finalizers are respected.
enableGarbageCollection: true,
storageFactory: c.extraConfig.storageFactory,
storageDecorator: generic.UndecoratedStorage,
}
glog.V(4).Infoln("Installing API groups")
// default namespace doesn't matter for etcd
providers := restStorageProviders("" /* default namespace */, server.StorageTypeEtcd, nil)
for _, provider := range providers {
groupInfo, err := provider.NewRESTStorage(c.apiResourceConfigSource, roFactory)
if IsErrAPIGroupDisabled(err) {
glog.Warningf("Skipping API group %v because it is not enabled", provider.GroupName())
continue
} else if err != nil {
glog.Errorf("Error initializing storage for provider %v: %v", provider.GroupName(), err)
return nil, err
}
glog.V(4).Infof("Installing API group %v", provider.GroupName())
if err := s.GenericAPIServer.InstallAPIGroup(groupInfo); err != nil {
glog.Fatalf("Error installing API group %v: %v", provider.GroupName(), err)
} else {
// we've sucessfully installed, so hook the stopCh to the destroy func of all the sucessfully installed apigroups
for _, mappings := range groupInfo.VersionedResourcesStorageMap { // gv to resource mappings
for _, storage := range mappings { // resource name (brokers, brokers/status) to backing storage
go func(store rest.Storage) {
s, ok := store.(*registry.Store)
if ok {
<-stopCh
s.DestroyFunc()
}
}(storage)
}
}
}
}
glog.Infoln("Finished installing API groups")
return s, nil
}