forked from openshift/origin
-
Notifications
You must be signed in to change notification settings - Fork 1
/
factory_object_mapping.go
413 lines (384 loc) · 14.9 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
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
package clientcmd
import (
"errors"
"fmt"
"sort"
"time"
"k8s.io/kubernetes/pkg/kubectl/categories"
"k8s.io/api/core/v1"
"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/client-go/dynamic"
restclient "k8s.io/client-go/rest"
kapi "k8s.io/kubernetes/pkg/apis/core"
"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"
"k8s.io/kubernetes/pkg/kubectl/validation"
kprinters "k8s.io/kubernetes/pkg/printers"
"github.com/openshift/origin/pkg/api/latest"
appsapi "github.com/openshift/origin/pkg/apps/apis/apps"
appsmanualclient "github.com/openshift/origin/pkg/apps/client/internalversion"
appscmd "github.com/openshift/origin/pkg/apps/cmd"
appsclient "github.com/openshift/origin/pkg/apps/generated/internalclientset"
appsutil "github.com/openshift/origin/pkg/apps/util"
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"
buildmanualclient "github.com/openshift/origin/pkg/build/client/internalversion"
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"
configcmd "github.com/openshift/origin/pkg/config/cmd"
imageapi "github.com/openshift/origin/pkg/image/apis/image"
"github.com/openshift/origin/pkg/oc/cli/describe"
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) CategoryExpander() categories.CategoryExpander {
return f.kubeObjectMappingFactory.CategoryExpander()
}
func (f *ring1Factory) ClientForMapping(mapping *meta.RESTMapping) (resource.RESTClient, error) {
// TODO only do this for legacy kinds
if latest.OriginKind(mapping.GroupVersionKind) {
cfg, err := f.clientAccessFactory.ClientConfig()
if err != nil {
return nil, err
}
if err := configcmd.SetLegacyOpenShiftDefaults(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) {
// TODO only do this for legacy kinds
if latest.OriginKind(mapping.GroupVersionKind) {
cfg, err := f.clientAccessFactory.ClientConfig()
if err != nil {
return nil, err
}
if err := configcmd.SetLegacyOpenShiftDefaults(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 {
kClient, err := f.clientAccessFactory.ClientSet()
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, 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 *appsapi.DeploymentConfig:
dopts, ok := options.(*appsapi.DeploymentLogOptions)
if !ok {
return nil, errors.New("provided options object is not a DeploymentLogOptions")
}
appsClient, err := f.clientAccessFactory.OpenshiftInternalAppsClient()
if err != nil {
return nil, err
}
return appsmanualclient.NewRolloutLogClient(appsClient.Apps().RESTClient(), t.Namespace).Logs(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")
}
buildClient, err := f.clientAccessFactory.OpenshiftInternalBuildClient()
if err != nil {
return nil, err
}
return buildmanualclient.NewBuildLogClient(buildClient.Build().RESTClient(), t.Namespace).Logs(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")
}
buildClient, err := f.clientAccessFactory.OpenshiftInternalBuildClient()
if err != nil {
return nil, err
}
logClient := buildmanualclient.NewBuildLogClient(buildClient.Build().RESTClient(), t.Namespace)
builds, err := buildClient.Build().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 logClient.Logs(desired, *bopts), nil
}
sort.Sort(sort.Reverse(buildapi.BuildSliceByCreationTimestamp(builds.Items)))
return logClient.Logs(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 appsapi.IsKindOrLegacy("DeploymentConfig", mapping.GroupVersionKind.GroupKind()) {
kc, err := f.clientAccessFactory.ClientSet()
if err != nil {
return nil, err
}
config, err := f.clientAccessFactory.OpenShiftClientConfig().ClientConfig()
if err != nil {
return nil, err
}
return appscmd.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 appsapi.IsKindOrLegacy("DeploymentConfig", gk):
kc, err := f.clientAccessFactory.ClientSet()
if err != nil {
return nil, err
}
config, err := f.clientAccessFactory.OpenShiftClientConfig().ClientConfig()
if err != nil {
return nil, err
}
return appscmd.NewDeploymentConfigReaper(appsclient.NewForConfigOrDie(config), kc), nil
case authorizationapi.IsKindOrLegacy("Role", gk):
authClient, err := f.clientAccessFactory.OpenshiftInternalAuthorizationClient()
if err != nil {
return nil, err
}
return authorizationreaper.NewRoleReaper(authClient.Authorization(), authClient.Authorization()), nil
case authorizationapi.IsKindOrLegacy("ClusterRole", gk):
authClient, err := f.clientAccessFactory.OpenshiftInternalAuthorizationClient()
if err != nil {
return nil, err
}
return authorizationreaper.NewClusterRoleReaper(authClient.Authorization(), authClient.Authorization(), authClient.Authorization()), nil
case userapi.IsKindOrLegacy("User", gk):
userClient, err := f.clientAccessFactory.OpenshiftInternalUserClient()
if err != nil {
return nil, err
}
authClient, err := f.clientAccessFactory.OpenshiftInternalAuthorizationClient()
if err != nil {
return nil, err
}
oauthClient, err := f.clientAccessFactory.OpenshiftInternalOAuthClient()
if err != nil {
return nil, err
}
securityClient, err := f.clientAccessFactory.OpenshiftInternalSecurityClient()
if err != nil {
return nil, err
}
return authenticationreaper.NewUserReaper(
userClient,
userClient,
authClient,
authClient,
oauthClient,
securityClient.Security().SecurityContextConstraints(),
), nil
case userapi.IsKindOrLegacy("Group", gk):
userClient, err := f.clientAccessFactory.OpenshiftInternalUserClient()
if err != nil {
return nil, err
}
authClient, err := f.clientAccessFactory.OpenshiftInternalAuthorizationClient()
if err != nil {
return nil, err
}
securityClient, err := f.clientAccessFactory.OpenshiftInternalSecurityClient()
if err != nil {
return nil, err
}
return authenticationreaper.NewGroupReaper(
userClient,
authClient,
authClient,
securityClient.Security().SecurityContextConstraints(),
), 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 appsapi.IsKindOrLegacy("DeploymentConfig", mapping.GroupVersionKind.GroupKind()) {
kc, err := f.clientAccessFactory.ClientSet()
if err != nil {
return nil, err
}
return appscmd.NewDeploymentConfigHistoryViewer(kc), nil
}
return f.kubeObjectMappingFactory.HistoryViewer(mapping)
}
func (f *ring1Factory) Rollbacker(mapping *meta.RESTMapping) (kubectl.Rollbacker, error) {
if appsapi.IsKindOrLegacy("DeploymentConfig", mapping.GroupVersionKind.GroupKind()) {
config, err := f.clientAccessFactory.OpenShiftClientConfig().ClientConfig()
if err != nil {
return nil, err
}
return appscmd.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 appsapi.IsKindOrLegacy("DeploymentConfig", mapping.GroupVersionKind.GroupKind()) {
return appscmd.NewDeploymentConfigStatusViewer(appsclient.NewForConfigOrDie(config)), nil
}
return f.kubeObjectMappingFactory.StatusViewer(mapping)
}
// ApproximatePodTemplateForObject returns a pod template object for the provided source.
// It may return both an error and a object. It attempt to return the best possible template
// available at the current time.
func (f *ring1Factory) ApproximatePodTemplateForObject(object runtime.Object) (*kapi.PodTemplateSpec, error) {
switch t := object.(type) {
case *imageapi.ImageStreamTag:
// create a minimal pod spec that uses the image referenced by the istag without any introspection
// it possible that we could someday do a better job introspecting it
return &kapi.PodTemplateSpec{
Spec: kapi.PodSpec{
RestartPolicy: kapi.RestartPolicyNever,
Containers: []kapi.Container{
{Name: "container-00", Image: t.Image.DockerImageReference},
},
},
}, nil
case *imageapi.ImageStreamImage:
// create a minimal pod spec that uses the image referenced by the istag without any introspection
// it possible that we could someday do a better job introspecting it
return &kapi.PodTemplateSpec{
Spec: kapi.PodSpec{
RestartPolicy: kapi.RestartPolicyNever,
Containers: []kapi.Container{
{Name: "container-00", Image: t.Image.DockerImageReference},
},
},
}, nil
case *appsapi.DeploymentConfig:
fallback := t.Spec.Template
kc, err := f.clientAccessFactory.ClientSet()
if err != nil {
return fallback, err
}
latestDeploymentName := appsutil.LatestDeploymentNameForConfig(t)
deployment, err := kc.Core().ReplicationControllers(t.Namespace).Get(latestDeploymentName, metav1.GetOptions{})
if err != nil {
return fallback, err
}
fallback = deployment.Spec.Template
pods, err := kc.Core().Pods(deployment.Namespace).List(metav1.ListOptions{LabelSelector: labels.SelectorFromSet(deployment.Spec.Selector).String()})
if err != nil {
return fallback, err
}
// If we have any pods available, find the newest
// pod with regards to our most recent deployment.
// If the fallback PodTemplateSpec is nil, prefer
// the newest pod available.
for i := range pods.Items {
pod := &pods.Items[i]
if fallback == nil || pod.CreationTimestamp.Before(&fallback.CreationTimestamp) {
fallback = &kapi.PodTemplateSpec{
ObjectMeta: pod.ObjectMeta,
Spec: pod.Spec,
}
}
}
return fallback, nil
default:
return f.kubeObjectMappingFactory.ApproximatePodTemplateForObject(object)
}
}
func (f *ring1Factory) AttachablePodForObject(object runtime.Object, timeout time.Duration) (*kapi.Pod, error) {
switch t := object.(type) {
case *appsapi.DeploymentConfig:
kc, err := f.clientAccessFactory.ClientSet()
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.String(), 1*time.Minute, f)
return pod, err
default:
return f.kubeObjectMappingFactory.AttachablePodForObject(object, timeout)
}
}
func (f *ring1Factory) Validator(validate bool) (validation.Schema, error) {
return f.kubeObjectMappingFactory.Validator(validate)
}
func (f *ring1Factory) OpenAPISchema() (openapi.Resources, error) {
return f.kubeObjectMappingFactory.OpenAPISchema()
}