forked from openshift/origin
-
Notifications
You must be signed in to change notification settings - Fork 1
/
factory_object_mapping.go
397 lines (367 loc) · 14.3 KB
/
factory_object_mapping.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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
package clientcmd
import (
"encoding/json"
"errors"
"fmt"
"sort"
"time"
"github.com/blang/semver"
"github.com/emicklei/go-restful-swagger12"
"k8s.io/apimachinery/pkg/api/meta"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/client-go/dynamic"
restclient "k8s.io/client-go/rest"
kapi "k8s.io/kubernetes/pkg/api"
"k8s.io/kubernetes/pkg/api/v1"
"k8s.io/kubernetes/pkg/api/validation"
"k8s.io/kubernetes/pkg/controller"
"k8s.io/kubernetes/pkg/kubectl"
kcmdutil "k8s.io/kubernetes/pkg/kubectl/cmd/util"
"k8s.io/kubernetes/pkg/kubectl/cmd/util/openapi"
"k8s.io/kubernetes/pkg/kubectl/resource"
kprinters "k8s.io/kubernetes/pkg/printers"
"github.com/openshift/origin/pkg/api/latest"
deployapi "github.com/openshift/origin/pkg/apps/apis/apps"
deploycmd "github.com/openshift/origin/pkg/apps/cmd"
appsclient "github.com/openshift/origin/pkg/apps/generated/internalclientset"
authorizationapi "github.com/openshift/origin/pkg/authorization/apis/authorization"
authorizationreaper "github.com/openshift/origin/pkg/authorization/reaper"
buildapi "github.com/openshift/origin/pkg/build/apis/build"
buildcmd "github.com/openshift/origin/pkg/build/cmd"
buildclient "github.com/openshift/origin/pkg/build/generated/internalclientset"
buildutil "github.com/openshift/origin/pkg/build/util"
"github.com/openshift/origin/pkg/client"
"github.com/openshift/origin/pkg/oc/cli/describe"
"github.com/openshift/origin/pkg/security/legacyclient"
userapi "github.com/openshift/origin/pkg/user/apis/user"
authenticationreaper "github.com/openshift/origin/pkg/user/reaper"
)
type ring1Factory struct {
clientAccessFactory ClientAccessFactory
kubeObjectMappingFactory kcmdutil.ObjectMappingFactory
}
func NewObjectMappingFactory(clientAccessFactory ClientAccessFactory) kcmdutil.ObjectMappingFactory {
return &ring1Factory{
clientAccessFactory: clientAccessFactory,
kubeObjectMappingFactory: kcmdutil.NewObjectMappingFactory(clientAccessFactory),
}
}
func (f *ring1Factory) Object() (meta.RESTMapper, runtime.ObjectTyper) {
return f.kubeObjectMappingFactory.Object()
}
func (f *ring1Factory) UnstructuredObject() (meta.RESTMapper, runtime.ObjectTyper, error) {
return f.kubeObjectMappingFactory.UnstructuredObject()
}
func (f *ring1Factory) CategoryExpander() resource.CategoryExpander {
upstreamExpander := f.kubeObjectMappingFactory.CategoryExpander()
var openshiftCategoryExpander resource.CategoryExpander
openshiftCategoryExpander = legacyOpeshiftCategoryExpander
discoveryClient, err := f.clientAccessFactory.DiscoveryClient()
if err == nil {
// wrap with discovery based filtering
openshiftCategoryExpander, err = resource.NewDiscoveryFilteredExpander(openshiftCategoryExpander, discoveryClient)
// you only have an error on missing discoveryClient, so this shouldn't fail. Check anyway.
kcmdutil.CheckErr(err)
}
return resource.UnionCategoryExpander{legacyOpeshiftCategoryExpander, upstreamExpander}
}
var legacyOpenshiftUserResources = []schema.GroupResource{
{Group: "", Resource: "buildconfigs"},
{Group: "", Resource: "builds"},
{Group: "", Resource: "imagestreams"},
{Group: "", Resource: "deploymentconfigs"},
{Group: "", Resource: "routes"},
}
// legacyOpeshiftCategoryExpander is the old hardcoded expansion for servers without listed categories
var legacyOpeshiftCategoryExpander resource.CategoryExpander = resource.SimpleCategoryExpander{
Expansions: map[string][]schema.GroupResource{
"all": legacyOpenshiftUserResources,
},
}
func (f *ring1Factory) ClientForMapping(mapping *meta.RESTMapping) (resource.RESTClient, error) {
if latest.OriginKind(mapping.GroupVersionKind) {
cfg, err := f.clientAccessFactory.ClientConfig()
if err != nil {
return nil, err
}
if err := client.SetOpenShiftDefaults(cfg); err != nil {
return nil, err
}
cfg.APIPath = "/apis"
if mapping.GroupVersionKind.Group == kapi.GroupName {
cfg.APIPath = "/oapi"
}
gv := mapping.GroupVersionKind.GroupVersion()
cfg.GroupVersion = &gv
return restclient.RESTClientFor(cfg)
}
return f.kubeObjectMappingFactory.ClientForMapping(mapping)
}
func (f *ring1Factory) UnstructuredClientForMapping(mapping *meta.RESTMapping) (resource.RESTClient, error) {
if latest.OriginKind(mapping.GroupVersionKind) {
cfg, err := f.clientAccessFactory.ClientConfig()
if err != nil {
return nil, err
}
if err := client.SetOpenShiftDefaults(cfg); err != nil {
return nil, err
}
cfg.APIPath = "/apis"
if mapping.GroupVersionKind.Group == kapi.GroupName {
cfg.APIPath = "/oapi"
}
gv := mapping.GroupVersionKind.GroupVersion()
cfg.ContentConfig = dynamic.ContentConfig()
cfg.GroupVersion = &gv
return restclient.RESTClientFor(cfg)
}
return f.kubeObjectMappingFactory.UnstructuredClientForMapping(mapping)
}
func (f *ring1Factory) Describer(mapping *meta.RESTMapping) (kprinters.Describer, error) {
// TODO we need to refactor the describer logic to handle misses or run serverside.
// for now we can special case our "sometimes origin, sometimes kube" resource
// I think it is correct for more code if this is NOT considered an origin type since
// it wasn't an origin type pre 3.6.
isSCC := mapping.GroupVersionKind.Kind == "SecurityContextConstraints"
if latest.OriginKind(mapping.GroupVersionKind) || isSCC {
oClient, kClient, err := f.clientAccessFactory.Clients()
if err != nil {
return nil, fmt.Errorf("unable to create client %s: %v", mapping.GroupVersionKind.Kind, err)
}
clientConfig, err := f.clientAccessFactory.ClientConfig()
if err != nil {
return nil, fmt.Errorf("unable to create client config %s: %v", mapping.GroupVersionKind.Kind, err)
}
mappingVersion := mapping.GroupVersionKind.GroupVersion()
cfg, err := f.clientAccessFactory.ClientConfigForVersion(&mappingVersion)
if err != nil {
return nil, fmt.Errorf("unable to load a client %s: %v", mapping.GroupVersionKind.Kind, err)
}
describer, ok := describe.DescriberFor(mapping.GroupVersionKind.GroupKind(), clientConfig, oClient, kClient, cfg.Host)
if !ok {
return nil, fmt.Errorf("no description has been implemented for %q", mapping.GroupVersionKind.Kind)
}
return describer, nil
}
return f.kubeObjectMappingFactory.Describer(mapping)
}
func (f *ring1Factory) LogsForObject(object, options runtime.Object, timeout time.Duration) (*restclient.Request, error) {
switch t := object.(type) {
case *deployapi.DeploymentConfig:
dopts, ok := options.(*deployapi.DeploymentLogOptions)
if !ok {
return nil, errors.New("provided options object is not a DeploymentLogOptions")
}
oc, _, err := f.clientAccessFactory.Clients()
if err != nil {
return nil, err
}
return oc.DeploymentLogs(t.Namespace).Get(t.Name, *dopts), nil
case *buildapi.Build:
bopts, ok := options.(*buildapi.BuildLogOptions)
if !ok {
return nil, errors.New("provided options object is not a BuildLogOptions")
}
if bopts.Version != nil {
return nil, errors.New("cannot specify a version and a build")
}
oc, _, err := f.clientAccessFactory.Clients()
if err != nil {
return nil, err
}
return oc.BuildLogs(t.Namespace).Get(t.Name, *bopts), nil
case *buildapi.BuildConfig:
bopts, ok := options.(*buildapi.BuildLogOptions)
if !ok {
return nil, errors.New("provided options object is not a BuildLogOptions")
}
oc, _, err := f.clientAccessFactory.Clients()
if err != nil {
return nil, err
}
builds, err := oc.Builds(t.Namespace).List(metav1.ListOptions{})
if err != nil {
return nil, err
}
builds.Items = buildapi.FilterBuilds(builds.Items, buildapi.ByBuildConfigPredicate(t.Name))
if len(builds.Items) == 0 {
return nil, fmt.Errorf("no builds found for %q", t.Name)
}
if bopts.Version != nil {
// If a version has been specified, try to get the logs from that build.
desired := buildutil.BuildNameForConfigVersion(t.Name, int(*bopts.Version))
return oc.BuildLogs(t.Namespace).Get(desired, *bopts), nil
}
sort.Sort(sort.Reverse(buildapi.BuildSliceByCreationTimestamp(builds.Items)))
return oc.BuildLogs(t.Namespace).Get(builds.Items[0].Name, *bopts), nil
default:
return f.kubeObjectMappingFactory.LogsForObject(object, options, timeout)
}
}
func (f *ring1Factory) Scaler(mapping *meta.RESTMapping) (kubectl.Scaler, error) {
if deployapi.IsKindOrLegacy("DeploymentConfig", mapping.GroupVersionKind.GroupKind()) {
_, kc, err := f.clientAccessFactory.Clients()
if err != nil {
return nil, err
}
config, err := f.clientAccessFactory.OpenShiftClientConfig().ClientConfig()
if err != nil {
return nil, err
}
return deploycmd.NewDeploymentConfigScaler(appsclient.NewForConfigOrDie(config), kc), nil
}
return f.kubeObjectMappingFactory.Scaler(mapping)
}
func (f *ring1Factory) Reaper(mapping *meta.RESTMapping) (kubectl.Reaper, error) {
gk := mapping.GroupVersionKind.GroupKind()
switch {
case deployapi.IsKindOrLegacy("DeploymentConfig", gk):
_, kc, err := f.clientAccessFactory.Clients()
if err != nil {
return nil, err
}
config, err := f.clientAccessFactory.OpenShiftClientConfig().ClientConfig()
if err != nil {
return nil, err
}
return deploycmd.NewDeploymentConfigReaper(appsclient.NewForConfigOrDie(config), kc), nil
case authorizationapi.IsKindOrLegacy("Role", gk):
oc, _, err := f.clientAccessFactory.Clients()
if err != nil {
return nil, err
}
return authorizationreaper.NewRoleReaper(oc, oc), nil
case authorizationapi.IsKindOrLegacy("ClusterRole", gk):
oc, _, err := f.clientAccessFactory.Clients()
if err != nil {
return nil, err
}
return authorizationreaper.NewClusterRoleReaper(oc, oc, oc), nil
case userapi.IsKindOrLegacy("User", gk):
oc, kc, err := f.clientAccessFactory.Clients()
if err != nil {
return nil, err
}
return authenticationreaper.NewUserReaper(
client.UsersInterface(oc),
client.GroupsInterface(oc),
client.ClusterRoleBindingsInterface(oc),
client.RoleBindingsNamespacer(oc),
client.OAuthClientAuthorizationsInterface(oc),
legacyclient.NewFromClient(kc.Core().RESTClient()),
), nil
case userapi.IsKindOrLegacy("Group", gk):
oc, kc, err := f.clientAccessFactory.Clients()
if err != nil {
return nil, err
}
return authenticationreaper.NewGroupReaper(
client.GroupsInterface(oc),
client.ClusterRoleBindingsInterface(oc),
client.RoleBindingsNamespacer(oc),
legacyclient.NewFromClient(kc.Core().RESTClient()),
), nil
case buildapi.IsKindOrLegacy("BuildConfig", gk):
config, err := f.clientAccessFactory.OpenShiftClientConfig().ClientConfig()
if err != nil {
return nil, err
}
return buildcmd.NewBuildConfigReaper(buildclient.NewForConfigOrDie(config)), nil
}
return f.kubeObjectMappingFactory.Reaper(mapping)
}
func (f *ring1Factory) HistoryViewer(mapping *meta.RESTMapping) (kubectl.HistoryViewer, error) {
if deployapi.IsKindOrLegacy("DeploymentConfig", mapping.GroupVersionKind.GroupKind()) {
_, kc, err := f.clientAccessFactory.Clients()
if err != nil {
return nil, err
}
return deploycmd.NewDeploymentConfigHistoryViewer(kc), nil
}
return f.kubeObjectMappingFactory.HistoryViewer(mapping)
}
func (f *ring1Factory) Rollbacker(mapping *meta.RESTMapping) (kubectl.Rollbacker, error) {
if deployapi.IsKindOrLegacy("DeploymentConfig", mapping.GroupVersionKind.GroupKind()) {
config, err := f.clientAccessFactory.OpenShiftClientConfig().ClientConfig()
if err != nil {
return nil, err
}
return deploycmd.NewDeploymentConfigRollbacker(appsclient.NewForConfigOrDie(config)), nil
}
return f.kubeObjectMappingFactory.Rollbacker(mapping)
}
func (f *ring1Factory) StatusViewer(mapping *meta.RESTMapping) (kubectl.StatusViewer, error) {
config, err := f.clientAccessFactory.OpenShiftClientConfig().ClientConfig()
if err != nil {
return nil, err
}
if deployapi.IsKindOrLegacy("DeploymentConfig", mapping.GroupVersionKind.GroupKind()) {
return deploycmd.NewDeploymentConfigStatusViewer(appsclient.NewForConfigOrDie(config)), nil
}
return f.kubeObjectMappingFactory.StatusViewer(mapping)
}
func (f *ring1Factory) AttachablePodForObject(object runtime.Object, timeout time.Duration) (*kapi.Pod, error) {
switch t := object.(type) {
case *deployapi.DeploymentConfig:
_, kc, err := f.clientAccessFactory.Clients()
if err != nil {
return nil, err
}
selector := labels.SelectorFromSet(t.Spec.Selector)
f := func(pods []*v1.Pod) sort.Interface { return sort.Reverse(controller.ActivePods(pods)) }
pod, _, err := kcmdutil.GetFirstPod(kc.Core(), t.Namespace, selector, 1*time.Minute, f)
return pod, err
default:
return f.kubeObjectMappingFactory.AttachablePodForObject(object, timeout)
}
}
func (f *ring1Factory) Validator(validate bool, cacheDir string) (validation.Schema, error) {
return f.kubeObjectMappingFactory.Validator(validate, cacheDir)
}
func (f *ring1Factory) SwaggerSchema(gvk schema.GroupVersionKind) (*swagger.ApiDeclaration, error) {
if !latest.OriginLegacyKind(gvk) {
return f.kubeObjectMappingFactory.SwaggerSchema(gvk)
}
// TODO: we need to register the OpenShift API under the Kube group, and start returning the OpenShift
// group from the scheme.
oc, _, err := f.clientAccessFactory.Clients()
if err != nil {
return nil, err
}
return f.OriginSwaggerSchema(oc.RESTClient, gvk.GroupVersion())
}
func (f *ring1Factory) OpenAPISchema(cacheDir string) (*openapi.Resources, error) {
return f.kubeObjectMappingFactory.OpenAPISchema(cacheDir)
}
// OriginSwaggerSchema returns a swagger API doc for an Origin schema under the /oapi prefix.
func (f *ring1Factory) OriginSwaggerSchema(client *restclient.RESTClient, version schema.GroupVersion) (*swagger.ApiDeclaration, error) {
if version.Empty() {
return nil, fmt.Errorf("groupVersion cannot be empty")
}
body, err := client.Get().AbsPath("/").Suffix("swaggerapi", "oapi", version.Version).Do().Raw()
if err != nil {
return nil, err
}
var schema swagger.ApiDeclaration
err = json.Unmarshal(body, &schema)
if err != nil {
return nil, fmt.Errorf("got '%s': %v", string(body), err)
}
return &schema, nil
}
// useDiscoveryRESTMapper checks the server version to see if its recent enough to have
// enough discovery information avaiable to reliably build a RESTMapper. If not, use the
// hardcoded mapper in this client (legacy behavior)
func useDiscoveryRESTMapper(serverVersion string) bool {
serverSemVer, err := semver.Parse(serverVersion[1:])
if err != nil {
return false
}
if serverSemVer.LT(semver.MustParse("1.3.0")) {
return false
}
return true
}