forked from openshift/origin
/
factory.go
159 lines (138 loc) · 5.49 KB
/
factory.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
package clientcmd
import (
"fmt"
"os"
"github.com/GoogleCloudPlatform/kubernetes/pkg/api"
"github.com/GoogleCloudPlatform/kubernetes/pkg/api/meta"
kclient "github.com/GoogleCloudPlatform/kubernetes/pkg/client"
"github.com/GoogleCloudPlatform/kubernetes/pkg/client/clientcmd"
"github.com/GoogleCloudPlatform/kubernetes/pkg/kubectl"
kubecmd "github.com/GoogleCloudPlatform/kubernetes/pkg/kubectl/cmd"
"github.com/GoogleCloudPlatform/kubernetes/pkg/kubectl/resource"
"github.com/GoogleCloudPlatform/kubernetes/pkg/runtime"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
"github.com/openshift/origin/pkg/api/latest"
"github.com/openshift/origin/pkg/client"
"github.com/openshift/origin/pkg/cmd/cli/describe"
)
const defaultClusterURL = "https://localhost:8443"
// NewFactory creates a default Factory for commands that should share identical server
// connection behavior. Most commands should use this method to get a factory.
func New(flags *pflag.FlagSet) *Factory {
// Override global default to https and port 8443
clientcmd.DefaultCluster.Server = defaultClusterURL
// TODO: there should be two client configs, one for OpenShift, and one for Kubernetes
clientConfig := DefaultClientConfig(flags)
f := NewFactory(clientConfig)
f.BindFlags(flags)
return f
}
// Copy of kubectl/cmd/DefaultClientConfig, using NewNonInteractiveDeferredLoadingClientConfig
func DefaultClientConfig(flags *pflag.FlagSet) clientcmd.ClientConfig {
loadingRules := clientcmd.NewClientConfigLoadingRules()
loadingRules.EnvVarPath = os.Getenv(clientcmd.RecommendedConfigPathEnvVar)
flags.StringVar(&loadingRules.CommandLinePath, "kubeconfig", "", "Path to the kubeconfig file to use for CLI requests.")
overrides := &clientcmd.ConfigOverrides{}
overrideFlags := clientcmd.RecommendedConfigOverrideFlags("")
overrideFlags.ContextOverrideFlags.NamespaceShort = "n"
clientcmd.BindOverrideFlags(overrides, flags, overrideFlags)
clientConfig := clientcmd.NewNonInteractiveDeferredLoadingClientConfig(loadingRules, overrides)
return clientConfig
}
// Factory provides common options for OpenShift commands
type Factory struct {
*kubecmd.Factory
OpenShiftClientConfig clientcmd.ClientConfig
}
// NewFactory creates an object that holds common methods across all OpenShift commands
func NewFactory(clientConfig clientcmd.ClientConfig) *Factory {
mapper := ShortcutExpander{kubectl.ShortcutExpander{latest.RESTMapper}}
w := &Factory{kubecmd.NewFactory(clientConfig), clientConfig}
w.Object = func(cmd *cobra.Command) (meta.RESTMapper, runtime.ObjectTyper) {
return mapper, api.Scheme
}
// Save original RESTClient function
kRESTClientFunc := w.Factory.RESTClient
w.RESTClient = func(cmd *cobra.Command, mapping *meta.RESTMapping) (resource.RESTClient, error) {
if latest.OriginKind(mapping.Kind, mapping.APIVersion) {
cfg, err := w.OpenShiftClientConfig.ClientConfig()
if err != nil {
return nil, fmt.Errorf("unable to find client config %s: %v", mapping.Kind, err)
}
cli, err := client.New(cfg)
if err != nil {
return nil, fmt.Errorf("unable to create client %s: %v", mapping.Kind, err)
}
return cli.RESTClient, nil
}
return kRESTClientFunc(cmd, mapping)
}
// Save original Describer function
kDescriberFunc := w.Factory.Describer
w.Describer = func(cmd *cobra.Command, mapping *meta.RESTMapping) (kubectl.Describer, error) {
if latest.OriginKind(mapping.Kind, mapping.APIVersion) {
cfg, err := w.OpenShiftClientConfig.ClientConfig()
if err != nil {
return nil, fmt.Errorf("unable to describe %s: %v", mapping.Kind, err)
}
cli, err := client.New(cfg)
if err != nil {
return nil, fmt.Errorf("unable to describe %s: %v", mapping.Kind, err)
}
kubeClient, err := kclient.New(cfg)
if err != nil {
return nil, fmt.Errorf("unable to describe %s: %v", mapping.Kind, err)
}
describer, ok := describe.DescriberFor(mapping.Kind, cli, kubeClient, "")
if !ok {
return nil, fmt.Errorf("no description has been implemented for %q", mapping.Kind)
}
return describer, nil
}
return kDescriberFunc(cmd, mapping)
}
w.Printer = func(cmd *cobra.Command, mapping *meta.RESTMapping, noHeaders bool) (kubectl.ResourcePrinter, error) {
return describe.NewHumanReadablePrinter(noHeaders), nil
}
return w
}
// Clients returns an OpenShift and Kubernetes client.
func (f *Factory) Clients(cmd *cobra.Command) (*client.Client, *kclient.Client, error) {
os, err := f.OpenShiftClientConfig.ClientConfig()
if err != nil {
return nil, nil, err
}
oc, err := client.New(os)
if err != nil {
return nil, nil, err
}
kc, err := f.Client(cmd)
if err != nil {
return nil, nil, err
}
return oc, kc, nil
}
// ShortcutExpander is a RESTMapper that can be used for OpenShift resources.
type ShortcutExpander struct {
meta.RESTMapper
}
// VersionAndKindForResource implements meta.RESTMapper. It expands the resource first, then invokes the wrapped
// mapper.
func (e ShortcutExpander) VersionAndKindForResource(resource string) (defaultVersion, kind string, err error) {
resource = expandResourceShortcut(resource)
return e.RESTMapper.VersionAndKindForResource(resource)
}
// expandResourceShortcut will return the expanded version of resource
// (something that a pkg/api/meta.RESTMapper can understand), if it is
// indeed a shortcut. Otherwise, will return resource unmodified.
func expandResourceShortcut(resource string) string {
shortForms := map[string]string{
"dc": "deploymentConfigs",
"bc": "buildConfigs",
}
if expanded, ok := shortForms[resource]; ok {
return expanded
}
return resource
}