forked from openshift/origin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
apiserver.go
215 lines (189 loc) · 8.25 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
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
package apiserver
import (
"fmt"
"sync"
"time"
"k8s.io/apimachinery/pkg/apimachinery/registered"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/serializer"
"k8s.io/apiserver/pkg/registry/rest"
genericapiserver "k8s.io/apiserver/pkg/server"
restclient "k8s.io/client-go/rest"
"k8s.io/client-go/util/flowcontrol"
authorizationclient "k8s.io/kubernetes/pkg/client/clientset_generated/internalclientset/typed/authorization/internalversion"
coreclient "k8s.io/kubernetes/pkg/client/clientset_generated/internalclientset/typed/core/internalversion"
imageapiv1 "github.com/openshift/api/image/v1"
imageclientv1 "github.com/openshift/client-go/image/clientset/versioned"
configapi "github.com/openshift/origin/pkg/cmd/server/apis/config"
imageadmission "github.com/openshift/origin/pkg/image/admission"
imageapi "github.com/openshift/origin/pkg/image/apis/image"
"github.com/openshift/origin/pkg/image/apis/image/validation/whitelist"
imageclient "github.com/openshift/origin/pkg/image/generated/internalclientset"
"github.com/openshift/origin/pkg/image/importer"
imageimporter "github.com/openshift/origin/pkg/image/importer"
"github.com/openshift/origin/pkg/image/importer/dockerv1client"
"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/imagesecret"
"github.com/openshift/origin/pkg/image/registry/imagesignature"
"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/imagestreamimport"
"github.com/openshift/origin/pkg/image/registry/imagestreammapping"
"github.com/openshift/origin/pkg/image/registry/imagestreamtag"
)
type ExtraConfig struct {
KubeAPIServerClientConfig *restclient.Config
LimitVerifier imageadmission.LimitVerifier
RegistryHostnameRetriever imageapi.RegistryHostnameRetriever
AllowedRegistriesForImport *configapi.AllowedRegistries
MaxImagesBulkImportedPerRepository int
// 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 ImageAPIServerConfig struct {
GenericConfig *genericapiserver.RecommendedConfig
ExtraConfig ExtraConfig
}
type ImageAPIServer 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 *ImageAPIServerConfig) Complete() completedConfig {
cfg := completedConfig{
c.GenericConfig.Complete(),
&c.ExtraConfig,
}
return cfg
}
// New returns a new instance of ImageAPIServer from the given config.
func (c completedConfig) New(delegationTarget genericapiserver.DelegationTarget) (*ImageAPIServer, error) {
genericServer, err := c.GenericConfig.New("image.openshift.io-apiserver", delegationTarget)
if err != nil {
return nil, err
}
s := &ImageAPIServer{
GenericAPIServer: genericServer,
}
v1Storage, err := c.V1RESTStorage()
if err != nil {
return nil, err
}
apiGroupInfo := genericapiserver.NewDefaultAPIGroupInfo(imageapiv1.GroupName, c.ExtraConfig.Registry, c.ExtraConfig.Scheme, metav1.ParameterCodec, c.ExtraConfig.Codecs)
apiGroupInfo.GroupMeta.GroupVersion = imageapiv1.SchemeGroupVersion
apiGroupInfo.VersionedResourcesStorageMap[imageapiv1.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) {
// TODO: allow the system CAs and the local CAs to be joined together.
importTransport, err := restclient.TransportFor(&restclient.Config{})
if err != nil {
return nil, fmt.Errorf("unable to configure a default transport for importing: %v", err)
}
insecureImportTransport, err := restclient.TransportFor(&restclient.Config{
TLSClientConfig: restclient.TLSClientConfig{
Insecure: true,
},
})
if err != nil {
return nil, fmt.Errorf("unable to configure a default transport for importing: %v", err)
}
coreClient, err := coreclient.NewForConfig(c.ExtraConfig.KubeAPIServerClientConfig)
if err != nil {
return nil, err
}
authorizationClient, err := authorizationclient.NewForConfig(c.ExtraConfig.KubeAPIServerClientConfig)
if err != nil {
return nil, err
}
imageClient, err := imageclient.NewForConfig(c.GenericConfig.LoopbackClientConfig)
if err != nil {
return nil, err
}
imageV1Client, err := imageclientv1.NewForConfig(c.GenericConfig.LoopbackClientConfig)
if err != nil {
return nil, err
}
imageStorage, err := imageetcd.NewREST(c.GenericConfig.RESTOptionsGetter)
if err != nil {
return nil, fmt.Errorf("error building REST storage: %v", err)
}
var whitelister whitelist.RegistryWhitelister
if c.ExtraConfig.AllowedRegistriesForImport != nil {
whitelister, err = whitelist.NewRegistryWhitelister(
*c.ExtraConfig.AllowedRegistriesForImport,
c.ExtraConfig.RegistryHostnameRetriever)
if err != nil {
return nil, fmt.Errorf("error building registry whitelister: %v", err)
}
} else {
whitelister = whitelist.WhitelistAllRegistries()
}
imageRegistry := image.NewRegistry(imageStorage)
imageSignatureStorage := imagesignature.NewREST(imageClient.Image())
imageStreamSecretsStorage := imagesecret.NewREST(coreClient)
imageStreamStorage, imageStreamStatusStorage, internalImageStreamStorage, err := imagestreametcd.NewREST(c.GenericConfig.RESTOptionsGetter, c.ExtraConfig.RegistryHostnameRetriever, authorizationClient.SubjectAccessReviews(), c.ExtraConfig.LimitVerifier, whitelister)
if err != nil {
return nil, fmt.Errorf("error building REST storage: %v", err)
}
imageStreamRegistry := imagestream.NewRegistry(imageStreamStorage, imageStreamStatusStorage, internalImageStreamStorage)
imageStreamMappingStorage := imagestreammapping.NewREST(imageRegistry, imageStreamRegistry, c.ExtraConfig.RegistryHostnameRetriever)
imageStreamTagStorage := imagestreamtag.NewREST(imageRegistry, imageStreamRegistry, whitelister)
importerCache, err := imageimporter.NewImageStreamLayerCache(imageimporter.DefaultImageStreamLayerCacheSize)
if err != nil {
return nil, fmt.Errorf("error building REST storage: %v", err)
}
importerFn := func(r importer.RepositoryRetriever) imageimporter.Interface {
return imageimporter.NewImageStreamImporter(r, c.ExtraConfig.MaxImagesBulkImportedPerRepository, flowcontrol.NewTokenBucketRateLimiter(2.0, 3), &importerCache)
}
importerDockerClientFn := func() dockerv1client.Client {
return dockerv1client.NewClient(20*time.Second, false)
}
imageStreamImportStorage := imagestreamimport.NewREST(
importerFn,
imageStreamRegistry,
internalImageStreamStorage,
imageStorage,
imageV1Client.ImageV1(),
importTransport,
insecureImportTransport,
importerDockerClientFn,
whitelister,
authorizationClient.SubjectAccessReviews())
imageStreamImageStorage := imagestreamimage.NewREST(imageRegistry, imageStreamRegistry)
v1Storage := map[string]rest.Storage{}
v1Storage["images"] = imageStorage
v1Storage["imagesignatures"] = imageSignatureStorage
v1Storage["imageStreams/secrets"] = imageStreamSecretsStorage
v1Storage["imageStreams"] = imageStreamStorage
v1Storage["imageStreams/status"] = imageStreamStatusStorage
v1Storage["imageStreamImports"] = imageStreamImportStorage
v1Storage["imageStreamImages"] = imageStreamImageStorage
v1Storage["imageStreamMappings"] = imageStreamMappingStorage
v1Storage["imageStreamTags"] = imageStreamTagStorage
return v1Storage, nil
}