2014-10-06 01:24:19 +00:00
|
|
|
/*
|
|
|
|
Copyright 2014 Google Inc. All rights reserved.
|
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"os"
|
|
|
|
|
2014-10-21 18:11:53 +00:00
|
|
|
"github.com/GoogleCloudPlatform/kubernetes/pkg/api"
|
2014-10-06 01:24:19 +00:00
|
|
|
"github.com/GoogleCloudPlatform/kubernetes/pkg/api/latest"
|
2014-10-27 02:21:31 +00:00
|
|
|
"github.com/GoogleCloudPlatform/kubernetes/pkg/api/meta"
|
2014-11-26 03:50:31 +00:00
|
|
|
"github.com/GoogleCloudPlatform/kubernetes/pkg/api/validation"
|
|
|
|
"github.com/GoogleCloudPlatform/kubernetes/pkg/client"
|
2014-11-17 18:29:45 +00:00
|
|
|
"github.com/GoogleCloudPlatform/kubernetes/pkg/client/clientcmd"
|
2014-10-06 01:24:19 +00:00
|
|
|
"github.com/GoogleCloudPlatform/kubernetes/pkg/kubectl"
|
2014-12-17 13:03:03 +00:00
|
|
|
cmdconfig "github.com/GoogleCloudPlatform/kubernetes/pkg/kubectl/cmd/config"
|
2015-02-05 00:14:48 +00:00
|
|
|
cmdutil "github.com/GoogleCloudPlatform/kubernetes/pkg/kubectl/cmd/util"
|
2014-12-31 00:42:55 +00:00
|
|
|
"github.com/GoogleCloudPlatform/kubernetes/pkg/kubectl/resource"
|
2014-10-27 02:21:31 +00:00
|
|
|
"github.com/GoogleCloudPlatform/kubernetes/pkg/runtime"
|
2015-01-08 18:29:37 +00:00
|
|
|
"github.com/GoogleCloudPlatform/kubernetes/pkg/util"
|
2014-11-17 18:29:45 +00:00
|
|
|
|
2014-10-06 01:24:19 +00:00
|
|
|
"github.com/golang/glog"
|
|
|
|
"github.com/spf13/cobra"
|
2014-12-28 05:27:52 +00:00
|
|
|
"github.com/spf13/pflag"
|
2014-10-06 01:24:19 +00:00
|
|
|
)
|
|
|
|
|
2015-01-07 20:59:22 +00:00
|
|
|
const (
|
|
|
|
FlagMatchBinaryVersion = "match-server-version"
|
|
|
|
)
|
|
|
|
|
2014-10-27 19:56:34 +00:00
|
|
|
// Factory provides abstractions that allow the Kubectl command to be extended across multiple types
|
|
|
|
// of resources and different API sets.
|
2014-12-31 00:42:55 +00:00
|
|
|
// TODO: make the functions interfaces
|
2014-10-27 02:21:31 +00:00
|
|
|
type Factory struct {
|
2014-12-28 05:27:52 +00:00
|
|
|
clients *clientCache
|
|
|
|
flags *pflag.FlagSet
|
|
|
|
|
2014-12-31 23:35:52 +00:00
|
|
|
// Returns interfaces for dealing with arbitrary runtime.Objects.
|
|
|
|
Object func(cmd *cobra.Command) (meta.RESTMapper, runtime.ObjectTyper)
|
2014-12-28 05:27:52 +00:00
|
|
|
// Returns a client for accessing Kubernetes resources or an error.
|
|
|
|
Client func(cmd *cobra.Command) (*client.Client, error)
|
|
|
|
// Returns a client.Config for accessing the Kubernetes server.
|
|
|
|
ClientConfig func(cmd *cobra.Command) (*client.Config, error)
|
|
|
|
// Returns a RESTClient for working with the specified RESTMapping or an error. This is intended
|
|
|
|
// for working with arbitrary resources and is not guaranteed to point to a Kubernetes APIServer.
|
2014-12-31 23:35:52 +00:00
|
|
|
RESTClient func(cmd *cobra.Command, mapping *meta.RESTMapping) (resource.RESTClient, error)
|
2014-12-28 05:27:52 +00:00
|
|
|
// Returns a Describer for displaying the specified RESTMapping type or an error.
|
|
|
|
Describer func(cmd *cobra.Command, mapping *meta.RESTMapping) (kubectl.Describer, error)
|
|
|
|
// Returns a Printer for formatting objects of the given type or an error.
|
|
|
|
Printer func(cmd *cobra.Command, mapping *meta.RESTMapping, noHeaders bool) (kubectl.ResourcePrinter, error)
|
2015-01-09 23:53:06 +00:00
|
|
|
// Returns a Resizer for changing the size of the specified RESTMapping type or an error
|
|
|
|
Resizer func(cmd *cobra.Command, mapping *meta.RESTMapping) (kubectl.Resizer, error)
|
2015-01-22 17:46:38 +00:00
|
|
|
// Returns a Reaper for gracefully shutting down resources.
|
|
|
|
Reaper func(cmd *cobra.Command, mapping *meta.RESTMapping) (kubectl.Reaper, error)
|
2014-12-28 05:27:52 +00:00
|
|
|
// Returns a schema that can validate objects stored on disk.
|
|
|
|
Validator func(*cobra.Command) (validation.Schema, error)
|
2015-01-02 18:08:37 +00:00
|
|
|
// Returns the default namespace to use in cases where no other namespace is specified
|
|
|
|
DefaultNamespace func(cmd *cobra.Command) (string, error)
|
2014-10-27 02:21:31 +00:00
|
|
|
}
|
|
|
|
|
2014-10-27 19:56:34 +00:00
|
|
|
// NewFactory creates a factory with the default Kubernetes resources defined
|
2015-01-20 22:07:36 +00:00
|
|
|
// if optionalClientConfig is nil, then flags will be bound to a new clientcmd.ClientConfig.
|
|
|
|
// if optionalClientConfig is not nil, then this factory will make use of it.
|
|
|
|
func NewFactory(optionalClientConfig clientcmd.ClientConfig) *Factory {
|
2014-12-28 05:27:52 +00:00
|
|
|
mapper := kubectl.ShortcutExpander{latest.RESTMapper}
|
|
|
|
|
|
|
|
flags := pflag.NewFlagSet("", pflag.ContinueOnError)
|
2015-01-20 22:07:36 +00:00
|
|
|
|
|
|
|
clientConfig := optionalClientConfig
|
|
|
|
if optionalClientConfig == nil {
|
|
|
|
clientConfig = DefaultClientConfig(flags)
|
|
|
|
}
|
|
|
|
|
2014-12-28 05:27:52 +00:00
|
|
|
clients := &clientCache{
|
|
|
|
clients: make(map[string]*client.Client),
|
|
|
|
loader: clientConfig,
|
2015-01-07 20:59:22 +00:00
|
|
|
}
|
|
|
|
|
2014-12-28 05:27:52 +00:00
|
|
|
return &Factory{
|
|
|
|
clients: clients,
|
|
|
|
flags: flags,
|
|
|
|
|
2014-12-31 23:35:52 +00:00
|
|
|
Object: func(cmd *cobra.Command) (meta.RESTMapper, runtime.ObjectTyper) {
|
2015-01-02 18:08:37 +00:00
|
|
|
cfg, err := clientConfig.ClientConfig()
|
|
|
|
checkErr(err)
|
|
|
|
cmdApiVersion := cfg.Version
|
|
|
|
|
|
|
|
return kubectl.OutputVersionMapper{mapper, cmdApiVersion}, api.Scheme
|
2014-12-31 23:35:52 +00:00
|
|
|
},
|
2014-12-28 05:27:52 +00:00
|
|
|
Client: func(cmd *cobra.Command) (*client.Client, error) {
|
|
|
|
return clients.ClientForVersion("")
|
|
|
|
},
|
|
|
|
ClientConfig: func(cmd *cobra.Command) (*client.Config, error) {
|
|
|
|
return clients.ClientConfigForVersion("")
|
|
|
|
},
|
2014-12-31 23:35:52 +00:00
|
|
|
RESTClient: func(cmd *cobra.Command, mapping *meta.RESTMapping) (resource.RESTClient, error) {
|
2014-12-28 05:27:52 +00:00
|
|
|
client, err := clients.ClientForVersion(mapping.APIVersion)
|
2014-11-17 18:29:45 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2014-12-28 05:27:52 +00:00
|
|
|
return client.RESTClient, nil
|
|
|
|
},
|
|
|
|
Describer: func(cmd *cobra.Command, mapping *meta.RESTMapping) (kubectl.Describer, error) {
|
|
|
|
client, err := clients.ClientForVersion(mapping.APIVersion)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
describer, ok := kubectl.DescriberFor(mapping.Kind, client)
|
|
|
|
if !ok {
|
|
|
|
return nil, fmt.Errorf("no description has been implemented for %q", mapping.Kind)
|
|
|
|
}
|
|
|
|
return describer, nil
|
|
|
|
},
|
|
|
|
Printer: func(cmd *cobra.Command, mapping *meta.RESTMapping, noHeaders bool) (kubectl.ResourcePrinter, error) {
|
|
|
|
return kubectl.NewHumanReadablePrinter(noHeaders), nil
|
|
|
|
},
|
2015-01-09 23:53:06 +00:00
|
|
|
Resizer: func(cmd *cobra.Command, mapping *meta.RESTMapping) (kubectl.Resizer, error) {
|
|
|
|
client, err := clients.ClientForVersion(mapping.APIVersion)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2015-01-22 17:46:38 +00:00
|
|
|
return kubectl.ResizerFor(mapping.Kind, client)
|
|
|
|
},
|
|
|
|
Reaper: func(cmd *cobra.Command, mapping *meta.RESTMapping) (kubectl.Reaper, error) {
|
|
|
|
client, err := clients.ClientForVersion(mapping.APIVersion)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2015-01-09 23:53:06 +00:00
|
|
|
}
|
2015-01-22 17:46:38 +00:00
|
|
|
return kubectl.ReaperFor(mapping.Kind, client)
|
2015-01-09 23:53:06 +00:00
|
|
|
},
|
2014-12-28 05:27:52 +00:00
|
|
|
Validator: func(cmd *cobra.Command) (validation.Schema, error) {
|
2015-02-05 00:14:48 +00:00
|
|
|
if cmdutil.GetFlagBool(cmd, "validate") {
|
2014-12-28 05:27:52 +00:00
|
|
|
client, err := clients.ClientForVersion("")
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &clientSwaggerSchema{client, api.Scheme}, nil
|
|
|
|
}
|
2015-01-07 20:59:22 +00:00
|
|
|
return validation.NullSchema{}, nil
|
2014-12-28 05:27:52 +00:00
|
|
|
},
|
2015-01-02 18:08:37 +00:00
|
|
|
DefaultNamespace: func(cmd *cobra.Command) (string, error) {
|
|
|
|
return clientConfig.Namespace()
|
|
|
|
},
|
2015-01-07 20:59:22 +00:00
|
|
|
}
|
2014-12-28 05:27:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// BindFlags adds any flags that are common to all kubectl sub commands.
|
|
|
|
func (f *Factory) BindFlags(flags *pflag.FlagSet) {
|
|
|
|
// any flags defined by external projects (not part of pflags)
|
|
|
|
util.AddAllFlagsToPFlagSet(flags)
|
|
|
|
|
2015-01-14 22:54:39 +00:00
|
|
|
// This is necessary as github.com/spf13/cobra doesn't support "global"
|
|
|
|
// pflags currently. See https://github.com/spf13/cobra/issues/44.
|
|
|
|
util.AddPFlagSetToPFlagSet(pflag.CommandLine, flags)
|
|
|
|
|
2014-12-28 05:27:52 +00:00
|
|
|
if f.flags != nil {
|
|
|
|
f.flags.VisitAll(func(flag *pflag.Flag) {
|
|
|
|
flags.AddFlag(flag)
|
|
|
|
})
|
2015-01-07 20:59:22 +00:00
|
|
|
}
|
2014-12-28 05:27:52 +00:00
|
|
|
|
|
|
|
// Globally persistent flags across all subcommands.
|
|
|
|
// TODO Change flag names to consts to allow safer lookup from subcommands.
|
|
|
|
// TODO Add a verbose flag that turns on glog logging. Probably need a way
|
|
|
|
// to do that automatically for every subcommand.
|
|
|
|
flags.BoolVar(&f.clients.matchVersion, FlagMatchBinaryVersion, false, "Require server version to match client version")
|
|
|
|
flags.Bool("validate", false, "If true, use a schema to validate the input before sending it")
|
2014-10-27 19:56:34 +00:00
|
|
|
}
|
|
|
|
|
2014-12-28 05:27:52 +00:00
|
|
|
// NewKubectlCommand creates the `kubectl` command and its nested children.
|
|
|
|
func (f *Factory) NewKubectlCommand(out io.Writer) *cobra.Command {
|
2014-10-06 01:24:19 +00:00
|
|
|
// Parent command to which all subcommands are added.
|
|
|
|
cmds := &cobra.Command{
|
|
|
|
Use: "kubectl",
|
|
|
|
Short: "kubectl controls the Kubernetes cluster manager",
|
|
|
|
Long: `kubectl controls the Kubernetes cluster manager.
|
|
|
|
|
|
|
|
Find more information at https://github.com/GoogleCloudPlatform/kubernetes.`,
|
|
|
|
Run: runHelp,
|
|
|
|
}
|
|
|
|
|
2014-12-28 05:27:52 +00:00
|
|
|
f.BindFlags(cmds.PersistentFlags())
|
2014-10-06 01:24:19 +00:00
|
|
|
|
2014-11-17 18:29:45 +00:00
|
|
|
cmds.AddCommand(f.NewCmdVersion(out))
|
|
|
|
cmds.AddCommand(f.NewCmdProxy(out))
|
2014-10-27 02:21:31 +00:00
|
|
|
|
2014-10-27 19:56:34 +00:00
|
|
|
cmds.AddCommand(f.NewCmdGet(out))
|
|
|
|
cmds.AddCommand(f.NewCmdDescribe(out))
|
|
|
|
cmds.AddCommand(f.NewCmdCreate(out))
|
|
|
|
cmds.AddCommand(f.NewCmdUpdate(out))
|
|
|
|
cmds.AddCommand(f.NewCmdDelete(out))
|
2014-10-27 02:21:31 +00:00
|
|
|
|
2014-12-17 13:03:03 +00:00
|
|
|
cmds.AddCommand(cmdconfig.NewCmdConfig(out))
|
2014-10-21 18:11:53 +00:00
|
|
|
cmds.AddCommand(NewCmdNamespace(out))
|
2014-11-17 18:29:45 +00:00
|
|
|
cmds.AddCommand(f.NewCmdLog(out))
|
2014-12-10 21:48:48 +00:00
|
|
|
cmds.AddCommand(f.NewCmdRollingUpdate(out))
|
2015-01-09 23:53:06 +00:00
|
|
|
cmds.AddCommand(f.NewCmdResize(out))
|
2014-10-06 01:24:19 +00:00
|
|
|
|
2015-01-12 23:30:52 +00:00
|
|
|
cmds.AddCommand(f.NewCmdRunContainer(out))
|
2015-01-22 17:46:38 +00:00
|
|
|
cmds.AddCommand(f.NewCmdStop(out))
|
2015-01-17 01:52:27 +00:00
|
|
|
cmds.AddCommand(f.NewCmdExposeService(out))
|
2015-01-12 23:30:52 +00:00
|
|
|
|
2014-12-28 05:27:52 +00:00
|
|
|
return cmds
|
2014-10-06 01:24:19 +00:00
|
|
|
}
|
|
|
|
|
2015-02-05 00:14:48 +00:00
|
|
|
// PrintObject prints an api object given command line flags to modify the output format
|
|
|
|
func (f *Factory) PrintObject(cmd *cobra.Command, obj runtime.Object, out io.Writer) error {
|
|
|
|
mapper, _ := f.Object(cmd)
|
|
|
|
_, kind, err := api.Scheme.ObjectVersionAndKind(obj)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
mapping, err := mapper.RESTMapping(kind)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
printer, err := f.PrinterForMapping(cmd, mapping)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return printer.PrintObj(obj, out)
|
|
|
|
}
|
|
|
|
|
|
|
|
// PrinterForMapping returns a printer suitable for displaying the provided resource type.
|
|
|
|
// Requires that printer flags have been added to cmd (see AddPrinterFlags).
|
|
|
|
func (f *Factory) PrinterForMapping(cmd *cobra.Command, mapping *meta.RESTMapping) (kubectl.ResourcePrinter, error) {
|
|
|
|
printer, ok, err := cmdutil.PrinterForCommand(cmd)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if ok {
|
|
|
|
clientConfig, err := f.ClientConfig(cmd)
|
|
|
|
checkErr(err)
|
|
|
|
defaultVersion := clientConfig.Version
|
|
|
|
|
|
|
|
version := cmdutil.OutputVersion(cmd, defaultVersion)
|
|
|
|
if len(version) == 0 {
|
|
|
|
version = mapping.APIVersion
|
|
|
|
}
|
|
|
|
if len(version) == 0 {
|
|
|
|
return nil, fmt.Errorf("you must specify an output-version when using this output format")
|
|
|
|
}
|
|
|
|
printer = kubectl.NewVersionedPrinter(printer, mapping.ObjectConvertor, version)
|
|
|
|
} else {
|
|
|
|
printer, err = f.Printer(cmd, mapping, cmdutil.GetFlagBool(cmd, "no-headers"))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return printer, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// ClientMapperForCommand returns a ClientMapper for the given command and factory.
|
|
|
|
func (f *Factory) ClientMapperForCommand(cmd *cobra.Command) resource.ClientMapper {
|
|
|
|
return resource.ClientMapperFunc(func(mapping *meta.RESTMapping) (resource.RESTClient, error) {
|
|
|
|
return f.RESTClient(cmd, mapping)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2014-12-28 05:27:52 +00:00
|
|
|
// DefaultClientConfig creates a clientcmd.ClientConfig with the following hierarchy:
|
2015-01-07 20:59:22 +00:00
|
|
|
// 1. Use the kubeconfig builder. The number of merges and overrides here gets a little crazy. Stay with me.
|
|
|
|
// 1. Merge together the kubeconfig itself. This is done with the following hierarchy and merge rules:
|
|
|
|
// 1. CommandLineLocation - this parsed from the command line, so it must be late bound
|
|
|
|
// 2. EnvVarLocation
|
|
|
|
// 3. CurrentDirectoryLocation
|
|
|
|
// 4. HomeDirectoryLocation
|
|
|
|
// Empty filenames are ignored. Files with non-deserializable content produced errors.
|
|
|
|
// The first file to set a particular value or map key wins and the value or map key is never changed.
|
|
|
|
// This means that the first file to set CurrentContext will have its context preserved. It also means
|
|
|
|
// that if two files specify a "red-user", only values from the first file's red-user are used. Even
|
|
|
|
// non-conflicting entries from the second file's "red-user" are discarded.
|
|
|
|
// 2. Determine the context to use based on the first hit in this chain
|
|
|
|
// 1. command line argument - again, parsed from the command line, so it must be late bound
|
|
|
|
// 2. CurrentContext from the merged kubeconfig file
|
|
|
|
// 3. Empty is allowed at this stage
|
|
|
|
// 3. Determine the cluster info and auth info to use. At this point, we may or may not have a context. They
|
|
|
|
// are built based on the first hit in this chain. (run it twice, once for auth, once for cluster)
|
|
|
|
// 1. command line argument
|
|
|
|
// 2. If context is present, then use the context value
|
|
|
|
// 3. Empty is allowed
|
|
|
|
// 4. Determine the actual cluster info to use. At this point, we may or may not have a cluster info. Build
|
|
|
|
// each piece of the cluster info based on the chain:
|
|
|
|
// 1. command line argument
|
|
|
|
// 2. If cluster info is present and a value for the attribute is present, use it.
|
|
|
|
// 3. If you don't have a server location, bail.
|
|
|
|
// 5. Auth info is build using the same rules as cluster info, EXCEPT that you can only have one authentication
|
|
|
|
// technique per auth info. The following conditions result in an error:
|
|
|
|
// 1. If there are two conflicting techniques specified from the command line, fail.
|
|
|
|
// 2. If the command line does not specify one, and the auth info has conflicting techniques, fail.
|
|
|
|
// 3. If the command line specifies one and the auth info specifies another, honor the command line technique.
|
|
|
|
// 2. Use default values and potentially prompt for auth information
|
2014-12-28 05:27:52 +00:00
|
|
|
func DefaultClientConfig(flags *pflag.FlagSet) clientcmd.ClientConfig {
|
2015-01-07 20:59:22 +00:00
|
|
|
loadingRules := clientcmd.NewClientConfigLoadingRules()
|
|
|
|
loadingRules.EnvVarPath = os.Getenv(clientcmd.RecommendedConfigPathEnvVar)
|
2014-12-28 05:27:52 +00:00
|
|
|
flags.StringVar(&loadingRules.CommandLinePath, "kubeconfig", "", "Path to the kubeconfig file to use for CLI requests.")
|
2015-01-07 20:59:22 +00:00
|
|
|
|
|
|
|
overrides := &clientcmd.ConfigOverrides{}
|
2015-01-20 21:21:27 +00:00
|
|
|
flagNames := clientcmd.RecommendedConfigOverrideFlags("")
|
|
|
|
// short flagnames are disabled by default. These are here for compatibility with existing scripts
|
|
|
|
flagNames.AuthOverrideFlags.AuthPathShort = "a"
|
|
|
|
flagNames.ClusterOverrideFlags.APIServerShort = "s"
|
|
|
|
|
|
|
|
clientcmd.BindOverrideFlags(overrides, flags, flagNames)
|
2015-01-07 20:59:22 +00:00
|
|
|
clientConfig := clientcmd.NewInteractiveDeferredLoadingClientConfig(loadingRules, overrides, os.Stdin)
|
|
|
|
|
|
|
|
return clientConfig
|
|
|
|
}
|
|
|
|
|
2014-10-06 01:24:19 +00:00
|
|
|
func checkErr(err error) {
|
|
|
|
if err != nil {
|
2014-11-12 21:31:24 +00:00
|
|
|
glog.FatalDepth(1, err)
|
2014-10-06 01:24:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func usageError(cmd *cobra.Command, format string, args ...interface{}) {
|
|
|
|
glog.Errorf(format, args...)
|
|
|
|
glog.Errorf("See '%s -h' for help.", cmd.CommandPath())
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
|
|
|
|
func runHelp(cmd *cobra.Command, args []string) {
|
|
|
|
cmd.Help()
|
|
|
|
}
|
|
|
|
|
2014-11-26 04:10:21 +00:00
|
|
|
type clientSwaggerSchema struct {
|
|
|
|
c *client.Client
|
|
|
|
t runtime.ObjectTyper
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *clientSwaggerSchema) ValidateBytes(data []byte) error {
|
|
|
|
version, _, err := c.t.DataVersionAndKind(data)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
schemaData, err := c.c.RESTClient.Get().
|
2014-12-26 20:06:25 +00:00
|
|
|
AbsPath("/swaggerapi/api", version).
|
2014-11-26 04:10:21 +00:00
|
|
|
Do().
|
|
|
|
Raw()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
schema, err := validation.NewSwaggerSchemaFromBytes(schemaData)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return schema.ValidateBytes(data)
|
|
|
|
}
|
2015-01-07 20:59:22 +00:00
|
|
|
|
2014-12-28 05:27:52 +00:00
|
|
|
// clientCache caches previously loaded clients for reuse, and ensures MatchServerVersion
|
|
|
|
// is invoked only once
|
|
|
|
type clientCache struct {
|
|
|
|
loader clientcmd.ClientConfig
|
|
|
|
clients map[string]*client.Client
|
|
|
|
defaultConfig *client.Config
|
|
|
|
matchVersion bool
|
|
|
|
}
|
2015-01-07 20:59:22 +00:00
|
|
|
|
2014-12-28 05:27:52 +00:00
|
|
|
// ClientConfigForVersion returns the correct config for a server
|
|
|
|
func (c *clientCache) ClientConfigForVersion(version string) (*client.Config, error) {
|
|
|
|
if c.defaultConfig == nil {
|
|
|
|
config, err := c.loader.ClientConfig()
|
2015-01-07 20:59:22 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2014-12-28 05:27:52 +00:00
|
|
|
c.defaultConfig = config
|
|
|
|
if c.matchVersion {
|
|
|
|
if err := client.MatchesServerVersion(config); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// TODO: have a better config copy method
|
|
|
|
config := *c.defaultConfig
|
2015-01-29 21:31:22 +00:00
|
|
|
if len(version) != 0 {
|
|
|
|
config.Version = version
|
|
|
|
}
|
2015-01-13 23:50:36 +00:00
|
|
|
client.SetKubernetesDefaults(&config)
|
2015-01-29 21:31:22 +00:00
|
|
|
|
2014-12-28 05:27:52 +00:00
|
|
|
return &config, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// ClientForVersion initializes or reuses a client for the specified version, or returns an
|
|
|
|
// error if that is not possible
|
|
|
|
func (c *clientCache) ClientForVersion(version string) (*client.Client, error) {
|
|
|
|
config, err := c.ClientConfigForVersion(version)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if client, ok := c.clients[config.Version]; ok {
|
|
|
|
return client, nil
|
2015-01-07 20:59:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
client, err := client.New(config)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2014-12-28 05:27:52 +00:00
|
|
|
c.clients[config.Version] = client
|
2015-01-07 20:59:22 +00:00
|
|
|
return client, nil
|
|
|
|
}
|