// NewFactory creates an object that holds common methods across all OpenShift commands func NewFactory(clientConfig kclientcmd.ClientConfig) *Factory { mapper := ShortcutExpander{kubectl.ShortcutExpander{latest.RESTMapper}} clients := &clientCache{ clients: make(map[string]*client.Client), loader: clientConfig, } generators := map[string]kubectl.Generator{ "route/v1": routegen.RouteGenerator{}, } w := &Factory{ Factory: cmdutil.NewFactory(clientConfig), OpenShiftClientConfig: clientConfig, clients: clients, } w.Object = func() (meta.RESTMapper, runtime.ObjectTyper) { if cfg, err := clientConfig.ClientConfig(); err == nil { return kubectl.OutputVersionMapper{mapper, cfg.Version}, api.Scheme } return mapper, api.Scheme } kRESTClient := w.Factory.RESTClient w.RESTClient = func(mapping *meta.RESTMapping) (resource.RESTClient, error) { if latest.OriginKind(mapping.Kind, mapping.APIVersion) { client, err := clients.ClientForVersion(mapping.APIVersion) if err != nil { return nil, err } return client.RESTClient, nil } return kRESTClient(mapping) } // Save original Describer function kDescriberFunc := w.Factory.Describer w.Describer = func(mapping *meta.RESTMapping) (kubectl.Describer, error) { if latest.OriginKind(mapping.Kind, mapping.APIVersion) { oClient, kClient, err := w.Clients() if err != nil { return nil, fmt.Errorf("unable to create client %s: %v", mapping.Kind, err) } cfg, err := clients.ClientConfigForVersion(mapping.APIVersion) if err != nil { return nil, fmt.Errorf("unable to load a client %s: %v", mapping.Kind, err) } describer, ok := describe.DescriberFor(mapping.Kind, oClient, kClient, cfg.Host) if !ok { return nil, fmt.Errorf("no description has been implemented for %q", mapping.Kind) } return describer, nil } return kDescriberFunc(mapping) } w.Scaler = func(mapping *meta.RESTMapping) (kubectl.Scaler, error) { oc, kc, err := w.Clients() if err != nil { return nil, err } return deploy.ScalerFor(mapping.Kind, oc, kc) } w.Reaper = func(mapping *meta.RESTMapping) (kubectl.Reaper, error) { oc, kc, err := w.Clients() if err != nil { return nil, err } return deployreaper.ReaperFor(mapping.Kind, oc, kc) } kGeneratorFunc := w.Factory.Generator w.Generator = func(name string) (kubectl.Generator, bool) { if generator, ok := generators[name]; ok { return generator, true } return kGeneratorFunc(name) } w.PodSelectorForObject = func(object runtime.Object) (string, error) { switch t := object.(type) { case *deployapi.DeploymentConfig: return kubectl.MakeLabels(t.Template.ControllerTemplate.Selector), nil case *api.ReplicationController: return kubectl.MakeLabels(t.Spec.Selector), nil case *api.Pod: if len(t.Labels) == 0 { return "", fmt.Errorf("the pod has no labels and cannot be exposed") } return kubectl.MakeLabels(t.Labels), nil case *api.Service: if t.Spec.Selector == nil { return "", fmt.Errorf("the service has no pod selector set") } return kubectl.MakeLabels(t.Spec.Selector), nil default: kind, err := meta.NewAccessor().Kind(object) if err != nil { return "", err } return "", fmt.Errorf("it is not possible to get a pod selector from %s", kind) } } w.PortsForObject = func(object runtime.Object) ([]string, error) { switch t := object.(type) { case *deployapi.DeploymentConfig: return getPorts(t.Template.ControllerTemplate.Template.Spec), nil case *api.ReplicationController: return getPorts(t.Spec.Template.Spec), nil case *api.Pod: return getPorts(t.Spec), nil default: kind, err := meta.NewAccessor().Kind(object) if err != nil { return nil, err } return nil, fmt.Errorf("it is not possible to get ports from %s", kind) } } w.Printer = func(mapping *meta.RESTMapping, noHeaders, withNamespace bool) (kubectl.ResourcePrinter, error) { return describe.NewHumanReadablePrinter(noHeaders, withNamespace), nil } return w }
// NewFactory creates an object that holds common methods across all OpenShift commands func NewFactory(clientConfig kclientcmd.ClientConfig) *Factory { mapper := ShortcutExpander{kubectl.ShortcutExpander{latest.RESTMapper}} clients := &clientCache{ clients: make(map[string]*client.Client), loader: clientConfig, } generators := map[string]kubectl.Generator{ "route/v1": routegen.RouteGenerator{}, } w := &Factory{ Factory: cmdutil.NewFactory(clientConfig), OpenShiftClientConfig: clientConfig, clients: clients, } w.Object = func() (meta.RESTMapper, runtime.ObjectTyper) { if cfg, err := clientConfig.ClientConfig(); err == nil { return kubectl.OutputVersionMapper{mapper, cfg.Version}, api.Scheme } return mapper, api.Scheme } kRESTClient := w.Factory.RESTClient w.RESTClient = func(mapping *meta.RESTMapping) (resource.RESTClient, error) { if latest.OriginKind(mapping.Kind, mapping.APIVersion) { client, err := clients.ClientForVersion(mapping.APIVersion) if err != nil { return nil, err } return client.RESTClient, nil } return kRESTClient(mapping) } // Save original Describer function kDescriberFunc := w.Factory.Describer w.Describer = func(mapping *meta.RESTMapping) (kubectl.Describer, error) { if latest.OriginKind(mapping.Kind, mapping.APIVersion) { oClient, kClient, err := w.Clients() if err != nil { return nil, fmt.Errorf("unable to create client %s: %v", mapping.Kind, err) } cfg, err := clients.ClientConfigForVersion(mapping.APIVersion) if err != nil { return nil, fmt.Errorf("unable to load a client %s: %v", mapping.Kind, err) } describer, ok := describe.DescriberFor(mapping.Kind, oClient, kClient, cfg.Host) if !ok { return nil, fmt.Errorf("no description has been implemented for %q", mapping.Kind) } return describer, nil } return kDescriberFunc(mapping) } kScalerFunc := w.Factory.Scaler w.Scaler = func(mapping *meta.RESTMapping) (kubectl.Scaler, error) { oc, kc, err := w.Clients() if err != nil { return nil, err } if mapping.Kind == "DeploymentConfig" { return deployscaler.NewDeploymentConfigScaler(oc, kc), nil } return kScalerFunc(mapping) } kReaperFunc := w.Factory.Reaper w.Reaper = func(mapping *meta.RESTMapping) (kubectl.Reaper, error) { oc, kc, err := w.Clients() if err != nil { return nil, err } if mapping.Kind == "DeploymentConfig" { return deployreaper.NewDeploymentConfigReaper(oc, kc), nil } return kReaperFunc(mapping) } kGeneratorFunc := w.Factory.Generator w.Generator = func(name string) (kubectl.Generator, bool) { if generator, ok := generators[name]; ok { return generator, true } return kGeneratorFunc(name) } kPodSelectorForObjectFunc := w.Factory.PodSelectorForObject w.PodSelectorForObject = func(object runtime.Object) (string, error) { switch t := object.(type) { case *deployapi.DeploymentConfig: return kubectl.MakeLabels(t.Template.ControllerTemplate.Selector), nil default: return kPodSelectorForObjectFunc(object) } } kPortsForObjectFunc := w.Factory.PortsForObject w.PortsForObject = func(object runtime.Object) ([]string, error) { switch t := object.(type) { case *deployapi.DeploymentConfig: return getPorts(t.Template.ControllerTemplate.Template.Spec), nil default: return kPortsForObjectFunc(object) } } w.Printer = func(mapping *meta.RESTMapping, noHeaders, withNamespace, wide bool, columnLabels []string) (kubectl.ResourcePrinter, error) { return describe.NewHumanReadablePrinter(noHeaders, withNamespace, wide, columnLabels), nil } return w }