forked from openshift/origin
-
Notifications
You must be signed in to change notification settings - Fork 1
/
apiserver.go
162 lines (139 loc) · 5.54 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
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
package apiserver
import (
"fmt"
"sync"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/serializer"
"k8s.io/apiserver/pkg/registry/rest"
genericapiserver "k8s.io/apiserver/pkg/server"
corev1 "k8s.io/client-go/kubernetes/typed/core/v1"
restclient "k8s.io/client-go/rest"
coreclient "k8s.io/kubernetes/pkg/client/clientset_generated/internalclientset/typed/core/internalversion"
oauthapiv1 "github.com/openshift/api/oauth/v1"
configapi "github.com/openshift/origin/pkg/cmd/server/api"
oauthapi "github.com/openshift/origin/pkg/oauth/apis/oauth"
oauthclient "github.com/openshift/origin/pkg/oauth/generated/internalclientset/typed/oauth/internalversion"
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"
routeclient "github.com/openshift/origin/pkg/route/generated/internalclientset"
saoauth "github.com/openshift/origin/pkg/serviceaccounts/oauthclient"
"k8s.io/apimachinery/pkg/apimachinery/registered"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
type ExtraConfig struct {
CoreAPIServerClientConfig *restclient.Config
ServiceAccountMethod configapi.GrantHandlerType
// TODO these should all become local eventually
Scheme *runtime.Scheme
Registry *registered.APIRegistrationManager
Codecs serializer.CodecFactory
makeV1Storage sync.Once
v1Storage map[string]rest.Storage
v1StorageErr error
}
type OAuthAPIServerConfig struct {
GenericConfig *genericapiserver.RecommendedConfig
ExtraConfig ExtraConfig
}
type OAuthAPIServer struct {
GenericAPIServer *genericapiserver.GenericAPIServer
}
type completedConfig struct {
GenericConfig genericapiserver.CompletedConfig
ExtraConfig *ExtraConfig
}
type CompletedConfig struct {
// Embed a private pointer that cannot be instantiated outside of this package.
*completedConfig
}
// Complete fills in any fields not set that are required to have valid data. It's mutating the receiver.
func (c *OAuthAPIServerConfig) Complete() completedConfig {
cfg := completedConfig{
c.GenericConfig.Complete(),
&c.ExtraConfig,
}
return cfg
}
// New returns a new instance of OAuthAPIServer from the given config.
func (c completedConfig) New(delegationTarget genericapiserver.DelegationTarget) (*OAuthAPIServer, error) {
genericServer, err := c.GenericConfig.New("oauth.openshift.io-apiserver", delegationTarget)
if err != nil {
return nil, err
}
s := &OAuthAPIServer{
GenericAPIServer: genericServer,
}
v1Storage, err := c.V1RESTStorage()
if err != nil {
return nil, err
}
apiGroupInfo := genericapiserver.NewDefaultAPIGroupInfo(oauthapiv1.GroupName, c.ExtraConfig.Registry, c.ExtraConfig.Scheme, metav1.ParameterCodec, c.ExtraConfig.Codecs)
apiGroupInfo.GroupMeta.GroupVersion = oauthapiv1.SchemeGroupVersion
apiGroupInfo.VersionedResourcesStorageMap[oauthapiv1.SchemeGroupVersion.Version] = v1Storage
if err := s.GenericAPIServer.InstallAPIGroup(&apiGroupInfo); err != nil {
return nil, err
}
return s, nil
}
func (c *completedConfig) V1RESTStorage() (map[string]rest.Storage, error) {
c.ExtraConfig.makeV1Storage.Do(func() {
c.ExtraConfig.v1Storage, c.ExtraConfig.v1StorageErr = c.newV1RESTStorage()
})
return c.ExtraConfig.v1Storage, c.ExtraConfig.v1StorageErr
}
func (c *completedConfig) newV1RESTStorage() (map[string]rest.Storage, error) {
clientStorage, err := clientetcd.NewREST(c.GenericConfig.RESTOptionsGetter)
if err != nil {
return nil, fmt.Errorf("error building REST storage: %v", err)
}
// If OAuth is disabled, set the strategy to Deny
saAccountGrantMethod := oauthapi.GrantHandlerDeny
if len(c.ExtraConfig.ServiceAccountMethod) > 0 {
// Otherwise, take the value provided in master-config.yaml
saAccountGrantMethod = oauthapi.GrantHandlerType(c.ExtraConfig.ServiceAccountMethod)
}
oauthClient, err := oauthclient.NewForConfig(c.GenericConfig.LoopbackClientConfig)
if err != nil {
return nil, err
}
coreClient, err := coreclient.NewForConfig(c.ExtraConfig.CoreAPIServerClientConfig)
if err != nil {
return nil, err
}
routeClient, err := routeclient.NewForConfig(c.ExtraConfig.CoreAPIServerClientConfig)
if err != nil {
return nil, err
}
coreV1Client, err := corev1.NewForConfig(c.ExtraConfig.CoreAPIServerClientConfig)
if err != nil {
return nil, err
}
combinedOAuthClientGetter := saoauth.NewServiceAccountOAuthClientGetter(
coreClient,
coreClient,
coreV1Client.Events(""),
routeClient.Route(),
oauthClient.OAuthClients(),
saAccountGrantMethod,
)
authorizeTokenStorage, err := authorizetokenetcd.NewREST(c.GenericConfig.RESTOptionsGetter, combinedOAuthClientGetter)
if err != nil {
return nil, fmt.Errorf("error building REST storage: %v", err)
}
accessTokenStorage, err := accesstokenetcd.NewREST(c.GenericConfig.RESTOptionsGetter, combinedOAuthClientGetter)
if err != nil {
return nil, fmt.Errorf("error building REST storage: %v", err)
}
clientAuthorizationStorage, err := clientauthetcd.NewREST(c.GenericConfig.RESTOptionsGetter, combinedOAuthClientGetter)
if err != nil {
return nil, fmt.Errorf("error building REST storage: %v", err)
}
v1Storage := map[string]rest.Storage{}
v1Storage["oAuthAuthorizeTokens"] = authorizeTokenStorage
v1Storage["oAuthAccessTokens"] = accessTokenStorage
v1Storage["oAuthClients"] = clientStorage
v1Storage["oAuthClientAuthorizations"] = clientAuthorizationStorage
return v1Storage, nil
}