forked from openshift/origin
-
Notifications
You must be signed in to change notification settings - Fork 1
/
apiserver.go
136 lines (114 loc) · 4.11 KB
/
apiserver.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
package server
import (
"fmt"
"time"
"k8s.io/apimachinery/pkg/apimachinery/announced"
"k8s.io/apimachinery/pkg/apimachinery/registered"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/apimachinery/pkg/runtime/serializer"
genericapiserver "k8s.io/apiserver/pkg/server"
restclient "k8s.io/client-go/rest"
"k8s.io/client-go/tools/cache"
"k8s.io/kubernetes/pkg/apis/core/install"
"k8s.io/kubernetes/pkg/controller"
templateapiv1 "github.com/openshift/api/template/v1"
templateclientset "github.com/openshift/client-go/template/clientset/versioned"
templateinformer "github.com/openshift/client-go/template/informers/externalversions"
templateapi "github.com/openshift/origin/pkg/template/apis/template"
templateservicebroker "github.com/openshift/origin/pkg/templateservicebroker/servicebroker"
)
// TODO: this file breaks the layering of pkg/openservicebroker and
// pkg/template/servicebroker; assuming that the latter will move out of origin
// in 3.7, will leave as is for now.
var (
groupFactoryRegistry = make(announced.APIGroupFactoryRegistry)
registry = registered.NewOrDie("")
Scheme = runtime.NewScheme()
Codecs = serializer.NewCodecFactory(Scheme)
// if you modify this, make sure you update the crEncoder
unversionedVersion = schema.GroupVersion{Group: "", Version: "v1"}
unversionedTypes = []runtime.Object{
&metav1.Status{},
&metav1.WatchEvent{},
&metav1.APIVersions{},
&metav1.APIGroupList{},
&metav1.APIGroup{},
&metav1.APIResourceList{},
}
)
func init() {
install.Install(groupFactoryRegistry, registry, Scheme)
// we need to add the options to empty v1
metav1.AddToGroupVersion(Scheme, schema.GroupVersion{Group: "", Version: "v1"})
Scheme.AddUnversionedTypes(unversionedVersion, unversionedTypes...)
}
type ExtraConfig struct {
TemplateNamespaces []string
}
type TemplateServiceBrokerConfig struct {
GenericConfig *genericapiserver.RecommendedConfig
ExtraConfig ExtraConfig
}
type TemplateServiceBrokerServer struct {
GenericAPIServer *genericapiserver.GenericAPIServer
}
type completedTemplateServiceBrokerConfig struct {
GenericConfig genericapiserver.CompletedConfig
ExtraConfig *ExtraConfig
}
// Complete fills in any fields not set that are required to have valid data. It's mutating the receiver.
func (c *TemplateServiceBrokerConfig) Complete() completedTemplateServiceBrokerConfig {
cfg := completedTemplateServiceBrokerConfig{
c.GenericConfig.Complete(),
&c.ExtraConfig,
}
return cfg
}
func (c completedTemplateServiceBrokerConfig) New(delegationTarget genericapiserver.DelegationTarget) (*TemplateServiceBrokerServer, error) {
genericServer, err := c.GenericConfig.New("template-service-broker", delegationTarget)
if err != nil {
return nil, err
}
s := &TemplateServiceBrokerServer{
GenericAPIServer: genericServer,
}
clientConfig, err := restclient.InClusterConfig()
if err != nil {
return nil, err
}
templateClient, err := templateclientset.NewForConfig(clientConfig)
if err != nil {
return nil, err
}
templateInformers := templateinformer.NewSharedInformerFactory(templateClient, 5*time.Minute)
templateInformers.Template().V1().Templates().Informer().AddIndexers(cache.Indexers{
templateapi.TemplateUIDIndex: func(obj interface{}) ([]string, error) {
return []string{string(obj.(*templateapiv1.Template).UID)}, nil
},
})
broker, err := templateservicebroker.NewBroker(
clientConfig,
templateInformers.Template().V1().Templates(),
c.ExtraConfig.TemplateNamespaces,
)
if err != nil {
return nil, err
}
if err := s.GenericAPIServer.AddPostStartHook("template-service-broker-synctemplates", func(context genericapiserver.PostStartHookContext) error {
templateInformers.Start(context.StopCh)
if !controller.WaitForCacheSync("tsb", context.StopCh, templateInformers.Template().V1().Templates().Informer().HasSynced) {
return fmt.Errorf("unable to sync caches")
}
return nil
}); err != nil {
return nil, err
}
Route(
s.GenericAPIServer.Handler.GoRestfulContainer,
templateapi.ServiceBrokerRoot,
broker,
)
return s, nil
}