Commit 8a4f2259 authored by Clayton Coleman's avatar Clayton Coleman

Make expandResourceShortcuts part of RESTMapper on client

parent a1ee782d
...@@ -19,11 +19,15 @@ package main ...@@ -19,11 +19,15 @@ package main
import ( import (
"os" "os"
"github.com/GoogleCloudPlatform/kubernetes/pkg/client/clientcmd"
"github.com/GoogleCloudPlatform/kubernetes/pkg/kubectl/cmd" "github.com/GoogleCloudPlatform/kubernetes/pkg/kubectl/cmd"
"github.com/golang/glog"
) )
func main() { func main() {
clientBuilder := clientcmd.NewInteractiveClientConfig(clientcmd.Config{}, "", &clientcmd.ConfigOverrides{}, os.Stdin) cmd := cmd.NewFactory().NewKubectlCommand(os.Stdout)
cmd.NewFactory(clientBuilder).Run(os.Stdout) if err := cmd.Execute(); err != nil {
glog.Errorf("error: %v", err)
os.Exit(1)
}
} }
...@@ -100,7 +100,7 @@ func NewTestFactory() (*Factory, *testFactory, runtime.Codec) { ...@@ -100,7 +100,7 @@ func NewTestFactory() (*Factory, *testFactory, runtime.Codec) {
return &Factory{ return &Factory{
Mapper: mapper, Mapper: mapper,
Typer: scheme, Typer: scheme,
Client: func(*cobra.Command, *meta.RESTMapping) (kubectl.RESTClient, error) { RESTClient: func(*cobra.Command, *meta.RESTMapping) (kubectl.RESTClient, error) {
return t.Client, t.Err return t.Client, t.Err
}, },
Describer: func(*cobra.Command, *meta.RESTMapping) (kubectl.Describer, error) { Describer: func(*cobra.Command, *meta.RESTMapping) (kubectl.Describer, error) {
......
...@@ -46,7 +46,7 @@ Examples: ...@@ -46,7 +46,7 @@ Examples:
schema, err := f.Validator(cmd) schema, err := f.Validator(cmd)
checkErr(err) checkErr(err)
mapping, namespace, name, data := ResourceFromFile(cmd, filename, f.Typer, f.Mapper, schema) mapping, namespace, name, data := ResourceFromFile(cmd, filename, f.Typer, f.Mapper, schema)
client, err := f.Client(cmd, mapping) client, err := f.RESTClient(cmd, mapping)
checkErr(err) checkErr(err)
// use the default namespace if not specified, or check for conflict with the file's namespace // use the default namespace if not specified, or check for conflict with the file's namespace
......
...@@ -79,7 +79,7 @@ Examples: ...@@ -79,7 +79,7 @@ Examples:
<creates all resources listed in config.json>`, <creates all resources listed in config.json>`,
Run: func(cmd *cobra.Command, args []string) { Run: func(cmd *cobra.Command, args []string) {
clientFunc := func(mapper *meta.RESTMapping) (config.RESTClientPoster, error) { clientFunc := func(mapper *meta.RESTMapping) (config.RESTClientPoster, error) {
client, err := f.Client(cmd, mapper) client, err := f.RESTClient(cmd, mapper)
checkErr(err) checkErr(err)
return client, nil return client, nil
} }
......
...@@ -59,7 +59,7 @@ Examples: ...@@ -59,7 +59,7 @@ Examples:
checkErr(err) checkErr(err)
selector := GetFlagString(cmd, "selector") selector := GetFlagString(cmd, "selector")
found := 0 found := 0
ResourcesFromArgsOrFile(cmd, args, filename, selector, f.Typer, f.Mapper, f.Client, schema).Visit(func(r *resource.Info) error { ResourcesFromArgsOrFile(cmd, args, filename, selector, f.Typer, f.Mapper, f.RESTClient, schema).Visit(func(r *resource.Info) error {
found++ found++
if err := resource.NewHelper(r.Client, r.Mapping).Delete(r.Namespace, r.Name); err != nil { if err := resource.NewHelper(r.Client, r.Mapping).Delete(r.Namespace, r.Name); err != nil {
return err return err
......
...@@ -56,7 +56,7 @@ Examples: ...@@ -56,7 +56,7 @@ Examples:
labelSelector, err := labels.ParseSelector(selector) labelSelector, err := labels.ParseSelector(selector)
checkErr(err) checkErr(err)
client, err := f.Client(cmd, mapping) client, err := f.RESTClient(cmd, mapping)
checkErr(err) checkErr(err)
outputFormat := GetFlagString(cmd, "output") outputFormat := GetFlagString(cmd, "output")
......
...@@ -21,8 +21,6 @@ import ( ...@@ -21,8 +21,6 @@ import (
"strconv" "strconv"
"github.com/spf13/cobra" "github.com/spf13/cobra"
"github.com/GoogleCloudPlatform/kubernetes/pkg/client"
) )
func (f *Factory) NewCmdLog(out io.Writer) *cobra.Command { func (f *Factory) NewCmdLog(out io.Writer) *cobra.Command {
...@@ -46,9 +44,7 @@ Examples: ...@@ -46,9 +44,7 @@ Examples:
} }
namespace := GetKubeNamespace(cmd) namespace := GetKubeNamespace(cmd)
config, err := f.ClientConfig.ClientConfig() client, err := f.Client(cmd)
checkErr(err)
client, err := client.New(config)
checkErr(err) checkErr(err)
podID := args[0] podID := args[0]
......
...@@ -33,7 +33,7 @@ func (f *Factory) NewCmdProxy(out io.Writer) *cobra.Command { ...@@ -33,7 +33,7 @@ func (f *Factory) NewCmdProxy(out io.Writer) *cobra.Command {
port := GetFlagInt(cmd, "port") port := GetFlagInt(cmd, "port")
glog.Infof("Starting to serve on localhost:%d", port) glog.Infof("Starting to serve on localhost:%d", port)
clientConfig, err := f.ClientConfig.ClientConfig() clientConfig, err := f.ClientConfig(cmd)
checkErr(err) checkErr(err)
server, err := kubectl.NewProxyServer(GetFlagString(cmd, "www"), clientConfig, port) server, err := kubectl.NewProxyServer(GetFlagString(cmd, "www"), clientConfig, port)
......
...@@ -63,7 +63,7 @@ func ResourcesFromArgsOrFile( ...@@ -63,7 +63,7 @@ func ResourcesFromArgsOrFile(
} }
types := SplitResourceArgument(args[0]) types := SplitResourceArgument(args[0])
for _, arg := range types { for _, arg := range types {
resourceName := kubectl.ExpandResourceShortcut(arg) resourceName := arg
if len(resourceName) == 0 { if len(resourceName) == 0 {
usageError(cmd, "Unknown resource %s", resourceName) usageError(cmd, "Unknown resource %s", resourceName)
} }
...@@ -91,7 +91,7 @@ func ResourceFromArgsOrFile(cmd *cobra.Command, args []string, filename string, ...@@ -91,7 +91,7 @@ func ResourceFromArgsOrFile(cmd *cobra.Command, args []string, filename string,
} }
if len(args) == 2 { if len(args) == 2 {
resource := kubectl.ExpandResourceShortcut(args[0]) resource := args[0]
namespace = GetKubeNamespace(cmd) namespace = GetKubeNamespace(cmd)
name = args[1] name = args[1]
if len(name) == 0 || len(resource) == 0 { if len(name) == 0 || len(resource) == 0 {
...@@ -129,7 +129,7 @@ func ResourceFromArgs(cmd *cobra.Command, args []string, mapper meta.RESTMapper) ...@@ -129,7 +129,7 @@ func ResourceFromArgs(cmd *cobra.Command, args []string, mapper meta.RESTMapper)
usageError(cmd, "Must provide resource and name command line params") usageError(cmd, "Must provide resource and name command line params")
} }
resource := kubectl.ExpandResourceShortcut(args[0]) resource := args[0]
namespace = GetKubeNamespace(cmd) namespace = GetKubeNamespace(cmd)
name = args[1] name = args[1]
if len(name) == 0 || len(resource) == 0 { if len(name) == 0 || len(resource) == 0 {
...@@ -152,7 +152,7 @@ func ResourceOrTypeFromArgs(cmd *cobra.Command, args []string, mapper meta.RESTM ...@@ -152,7 +152,7 @@ func ResourceOrTypeFromArgs(cmd *cobra.Command, args []string, mapper meta.RESTM
usageError(cmd, "Must provide resource or a resource and name as command line params") usageError(cmd, "Must provide resource or a resource and name as command line params")
} }
resource := kubectl.ExpandResourceShortcut(args[0]) resource := args[0]
if len(resource) == 0 { if len(resource) == 0 {
usageError(cmd, "Must provide resource or a resource and name as command line params") usageError(cmd, "Must provide resource or a resource and name as command line params")
} }
......
...@@ -21,7 +21,6 @@ import ( ...@@ -21,7 +21,6 @@ import (
"io" "io"
"github.com/GoogleCloudPlatform/kubernetes/pkg/api" "github.com/GoogleCloudPlatform/kubernetes/pkg/api"
"github.com/GoogleCloudPlatform/kubernetes/pkg/client"
"github.com/GoogleCloudPlatform/kubernetes/pkg/kubectl" "github.com/GoogleCloudPlatform/kubernetes/pkg/kubectl"
"github.com/spf13/cobra" "github.com/spf13/cobra"
) )
...@@ -69,9 +68,7 @@ $ cat frontend-v2.json | kubectl rollingupdate frontend-v1 -f - ...@@ -69,9 +68,7 @@ $ cat frontend-v2.json | kubectl rollingupdate frontend-v1 -f -
err = CompareNamespaceFromFile(cmd, namespace) err = CompareNamespaceFromFile(cmd, namespace)
checkErr(err) checkErr(err)
config, err := f.ClientConfig.ClientConfig() client, err := f.Client(cmd)
checkErr(err)
client, err := client.New(config)
checkErr(err) checkErr(err)
obj, err := mapping.Codec.Decode(data) obj, err := mapping.Codec.Decode(data)
......
...@@ -46,7 +46,7 @@ Examples: ...@@ -46,7 +46,7 @@ Examples:
schema, err := f.Validator(cmd) schema, err := f.Validator(cmd)
checkErr(err) checkErr(err)
mapping, namespace, name, data := ResourceFromFile(cmd, filename, f.Typer, f.Mapper, schema) mapping, namespace, name, data := ResourceFromFile(cmd, filename, f.Typer, f.Mapper, schema)
client, err := f.Client(cmd, mapping) client, err := f.RESTClient(cmd, mapping)
checkErr(err) checkErr(err)
err = CompareNamespaceFromFile(cmd, namespace) err = CompareNamespaceFromFile(cmd, namespace)
......
...@@ -21,7 +21,6 @@ import ( ...@@ -21,7 +21,6 @@ import (
"github.com/spf13/cobra" "github.com/spf13/cobra"
"github.com/GoogleCloudPlatform/kubernetes/pkg/client"
"github.com/GoogleCloudPlatform/kubernetes/pkg/kubectl" "github.com/GoogleCloudPlatform/kubernetes/pkg/kubectl"
) )
...@@ -32,14 +31,13 @@ func (f *Factory) NewCmdVersion(out io.Writer) *cobra.Command { ...@@ -32,14 +31,13 @@ func (f *Factory) NewCmdVersion(out io.Writer) *cobra.Command {
Run: func(cmd *cobra.Command, args []string) { Run: func(cmd *cobra.Command, args []string) {
if GetFlagBool(cmd, "client") { if GetFlagBool(cmd, "client") {
kubectl.GetClientVersion(out) kubectl.GetClientVersion(out)
} else { return
config, err := f.ClientConfig.ClientConfig()
checkErr(err)
client, err := client.New(config)
checkErr(err)
kubectl.GetVersion(out, client)
} }
client, err := f.Client(cmd)
checkErr(err)
kubectl.GetVersion(out, client)
}, },
} }
cmd.Flags().BoolP("client", "c", false, "Client version only (no server required)") cmd.Flags().BoolP("client", "c", false, "Client version only (no server required)")
......
...@@ -26,6 +26,7 @@ import ( ...@@ -26,6 +26,7 @@ import (
"strings" "strings"
"github.com/GoogleCloudPlatform/kubernetes/pkg/api" "github.com/GoogleCloudPlatform/kubernetes/pkg/api"
"github.com/GoogleCloudPlatform/kubernetes/pkg/api/meta"
"github.com/GoogleCloudPlatform/kubernetes/pkg/client" "github.com/GoogleCloudPlatform/kubernetes/pkg/client"
"github.com/GoogleCloudPlatform/kubernetes/pkg/labels" "github.com/GoogleCloudPlatform/kubernetes/pkg/labels"
"github.com/GoogleCloudPlatform/kubernetes/pkg/util" "github.com/GoogleCloudPlatform/kubernetes/pkg/util"
...@@ -111,12 +112,23 @@ func makeImageList(spec *api.PodSpec) string { ...@@ -111,12 +112,23 @@ func makeImageList(spec *api.PodSpec) string {
return strings.Join(listOfImages(spec), ",") return strings.Join(listOfImages(spec), ",")
} }
// ExpandResourceShortcut will return the expanded version of resource // ShortcutExpander is a RESTMapper that can be used for Kubernetes
// 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 // (something that a pkg/api/meta.RESTMapper can understand), if it is
// indeed a shortcut. Otherwise, will return resource unmodified. // indeed a shortcut. Otherwise, will return resource unmodified.
// TODO: Combine with RESTMapper stuff to provide a general solution func expandResourceShortcut(resource string) string {
// to this problem.
func ExpandResourceShortcut(resource string) string {
shortForms := map[string]string{ shortForms := map[string]string{
"po": "pods", "po": "pods",
"rc": "replicationcontrollers", "rc": "replicationcontrollers",
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment