From 593d8e0d7cf738ba92074aa177db8eacdc3167e9 Mon Sep 17 00:00:00 2001 From: AdoHe Date: Thu, 13 Oct 2016 08:09:26 +0800 Subject: [PATCH 1/3] refactor Factory make it interface --- pkg/kubectl/cmd/util/factory.go | 1212 ++++++++++++++------------ pkg/kubectl/cmd/util/factory_test.go | 14 +- 2 files changed, 639 insertions(+), 587 deletions(-) diff --git a/pkg/kubectl/cmd/util/factory.go b/pkg/kubectl/cmd/util/factory.go index 91046d505c8..78ae76cda2d 100644 --- a/pkg/kubectl/cmd/util/factory.go +++ b/pkg/kubectl/cmd/util/factory.go @@ -77,87 +77,107 @@ const ( // TODO: make the functions interfaces // TODO: pass the various interfaces on the factory directly into the command constructors (so the // commands are decoupled from the factory). -type Factory struct { - clients *ClientCache - flags *pflag.FlagSet +type Factory interface { + // Returns internal flagset + FlagSet() *pflag.FlagSet // Returns interfaces for dealing with arbitrary runtime.Objects. - Object func() (meta.RESTMapper, runtime.ObjectTyper) + Object() (meta.RESTMapper, runtime.ObjectTyper) // Returns interfaces for dealing with arbitrary // runtime.Unstructured. This performs API calls to discover types. - UnstructuredObject func() (meta.RESTMapper, runtime.ObjectTyper, error) + UnstructuredObject() (meta.RESTMapper, runtime.ObjectTyper, error) // Returns interfaces for decoding objects - if toInternal is set, decoded objects will be converted // into their internal form (if possible). Eventually the internal form will be removed as an option, // and only versioned objects will be returned. - Decoder func(toInternal bool) runtime.Decoder + Decoder(toInternal bool) runtime.Decoder // Returns an encoder capable of encoding a provided object into JSON in the default desired version. - JSONEncoder func() runtime.Encoder + JSONEncoder() runtime.Encoder // ClientSet gives you back an internal, generated clientset - ClientSet func() (*internalclientset.Clientset, error) + ClientSet() (*internalclientset.Clientset, error) // Returns a RESTClient for accessing Kubernetes resources or an error. - RESTClient func() (*restclient.RESTClient, error) + RESTClient() (*restclient.RESTClient, error) // Returns a client.Config for accessing the Kubernetes server. - ClientConfig func() (*restclient.Config, error) + ClientConfig() (*restclient.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. - ClientForMapping func(mapping *meta.RESTMapping) (resource.RESTClient, error) + ClientForMapping(mapping *meta.RESTMapping) (resource.RESTClient, error) // Returns a RESTClient for working with Unstructured objects. - UnstructuredClientForMapping func(mapping *meta.RESTMapping) (resource.RESTClient, error) + UnstructuredClientForMapping(mapping *meta.RESTMapping) (resource.RESTClient, error) // Returns a Describer for displaying the specified RESTMapping type or an error. - Describer func(mapping *meta.RESTMapping) (kubectl.Describer, error) + Describer(mapping *meta.RESTMapping) (kubectl.Describer, error) // Returns a Printer for formatting objects of the given type or an error. - Printer func(mapping *meta.RESTMapping, options kubectl.PrintOptions) (kubectl.ResourcePrinter, error) + Printer(mapping *meta.RESTMapping, options kubectl.PrintOptions) (kubectl.ResourcePrinter, error) // Returns a Scaler for changing the size of the specified RESTMapping type or an error - Scaler func(mapping *meta.RESTMapping) (kubectl.Scaler, error) + Scaler(mapping *meta.RESTMapping) (kubectl.Scaler, error) // Returns a Reaper for gracefully shutting down resources. - Reaper func(mapping *meta.RESTMapping) (kubectl.Reaper, error) + Reaper(mapping *meta.RESTMapping) (kubectl.Reaper, error) // Returns a HistoryViewer for viewing change history - HistoryViewer func(mapping *meta.RESTMapping) (kubectl.HistoryViewer, error) + HistoryViewer(mapping *meta.RESTMapping) (kubectl.HistoryViewer, error) // Returns a Rollbacker for changing the rollback version of the specified RESTMapping type or an error - Rollbacker func(mapping *meta.RESTMapping) (kubectl.Rollbacker, error) + Rollbacker(mapping *meta.RESTMapping) (kubectl.Rollbacker, error) // Returns a StatusViewer for printing rollout status. - StatusViewer func(mapping *meta.RESTMapping) (kubectl.StatusViewer, error) + StatusViewer(mapping *meta.RESTMapping) (kubectl.StatusViewer, error) // MapBasedSelectorForObject returns the map-based selector associated with the provided object. If a // new set-based selector is provided, an error is returned if the selector cannot be converted to a // map-based selector - MapBasedSelectorForObject func(object runtime.Object) (string, error) + MapBasedSelectorForObject(object runtime.Object) (string, error) // PortsForObject returns the ports associated with the provided object - PortsForObject func(object runtime.Object) ([]string, error) + PortsForObject(object runtime.Object) ([]string, error) // ProtocolsForObject returns the mapping associated with the provided object - ProtocolsForObject func(object runtime.Object) (map[string]string, error) + ProtocolsForObject(object runtime.Object) (map[string]string, error) // LabelsForObject returns the labels associated with the provided object - LabelsForObject func(object runtime.Object) (map[string]string, error) + LabelsForObject(object runtime.Object) (map[string]string, error) // LogsForObject returns a request for the logs associated with the provided object - LogsForObject func(object, options runtime.Object) (*restclient.Request, error) + LogsForObject(object, options runtime.Object) (*restclient.Request, error) // PauseObject marks the provided object as paused ie. it will not be reconciled by its controller. - PauseObject func(object runtime.Object) (bool, error) + PauseObject(object runtime.Object) (bool, error) // ResumeObject resumes a paused object ie. it will be reconciled by its controller. - ResumeObject func(object runtime.Object) (bool, error) + ResumeObject(object runtime.Object) (bool, error) // Returns a schema that can validate objects stored on disk. - Validator func(validate bool, cacheDir string) (validation.Schema, error) + Validator(validate bool, cacheDir string) (validation.Schema, error) // SwaggerSchema returns the schema declaration for the provided group version kind. - SwaggerSchema func(unversioned.GroupVersionKind) (*swagger.ApiDeclaration, error) + SwaggerSchema(unversioned.GroupVersionKind) (*swagger.ApiDeclaration, error) // Returns the default namespace to use in cases where no // other namespace is specified and whether the namespace was // overridden. - DefaultNamespace func() (string, bool, error) + DefaultNamespace() (string, bool, error) // Generators returns the generators for the provided command - Generators func(cmdName string) map[string]kubectl.Generator + Generators(cmdName string) map[string]kubectl.Generator // Check whether the kind of resources could be exposed - CanBeExposed func(kind unversioned.GroupKind) error + CanBeExposed(kind unversioned.GroupKind) error // Check whether the kind of resources could be autoscaled - CanBeAutoscaled func(kind unversioned.GroupKind) error + CanBeAutoscaled(kind unversioned.GroupKind) error // AttachablePodForObject returns the pod to which to attach given an object. - AttachablePodForObject func(object runtime.Object) (*api.Pod, error) + AttachablePodForObject(object runtime.Object) (*api.Pod, error) // UpdatePodSpecForObject will call the provided function on the pod spec this object supports, // return false if no pod spec is supported, or return an error. - UpdatePodSpecForObject func(obj runtime.Object, fn func(*api.PodSpec) error) (bool, error) + UpdatePodSpecForObject(obj runtime.Object, fn func(*api.PodSpec) error) (bool, error) // EditorEnvs returns a group of environment variables that the edit command // can range over in order to determine if the user has specified an editor // of their choice. - EditorEnvs func() []string + EditorEnvs() []string // PrintObjectSpecificMessage prints object-specific messages on the provided writer - PrintObjectSpecificMessage func(obj runtime.Object, out io.Writer) + PrintObjectSpecificMessage(obj runtime.Object, out io.Writer) + + // Command will stringify and return all environment arguments ie. a command run by a client + // using the factory. + Command() string + // BindFlags adds any flags that are common to all kubectl sub commands. + BindFlags(flags *pflag.FlagSet) + // BindExternalFlags adds any flags defined by external projects (not part of pflags) + BindExternalFlags(flags *pflag.FlagSet) + + DefaultResourceFilterOptions(cmd *cobra.Command, withNamespace bool) *kubectl.PrintOptions + // DefaultResourceFilterFunc returns a collection of FilterFuncs suitable for filtering specific resource types. + DefaultResourceFilterFunc() kubectl.Filters + + // PrintObject prints an api object given command line flags to modify the output format + PrintObject(cmd *cobra.Command, mapper meta.RESTMapper, obj runtime.Object, out io.Writer) error + // PrinterForMapping returns a printer suitable for displaying the provided resource type. + // Requires that printer flags have been added to cmd (see AddPrinterFlags). + PrinterForMapping(cmd *cobra.Command, mapping *meta.RESTMapping, withNamespace bool) (kubectl.ResourcePrinter, error) + // One stop shopping for a Builder + NewBuilder() *resource.Builder } const ( @@ -273,10 +293,17 @@ func makeInterfacesFor(versionList []unversioned.GroupVersion) func(version unve } } +type factory struct { + flags *pflag.FlagSet + clientConfig clientcmd.ClientConfig + + clients *ClientCache +} + // NewFactory creates a factory with the default Kubernetes resources defined // 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 { +func NewFactory(optionalClientConfig clientcmd.ClientConfig) Factory { flags := pflag.NewFlagSet("", pflag.ContinueOnError) flags.SetNormalizeFunc(utilflag.WarnWordSepNormalizeFunc) // Warn for "_" flags @@ -286,559 +313,593 @@ func NewFactory(optionalClientConfig clientcmd.ClientConfig) *Factory { } clients := NewClientCache(clientConfig) + return &factory{ + flags: flags, + clientConfig: clientConfig, + clients: clients, + } +} - return &Factory{ - clients: clients, - flags: flags, +func (f *factory) FlagSet() *pflag.FlagSet { + return f.flags +} - Object: func() (meta.RESTMapper, runtime.ObjectTyper) { - cfg, err := clientConfig.ClientConfig() - checkErrWithPrefix("failed to get client config: ", err) - cmdApiVersion := unversioned.GroupVersion{} - if cfg.GroupVersion != nil { - cmdApiVersion = *cfg.GroupVersion +func (f *factory) Object() (meta.RESTMapper, runtime.ObjectTyper) { + cfg, err := f.clientConfig.ClientConfig() + checkErrWithPrefix("failed to get client config: ", err) + cmdApiVersion := unversioned.GroupVersion{} + if cfg.GroupVersion != nil { + cmdApiVersion = *cfg.GroupVersion + } + + mapper := registered.RESTMapper() + discoveryClient, err := discovery.NewDiscoveryClientForConfig(cfg) + // if we can find the server version and it's current enough to have discovery information, use it. Otherwise, + // fallback to our hardcoded list + if err == nil { + if serverVersion, err := discoveryClient.ServerVersion(); err == nil && useDiscoveryRESTMapper(serverVersion.GitVersion) { + // register third party resources with the api machinery groups. This probably should be done, but + // its consistent with old code, so we'll start with it. + if err := registerThirdPartyResources(discoveryClient); err != nil { + fmt.Fprintf(os.Stderr, "Unable to register third party resources: %v\n", err) } + // ThirdPartyResourceData is special. It's not discoverable, but needed for thirdparty resource listing + // TODO eliminate this once we're truly generic. + thirdPartyResourceDataMapper := meta.NewDefaultRESTMapper([]unversioned.GroupVersion{extensionsv1beta1.SchemeGroupVersion}, registered.InterfacesFor) + thirdPartyResourceDataMapper.Add(extensionsv1beta1.SchemeGroupVersion.WithKind("ThirdPartyResourceData"), meta.RESTScopeNamespace) - mapper := registered.RESTMapper() - discoveryClient, err := discovery.NewDiscoveryClientForConfig(cfg) - // if we can find the server version and it's current enough to have discovery information, use it. Otherwise, - // fallback to our hardcoded list + mapper = meta.FirstHitRESTMapper{ + MultiRESTMapper: meta.MultiRESTMapper{ + discovery.NewDeferredDiscoveryRESTMapper(discoveryClient, registered.InterfacesFor), + thirdPartyResourceDataMapper, + }, + } + } + } + + // wrap with shortcuts + mapper = NewShortcutExpander(mapper, discoveryClient) + // wrap with output preferences + mapper = kubectl.OutputVersionMapper{RESTMapper: mapper, OutputVersions: []unversioned.GroupVersion{cmdApiVersion}} + return mapper, api.Scheme +} + +func (f *factory) UnstructuredObject() (meta.RESTMapper, runtime.ObjectTyper, error) { + cfg, err := f.clients.ClientConfigForVersion(nil) + if err != nil { + return nil, nil, err + } + + dc, err := discovery.NewDiscoveryClientForConfig(cfg) + if err != nil { + return nil, nil, err + } + + groupResources, err := discovery.GetAPIGroupResources(dc) + if err != nil { + return nil, nil, err + } + + // Register unknown APIs as third party for now to make + // validation happy. TODO perhaps make a dynamic schema + // validator to avoid this. + for _, group := range groupResources { + for _, version := range group.Group.Versions { + gv := unversioned.GroupVersion{Group: group.Group.Name, Version: version.Version} + if !registered.IsRegisteredVersion(gv) { + registered.AddThirdPartyAPIGroupVersions(gv) + } + } + } + + mapper := discovery.NewRESTMapper(groupResources, meta.InterfacesForUnstructured) + + typer := discovery.NewUnstructuredObjectTyper(groupResources) + + return NewShortcutExpander(mapper, dc), typer, nil +} + +func (f *factory) RESTClient() (*restclient.RESTClient, error) { + clientConfig, err := f.clients.ClientConfigForVersion(nil) + if err != nil { + return nil, err + } + return restclient.RESTClientFor(clientConfig) +} + +func (f *factory) ClientSet() (*internalclientset.Clientset, error) { + return f.clients.ClientSetForVersion(nil) +} + +func (f *factory) ClientConfig() (*restclient.Config, error) { + return f.clients.ClientConfigForVersion(nil) +} + +func (f *factory) ClientForMapping(mapping *meta.RESTMapping) (resource.RESTClient, error) { + cfg, err := f.clientConfig.ClientConfig() + if err != nil { + return nil, err + } + if err := client.SetKubernetesDefaults(cfg); err != nil { + return nil, err + } + gvk := mapping.GroupVersionKind + switch gvk.Group { + case federation.GroupName: + mappingVersion := mapping.GroupVersionKind.GroupVersion() + return f.clients.FederationClientForVersion(&mappingVersion) + case api.GroupName: + cfg.APIPath = "/api" + default: + cfg.APIPath = "/apis" + } + gv := gvk.GroupVersion() + cfg.GroupVersion = &gv + if registered.IsThirdPartyAPIGroupVersion(gvk.GroupVersion()) { + cfg.NegotiatedSerializer = thirdpartyresourcedata.NewNegotiatedSerializer(api.Codecs, gvk.Kind, gv, gv) + } + return restclient.RESTClientFor(cfg) +} + +func (f *factory) UnstructuredClientForMapping(mapping *meta.RESTMapping) (resource.RESTClient, error) { + cfg, err := f.clientConfig.ClientConfig() + if err != nil { + return nil, err + } + if err := restclient.SetKubernetesDefaults(cfg); err != nil { + return nil, err + } + cfg.APIPath = "/apis" + if mapping.GroupVersionKind.Group == api.GroupName { + cfg.APIPath = "/api" + } + gv := mapping.GroupVersionKind.GroupVersion() + cfg.ContentConfig = dynamic.ContentConfig() + cfg.GroupVersion = &gv + return restclient.RESTClientFor(cfg) +} + +func (f *factory) Describer(mapping *meta.RESTMapping) (kubectl.Describer, error) { + mappingVersion := mapping.GroupVersionKind.GroupVersion() + if mapping.GroupVersionKind.Group == federation.GroupName { + fedClientSet, err := f.clients.FederationClientSetForVersion(&mappingVersion) + if err != nil { + return nil, err + } + if mapping.GroupVersionKind.Kind == "Cluster" { + return &kubectl.ClusterDescriber{Interface: fedClientSet}, nil + } + } + clientset, err := f.clients.ClientSetForVersion(&mappingVersion) + if err != nil { + return nil, err + } + if describer, ok := kubectl.DescriberFor(mapping.GroupVersionKind.GroupKind(), clientset); ok { + return describer, nil + } + return nil, fmt.Errorf("no description has been implemented for %q", mapping.GroupVersionKind.Kind) +} + +func (f *factory) Decoder(toInternal bool) runtime.Decoder { + var decoder runtime.Decoder + if toInternal { + decoder = api.Codecs.UniversalDecoder() + } else { + decoder = api.Codecs.UniversalDeserializer() + } + return thirdpartyresourcedata.NewDecoder(decoder, "") +} + +func (f *factory) JSONEncoder() runtime.Encoder { + return api.Codecs.LegacyCodec(registered.EnabledVersions()...) +} + +func (f *factory) Printer(mapping *meta.RESTMapping, options kubectl.PrintOptions) (kubectl.ResourcePrinter, error) { + return kubectl.NewHumanReadablePrinter(options), nil +} + +func (f *factory) MapBasedSelectorForObject(object runtime.Object) (string, error) { + // TODO: replace with a swagger schema based approach (identify pod selector via schema introspection) + switch t := object.(type) { + 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 + case *extensions.Deployment: + // TODO(madhusudancs): Make this smarter by admitting MatchExpressions with Equals + // operator, DoubleEquals operator and In operator with only one element in the set. + if len(t.Spec.Selector.MatchExpressions) > 0 { + return "", fmt.Errorf("couldn't convert expressions - \"%+v\" to map-based selector format", t.Spec.Selector.MatchExpressions) + } + return kubectl.MakeLabels(t.Spec.Selector.MatchLabels), nil + case *extensions.ReplicaSet: + // TODO(madhusudancs): Make this smarter by admitting MatchExpressions with Equals + // operator, DoubleEquals operator and In operator with only one element in the set. + if len(t.Spec.Selector.MatchExpressions) > 0 { + return "", fmt.Errorf("couldn't convert expressions - \"%+v\" to map-based selector format", t.Spec.Selector.MatchExpressions) + } + return kubectl.MakeLabels(t.Spec.Selector.MatchLabels), nil + default: + gvks, _, err := api.Scheme.ObjectKinds(object) + if err != nil { + return "", err + } + return "", fmt.Errorf("cannot extract pod selector from %v", gvks[0]) + } +} + +func (f *factory) PortsForObject(object runtime.Object) ([]string, error) { + // TODO: replace with a swagger schema based approach (identify pod selector via schema introspection) + switch t := object.(type) { + case *api.ReplicationController: + return getPorts(t.Spec.Template.Spec), nil + case *api.Pod: + return getPorts(t.Spec), nil + case *api.Service: + return getServicePorts(t.Spec), nil + case *extensions.Deployment: + return getPorts(t.Spec.Template.Spec), nil + case *extensions.ReplicaSet: + return getPorts(t.Spec.Template.Spec), nil + default: + gvks, _, err := api.Scheme.ObjectKinds(object) + if err != nil { + return nil, err + } + return nil, fmt.Errorf("cannot extract ports from %v", gvks[0]) + } +} + +func (f *factory) ProtocolsForObject(object runtime.Object) (map[string]string, error) { + // TODO: replace with a swagger schema based approach (identify pod selector via schema introspection) + switch t := object.(type) { + case *api.ReplicationController: + return getProtocols(t.Spec.Template.Spec), nil + case *api.Pod: + return getProtocols(t.Spec), nil + case *api.Service: + return getServiceProtocols(t.Spec), nil + case *extensions.Deployment: + return getProtocols(t.Spec.Template.Spec), nil + case *extensions.ReplicaSet: + return getProtocols(t.Spec.Template.Spec), nil + default: + gvks, _, err := api.Scheme.ObjectKinds(object) + if err != nil { + return nil, err + } + return nil, fmt.Errorf("cannot extract protocols from %v", gvks[0]) + } +} + +func (f *factory) LabelsForObject(object runtime.Object) (map[string]string, error) { + return meta.NewAccessor().Labels(object) +} + +func (f *factory) LogsForObject(object, options runtime.Object) (*restclient.Request, error) { + clientset, err := f.clients.ClientSetForVersion(nil) + if err != nil { + return nil, err + } + + switch t := object.(type) { + case *api.Pod: + opts, ok := options.(*api.PodLogOptions) + if !ok { + return nil, errors.New("provided options object is not a PodLogOptions") + } + return clientset.Core().Pods(t.Namespace).GetLogs(t.Name, opts), nil + + case *api.ReplicationController: + opts, ok := options.(*api.PodLogOptions) + if !ok { + return nil, errors.New("provided options object is not a PodLogOptions") + } + selector := labels.SelectorFromSet(t.Spec.Selector) + sortBy := func(pods []*api.Pod) sort.Interface { return controller.ByLogging(pods) } + pod, numPods, err := GetFirstPod(clientset.Core(), t.Namespace, selector, 20*time.Second, sortBy) + if err != nil { + return nil, err + } + if numPods > 1 { + fmt.Fprintf(os.Stderr, "Found %v pods, using pod/%v\n", numPods, pod.Name) + } + + return clientset.Core().Pods(pod.Namespace).GetLogs(pod.Name, opts), nil + + case *extensions.ReplicaSet: + opts, ok := options.(*api.PodLogOptions) + if !ok { + return nil, errors.New("provided options object is not a PodLogOptions") + } + selector, err := unversioned.LabelSelectorAsSelector(t.Spec.Selector) + if err != nil { + return nil, fmt.Errorf("invalid label selector: %v", err) + } + sortBy := func(pods []*api.Pod) sort.Interface { return controller.ByLogging(pods) } + pod, numPods, err := GetFirstPod(clientset.Core(), t.Namespace, selector, 20*time.Second, sortBy) + if err != nil { + return nil, err + } + if numPods > 1 { + fmt.Fprintf(os.Stderr, "Found %v pods, using pod/%v\n", numPods, pod.Name) + } + + return clientset.Core().Pods(pod.Namespace).GetLogs(pod.Name, opts), nil + + default: + gvks, _, err := api.Scheme.ObjectKinds(object) + if err != nil { + return nil, err + } + return nil, fmt.Errorf("cannot get the logs from %v", gvks[0]) + } +} + +func (f *factory) PauseObject(object runtime.Object) (bool, error) { + clientset, err := f.clients.ClientSetForVersion(nil) + if err != nil { + return false, err + } + + switch t := object.(type) { + case *extensions.Deployment: + if t.Spec.Paused { + return true, nil + } + t.Spec.Paused = true + _, err := clientset.Extensions().Deployments(t.Namespace).Update(t) + return false, err + default: + gvks, _, err := api.Scheme.ObjectKinds(object) + if err != nil { + return false, err + } + return false, fmt.Errorf("cannot pause %v", gvks[0]) + } +} + +func (f *factory) ResumeObject(object runtime.Object) (bool, error) { + clientset, err := f.clients.ClientSetForVersion(nil) + if err != nil { + return false, err + } + + switch t := object.(type) { + case *extensions.Deployment: + if !t.Spec.Paused { + return true, nil + } + t.Spec.Paused = false + _, err := clientset.Extensions().Deployments(t.Namespace).Update(t) + return false, err + default: + gvks, _, err := api.Scheme.ObjectKinds(object) + if err != nil { + return false, err + } + return false, fmt.Errorf("cannot resume %v", gvks[0]) + } +} + +func (f *factory) Scaler(mapping *meta.RESTMapping) (kubectl.Scaler, error) { + mappingVersion := mapping.GroupVersionKind.GroupVersion() + clientset, err := f.clients.ClientSetForVersion(&mappingVersion) + if err != nil { + return nil, err + } + return kubectl.ScalerFor(mapping.GroupVersionKind.GroupKind(), clientset) +} + +func (f *factory) Reaper(mapping *meta.RESTMapping) (kubectl.Reaper, error) { + mappingVersion := mapping.GroupVersionKind.GroupVersion() + clientset, err := f.clients.ClientSetForVersion(&mappingVersion) + if err != nil { + return nil, err + } + return kubectl.ReaperFor(mapping.GroupVersionKind.GroupKind(), clientset) +} + +func (f *factory) HistoryViewer(mapping *meta.RESTMapping) (kubectl.HistoryViewer, error) { + mappingVersion := mapping.GroupVersionKind.GroupVersion() + clientset, err := f.clients.ClientSetForVersion(&mappingVersion) + if err != nil { + return nil, err + } + return kubectl.HistoryViewerFor(mapping.GroupVersionKind.GroupKind(), clientset) +} + +func (f *factory) Rollbacker(mapping *meta.RESTMapping) (kubectl.Rollbacker, error) { + mappingVersion := mapping.GroupVersionKind.GroupVersion() + clientset, err := f.clients.ClientSetForVersion(&mappingVersion) + if err != nil { + return nil, err + } + return kubectl.RollbackerFor(mapping.GroupVersionKind.GroupKind(), clientset) +} + +func (f *factory) StatusViewer(mapping *meta.RESTMapping) (kubectl.StatusViewer, error) { + mappingVersion := mapping.GroupVersionKind.GroupVersion() + clientset, err := f.clients.ClientSetForVersion(&mappingVersion) + if err != nil { + return nil, err + } + return kubectl.StatusViewerFor(mapping.GroupVersionKind.GroupKind(), clientset) +} + +func (f *factory) Validator(validate bool, cacheDir string) (validation.Schema, error) { + if validate { + clientConfig, err := f.clients.ClientConfigForVersion(nil) + if err != nil { + return nil, err + } + restclient, err := restclient.RESTClientFor(clientConfig) + if err != nil { + return nil, err + } + clientset, err := f.clients.ClientSetForVersion(nil) + if err != nil { + return nil, err + } + dir := cacheDir + if len(dir) > 0 { + version, err := clientset.Discovery().ServerVersion() if err == nil { - if serverVersion, err := discoveryClient.ServerVersion(); err == nil && useDiscoveryRESTMapper(serverVersion.GitVersion) { - // register third party resources with the api machinery groups. This probably should be done, but - // its consistent with old code, so we'll start with it. - if err := registerThirdPartyResources(discoveryClient); err != nil { - fmt.Fprintf(os.Stderr, "Unable to register third party resources: %v\n", err) - } - // ThirdPartyResourceData is special. It's not discoverable, but needed for thirdparty resource listing - // TODO eliminate this once we're truly generic. - thirdPartyResourceDataMapper := meta.NewDefaultRESTMapper([]unversioned.GroupVersion{extensionsv1beta1.SchemeGroupVersion}, registered.InterfacesFor) - thirdPartyResourceDataMapper.Add(extensionsv1beta1.SchemeGroupVersion.WithKind("ThirdPartyResourceData"), meta.RESTScopeNamespace) - - mapper = meta.FirstHitRESTMapper{ - MultiRESTMapper: meta.MultiRESTMapper{ - discovery.NewDeferredDiscoveryRESTMapper(discoveryClient, registered.InterfacesFor), - thirdPartyResourceDataMapper, - }, - } - } - } - - // wrap with shortcuts - mapper = NewShortcutExpander(mapper, discoveryClient) - // wrap with output preferences - mapper = kubectl.OutputVersionMapper{RESTMapper: mapper, OutputVersions: []unversioned.GroupVersion{cmdApiVersion}} - return mapper, api.Scheme - }, - UnstructuredObject: func() (meta.RESTMapper, runtime.ObjectTyper, error) { - cfg, err := clients.ClientConfigForVersion(nil) - if err != nil { - return nil, nil, err - } - - dc, err := discovery.NewDiscoveryClientForConfig(cfg) - if err != nil { - return nil, nil, err - } - - groupResources, err := discovery.GetAPIGroupResources(dc) - if err != nil { - return nil, nil, err - } - - // Register unknown APIs as third party for now to make - // validation happy. TODO perhaps make a dynamic schema - // validator to avoid this. - for _, group := range groupResources { - for _, version := range group.Group.Versions { - gv := unversioned.GroupVersion{Group: group.Group.Name, Version: version.Version} - if !registered.IsRegisteredVersion(gv) { - registered.AddThirdPartyAPIGroupVersions(gv) - } - } - } - - mapper := discovery.NewRESTMapper(groupResources, meta.InterfacesForUnstructured) - - typer := discovery.NewUnstructuredObjectTyper(groupResources) - - return NewShortcutExpander(mapper, dc), typer, nil - }, - RESTClient: func() (*restclient.RESTClient, error) { - clientConfig, err := clients.ClientConfigForVersion(nil) - if err != nil { - return nil, err - } - return restclient.RESTClientFor(clientConfig) - }, - ClientSet: func() (*internalclientset.Clientset, error) { - return clients.ClientSetForVersion(nil) - }, - ClientConfig: func() (*restclient.Config, error) { - return clients.ClientConfigForVersion(nil) - }, - ClientForMapping: func(mapping *meta.RESTMapping) (resource.RESTClient, error) { - cfg, err := clientConfig.ClientConfig() - if err != nil { - return nil, err - } - if err := client.SetKubernetesDefaults(cfg); err != nil { - return nil, err - } - gvk := mapping.GroupVersionKind - switch gvk.Group { - case federation.GroupName: - mappingVersion := mapping.GroupVersionKind.GroupVersion() - return clients.FederationClientForVersion(&mappingVersion) - case api.GroupName: - cfg.APIPath = "/api" - default: - cfg.APIPath = "/apis" - } - gv := gvk.GroupVersion() - cfg.GroupVersion = &gv - if registered.IsThirdPartyAPIGroupVersion(gvk.GroupVersion()) { - cfg.NegotiatedSerializer = thirdpartyresourcedata.NewNegotiatedSerializer(api.Codecs, gvk.Kind, gv, gv) - } - return restclient.RESTClientFor(cfg) - }, - UnstructuredClientForMapping: func(mapping *meta.RESTMapping) (resource.RESTClient, error) { - cfg, err := clientConfig.ClientConfig() - if err != nil { - return nil, err - } - if err := restclient.SetKubernetesDefaults(cfg); err != nil { - return nil, err - } - cfg.APIPath = "/apis" - if mapping.GroupVersionKind.Group == api.GroupName { - cfg.APIPath = "/api" - } - gv := mapping.GroupVersionKind.GroupVersion() - cfg.ContentConfig = dynamic.ContentConfig() - cfg.GroupVersion = &gv - return restclient.RESTClientFor(cfg) - }, - Describer: func(mapping *meta.RESTMapping) (kubectl.Describer, error) { - mappingVersion := mapping.GroupVersionKind.GroupVersion() - if mapping.GroupVersionKind.Group == federation.GroupName { - fedClientSet, err := clients.FederationClientSetForVersion(&mappingVersion) - if err != nil { - return nil, err - } - if mapping.GroupVersionKind.Kind == "Cluster" { - return &kubectl.ClusterDescriber{Interface: fedClientSet}, nil - } - } - clientset, err := clients.ClientSetForVersion(&mappingVersion) - if err != nil { - return nil, err - } - if describer, ok := kubectl.DescriberFor(mapping.GroupVersionKind.GroupKind(), clientset); ok { - return describer, nil - } - return nil, fmt.Errorf("no description has been implemented for %q", mapping.GroupVersionKind.Kind) - }, - Decoder: func(toInternal bool) runtime.Decoder { - var decoder runtime.Decoder - if toInternal { - decoder = api.Codecs.UniversalDecoder() + dir = path.Join(cacheDir, version.String()) } else { - decoder = api.Codecs.UniversalDeserializer() + dir = "" // disable caching as a fallback } - return thirdpartyresourcedata.NewDecoder(decoder, "") + } + fedClient, err := f.clients.FederationClientForVersion(nil) + if err != nil { + return nil, err + } + return &clientSwaggerSchema{ + c: restclient, + fedc: fedClient, + cacheDir: dir, + }, nil + } + return validation.NullSchema{}, nil +} - }, - JSONEncoder: func() runtime.Encoder { - return api.Codecs.LegacyCodec(registered.EnabledVersions()...) - }, - Printer: func(mapping *meta.RESTMapping, options kubectl.PrintOptions) (kubectl.ResourcePrinter, error) { - return kubectl.NewHumanReadablePrinter(options), nil - }, - MapBasedSelectorForObject: func(object runtime.Object) (string, error) { - // TODO: replace with a swagger schema based approach (identify pod selector via schema introspection) - switch t := object.(type) { - 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 - case *extensions.Deployment: - // TODO(madhusudancs): Make this smarter by admitting MatchExpressions with Equals - // operator, DoubleEquals operator and In operator with only one element in the set. - if len(t.Spec.Selector.MatchExpressions) > 0 { - return "", fmt.Errorf("couldn't convert expressions - \"%+v\" to map-based selector format", t.Spec.Selector.MatchExpressions) - } - return kubectl.MakeLabels(t.Spec.Selector.MatchLabels), nil - case *extensions.ReplicaSet: - // TODO(madhusudancs): Make this smarter by admitting MatchExpressions with Equals - // operator, DoubleEquals operator and In operator with only one element in the set. - if len(t.Spec.Selector.MatchExpressions) > 0 { - return "", fmt.Errorf("couldn't convert expressions - \"%+v\" to map-based selector format", t.Spec.Selector.MatchExpressions) - } - return kubectl.MakeLabels(t.Spec.Selector.MatchLabels), nil - default: - gvks, _, err := api.Scheme.ObjectKinds(object) - if err != nil { - return "", err - } - return "", fmt.Errorf("cannot extract pod selector from %v", gvks[0]) - } - }, - PortsForObject: func(object runtime.Object) ([]string, error) { - // TODO: replace with a swagger schema based approach (identify pod selector via schema introspection) - switch t := object.(type) { - case *api.ReplicationController: - return getPorts(t.Spec.Template.Spec), nil - case *api.Pod: - return getPorts(t.Spec), nil - case *api.Service: - return getServicePorts(t.Spec), nil - case *extensions.Deployment: - return getPorts(t.Spec.Template.Spec), nil - case *extensions.ReplicaSet: - return getPorts(t.Spec.Template.Spec), nil - default: - gvks, _, err := api.Scheme.ObjectKinds(object) - if err != nil { - return nil, err - } - return nil, fmt.Errorf("cannot extract ports from %v", gvks[0]) - } - }, - ProtocolsForObject: func(object runtime.Object) (map[string]string, error) { - // TODO: replace with a swagger schema based approach (identify pod selector via schema introspection) - switch t := object.(type) { - case *api.ReplicationController: - return getProtocols(t.Spec.Template.Spec), nil - case *api.Pod: - return getProtocols(t.Spec), nil - case *api.Service: - return getServiceProtocols(t.Spec), nil - case *extensions.Deployment: - return getProtocols(t.Spec.Template.Spec), nil - case *extensions.ReplicaSet: - return getProtocols(t.Spec.Template.Spec), nil - default: - gvks, _, err := api.Scheme.ObjectKinds(object) - if err != nil { - return nil, err - } - return nil, fmt.Errorf("cannot extract protocols from %v", gvks[0]) - } - }, - LabelsForObject: func(object runtime.Object) (map[string]string, error) { - return meta.NewAccessor().Labels(object) - }, - LogsForObject: func(object, options runtime.Object) (*restclient.Request, error) { - clientset, err := clients.ClientSetForVersion(nil) - if err != nil { - return nil, err - } +func (f *factory) SwaggerSchema(gvk unversioned.GroupVersionKind) (*swagger.ApiDeclaration, error) { + version := gvk.GroupVersion() + clientset, err := f.clients.ClientSetForVersion(&version) + if err != nil { + return nil, err + } + return clientset.Discovery().SwaggerSchema(version) +} - switch t := object.(type) { - case *api.Pod: - opts, ok := options.(*api.PodLogOptions) - if !ok { - return nil, errors.New("provided options object is not a PodLogOptions") - } - return clientset.Core().Pods(t.Namespace).GetLogs(t.Name, opts), nil +func (f *factory) DefaultNamespace() (string, bool, error) { + return f.clientConfig.Namespace() +} - case *api.ReplicationController: - opts, ok := options.(*api.PodLogOptions) - if !ok { - return nil, errors.New("provided options object is not a PodLogOptions") - } - selector := labels.SelectorFromSet(t.Spec.Selector) - sortBy := func(pods []*api.Pod) sort.Interface { return controller.ByLogging(pods) } - pod, numPods, err := GetFirstPod(clientset.Core(), t.Namespace, selector, 20*time.Second, sortBy) - if err != nil { - return nil, err - } - if numPods > 1 { - fmt.Fprintf(os.Stderr, "Found %v pods, using pod/%v\n", numPods, pod.Name) - } +func (f *factory) Generators(cmdName string) map[string]kubectl.Generator { + return DefaultGenerators(cmdName) +} - return clientset.Core().Pods(pod.Namespace).GetLogs(pod.Name, opts), nil +func (f *factory) CanBeExposed(kind unversioned.GroupKind) error { + switch kind { + case api.Kind("ReplicationController"), api.Kind("Service"), api.Kind("Pod"), extensions.Kind("Deployment"), extensions.Kind("ReplicaSet"): + // nothing to do here + default: + return fmt.Errorf("cannot expose a %s", kind) + } + return nil +} - case *extensions.ReplicaSet: - opts, ok := options.(*api.PodLogOptions) - if !ok { - return nil, errors.New("provided options object is not a PodLogOptions") - } - selector, err := unversioned.LabelSelectorAsSelector(t.Spec.Selector) - if err != nil { - return nil, fmt.Errorf("invalid label selector: %v", err) - } - sortBy := func(pods []*api.Pod) sort.Interface { return controller.ByLogging(pods) } - pod, numPods, err := GetFirstPod(clientset.Core(), t.Namespace, selector, 20*time.Second, sortBy) - if err != nil { - return nil, err - } - if numPods > 1 { - fmt.Fprintf(os.Stderr, "Found %v pods, using pod/%v\n", numPods, pod.Name) - } +func (f *factory) CanBeAutoscaled(kind unversioned.GroupKind) error { + switch kind { + case api.Kind("ReplicationController"), extensions.Kind("Deployment"), extensions.Kind("ReplicaSet"): + // nothing to do here + default: + return fmt.Errorf("cannot autoscale a %v", kind) + } + return nil +} - return clientset.Core().Pods(pod.Namespace).GetLogs(pod.Name, opts), nil +func (f *factory) AttachablePodForObject(object runtime.Object) (*api.Pod, error) { + clientset, err := f.clients.ClientSetForVersion(nil) + if err != nil { + return nil, err + } + switch t := object.(type) { + case *api.ReplicationController: + selector := labels.SelectorFromSet(t.Spec.Selector) + sortBy := func(pods []*api.Pod) sort.Interface { return sort.Reverse(controller.ActivePods(pods)) } + pod, _, err := GetFirstPod(clientset.Core(), t.Namespace, selector, 1*time.Minute, sortBy) + return pod, err + case *extensions.Deployment: + selector, err := unversioned.LabelSelectorAsSelector(t.Spec.Selector) + if err != nil { + return nil, fmt.Errorf("invalid label selector: %v", err) + } + sortBy := func(pods []*api.Pod) sort.Interface { return sort.Reverse(controller.ActivePods(pods)) } + pod, _, err := GetFirstPod(clientset.Core(), t.Namespace, selector, 1*time.Minute, sortBy) + return pod, err + case *batch.Job: + selector, err := unversioned.LabelSelectorAsSelector(t.Spec.Selector) + if err != nil { + return nil, fmt.Errorf("invalid label selector: %v", err) + } + sortBy := func(pods []*api.Pod) sort.Interface { return sort.Reverse(controller.ActivePods(pods)) } + pod, _, err := GetFirstPod(clientset.Core(), t.Namespace, selector, 1*time.Minute, sortBy) + return pod, err + case *api.Pod: + return t, nil + default: + gvks, _, err := api.Scheme.ObjectKinds(object) + if err != nil { + return nil, err + } + return nil, fmt.Errorf("cannot attach to %v: not implemented", gvks[0]) + } +} - default: - gvks, _, err := api.Scheme.ObjectKinds(object) - if err != nil { - return nil, err - } - return nil, fmt.Errorf("cannot get the logs from %v", gvks[0]) - } - }, - PauseObject: func(object runtime.Object) (bool, error) { - clientset, err := clients.ClientSetForVersion(nil) - if err != nil { - return false, err - } +func (f *factory) UpdatePodSpecForObject(obj runtime.Object, fn func(*api.PodSpec) error) (bool, error) { + // TODO: replace with a swagger schema based approach (identify pod template via schema introspection) + switch t := obj.(type) { + case *api.Pod: + return true, fn(&t.Spec) + case *api.ReplicationController: + if t.Spec.Template == nil { + t.Spec.Template = &api.PodTemplateSpec{} + } + return true, fn(&t.Spec.Template.Spec) + case *extensions.Deployment: + return true, fn(&t.Spec.Template.Spec) + case *extensions.DaemonSet: + return true, fn(&t.Spec.Template.Spec) + case *extensions.ReplicaSet: + return true, fn(&t.Spec.Template.Spec) + case *apps.PetSet: + return true, fn(&t.Spec.Template.Spec) + case *batch.Job: + return true, fn(&t.Spec.Template.Spec) + default: + return false, fmt.Errorf("the object is not a pod or does not have a pod template") + } +} - switch t := object.(type) { - case *extensions.Deployment: - if t.Spec.Paused { - return true, nil - } - t.Spec.Paused = true - _, err := clientset.Extensions().Deployments(t.Namespace).Update(t) - return false, err - default: - gvks, _, err := api.Scheme.ObjectKinds(object) - if err != nil { - return false, err - } - return false, fmt.Errorf("cannot pause %v", gvks[0]) - } - }, - ResumeObject: func(object runtime.Object) (bool, error) { - clientset, err := clients.ClientSetForVersion(nil) - if err != nil { - return false, err - } +func (f *factory) EditorEnvs() []string { + return []string{"KUBE_EDITOR", "EDITOR"} +} - switch t := object.(type) { - case *extensions.Deployment: - if !t.Spec.Paused { - return true, nil - } - t.Spec.Paused = false - _, err := clientset.Extensions().Deployments(t.Namespace).Update(t) - return false, err - default: - gvks, _, err := api.Scheme.ObjectKinds(object) - if err != nil { - return false, err - } - return false, fmt.Errorf("cannot resume %v", gvks[0]) - } - }, - Scaler: func(mapping *meta.RESTMapping) (kubectl.Scaler, error) { - mappingVersion := mapping.GroupVersionKind.GroupVersion() - clientset, err := clients.ClientSetForVersion(&mappingVersion) - if err != nil { - return nil, err - } - return kubectl.ScalerFor(mapping.GroupVersionKind.GroupKind(), clientset) - }, - Reaper: func(mapping *meta.RESTMapping) (kubectl.Reaper, error) { - mappingVersion := mapping.GroupVersionKind.GroupVersion() - clientset, err := clients.ClientSetForVersion(&mappingVersion) - if err != nil { - return nil, err - } - return kubectl.ReaperFor(mapping.GroupVersionKind.GroupKind(), clientset) - }, - HistoryViewer: func(mapping *meta.RESTMapping) (kubectl.HistoryViewer, error) { - mappingVersion := mapping.GroupVersionKind.GroupVersion() - clientset, err := clients.ClientSetForVersion(&mappingVersion) - if err != nil { - return nil, err - } - return kubectl.HistoryViewerFor(mapping.GroupVersionKind.GroupKind(), clientset) - }, - Rollbacker: func(mapping *meta.RESTMapping) (kubectl.Rollbacker, error) { - mappingVersion := mapping.GroupVersionKind.GroupVersion() - clientset, err := clients.ClientSetForVersion(&mappingVersion) - if err != nil { - return nil, err - } - return kubectl.RollbackerFor(mapping.GroupVersionKind.GroupKind(), clientset) - }, - StatusViewer: func(mapping *meta.RESTMapping) (kubectl.StatusViewer, error) { - mappingVersion := mapping.GroupVersionKind.GroupVersion() - clientset, err := clients.ClientSetForVersion(&mappingVersion) - if err != nil { - return nil, err - } - return kubectl.StatusViewerFor(mapping.GroupVersionKind.GroupKind(), clientset) - }, - Validator: func(validate bool, cacheDir string) (validation.Schema, error) { - if validate { - clientConfig, err := clients.ClientConfigForVersion(nil) - if err != nil { - return nil, err - } - restclient, err := restclient.RESTClientFor(clientConfig) - if err != nil { - return nil, err - } - clientset, err := clients.ClientSetForVersion(nil) - if err != nil { - return nil, err - } - dir := cacheDir - if len(dir) > 0 { - version, err := clientset.Discovery().ServerVersion() - if err == nil { - dir = path.Join(cacheDir, version.String()) - } else { - dir = "" // disable caching as a fallback - } - } - fedClient, err := clients.FederationClientForVersion(nil) - if err != nil { - return nil, err - } - return &clientSwaggerSchema{ - c: restclient, - fedc: fedClient, - cacheDir: dir, - }, nil - } - return validation.NullSchema{}, nil - }, - SwaggerSchema: func(gvk unversioned.GroupVersionKind) (*swagger.ApiDeclaration, error) { - version := gvk.GroupVersion() - clientset, err := clients.ClientSetForVersion(&version) - if err != nil { - return nil, err - } - return clientset.Discovery().SwaggerSchema(version) - }, - DefaultNamespace: func() (string, bool, error) { - return clientConfig.Namespace() - }, - Generators: func(cmdName string) map[string]kubectl.Generator { - return DefaultGenerators(cmdName) - }, - CanBeExposed: func(kind unversioned.GroupKind) error { - switch kind { - case api.Kind("ReplicationController"), api.Kind("Service"), api.Kind("Pod"), extensions.Kind("Deployment"), extensions.Kind("ReplicaSet"): - // nothing to do here - default: - return fmt.Errorf("cannot expose a %s", kind) - } - return nil - }, - CanBeAutoscaled: func(kind unversioned.GroupKind) error { - switch kind { - case api.Kind("ReplicationController"), extensions.Kind("Deployment"), extensions.Kind("ReplicaSet"): - // nothing to do here - default: - return fmt.Errorf("cannot autoscale a %v", kind) - } - return nil - }, - AttachablePodForObject: func(object runtime.Object) (*api.Pod, error) { - clientset, err := clients.ClientSetForVersion(nil) - if err != nil { - return nil, err - } - switch t := object.(type) { - case *api.ReplicationController: - selector := labels.SelectorFromSet(t.Spec.Selector) - sortBy := func(pods []*api.Pod) sort.Interface { return sort.Reverse(controller.ActivePods(pods)) } - pod, _, err := GetFirstPod(clientset.Core(), t.Namespace, selector, 1*time.Minute, sortBy) - return pod, err - case *extensions.Deployment: - selector, err := unversioned.LabelSelectorAsSelector(t.Spec.Selector) - if err != nil { - return nil, fmt.Errorf("invalid label selector: %v", err) - } - sortBy := func(pods []*api.Pod) sort.Interface { return sort.Reverse(controller.ActivePods(pods)) } - pod, _, err := GetFirstPod(clientset.Core(), t.Namespace, selector, 1*time.Minute, sortBy) - return pod, err - case *batch.Job: - selector, err := unversioned.LabelSelectorAsSelector(t.Spec.Selector) - if err != nil { - return nil, fmt.Errorf("invalid label selector: %v", err) - } - sortBy := func(pods []*api.Pod) sort.Interface { return sort.Reverse(controller.ActivePods(pods)) } - pod, _, err := GetFirstPod(clientset.Core(), t.Namespace, selector, 1*time.Minute, sortBy) - return pod, err - case *api.Pod: - return t, nil - default: - gvks, _, err := api.Scheme.ObjectKinds(object) - if err != nil { - return nil, err - } - return nil, fmt.Errorf("cannot attach to %v: not implemented", gvks[0]) - } - }, - // UpdatePodSpecForObject update the pod specification for the provided object - UpdatePodSpecForObject: func(obj runtime.Object, fn func(*api.PodSpec) error) (bool, error) { - // TODO: replace with a swagger schema based approach (identify pod template via schema introspection) - switch t := obj.(type) { - case *api.Pod: - return true, fn(&t.Spec) - case *api.ReplicationController: - if t.Spec.Template == nil { - t.Spec.Template = &api.PodTemplateSpec{} - } - return true, fn(&t.Spec.Template.Spec) - case *extensions.Deployment: - return true, fn(&t.Spec.Template.Spec) - case *extensions.DaemonSet: - return true, fn(&t.Spec.Template.Spec) - case *extensions.ReplicaSet: - return true, fn(&t.Spec.Template.Spec) - case *apps.PetSet: - return true, fn(&t.Spec.Template.Spec) - case *batch.Job: - return true, fn(&t.Spec.Template.Spec) - default: - return false, fmt.Errorf("the object is not a pod or does not have a pod template") - } - }, - EditorEnvs: func() []string { - return []string{"KUBE_EDITOR", "EDITOR"} - }, - PrintObjectSpecificMessage: func(obj runtime.Object, out io.Writer) { - switch obj := obj.(type) { - case *api.Service: - if obj.Spec.Type == api.ServiceTypeNodePort { - msg := fmt.Sprintf( - `You have exposed your service on an external port on all nodes in your +func (f *factory) PrintObjectSpecificMessage(obj runtime.Object, out io.Writer) { + switch obj := obj.(type) { + case *api.Service: + if obj.Spec.Type == api.ServiceTypeNodePort { + msg := fmt.Sprintf( + `You have exposed your service on an external port on all nodes in your cluster. If you want to expose this service to the external internet, you may need to set up firewall rules for the service port(s) (%s) to serve traffic. See http://kubernetes.io/docs/user-guide/services-firewalls for more details. `, - makePortsString(obj.Spec.Ports, true)) - out.Write([]byte(msg)) - } + makePortsString(obj.Spec.Ports, true)) + out.Write([]byte(msg)) + } - if _, ok := obj.Annotations[service.AnnotationLoadBalancerSourceRangesKey]; ok { - msg := fmt.Sprintf( - `You are using service annotation [service.beta.kubernetes.io/load-balancer-source-ranges]. + if _, ok := obj.Annotations[service.AnnotationLoadBalancerSourceRangesKey]; ok { + msg := fmt.Sprintf( + `You are using service annotation [service.beta.kubernetes.io/load-balancer-source-ranges]. It has been promoted to field [loadBalancerSourceRanges] in service spec. This annotation will be deprecated in the future. Please use the loadBalancerSourceRanges field instead. See http://kubernetes.io/docs/user-guide/services-firewalls for more details. `) - out.Write([]byte(msg)) - } - } - }, + out.Write([]byte(msg)) + } } } @@ -883,10 +944,8 @@ func GetFirstPod(client coreclient.PodsGetter, namespace string, selector labels return pod, 1, nil } -// Command will stringify and return all environment arguments ie. a command run by a client -// using the factory. // TODO: We need to filter out stuff like secrets. -func (f *Factory) Command() string { +func (f *factory) Command() string { if len(os.Args) == 0 { return "" } @@ -895,8 +954,7 @@ func (f *Factory) Command() string { return strings.Join(args, " ") } -// BindFlags adds any flags that are common to all kubectl sub commands. -func (f *Factory) BindFlags(flags *pflag.FlagSet) { +func (f *factory) BindFlags(flags *pflag.FlagSet) { // Merge factory's flags flags.AddFlagSet(f.flags) @@ -911,8 +969,7 @@ func (f *Factory) BindFlags(flags *pflag.FlagSet) { flags.SetNormalizeFunc(utilflag.WordSepNormalizeFunc) } -// BindCommonFlags adds any flags defined by external projects (not part of pflags) -func (f *Factory) BindExternalFlags(flags *pflag.FlagSet) { +func (f *factory) BindExternalFlags(flags *pflag.FlagSet) { // any flags defined by external projects (not part of pflags) flags.AddGoFlagSet(flag.CommandLine) } @@ -1179,7 +1236,7 @@ func DefaultClientConfig(flags *pflag.FlagSet) clientcmd.ClientConfig { return clientConfig } -func (f *Factory) DefaultResourceFilterOptions(cmd *cobra.Command, withNamespace bool) *kubectl.PrintOptions { +func (f *factory) DefaultResourceFilterOptions(cmd *cobra.Command, withNamespace bool) *kubectl.PrintOptions { columnLabel, err := cmd.Flags().GetStringSlice("label-columns") if err != nil { columnLabel = []string{} @@ -1197,13 +1254,11 @@ func (f *Factory) DefaultResourceFilterOptions(cmd *cobra.Command, withNamespace return opts } -// DefaultResourceFilterFunc returns a collection of FilterFuncs suitable for filtering specific resource types. -func (f *Factory) DefaultResourceFilterFunc() kubectl.Filters { +func (f *factory) DefaultResourceFilterFunc() kubectl.Filters { return kubectl.NewResourceFilter() } -// PrintObject prints an api object given command line flags to modify the output format -func (f *Factory) PrintObject(cmd *cobra.Command, mapper meta.RESTMapper, obj runtime.Object, out io.Writer) error { +func (f *factory) PrintObject(cmd *cobra.Command, mapper meta.RESTMapper, obj runtime.Object, out io.Writer) error { gvks, _, err := api.Scheme.ObjectKinds(obj) if err != nil { return err @@ -1221,9 +1276,7 @@ func (f *Factory) PrintObject(cmd *cobra.Command, mapper meta.RESTMapper, obj ru 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, withNamespace bool) (kubectl.ResourcePrinter, error) { +func (f *factory) PrinterForMapping(cmd *cobra.Command, mapping *meta.RESTMapping, withNamespace bool) (kubectl.ResourcePrinter, error) { printer, ok, err := PrinterForCommand(cmd) if err != nil { return nil, err @@ -1273,8 +1326,7 @@ func (f *Factory) PrinterForMapping(cmd *cobra.Command, mapping *meta.RESTMappin return printer, nil } -// One stop shopping for a Builder -func (f *Factory) NewBuilder() *resource.Builder { +func (f *factory) NewBuilder() *resource.Builder { mapper, typer := f.Object() return resource.NewBuilder(mapper, typer, resource.ClientMapperFunc(f.ClientForMapping), f.Decoder(true)) diff --git a/pkg/kubectl/cmd/util/factory_test.go b/pkg/kubectl/cmd/util/factory_test.go index 9b746974b4b..e51dc031059 100644 --- a/pkg/kubectl/cmd/util/factory_test.go +++ b/pkg/kubectl/cmd/util/factory_test.go @@ -55,7 +55,7 @@ import ( func TestNewFactoryDefaultFlagBindings(t *testing.T) { factory := NewFactory(nil) - if !factory.flags.HasFlags() { + if !factory.FlagSet().HasFlags() { t.Errorf("Expected flags, but didn't get any") } } @@ -64,8 +64,8 @@ func TestNewFactoryNoFlagBindings(t *testing.T) { clientConfig := clientcmd.NewDefaultClientConfig(*clientcmdapi.NewConfig(), &clientcmd.ConfigOverrides{}) factory := NewFactory(clientConfig) - if factory.flags.HasFlags() { - t.Errorf("Expected zero flags, but got %v", factory.flags) + if factory.FlagSet().HasFlags() { + t.Errorf("Expected zero flags, but got %v", factory.FlagSet()) } } @@ -229,12 +229,12 @@ func TestCanBeExposed(t *testing.T) { func TestFlagUnderscoreRenaming(t *testing.T) { factory := NewFactory(nil) - factory.flags.SetNormalizeFunc(flag.WordSepNormalizeFunc) - factory.flags.Bool("valid_flag", false, "bool value") + factory.FlagSet().SetNormalizeFunc(flag.WordSepNormalizeFunc) + factory.FlagSet().Bool("valid_flag", false, "bool value") // In case of failure of this test check this PR: spf13/pflag#23 - if factory.flags.Lookup("valid_flag").Name != "valid-flag" { - t.Fatalf("Expected flag name to be valid-flag, got %s", factory.flags.Lookup("valid_flag").Name) + if factory.FlagSet().Lookup("valid_flag").Name != "valid-flag" { + t.Fatalf("Expected flag name to be valid-flag, got %s", factory.FlagSet().Lookup("valid_flag").Name) } } From dfae1a27c245334ceaff869786f74392c8321b94 Mon Sep 17 00:00:00 2001 From: AdoHe Date: Thu, 13 Oct 2016 08:11:07 +0800 Subject: [PATCH 2/3] add fake factories for test --- pkg/kubectl/cmd/cmd_test.go | 509 +++++++++++++++++++++++++----------- 1 file changed, 356 insertions(+), 153 deletions(-) diff --git a/pkg/kubectl/cmd/cmd_test.go b/pkg/kubectl/cmd/cmd_test.go index 072bfe7bdb9..3677842488a 100644 --- a/pkg/kubectl/cmd/cmd_test.go +++ b/pkg/kubectl/cmd/cmd_test.go @@ -28,6 +28,10 @@ import ( "testing" "time" + "github.com/emicklei/go-restful/swagger" + "github.com/spf13/cobra" + "github.com/spf13/pflag" + "k8s.io/kubernetes/pkg/api" "k8s.io/kubernetes/pkg/api/meta" "k8s.io/kubernetes/pkg/api/testapi" @@ -210,7 +214,12 @@ type testFactory struct { Err error } -func NewTestFactory() (*cmdutil.Factory, *testFactory, runtime.Codec, runtime.NegotiatedSerializer) { +type fakeFactory struct { + tf *testFactory + Codec runtime.Codec +} + +func NewTestFactory() (cmdutil.Factory, *testFactory, runtime.Codec, runtime.NegotiatedSerializer) { scheme, mapper, codec := newExternalScheme() t := &testFactory{ Validator: validation.NullSchema{}, @@ -220,167 +229,361 @@ func NewTestFactory() (*cmdutil.Factory, *testFactory, runtime.Codec, runtime.Ne negotiatedSerializer := serializer.NegotiatedSerializerWrapper( runtime.SerializerInfo{Serializer: codec}, runtime.StreamSerializerInfo{}) - return &cmdutil.Factory{ - Object: func() (meta.RESTMapper, runtime.ObjectTyper) { - priorityRESTMapper := meta.PriorityRESTMapper{ - Delegate: t.Mapper, - ResourcePriority: []unversioned.GroupVersionResource{ - {Group: meta.AnyGroup, Version: "v1", Resource: meta.AnyResource}, - }, - KindPriority: []unversioned.GroupVersionKind{ - {Group: meta.AnyGroup, Version: "v1", Kind: meta.AnyKind}, - }, - } - return priorityRESTMapper, t.Typer - }, - ClientForMapping: func(*meta.RESTMapping) (resource.RESTClient, error) { - return t.Client, t.Err - }, - Decoder: func(bool) runtime.Decoder { - return codec - }, - JSONEncoder: func() runtime.Encoder { - return codec - }, - Describer: func(*meta.RESTMapping) (kubectl.Describer, error) { - return t.Describer, t.Err - }, - Printer: func(mapping *meta.RESTMapping, options kubectl.PrintOptions) (kubectl.ResourcePrinter, error) { - return t.Printer, t.Err - }, - Validator: func(validate bool, cacheDir string) (validation.Schema, error) { - return t.Validator, t.Err - }, - DefaultNamespace: func() (string, bool, error) { - return t.Namespace, false, t.Err - }, - ClientConfig: func() (*restclient.Config, error) { - return t.ClientConfig, t.Err - }, + return &fakeFactory{ + tf: t, + Codec: codec, }, t, codec, negotiatedSerializer } -func NewMixedFactory(apiClient resource.RESTClient) (*cmdutil.Factory, *testFactory, runtime.Codec) { - f, t, c, _ := NewTestFactory() - var multiRESTMapper meta.MultiRESTMapper - multiRESTMapper = append(multiRESTMapper, t.Mapper) - multiRESTMapper = append(multiRESTMapper, testapi.Default.RESTMapper()) - f.Object = func() (meta.RESTMapper, runtime.ObjectTyper) { - priorityRESTMapper := meta.PriorityRESTMapper{ - Delegate: multiRESTMapper, - ResourcePriority: []unversioned.GroupVersionResource{ - {Group: meta.AnyGroup, Version: "v1", Resource: meta.AnyResource}, - }, - KindPriority: []unversioned.GroupVersionKind{ - {Group: meta.AnyGroup, Version: "v1", Kind: meta.AnyKind}, - }, - } - return priorityRESTMapper, runtime.MultiObjectTyper{t.Typer, api.Scheme} - } - f.ClientForMapping = func(m *meta.RESTMapping) (resource.RESTClient, error) { - if m.ObjectConvertor == api.Scheme { - return apiClient, t.Err - } - return t.Client, t.Err - } - return f, t, c +func (f *fakeFactory) FlagSet() *pflag.FlagSet { + return nil } -func NewAPIFactory() (*cmdutil.Factory, *testFactory, runtime.Codec, runtime.NegotiatedSerializer) { +func (f *fakeFactory) Object() (meta.RESTMapper, runtime.ObjectTyper) { + priorityRESTMapper := meta.PriorityRESTMapper{ + Delegate: f.tf.Mapper, + ResourcePriority: []unversioned.GroupVersionResource{ + {Group: meta.AnyGroup, Version: "v1", Resource: meta.AnyResource}, + }, + KindPriority: []unversioned.GroupVersionKind{ + {Group: meta.AnyGroup, Version: "v1", Kind: meta.AnyKind}, + }, + } + return priorityRESTMapper, f.tf.Typer +} + +func (f *fakeFactory) UnstructuredObject() (meta.RESTMapper, runtime.ObjectTyper, error) { + return nil, nil, nil +} + +func (f *fakeFactory) Decoder(bool) runtime.Decoder { + return f.Codec +} + +func (f *fakeFactory) JSONEncoder() runtime.Encoder { + return f.Codec +} + +func (f *fakeFactory) RESTClient() (*restclient.RESTClient, error) { + return nil, nil +} + +func (f *fakeFactory) ClientSet() (*internalclientset.Clientset, error) { + return nil, nil +} + +func (f *fakeFactory) ClientConfig() (*restclient.Config, error) { + return f.tf.ClientConfig, f.tf.Err +} + +func (f *fakeFactory) ClientForMapping(*meta.RESTMapping) (resource.RESTClient, error) { + return f.tf.Client, f.tf.Err +} + +func (f *fakeFactory) UnstructuredClientForMapping(*meta.RESTMapping) (resource.RESTClient, error) { + return nil, nil +} + +func (f *fakeFactory) Describer(*meta.RESTMapping) (kubectl.Describer, error) { + return f.tf.Describer, f.tf.Err +} + +func (f *fakeFactory) Printer(mapping *meta.RESTMapping, options kubectl.PrintOptions) (kubectl.ResourcePrinter, error) { + return f.tf.Printer, f.tf.Err +} + +func (f *fakeFactory) Scaler(*meta.RESTMapping) (kubectl.Scaler, error) { + return nil, nil +} + +func (f *fakeFactory) Reaper(*meta.RESTMapping) (kubectl.Reaper, error) { + return nil, nil +} + +func (f *fakeFactory) HistoryViewer(*meta.RESTMapping) (kubectl.HistoryViewer, error) { + return nil, nil +} + +func (f *fakeFactory) Rollbacker(*meta.RESTMapping) (kubectl.Rollbacker, error) { + return nil, nil +} + +func (f *fakeFactory) StatusViewer(*meta.RESTMapping) (kubectl.StatusViewer, error) { + return nil, nil +} + +func (f *fakeFactory) MapBasedSelectorForObject(runtime.Object) (string, error) { + return "", nil +} + +func (f *fakeFactory) PortsForObject(runtime.Object) ([]string, error) { + return nil, nil +} + +func (f *fakeFactory) ProtocolsForObject(runtime.Object) (map[string]string, error) { + return nil, nil +} + +func (f *fakeFactory) LabelsForObject(runtime.Object) (map[string]string, error) { + return nil, nil +} + +func (f *fakeFactory) LogsForObject(object, options runtime.Object) (*restclient.Request, error) { + return nil, nil +} + +func (f *fakeFactory) PauseObject(runtime.Object) (bool, error) { + return false, nil +} + +func (f *fakeFactory) ResumeObject(runtime.Object) (bool, error) { + return false, nil +} + +func (f *fakeFactory) Validator(validate bool, cacheDir string) (validation.Schema, error) { + return f.tf.Validator, f.tf.Err +} + +func (f *fakeFactory) SwaggerSchema(unversioned.GroupVersionKind) (*swagger.ApiDeclaration, error) { + return nil, nil +} + +func (f *fakeFactory) DefaultNamespace() (string, bool, error) { + return f.tf.Namespace, false, f.tf.Err +} + +func (f *fakeFactory) Generators(string) map[string]kubectl.Generator { + return nil +} + +func (f *fakeFactory) CanBeExposed(unversioned.GroupKind) error { + return nil +} + +func (f *fakeFactory) CanBeAutoscaled(unversioned.GroupKind) error { + return nil +} + +func (f *fakeFactory) AttachablePodForObject(ob runtime.Object) (*api.Pod, error) { + return nil, nil +} + +func (f *fakeFactory) UpdatePodSpecForObject(obj runtime.Object, fn func(*api.PodSpec) error) (bool, error) { + return false, nil +} + +func (f *fakeFactory) EditorEnvs() []string { + return nil +} + +func (f *fakeFactory) PrintObjectSpecificMessage(obj runtime.Object, out io.Writer) { +} + +func (f *fakeFactory) Command() string { + return "" +} + +func (f *fakeFactory) BindFlags(flags *pflag.FlagSet) { +} + +func (f *fakeFactory) BindExternalFlags(flags *pflag.FlagSet) { +} + +func (f *fakeFactory) PrintObject(cmd *cobra.Command, mapper meta.RESTMapper, obj runtime.Object, out io.Writer) error { + return nil +} + +func (f *fakeFactory) PrinterForMapping(cmd *cobra.Command, mapping *meta.RESTMapping, withNamespace bool) (kubectl.ResourcePrinter, error) { + return f.tf.Printer, f.tf.Err +} + +func (f *fakeFactory) NewBuilder() *resource.Builder { + return nil +} + +func (f *fakeFactory) DefaultResourceFilterOptions(cmd *cobra.Command, withNamespace bool) *kubectl.PrintOptions { + return &kubectl.PrintOptions{} +} + +func (f *fakeFactory) DefaultResourceFilterFunc() kubectl.Filters { + return nil +} + +type fakeMixedFactory struct { + cmdutil.Factory + tf *testFactory + apiClient resource.RESTClient +} + +func (f *fakeMixedFactory) Object() (meta.RESTMapper, runtime.ObjectTyper) { + var multiRESTMapper meta.MultiRESTMapper + multiRESTMapper = append(multiRESTMapper, f.tf.Mapper) + multiRESTMapper = append(multiRESTMapper, testapi.Default.RESTMapper()) + priorityRESTMapper := meta.PriorityRESTMapper{ + Delegate: multiRESTMapper, + ResourcePriority: []unversioned.GroupVersionResource{ + {Group: meta.AnyGroup, Version: "v1", Resource: meta.AnyResource}, + }, + KindPriority: []unversioned.GroupVersionKind{ + {Group: meta.AnyGroup, Version: "v1", Kind: meta.AnyKind}, + }, + } + return priorityRESTMapper, runtime.MultiObjectTyper{f.tf.Typer, api.Scheme} +} + +func (f *fakeMixedFactory) ClientForMapping(m *meta.RESTMapping) (resource.RESTClient, error) { + if m.ObjectConvertor == api.Scheme { + return f.apiClient, f.tf.Err + } + return f.tf.Client, f.tf.Err +} + +func NewMixedFactory(apiClient resource.RESTClient) (cmdutil.Factory, *testFactory, runtime.Codec) { + f, t, c, _ := NewTestFactory() + return &fakeMixedFactory{ + Factory: f, + tf: t, + apiClient: apiClient, + }, t, c +} + +type fakeAPIFactory struct { + cmdutil.Factory + tf *testFactory +} + +func (f *fakeAPIFactory) Object() (meta.RESTMapper, runtime.ObjectTyper) { + return testapi.Default.RESTMapper(), api.Scheme +} + +func (f *fakeAPIFactory) UnstructuredObject() (meta.RESTMapper, runtime.ObjectTyper, error) { + groupResources := testDynamicResources() + mapper := discovery.NewRESTMapper(groupResources, meta.InterfacesForUnstructured) + typer := discovery.NewUnstructuredObjectTyper(groupResources) + + return cmdutil.NewShortcutExpander(mapper, nil), typer, nil +} + +func (f *fakeAPIFactory) Decoder(bool) runtime.Decoder { + return testapi.Default.Codec() +} + +func (f *fakeAPIFactory) JSONEncoder() runtime.Encoder { + return testapi.Default.Codec() +} + +func (f *fakeAPIFactory) ClientSet() (*internalclientset.Clientset, error) { + // Swap out the HTTP client out of the client with the fake's version. + fakeClient := f.tf.Client.(*fake.RESTClient) + restClient, err := restclient.RESTClientFor(f.tf.ClientConfig) + if err != nil { + panic(err) + } + restClient.Client = fakeClient.Client + return internalclientset.New(restClient), f.tf.Err +} + +func (f *fakeAPIFactory) RESTClient() (*restclient.RESTClient, error) { + // Swap out the HTTP client out of the client with the fake's version. + fakeClient := f.tf.Client.(*fake.RESTClient) + restClient, err := restclient.RESTClientFor(f.tf.ClientConfig) + if err != nil { + panic(err) + } + restClient.Client = fakeClient.Client + return restClient, f.tf.Err +} + +func (f *fakeAPIFactory) ClientConfig() (*restclient.Config, error) { + return f.tf.ClientConfig, f.tf.Err +} + +func (f *fakeAPIFactory) ClientForMapping(*meta.RESTMapping) (resource.RESTClient, error) { + return f.tf.Client, f.tf.Err +} + +func (f *fakeAPIFactory) UnstructuredClientForMapping(*meta.RESTMapping) (resource.RESTClient, error) { + return f.tf.Client, f.tf.Err +} + +func (f *fakeAPIFactory) Describer(*meta.RESTMapping) (kubectl.Describer, error) { + return f.tf.Describer, f.tf.Err +} + +func (f *fakeAPIFactory) Printer(mapping *meta.RESTMapping, options kubectl.PrintOptions) (kubectl.ResourcePrinter, error) { + return f.tf.Printer, f.tf.Err +} + +func (f *fakeAPIFactory) LogsForObject(object, options runtime.Object) (*restclient.Request, error) { + fakeClient := f.tf.Client.(*fake.RESTClient) + c := client.NewOrDie(f.tf.ClientConfig) + c.Client = fakeClient.Client + + switch t := object.(type) { + case *api.Pod: + opts, ok := options.(*api.PodLogOptions) + if !ok { + return nil, errors.New("provided options object is not a PodLogOptions") + } + return c.Pods(f.tf.Namespace).GetLogs(t.Name, opts), nil + default: + fqKinds, _, err := api.Scheme.ObjectKinds(object) + if err != nil { + return nil, err + } + return nil, fmt.Errorf("cannot get the logs from %v", fqKinds[0]) + } +} + +func (f *fakeAPIFactory) Validator(validate bool, cacheDir string) (validation.Schema, error) { + return f.tf.Validator, f.tf.Err +} + +func (f *fakeAPIFactory) DefaultNamespace() (string, bool, error) { + return f.tf.Namespace, false, f.tf.Err +} + +func (f *fakeAPIFactory) Generators(cmdName string) map[string]kubectl.Generator { + return cmdutil.DefaultGenerators(cmdName) +} + +func (f *fakeAPIFactory) PrintObject(cmd *cobra.Command, mapper meta.RESTMapper, obj runtime.Object, out io.Writer) error { + gvks, _, err := api.Scheme.ObjectKinds(obj) + if err != nil { + return err + } + + mapping, err := mapper.RESTMapping(gvks[0].GroupKind()) + if err != nil { + return err + } + + printer, err := f.PrinterForMapping(cmd, mapping, false) + if err != nil { + return err + } + return printer.PrintObj(obj, out) +} + +func (f *fakeAPIFactory) PrinterForMapping(cmd *cobra.Command, mapping *meta.RESTMapping, withNamespace bool) (kubectl.ResourcePrinter, error) { + return f.tf.Printer, f.tf.Err +} + +func (f *fakeAPIFactory) NewBuilder() *resource.Builder { + mapper, typer := f.Object() + + return resource.NewBuilder(mapper, typer, resource.ClientMapperFunc(f.ClientForMapping), f.Decoder(true)) +} + +func NewAPIFactory() (cmdutil.Factory, *testFactory, runtime.Codec, runtime.NegotiatedSerializer) { t := &testFactory{ Validator: validation.NullSchema{}, } - - f := &cmdutil.Factory{ - Object: func() (meta.RESTMapper, runtime.ObjectTyper) { - return testapi.Default.RESTMapper(), api.Scheme - }, - UnstructuredObject: func() (meta.RESTMapper, runtime.ObjectTyper, error) { - groupResources := testDynamicResources() - mapper := discovery.NewRESTMapper(groupResources, meta.InterfacesForUnstructured) - typer := discovery.NewUnstructuredObjectTyper(groupResources) - - return cmdutil.NewShortcutExpander(mapper, nil), typer, nil - }, - ClientSet: func() (*internalclientset.Clientset, error) { - // Swap out the HTTP client out of the client with the fake's version. - fakeClient := t.Client.(*fake.RESTClient) - restClient, err := restclient.RESTClientFor(t.ClientConfig) - if err != nil { - panic(err) - } - restClient.Client = fakeClient.Client - return internalclientset.New(restClient), t.Err - }, - RESTClient: func() (*restclient.RESTClient, error) { - // Swap out the HTTP client out of the client with the fake's version. - fakeClient := t.Client.(*fake.RESTClient) - restClient, err := restclient.RESTClientFor(t.ClientConfig) - if err != nil { - panic(err) - } - restClient.Client = fakeClient.Client - return restClient, t.Err - }, - ClientForMapping: func(*meta.RESTMapping) (resource.RESTClient, error) { - return t.Client, t.Err - }, - UnstructuredClientForMapping: func(*meta.RESTMapping) (resource.RESTClient, error) { - return t.Client, t.Err - }, - Decoder: func(bool) runtime.Decoder { - return testapi.Default.Codec() - }, - JSONEncoder: func() runtime.Encoder { - return testapi.Default.Codec() - }, - Describer: func(*meta.RESTMapping) (kubectl.Describer, error) { - return t.Describer, t.Err - }, - Printer: func(mapping *meta.RESTMapping, options kubectl.PrintOptions) (kubectl.ResourcePrinter, error) { - return t.Printer, t.Err - }, - Validator: func(validate bool, cacheDir string) (validation.Schema, error) { - return t.Validator, t.Err - }, - DefaultNamespace: func() (string, bool, error) { - return t.Namespace, false, t.Err - }, - ClientConfig: func() (*restclient.Config, error) { - return t.ClientConfig, t.Err - }, - Generators: func(cmdName string) map[string]kubectl.Generator { - return cmdutil.DefaultGenerators(cmdName) - }, - LogsForObject: func(object, options runtime.Object) (*restclient.Request, error) { - fakeClient := t.Client.(*fake.RESTClient) - c := client.NewOrDie(t.ClientConfig) - c.Client = fakeClient.Client - - switch t := object.(type) { - case *api.Pod: - opts, ok := options.(*api.PodLogOptions) - if !ok { - return nil, errors.New("provided options object is not a PodLogOptions") - } - return c.Pods(t.Namespace).GetLogs(t.Name, opts), nil - default: - fqKinds, _, err := api.Scheme.ObjectKinds(object) - if err != nil { - return nil, err - } - return nil, fmt.Errorf("cannot get the logs from %v", fqKinds[0]) - } - }, - } rf := cmdutil.NewFactory(nil) - f.MapBasedSelectorForObject = rf.MapBasedSelectorForObject - f.PortsForObject = rf.PortsForObject - f.ProtocolsForObject = rf.ProtocolsForObject - f.LabelsForObject = rf.LabelsForObject - f.CanBeExposed = rf.CanBeExposed - f.PrintObjectSpecificMessage = rf.PrintObjectSpecificMessage - return f, t, testapi.Default.Codec(), testapi.Default.NegotiatedSerializer() + return &fakeAPIFactory{ + Factory: rf, + tf: t, + }, t, testapi.Default.Codec(), testapi.Default.NegotiatedSerializer() } func objBody(codec runtime.Codec, obj runtime.Object) io.ReadCloser { From b2280a646ad4384ee223bc9f80a54645c14ad71d Mon Sep 17 00:00:00 2001 From: AdoHe Date: Thu, 13 Oct 2016 08:18:39 +0800 Subject: [PATCH 3/3] update various commands to adapt the new Factory interface --- cmd/kubeadm/app/cmd/cmd.go | 2 +- pkg/kubectl/cmd/annotate.go | 8 +-- pkg/kubectl/cmd/apiversions.go | 4 +- pkg/kubectl/cmd/apply.go | 4 +- pkg/kubectl/cmd/attach.go | 4 +- pkg/kubectl/cmd/autoscale.go | 4 +- pkg/kubectl/cmd/clusterinfo.go | 4 +- pkg/kubectl/cmd/clusterinfo_dump.go | 4 +- pkg/kubectl/cmd/cmd.go | 2 +- pkg/kubectl/cmd/completion.go | 4 +- pkg/kubectl/cmd/convert.go | 4 +- pkg/kubectl/cmd/create.go | 6 +- pkg/kubectl/cmd/create_configmap.go | 4 +- pkg/kubectl/cmd/create_deployment.go | 4 +- pkg/kubectl/cmd/create_namespace.go | 4 +- pkg/kubectl/cmd/create_quota.go | 4 +- pkg/kubectl/cmd/create_secret.go | 14 ++--- pkg/kubectl/cmd/create_service.go | 14 ++--- pkg/kubectl/cmd/create_serviceaccount.go | 4 +- pkg/kubectl/cmd/delete.go | 6 +- pkg/kubectl/cmd/describe.go | 6 +- pkg/kubectl/cmd/drain.go | 8 +-- pkg/kubectl/cmd/drain_test.go | 7 ++- pkg/kubectl/cmd/edit.go | 8 +-- pkg/kubectl/cmd/exec.go | 34 +++------- pkg/kubectl/cmd/exec_test.go | 73 +++++++--------------- pkg/kubectl/cmd/explain.go | 4 +- pkg/kubectl/cmd/expose.go | 4 +- pkg/kubectl/cmd/get.go | 4 +- pkg/kubectl/cmd/help.go | 2 +- pkg/kubectl/cmd/label.go | 6 +- pkg/kubectl/cmd/logs.go | 4 +- pkg/kubectl/cmd/patch.go | 4 +- pkg/kubectl/cmd/portforward.go | 4 +- pkg/kubectl/cmd/proxy.go | 4 +- pkg/kubectl/cmd/replace.go | 6 +- pkg/kubectl/cmd/rollingupdate.go | 4 +- pkg/kubectl/cmd/rollout/rollout.go | 2 +- pkg/kubectl/cmd/rollout/rollout_history.go | 4 +- pkg/kubectl/cmd/rollout/rollout_pause.go | 4 +- pkg/kubectl/cmd/rollout/rollout_resume.go | 4 +- pkg/kubectl/cmd/rollout/rollout_status.go | 4 +- pkg/kubectl/cmd/rollout/rollout_undo.go | 4 +- pkg/kubectl/cmd/run.go | 10 +-- pkg/kubectl/cmd/scale.go | 4 +- pkg/kubectl/cmd/set/set.go | 2 +- pkg/kubectl/cmd/set/set_image.go | 4 +- pkg/kubectl/cmd/stop.go | 4 +- pkg/kubectl/cmd/taint.go | 6 +- pkg/kubectl/cmd/top.go | 4 +- pkg/kubectl/cmd/top_node.go | 4 +- pkg/kubectl/cmd/top_pod.go | 4 +- pkg/kubectl/cmd/version.go | 4 +- 53 files changed, 154 insertions(+), 200 deletions(-) diff --git a/cmd/kubeadm/app/cmd/cmd.go b/cmd/kubeadm/app/cmd/cmd.go index f459469584f..25dd8fd8bf9 100644 --- a/cmd/kubeadm/app/cmd/cmd.go +++ b/cmd/kubeadm/app/cmd/cmd.go @@ -26,7 +26,7 @@ import ( "k8s.io/kubernetes/pkg/util/flag" ) -func NewKubeadmCommand(f *cmdutil.Factory, in io.Reader, out, err io.Writer) *cobra.Command { +func NewKubeadmCommand(f cmdutil.Factory, in io.Reader, out, err io.Writer) *cobra.Command { cmds := &cobra.Command{ Use: "kubeadm", Short: "kubeadm: easily bootstrap a secure Kubernetes cluster.", diff --git a/pkg/kubectl/cmd/annotate.go b/pkg/kubectl/cmd/annotate.go index 42b262f231d..aa7fbb9bc31 100644 --- a/pkg/kubectl/cmd/annotate.go +++ b/pkg/kubectl/cmd/annotate.go @@ -54,7 +54,7 @@ type AnnotateOptions struct { newAnnotations map[string]string removeAnnotations []string - // Common shared fields + // Common share fields out io.Writer } @@ -91,7 +91,7 @@ var ( kubectl annotate pods foo description-`) ) -func NewCmdAnnotate(f *cmdutil.Factory, out io.Writer) *cobra.Command { +func NewCmdAnnotate(f cmdutil.Factory, out io.Writer) *cobra.Command { options := &AnnotateOptions{} // retrieve a list of handled resources from printer as valid args @@ -138,7 +138,7 @@ func NewCmdAnnotate(f *cmdutil.Factory, out io.Writer) *cobra.Command { } // Complete adapts from the command line args and factory to the data required. -func (o *AnnotateOptions) Complete(f *cmdutil.Factory, out io.Writer, cmd *cobra.Command, args []string) (err error) { +func (o *AnnotateOptions) Complete(f cmdutil.Factory, out io.Writer, cmd *cobra.Command, args []string) (err error) { o.out = out o.local = cmdutil.GetFlagBool(cmd, "local") o.overwrite = cmdutil.GetFlagBool(cmd, "overwrite") @@ -172,7 +172,7 @@ func (o AnnotateOptions) Validate() error { } // RunAnnotate does the work -func (o AnnotateOptions) RunAnnotate(f *cmdutil.Factory, cmd *cobra.Command) error { +func (o AnnotateOptions) RunAnnotate(f cmdutil.Factory, cmd *cobra.Command) error { namespace, enforceNamespace, err := f.DefaultNamespace() if err != nil { return err diff --git a/pkg/kubectl/cmd/apiversions.go b/pkg/kubectl/cmd/apiversions.go index db57693df11..9b86555e906 100644 --- a/pkg/kubectl/cmd/apiversions.go +++ b/pkg/kubectl/cmd/apiversions.go @@ -28,7 +28,7 @@ import ( cmdutil "k8s.io/kubernetes/pkg/kubectl/cmd/util" ) -func NewCmdApiVersions(f *cmdutil.Factory, out io.Writer) *cobra.Command { +func NewCmdApiVersions(f cmdutil.Factory, out io.Writer) *cobra.Command { cmd := &cobra.Command{ Use: "api-versions", // apiversions is deprecated. @@ -42,7 +42,7 @@ func NewCmdApiVersions(f *cmdutil.Factory, out io.Writer) *cobra.Command { return cmd } -func RunApiVersions(f *cmdutil.Factory, w io.Writer) error { +func RunApiVersions(f cmdutil.Factory, w io.Writer) error { if len(os.Args) > 1 && os.Args[1] == "apiversions" { printDeprecationWarning("api-versions", "apiversions") } diff --git a/pkg/kubectl/cmd/apply.go b/pkg/kubectl/cmd/apply.go index 66854dc1230..008db766435 100644 --- a/pkg/kubectl/cmd/apply.go +++ b/pkg/kubectl/cmd/apply.go @@ -75,7 +75,7 @@ var ( cat pod.json | kubectl apply -f -`) ) -func NewCmdApply(f *cmdutil.Factory, out io.Writer) *cobra.Command { +func NewCmdApply(f cmdutil.Factory, out io.Writer) *cobra.Command { var options ApplyOptions cmd := &cobra.Command{ @@ -122,7 +122,7 @@ func validatePruneAll(prune, all bool, selector string) error { return nil } -func RunApply(f *cmdutil.Factory, cmd *cobra.Command, out io.Writer, options *ApplyOptions) error { +func RunApply(f cmdutil.Factory, cmd *cobra.Command, out io.Writer, options *ApplyOptions) error { shortOutput := cmdutil.GetFlagString(cmd, "output") == "name" schema, err := f.Validator(cmdutil.GetFlagBool(cmd, "validate"), cmdutil.GetFlagString(cmd, "schema-cache-dir")) if err != nil { diff --git a/pkg/kubectl/cmd/attach.go b/pkg/kubectl/cmd/attach.go index de21a0d636b..33deb58db56 100644 --- a/pkg/kubectl/cmd/attach.go +++ b/pkg/kubectl/cmd/attach.go @@ -48,7 +48,7 @@ var ( kubectl attach 123456-7890 -c ruby-container -i -t`) ) -func NewCmdAttach(f *cmdutil.Factory, cmdIn io.Reader, cmdOut, cmdErr io.Writer) *cobra.Command { +func NewCmdAttach(f cmdutil.Factory, cmdIn io.Reader, cmdOut, cmdErr io.Writer) *cobra.Command { options := &AttachOptions{ StreamOptions: StreamOptions{ In: cmdIn, @@ -113,7 +113,7 @@ type AttachOptions struct { } // Complete verifies command line arguments and loads data from the command environment -func (p *AttachOptions) Complete(f *cmdutil.Factory, cmd *cobra.Command, argsIn []string) error { +func (p *AttachOptions) Complete(f cmdutil.Factory, cmd *cobra.Command, argsIn []string) error { if len(argsIn) == 0 { return cmdutil.UsageError(cmd, "POD is required for attach") } diff --git a/pkg/kubectl/cmd/autoscale.go b/pkg/kubectl/cmd/autoscale.go index f4ac81239c8..8aa918d8fe0 100644 --- a/pkg/kubectl/cmd/autoscale.go +++ b/pkg/kubectl/cmd/autoscale.go @@ -45,7 +45,7 @@ var ( kubectl autoscale rc foo --max=5 --cpu-percent=80`) ) -func NewCmdAutoscale(f *cmdutil.Factory, out io.Writer) *cobra.Command { +func NewCmdAutoscale(f cmdutil.Factory, out io.Writer) *cobra.Command { options := &resource.FilenameOptions{} validArgs := []string{"deployment", "replicaset", "replicationcontroller"} @@ -79,7 +79,7 @@ func NewCmdAutoscale(f *cmdutil.Factory, out io.Writer) *cobra.Command { return cmd } -func RunAutoscale(f *cmdutil.Factory, out io.Writer, cmd *cobra.Command, args []string, options *resource.FilenameOptions) error { +func RunAutoscale(f cmdutil.Factory, out io.Writer, cmd *cobra.Command, args []string, options *resource.FilenameOptions) error { namespace, enforceNamespace, err := f.DefaultNamespace() if err != nil { return err diff --git a/pkg/kubectl/cmd/clusterinfo.go b/pkg/kubectl/cmd/clusterinfo.go index 87b76e6f8ff..76eb016a69b 100644 --- a/pkg/kubectl/cmd/clusterinfo.go +++ b/pkg/kubectl/cmd/clusterinfo.go @@ -33,7 +33,7 @@ import ( var longDescr = `Display addresses of the master and services with label kubernetes.io/cluster-service=true To further debug and diagnose cluster problems, use 'kubectl cluster-info dump'.` -func NewCmdClusterInfo(f *cmdutil.Factory, out io.Writer) *cobra.Command { +func NewCmdClusterInfo(f cmdutil.Factory, out io.Writer) *cobra.Command { cmd := &cobra.Command{ Use: "cluster-info", // clusterinfo is deprecated. @@ -50,7 +50,7 @@ func NewCmdClusterInfo(f *cmdutil.Factory, out io.Writer) *cobra.Command { return cmd } -func RunClusterInfo(f *cmdutil.Factory, out io.Writer, cmd *cobra.Command) error { +func RunClusterInfo(f cmdutil.Factory, out io.Writer, cmd *cobra.Command) error { if len(os.Args) > 1 && os.Args[1] == "clusterinfo" { printDeprecationWarning("cluster-info", "clusterinfo") } diff --git a/pkg/kubectl/cmd/clusterinfo_dump.go b/pkg/kubectl/cmd/clusterinfo_dump.go index 2d04da52333..e51f305d4ea 100644 --- a/pkg/kubectl/cmd/clusterinfo_dump.go +++ b/pkg/kubectl/cmd/clusterinfo_dump.go @@ -30,7 +30,7 @@ import ( ) // NewCmdCreateSecret groups subcommands to create various types of secrets -func NewCmdClusterInfoDump(f *cmdutil.Factory, cmdOut io.Writer) *cobra.Command { +func NewCmdClusterInfoDump(f cmdutil.Factory, cmdOut io.Writer) *cobra.Command { cmd := &cobra.Command{ Use: "dump", Short: "Dump lots of relevant info for debugging and diagnosis", @@ -84,7 +84,7 @@ func setupOutputWriter(cmd *cobra.Command, defaultWriter io.Writer, filename str return file } -func dumpClusterInfo(f *cmdutil.Factory, cmd *cobra.Command, args []string, out io.Writer) error { +func dumpClusterInfo(f cmdutil.Factory, cmd *cobra.Command, args []string, out io.Writer) error { clientset, err := f.ClientSet() if err != nil { return err diff --git a/pkg/kubectl/cmd/cmd.go b/pkg/kubectl/cmd/cmd.go index 198fa5c81a9..e5c13f93711 100644 --- a/pkg/kubectl/cmd/cmd.go +++ b/pkg/kubectl/cmd/cmd.go @@ -224,7 +224,7 @@ Use "{{.CommandPath}} [command] --help" for more information about a command.{{e ) // NewKubectlCommand creates the `kubectl` command and its nested children. -func NewKubectlCommand(f *cmdutil.Factory, in io.Reader, out, err io.Writer) *cobra.Command { +func NewKubectlCommand(f cmdutil.Factory, in io.Reader, out, err io.Writer) *cobra.Command { // Parent command to which all subcommands are added. cmds := &cobra.Command{ Use: "kubectl", diff --git a/pkg/kubectl/cmd/completion.go b/pkg/kubectl/cmd/completion.go index f7017daf667..e9a3a07d0c2 100644 --- a/pkg/kubectl/cmd/completion.go +++ b/pkg/kubectl/cmd/completion.go @@ -56,7 +56,7 @@ var ( } ) -func NewCmdCompletion(f *cmdutil.Factory, out io.Writer) *cobra.Command { +func NewCmdCompletion(f cmdutil.Factory, out io.Writer) *cobra.Command { shells := []string{} for s := range completion_shells { shells = append(shells, s) @@ -77,7 +77,7 @@ func NewCmdCompletion(f *cmdutil.Factory, out io.Writer) *cobra.Command { return cmd } -func RunCompletion(f *cmdutil.Factory, out io.Writer, cmd *cobra.Command, args []string) error { +func RunCompletion(f cmdutil.Factory, out io.Writer, cmd *cobra.Command, args []string) error { if len(args) == 0 { return cmdutil.UsageError(cmd, "Shell not specified.") } diff --git a/pkg/kubectl/cmd/convert.go b/pkg/kubectl/cmd/convert.go index e9aa7a72b3c..1f67f483ae1 100644 --- a/pkg/kubectl/cmd/convert.go +++ b/pkg/kubectl/cmd/convert.go @@ -62,7 +62,7 @@ var ( // NewCmdConvert creates a command object for the generic "convert" action, which // translates the config file into a given version. -func NewCmdConvert(f *cmdutil.Factory, out io.Writer) *cobra.Command { +func NewCmdConvert(f cmdutil.Factory, out io.Writer) *cobra.Command { options := &ConvertOptions{} cmd := &cobra.Command{ @@ -103,7 +103,7 @@ type ConvertOptions struct { } // Complete collects information required to run Convert command from command line. -func (o *ConvertOptions) Complete(f *cmdutil.Factory, out io.Writer, cmd *cobra.Command, args []string) (err error) { +func (o *ConvertOptions) Complete(f cmdutil.Factory, out io.Writer, cmd *cobra.Command, args []string) (err error) { o.outputVersion, err = cmdutil.OutputVersion(cmd, ®istered.EnabledVersionsForGroup(api.GroupName)[0]) if err != nil { return err diff --git a/pkg/kubectl/cmd/create.go b/pkg/kubectl/cmd/create.go index 16028213a53..d2893ff0c3f 100644 --- a/pkg/kubectl/cmd/create.go +++ b/pkg/kubectl/cmd/create.go @@ -43,7 +43,7 @@ var ( cat pod.json | kubectl create -f -`) ) -func NewCmdCreate(f *cmdutil.Factory, out io.Writer) *cobra.Command { +func NewCmdCreate(f cmdutil.Factory, out io.Writer) *cobra.Command { options := &resource.FilenameOptions{} cmd := &cobra.Command{ @@ -89,7 +89,7 @@ func ValidateArgs(cmd *cobra.Command, args []string) error { return nil } -func RunCreate(f *cmdutil.Factory, cmd *cobra.Command, out io.Writer, options *resource.FilenameOptions) error { +func RunCreate(f cmdutil.Factory, cmd *cobra.Command, out io.Writer, options *resource.FilenameOptions) error { schema, err := f.Validator(cmdutil.GetFlagBool(cmd, "validate"), cmdutil.GetFlagString(cmd, "schema-cache-dir")) if err != nil { return err @@ -183,7 +183,7 @@ type CreateSubcommandOptions struct { } // RunCreateSubcommand executes a create subcommand using the specified options -func RunCreateSubcommand(f *cmdutil.Factory, cmd *cobra.Command, out io.Writer, options *CreateSubcommandOptions) error { +func RunCreateSubcommand(f cmdutil.Factory, cmd *cobra.Command, out io.Writer, options *CreateSubcommandOptions) error { namespace, _, err := f.DefaultNamespace() if err != nil { return err diff --git a/pkg/kubectl/cmd/create_configmap.go b/pkg/kubectl/cmd/create_configmap.go index b4074246f85..04b14acc0be 100644 --- a/pkg/kubectl/cmd/create_configmap.go +++ b/pkg/kubectl/cmd/create_configmap.go @@ -53,7 +53,7 @@ var ( ) // ConfigMap is a command to ease creating ConfigMaps. -func NewCmdCreateConfigMap(f *cmdutil.Factory, cmdOut io.Writer) *cobra.Command { +func NewCmdCreateConfigMap(f cmdutil.Factory, cmdOut io.Writer) *cobra.Command { cmd := &cobra.Command{ Use: "configmap NAME [--from-file=[key=]source] [--from-literal=key1=value1] [--dry-run]", Aliases: []string{"cm"}, @@ -75,7 +75,7 @@ func NewCmdCreateConfigMap(f *cmdutil.Factory, cmdOut io.Writer) *cobra.Command } // CreateConfigMap is the implementation of the create configmap command. -func CreateConfigMap(f *cmdutil.Factory, cmdOut io.Writer, cmd *cobra.Command, args []string) error { +func CreateConfigMap(f cmdutil.Factory, cmdOut io.Writer, cmd *cobra.Command, args []string) error { name, err := NameFromCommandArgs(cmd, args) if err != nil { return err diff --git a/pkg/kubectl/cmd/create_deployment.go b/pkg/kubectl/cmd/create_deployment.go index f916b4296a2..2f98cbe5d79 100644 --- a/pkg/kubectl/cmd/create_deployment.go +++ b/pkg/kubectl/cmd/create_deployment.go @@ -37,7 +37,7 @@ var ( ) // NewCmdCreateDeployment is a macro command to create a new deployment -func NewCmdCreateDeployment(f *cmdutil.Factory, cmdOut io.Writer) *cobra.Command { +func NewCmdCreateDeployment(f cmdutil.Factory, cmdOut io.Writer) *cobra.Command { cmd := &cobra.Command{ Use: "deployment NAME --image=image [--dry-run]", Aliases: []string{"deploy"}, @@ -59,7 +59,7 @@ func NewCmdCreateDeployment(f *cmdutil.Factory, cmdOut io.Writer) *cobra.Command } // CreateDeployment implements the behavior to run the create deployment command -func CreateDeployment(f *cmdutil.Factory, cmdOut io.Writer, cmd *cobra.Command, args []string) error { +func CreateDeployment(f cmdutil.Factory, cmdOut io.Writer, cmd *cobra.Command, args []string) error { name, err := NameFromCommandArgs(cmd, args) if err != nil { return err diff --git a/pkg/kubectl/cmd/create_namespace.go b/pkg/kubectl/cmd/create_namespace.go index 5e511ee7985..7ab05b3c170 100644 --- a/pkg/kubectl/cmd/create_namespace.go +++ b/pkg/kubectl/cmd/create_namespace.go @@ -37,7 +37,7 @@ var ( ) // NewCmdCreateNamespace is a macro command to create a new namespace -func NewCmdCreateNamespace(f *cmdutil.Factory, cmdOut io.Writer) *cobra.Command { +func NewCmdCreateNamespace(f cmdutil.Factory, cmdOut io.Writer) *cobra.Command { cmd := &cobra.Command{ Use: "namespace NAME [--dry-run]", Aliases: []string{"ns"}, @@ -58,7 +58,7 @@ func NewCmdCreateNamespace(f *cmdutil.Factory, cmdOut io.Writer) *cobra.Command } // CreateNamespace implements the behavior to run the create namespace command -func CreateNamespace(f *cmdutil.Factory, cmdOut io.Writer, cmd *cobra.Command, args []string) error { +func CreateNamespace(f cmdutil.Factory, cmdOut io.Writer, cmd *cobra.Command, args []string) error { name, err := NameFromCommandArgs(cmd, args) if err != nil { return err diff --git a/pkg/kubectl/cmd/create_quota.go b/pkg/kubectl/cmd/create_quota.go index 40008f566c4..dd661c1b630 100644 --- a/pkg/kubectl/cmd/create_quota.go +++ b/pkg/kubectl/cmd/create_quota.go @@ -40,7 +40,7 @@ var ( ) // NewCmdCreateQuota is a macro command to create a new quota -func NewCmdCreateQuota(f *cmdutil.Factory, cmdOut io.Writer) *cobra.Command { +func NewCmdCreateQuota(f cmdutil.Factory, cmdOut io.Writer) *cobra.Command { cmd := &cobra.Command{ Use: "quota NAME [--hard=key1=value1,key2=value2] [--scopes=Scope1,Scope2] [--dry-run=bool]", Aliases: []string{"resourcequota"}, @@ -63,7 +63,7 @@ func NewCmdCreateQuota(f *cmdutil.Factory, cmdOut io.Writer) *cobra.Command { } // CreateQuota implements the behavior to run the create quota command -func CreateQuota(f *cmdutil.Factory, cmdOut io.Writer, cmd *cobra.Command, args []string) error { +func CreateQuota(f cmdutil.Factory, cmdOut io.Writer, cmd *cobra.Command, args []string) error { name, err := NameFromCommandArgs(cmd, args) if err != nil { return err diff --git a/pkg/kubectl/cmd/create_secret.go b/pkg/kubectl/cmd/create_secret.go index 61e5a5de324..6c12e36dbea 100644 --- a/pkg/kubectl/cmd/create_secret.go +++ b/pkg/kubectl/cmd/create_secret.go @@ -28,7 +28,7 @@ import ( ) // NewCmdCreateSecret groups subcommands to create various types of secrets -func NewCmdCreateSecret(f *cmdutil.Factory, cmdOut io.Writer) *cobra.Command { +func NewCmdCreateSecret(f cmdutil.Factory, cmdOut io.Writer) *cobra.Command { cmd := &cobra.Command{ Use: "secret", Short: "Create a secret using specified subcommand", @@ -70,7 +70,7 @@ var ( ) // NewCmdCreateSecretGeneric is a command to create generic secrets from files, directories, or literal values -func NewCmdCreateSecretGeneric(f *cmdutil.Factory, cmdOut io.Writer) *cobra.Command { +func NewCmdCreateSecretGeneric(f cmdutil.Factory, cmdOut io.Writer) *cobra.Command { cmd := &cobra.Command{ Use: "generic NAME [--type=string] [--from-file=[key=]source] [--from-literal=key1=value1] [--dry-run]", Short: "Create a secret from a local file, directory or literal value", @@ -92,7 +92,7 @@ func NewCmdCreateSecretGeneric(f *cmdutil.Factory, cmdOut io.Writer) *cobra.Comm } // CreateSecretGeneric is the implementation of the create secret generic command -func CreateSecretGeneric(f *cmdutil.Factory, cmdOut io.Writer, cmd *cobra.Command, args []string) error { +func CreateSecretGeneric(f cmdutil.Factory, cmdOut io.Writer, cmd *cobra.Command, args []string) error { name, err := NameFromCommandArgs(cmd, args) if err != nil { return err @@ -138,7 +138,7 @@ var ( ) // NewCmdCreateSecretDockerRegistry is a macro command for creating secrets to work with Docker registries -func NewCmdCreateSecretDockerRegistry(f *cmdutil.Factory, cmdOut io.Writer) *cobra.Command { +func NewCmdCreateSecretDockerRegistry(f cmdutil.Factory, cmdOut io.Writer) *cobra.Command { cmd := &cobra.Command{ Use: "docker-registry NAME --docker-username=user --docker-password=password --docker-email=email [--docker-server=string] [--from-literal=key1=value1] [--dry-run]", Short: "Create a secret for use with a Docker registry", @@ -165,7 +165,7 @@ func NewCmdCreateSecretDockerRegistry(f *cmdutil.Factory, cmdOut io.Writer) *cob } // CreateSecretDockerRegistry is the implementation of the create secret docker-registry command -func CreateSecretDockerRegistry(f *cmdutil.Factory, cmdOut io.Writer, cmd *cobra.Command, args []string) error { +func CreateSecretDockerRegistry(f cmdutil.Factory, cmdOut io.Writer, cmd *cobra.Command, args []string) error { name, err := NameFromCommandArgs(cmd, args) if err != nil { return err @@ -209,7 +209,7 @@ var ( ) // NewCmdCreateSecretTLS is a macro command for creating secrets to work with Docker registries -func NewCmdCreateSecretTLS(f *cmdutil.Factory, cmdOut io.Writer) *cobra.Command { +func NewCmdCreateSecretTLS(f cmdutil.Factory, cmdOut io.Writer) *cobra.Command { cmd := &cobra.Command{ Use: "tls NAME --cert=path/to/cert/file --key=path/to/key/file [--dry-run]", Short: "Create a TLS secret", @@ -230,7 +230,7 @@ func NewCmdCreateSecretTLS(f *cmdutil.Factory, cmdOut io.Writer) *cobra.Command } // CreateSecretTLS is the implementation of the create secret tls command -func CreateSecretTLS(f *cmdutil.Factory, cmdOut io.Writer, cmd *cobra.Command, args []string) error { +func CreateSecretTLS(f cmdutil.Factory, cmdOut io.Writer, cmd *cobra.Command, args []string) error { name, err := NameFromCommandArgs(cmd, args) if err != nil { return err diff --git a/pkg/kubectl/cmd/create_service.go b/pkg/kubectl/cmd/create_service.go index 115f4c54a89..a103193fe88 100644 --- a/pkg/kubectl/cmd/create_service.go +++ b/pkg/kubectl/cmd/create_service.go @@ -29,7 +29,7 @@ import ( ) // NewCmdCreateService is a macro command to create a new service -func NewCmdCreateService(f *cmdutil.Factory, cmdOut io.Writer) *cobra.Command { +func NewCmdCreateService(f cmdutil.Factory, cmdOut io.Writer) *cobra.Command { cmd := &cobra.Command{ Use: "service", Aliases: []string{"svc"}, @@ -63,7 +63,7 @@ func addPortFlags(cmd *cobra.Command) { } // NewCmdCreateServiceClusterIP is a command to create a clusterIP service -func NewCmdCreateServiceClusterIP(f *cmdutil.Factory, cmdOut io.Writer) *cobra.Command { +func NewCmdCreateServiceClusterIP(f cmdutil.Factory, cmdOut io.Writer) *cobra.Command { cmd := &cobra.Command{ Use: "clusterip NAME [--tcp=:] [--dry-run]", Short: "Create a clusterIP service.", @@ -84,7 +84,7 @@ func NewCmdCreateServiceClusterIP(f *cmdutil.Factory, cmdOut io.Writer) *cobra.C } // CreateServiceClusterIP implements the behavior to run the create service clusterIP command -func CreateServiceClusterIP(f *cmdutil.Factory, cmdOut io.Writer, cmd *cobra.Command, args []string) error { +func CreateServiceClusterIP(f cmdutil.Factory, cmdOut io.Writer, cmd *cobra.Command, args []string) error { name, err := NameFromCommandArgs(cmd, args) if err != nil { return err @@ -119,7 +119,7 @@ var ( ) // NewCmdCreateServiceNodePort is a macro command for creating a NodePort service -func NewCmdCreateServiceNodePort(f *cmdutil.Factory, cmdOut io.Writer) *cobra.Command { +func NewCmdCreateServiceNodePort(f cmdutil.Factory, cmdOut io.Writer) *cobra.Command { cmd := &cobra.Command{ Use: "nodeport NAME [--tcp=port:targetPort] [--dry-run]", Short: "Create a NodePort service.", @@ -140,7 +140,7 @@ func NewCmdCreateServiceNodePort(f *cmdutil.Factory, cmdOut io.Writer) *cobra.Co } // CreateServiceNodePort is the implementation of the create service nodeport command -func CreateServiceNodePort(f *cmdutil.Factory, cmdOut io.Writer, cmd *cobra.Command, args []string) error { +func CreateServiceNodePort(f cmdutil.Factory, cmdOut io.Writer, cmd *cobra.Command, args []string) error { name, err := NameFromCommandArgs(cmd, args) if err != nil { return err @@ -176,7 +176,7 @@ var ( ) // NewCmdCreateServiceLoadBalancer is a macro command for creating a LoadBalancer service -func NewCmdCreateServiceLoadBalancer(f *cmdutil.Factory, cmdOut io.Writer) *cobra.Command { +func NewCmdCreateServiceLoadBalancer(f cmdutil.Factory, cmdOut io.Writer) *cobra.Command { cmd := &cobra.Command{ Use: "loadbalancer NAME [--tcp=port:targetPort] [--dry-run]", Short: "Create a LoadBalancer service.", @@ -196,7 +196,7 @@ func NewCmdCreateServiceLoadBalancer(f *cmdutil.Factory, cmdOut io.Writer) *cobr } // CreateServiceLoadBalancer is the implementation of the service loadbalancer command -func CreateServiceLoadBalancer(f *cmdutil.Factory, cmdOut io.Writer, cmd *cobra.Command, args []string) error { +func CreateServiceLoadBalancer(f cmdutil.Factory, cmdOut io.Writer, cmd *cobra.Command, args []string) error { name, err := NameFromCommandArgs(cmd, args) if err != nil { return err diff --git a/pkg/kubectl/cmd/create_serviceaccount.go b/pkg/kubectl/cmd/create_serviceaccount.go index a2dd7891023..4b7a520d4a2 100644 --- a/pkg/kubectl/cmd/create_serviceaccount.go +++ b/pkg/kubectl/cmd/create_serviceaccount.go @@ -37,7 +37,7 @@ var ( ) // NewCmdCreateServiceAccount is a macro command to create a new service account -func NewCmdCreateServiceAccount(f *cmdutil.Factory, cmdOut io.Writer) *cobra.Command { +func NewCmdCreateServiceAccount(f cmdutil.Factory, cmdOut io.Writer) *cobra.Command { cmd := &cobra.Command{ Use: "serviceaccount NAME [--dry-run]", Aliases: []string{"sa"}, @@ -58,7 +58,7 @@ func NewCmdCreateServiceAccount(f *cmdutil.Factory, cmdOut io.Writer) *cobra.Com } // CreateServiceAccount implements the behavior to run the create service account command -func CreateServiceAccount(f *cmdutil.Factory, cmdOut io.Writer, cmd *cobra.Command, args []string) error { +func CreateServiceAccount(f cmdutil.Factory, cmdOut io.Writer, cmd *cobra.Command, args []string) error { name, err := NameFromCommandArgs(cmd, args) if err != nil { return err diff --git a/pkg/kubectl/cmd/delete.go b/pkg/kubectl/cmd/delete.go index daf7f808741..50f9f4b7db6 100644 --- a/pkg/kubectl/cmd/delete.go +++ b/pkg/kubectl/cmd/delete.go @@ -66,7 +66,7 @@ var ( kubectl delete pods --all`) ) -func NewCmdDelete(f *cmdutil.Factory, out io.Writer) *cobra.Command { +func NewCmdDelete(f cmdutil.Factory, out io.Writer) *cobra.Command { options := &resource.FilenameOptions{} // retrieve a list of handled resources from printer as valid args @@ -108,7 +108,7 @@ func NewCmdDelete(f *cmdutil.Factory, out io.Writer) *cobra.Command { return cmd } -func RunDelete(f *cmdutil.Factory, out io.Writer, cmd *cobra.Command, args []string, options *resource.FilenameOptions) error { +func RunDelete(f cmdutil.Factory, out io.Writer, cmd *cobra.Command, args []string, options *resource.FilenameOptions) error { cmdNamespace, enforceNamespace, err := f.DefaultNamespace() if err != nil { return err @@ -158,7 +158,7 @@ func RunDelete(f *cmdutil.Factory, out io.Writer, cmd *cobra.Command, args []str return DeleteResult(r, out, ignoreNotFound, shortOutput, mapper) } -func ReapResult(r *resource.Result, f *cmdutil.Factory, out io.Writer, isDefaultDelete, ignoreNotFound bool, timeout time.Duration, gracePeriod int, shortOutput bool, mapper meta.RESTMapper, quiet bool) error { +func ReapResult(r *resource.Result, f cmdutil.Factory, out io.Writer, isDefaultDelete, ignoreNotFound bool, timeout time.Duration, gracePeriod int, shortOutput bool, mapper meta.RESTMapper, quiet bool) error { found := 0 if ignoreNotFound { r = r.IgnoreErrors(errors.IsNotFound) diff --git a/pkg/kubectl/cmd/describe.go b/pkg/kubectl/cmd/describe.go index 5aac1dfc399..b3b6bc53369 100644 --- a/pkg/kubectl/cmd/describe.go +++ b/pkg/kubectl/cmd/describe.go @@ -67,7 +67,7 @@ var ( kubectl describe pods frontend`) ) -func NewCmdDescribe(f *cmdutil.Factory, out, cmdErr io.Writer) *cobra.Command { +func NewCmdDescribe(f cmdutil.Factory, out, cmdErr io.Writer) *cobra.Command { options := &resource.FilenameOptions{} describerSettings := &kubectl.DescriberSettings{} @@ -95,7 +95,7 @@ func NewCmdDescribe(f *cmdutil.Factory, out, cmdErr io.Writer) *cobra.Command { return cmd } -func RunDescribe(f *cmdutil.Factory, out, cmdErr io.Writer, cmd *cobra.Command, args []string, options *resource.FilenameOptions, describerSettings *kubectl.DescriberSettings) error { +func RunDescribe(f cmdutil.Factory, out, cmdErr io.Writer, cmd *cobra.Command, args []string, options *resource.FilenameOptions, describerSettings *kubectl.DescriberSettings) error { selector := cmdutil.GetFlagString(cmd, "selector") allNamespaces := cmdutil.GetFlagBool(cmd, "all-namespaces") cmdNamespace, enforceNamespace, err := f.DefaultNamespace() @@ -157,7 +157,7 @@ func RunDescribe(f *cmdutil.Factory, out, cmdErr io.Writer, cmd *cobra.Command, return utilerrors.NewAggregate(allErrs) } -func DescribeMatchingResources(mapper meta.RESTMapper, typer runtime.ObjectTyper, f *cmdutil.Factory, namespace, rsrc, prefix string, describerSettings *kubectl.DescriberSettings, out io.Writer, originalError error) error { +func DescribeMatchingResources(mapper meta.RESTMapper, typer runtime.ObjectTyper, f cmdutil.Factory, namespace, rsrc, prefix string, describerSettings *kubectl.DescriberSettings, out io.Writer, originalError error) error { r := resource.NewBuilder(mapper, typer, resource.ClientMapperFunc(f.ClientForMapping), f.Decoder(true)). NamespaceParam(namespace).DefaultNamespace(). ResourceTypeOrNameArgs(true, rsrc). diff --git a/pkg/kubectl/cmd/drain.go b/pkg/kubectl/cmd/drain.go index a08d47fc620..1a0ea7df8c9 100644 --- a/pkg/kubectl/cmd/drain.go +++ b/pkg/kubectl/cmd/drain.go @@ -40,7 +40,7 @@ import ( type DrainOptions struct { client *internalclientset.Clientset restClient *restclient.RESTClient - factory *cmdutil.Factory + factory cmdutil.Factory Force bool GracePeriodSeconds int IgnoreDaemonsets bool @@ -80,7 +80,7 @@ var ( `) ) -func NewCmdCordon(f *cmdutil.Factory, out io.Writer) *cobra.Command { +func NewCmdCordon(f cmdutil.Factory, out io.Writer) *cobra.Command { options := &DrainOptions{factory: f, out: out} cmd := &cobra.Command{ @@ -106,7 +106,7 @@ var ( `) ) -func NewCmdUncordon(f *cmdutil.Factory, out io.Writer) *cobra.Command { +func NewCmdUncordon(f cmdutil.Factory, out io.Writer) *cobra.Command { options := &DrainOptions{factory: f, out: out} cmd := &cobra.Command{ @@ -151,7 +151,7 @@ var ( `) ) -func NewCmdDrain(f *cmdutil.Factory, out io.Writer) *cobra.Command { +func NewCmdDrain(f cmdutil.Factory, out io.Writer) *cobra.Command { options := &DrainOptions{factory: f, out: out} cmd := &cobra.Command{ diff --git a/pkg/kubectl/cmd/drain_test.go b/pkg/kubectl/cmd/drain_test.go index fd285c8cdf4..3ee81fceeb2 100644 --- a/pkg/kubectl/cmd/drain_test.go +++ b/pkg/kubectl/cmd/drain_test.go @@ -69,7 +69,7 @@ func TestCordon(t *testing.T) { description string node *api.Node expected *api.Node - cmd func(*cmdutil.Factory, io.Writer) *cobra.Command + cmd func(cmdutil.Factory, io.Writer) *cobra.Command arg string expectFatal bool }{ @@ -177,7 +177,10 @@ func TestCordon(t *testing.T) { // Restore cmdutil behavior cmdutil.DefaultBehaviorOnFatal() }() - cmdutil.BehaviorOnFatal(func(e string, code int) { saw_fatal = true; panic(e) }) + cmdutil.BehaviorOnFatal(func(e string, code int) { + saw_fatal = true + panic(e) + }) cmd.SetArgs([]string{test.arg}) cmd.Execute() }() diff --git a/pkg/kubectl/cmd/edit.go b/pkg/kubectl/cmd/edit.go index 0748cf492ba..4f8457e559e 100644 --- a/pkg/kubectl/cmd/edit.go +++ b/pkg/kubectl/cmd/edit.go @@ -80,7 +80,7 @@ var ( var errExit = fmt.Errorf("exit directly") -func NewCmdEdit(f *cmdutil.Factory, out, errOut io.Writer) *cobra.Command { +func NewCmdEdit(f cmdutil.Factory, out, errOut io.Writer) *cobra.Command { options := &resource.FilenameOptions{} // retrieve a list of handled resources from printer as valid args @@ -121,7 +121,7 @@ func NewCmdEdit(f *cmdutil.Factory, out, errOut io.Writer) *cobra.Command { return cmd } -func RunEdit(f *cmdutil.Factory, out, errOut io.Writer, cmd *cobra.Command, args []string, options *resource.FilenameOptions) error { +func RunEdit(f cmdutil.Factory, out, errOut io.Writer, cmd *cobra.Command, args []string, options *resource.FilenameOptions) error { o, err := getPrinter(cmd) if err != nil { return err @@ -321,7 +321,7 @@ func getPrinter(cmd *cobra.Command) (*editPrinterOptions, error) { } } -func getMapperAndResult(f *cmdutil.Factory, args []string, options *resource.FilenameOptions) (meta.RESTMapper, *resource.Mapper, *resource.Result, string, error) { +func getMapperAndResult(f cmdutil.Factory, args []string, options *resource.FilenameOptions) (meta.RESTMapper, *resource.Mapper, *resource.Result, string, error) { cmdNamespace, enforceNamespace, err := f.DefaultNamespace() if err != nil { return nil, nil, nil, "", err @@ -441,7 +441,7 @@ func visitToPatch(originalObj runtime.Object, updates *resource.Info, mapper met return err } -func visitAnnotation(cmd *cobra.Command, f *cmdutil.Factory, updates *resource.Info, resourceMapper *resource.Mapper, encoder runtime.Encoder) ([]runtime.Object, error) { +func visitAnnotation(cmd *cobra.Command, f cmdutil.Factory, updates *resource.Info, resourceMapper *resource.Mapper, encoder runtime.Encoder) ([]runtime.Object, error) { mutatedObjects := []runtime.Object{} annotationVisitor := resource.NewFlattenListVisitor(updates, resourceMapper) // iterate through all items to apply annotations diff --git a/pkg/kubectl/cmd/exec.go b/pkg/kubectl/cmd/exec.go index bbe6f4319b1..132cf8eb7a1 100644 --- a/pkg/kubectl/cmd/exec.go +++ b/pkg/kubectl/cmd/exec.go @@ -30,7 +30,6 @@ import ( "k8s.io/kubernetes/pkg/client/restclient" "k8s.io/kubernetes/pkg/client/unversioned/remotecommand" cmdutil "k8s.io/kubernetes/pkg/kubectl/cmd/util" - "k8s.io/kubernetes/pkg/kubectl/resource" remotecommandserver "k8s.io/kubernetes/pkg/kubelet/server/remotecommand" "k8s.io/kubernetes/pkg/util/interrupt" "k8s.io/kubernetes/pkg/util/term" @@ -53,7 +52,7 @@ const ( execUsageStr = "expected 'exec POD_NAME COMMAND [ARG1] [ARG2] ... [ARGN]'.\nPOD_NAME and COMMAND are required arguments for the exec command" ) -func NewCmdExec(f *cmdutil.Factory, cmdIn io.Reader, cmdOut, cmdErr io.Writer) *cobra.Command { +func NewCmdExec(f cmdutil.Factory, cmdIn io.Reader, cmdOut, cmdErr io.Writer) *cobra.Command { options := &ExecOptions{ StreamOptions: StreamOptions{ In: cmdIn, @@ -140,7 +139,7 @@ type ExecOptions struct { } // Complete verifies command line arguments and loads data from the command environment -func (p *ExecOptions) Complete(f *cmdutil.Factory, cmd *cobra.Command, argsIn []string, argsLenAtDash int) error { +func (p *ExecOptions) Complete(f cmdutil.Factory, cmd *cobra.Command, argsIn []string, argsLenAtDash int) error { // Let kubectl exec follow rules for `--`, see #13004 issue if len(p.PodName) == 0 && (len(argsIn) == 0 || argsLenAtDash == 0) { return cmdutil.UsageError(cmd, execUsageStr) @@ -158,29 +157,6 @@ func (p *ExecOptions) Complete(f *cmdutil.Factory, cmd *cobra.Command, argsIn [] return cmdutil.UsageError(cmd, execUsageStr) } } - namespace, _, err := f.DefaultNamespace() - if err != nil { - return err - } - p.Namespace = namespace - - clientMapper := resource.ClientMapperFunc(f.ClientForMapping) - mapper, typer := f.Object() - decoder := f.Decoder(true) - - infos, err := resource.NewBuilder(mapper, typer, clientMapper, decoder). - NamespaceParam(p.Namespace).DefaultNamespace(). - ResourceNames("pods", p.PodName). - SingleResourceType(). - Do().Infos() - if err != nil { - return err - } - if len(infos) != 1 { - return cmdutil.UsageError(cmd, execUsageStr) - } - - p.PodName = infos[0].Name cmdParent := cmd.Parent() if cmdParent != nil { @@ -190,6 +166,12 @@ func (p *ExecOptions) Complete(f *cmdutil.Factory, cmd *cobra.Command, argsIn [] p.SuggestedCmdUsage = fmt.Sprintf("Use '%s describe pod/%s' to see all of the containers in this pod.", p.FullCmdName, p.PodName) } + namespace, _, err := f.DefaultNamespace() + if err != nil { + return err + } + p.Namespace = namespace + config, err := f.ClientConfig() if err != nil { return err diff --git a/pkg/kubectl/cmd/exec_test.go b/pkg/kubectl/cmd/exec_test.go index 57b6e496681..3d759bb7287 100644 --- a/pkg/kubectl/cmd/exec_test.go +++ b/pkg/kubectl/cmd/exec_test.go @@ -55,7 +55,7 @@ func TestPodAndContainer(t *testing.T) { p *ExecOptions name string expectError bool - expectedPod *api.Pod + expectedPod string expectedContainer string expectedArgs []string }{ @@ -81,18 +81,10 @@ func TestPodAndContainer(t *testing.T) { p: &ExecOptions{StreamOptions: StreamOptions{PodName: "foo"}}, args: []string{"cmd"}, argsLenAtDash: -1, - expectedPod: execPod(), + expectedPod: "foo", expectedArgs: []string{"cmd"}, name: "pod in flags", }, - { - p: &ExecOptions{StreamOptions: StreamOptions{PodName: "pod/foo"}}, - args: []string{"cmd"}, - argsLenAtDash: -1, - expectedPod: execPod(), - expectedArgs: []string{"cmd"}, - name: "pod with 'pod/' prefix in flags", - }, { p: &ExecOptions{}, args: []string{"foo", "cmd"}, @@ -111,23 +103,15 @@ func TestPodAndContainer(t *testing.T) { p: &ExecOptions{}, args: []string{"foo", "cmd"}, argsLenAtDash: -1, - expectedPod: execPod(), + expectedPod: "foo", expectedArgs: []string{"cmd"}, name: "cmd, w/o flags", }, - { - p: &ExecOptions{}, - args: []string{"pod/foo", "cmd"}, - argsLenAtDash: -1, - expectedPod: execPod(), - expectedArgs: []string{"cmd"}, - name: "pod with 'pod/' prefix, cmd, w/o flags", - }, { p: &ExecOptions{}, args: []string{"foo", "cmd"}, argsLenAtDash: 1, - expectedPod: execPod(), + expectedPod: "foo", expectedArgs: []string{"cmd"}, name: "cmd, cmd is behind dash", }, @@ -135,27 +119,17 @@ func TestPodAndContainer(t *testing.T) { p: &ExecOptions{StreamOptions: StreamOptions{ContainerName: "bar"}}, args: []string{"foo", "cmd"}, argsLenAtDash: -1, - expectedPod: execPod(), + expectedPod: "foo", expectedContainer: "bar", expectedArgs: []string{"cmd"}, name: "cmd, container in flag", }, } for _, test := range tests { - f, tf, codec, ns := NewAPIFactory() + f, tf, _, ns := NewAPIFactory() tf.Client = &fake.RESTClient{ NegotiatedSerializer: ns, - Client: fake.CreateHTTPClient(func(req *http.Request) (*http.Response, error) { - switch m := req.Method; { - case m == "GET": - body := objBody(codec, test.expectedPod) - return &http.Response{StatusCode: 200, Header: defaultHeader(), Body: body}, nil - default: - //Ensures no GET is performed when deleting by name - t.Errorf("%s: unexpected request: %s %#v\n%#v", test.name, req.Method, req.URL, req) - return nil, fmt.Errorf("unexpected request") - } - }), + Client: fake.CreateHTTPClient(func(req *http.Request) (*http.Response, error) { return nil, nil }), } tf.Namespace = "test" tf.ClientConfig = defaultClientConfig() @@ -172,8 +146,8 @@ func TestPodAndContainer(t *testing.T) { if err != nil { continue } - if options.PodName != test.expectedPod.Name { - t.Errorf("expected: %s, got: %s (%s)", test.expectedPod.Name, options.PodName, test.name) + if options.PodName != test.expectedPod { + t.Errorf("expected: %s, got: %s (%s)", test.expectedPod, options.PodName, test.name) } if options.ContainerName != test.expectedContainer { t.Errorf("expected: %s, got: %s (%s)", test.expectedContainer, options.ContainerName, test.name) @@ -187,24 +161,22 @@ func TestPodAndContainer(t *testing.T) { func TestExec(t *testing.T) { version := registered.GroupOrDie(api.GroupName).GroupVersion.Version tests := []struct { - name, shortPodPath, podPath, execPath, container string - pod *api.Pod - execErr bool + name, podPath, execPath, container string + pod *api.Pod + execErr bool }{ { - name: "pod exec", - shortPodPath: "/namespaces/test/pods/foo", - podPath: "/api/" + version + "/namespaces/test/pods/foo", - execPath: "/api/" + version + "/namespaces/test/pods/foo/exec", - pod: execPod(), + name: "pod exec", + podPath: "/api/" + version + "/namespaces/test/pods/foo", + execPath: "/api/" + version + "/namespaces/test/pods/foo/exec", + pod: execPod(), }, { - name: "pod exec error", - shortPodPath: "/namespaces/test/pods/foo", - podPath: "/api/" + version + "/namespaces/test/pods/foo", - execPath: "/api/" + version + "/namespaces/test/pods/foo/exec", - pod: execPod(), - execErr: true, + name: "pod exec error", + podPath: "/api/" + version + "/namespaces/test/pods/foo", + execPath: "/api/" + version + "/namespaces/test/pods/foo/exec", + pod: execPod(), + execErr: true, }, } for _, test := range tests { @@ -216,9 +188,6 @@ func TestExec(t *testing.T) { case p == test.podPath && m == "GET": body := objBody(codec, test.pod) return &http.Response{StatusCode: 200, Header: defaultHeader(), Body: body}, nil - case p == test.shortPodPath && m == "GET": - body := objBody(codec, test.pod) - return &http.Response{StatusCode: 200, Header: defaultHeader(), Body: body}, nil default: // Ensures no GET is performed when deleting by name t.Errorf("%s: unexpected request: %s %#v\n%#v", test.name, req.Method, req.URL, req) diff --git a/pkg/kubectl/cmd/explain.go b/pkg/kubectl/cmd/explain.go index d296e808d89..18133b705bf 100644 --- a/pkg/kubectl/cmd/explain.go +++ b/pkg/kubectl/cmd/explain.go @@ -44,7 +44,7 @@ var ( ) // NewCmdExplain returns a cobra command for swagger docs -func NewCmdExplain(f *cmdutil.Factory, out, cmdErr io.Writer) *cobra.Command { +func NewCmdExplain(f cmdutil.Factory, out, cmdErr io.Writer) *cobra.Command { cmd := &cobra.Command{ Use: "explain RESOURCE", Short: "Documentation of resources", @@ -61,7 +61,7 @@ func NewCmdExplain(f *cmdutil.Factory, out, cmdErr io.Writer) *cobra.Command { } // RunExplain executes the appropriate steps to print a model's documentation -func RunExplain(f *cmdutil.Factory, out, cmdErr io.Writer, cmd *cobra.Command, args []string) error { +func RunExplain(f cmdutil.Factory, out, cmdErr io.Writer, cmd *cobra.Command, args []string) error { if len(args) == 0 { fmt.Fprint(cmdErr, "You must specify the type of resource to explain. ", valid_resources) return cmdutil.UsageError(cmd, "Required resource not specified.") diff --git a/pkg/kubectl/cmd/expose.go b/pkg/kubectl/cmd/expose.go index 2f49b3b955e..06eb4f8b41f 100644 --- a/pkg/kubectl/cmd/expose.go +++ b/pkg/kubectl/cmd/expose.go @@ -73,7 +73,7 @@ var ( kubectl expose deployment nginx --port=80 --target-port=8000`) ) -func NewCmdExposeService(f *cmdutil.Factory, out io.Writer) *cobra.Command { +func NewCmdExposeService(f cmdutil.Factory, out io.Writer) *cobra.Command { options := &resource.FilenameOptions{} validArgs, argAliases := []string{}, []string{} @@ -123,7 +123,7 @@ func NewCmdExposeService(f *cmdutil.Factory, out io.Writer) *cobra.Command { return cmd } -func RunExpose(f *cmdutil.Factory, out io.Writer, cmd *cobra.Command, args []string, options *resource.FilenameOptions) error { +func RunExpose(f cmdutil.Factory, out io.Writer, cmd *cobra.Command, args []string, options *resource.FilenameOptions) error { namespace, enforceNamespace, err := f.DefaultNamespace() if err != nil { return err diff --git a/pkg/kubectl/cmd/get.go b/pkg/kubectl/cmd/get.go index f8b0d2f018e..0bce6801d8d 100644 --- a/pkg/kubectl/cmd/get.go +++ b/pkg/kubectl/cmd/get.go @@ -81,7 +81,7 @@ var ( // NewCmdGet creates a command object for the generic "get" action, which // retrieves one or more resources from a server. -func NewCmdGet(f *cmdutil.Factory, out io.Writer, errOut io.Writer) *cobra.Command { +func NewCmdGet(f cmdutil.Factory, out io.Writer, errOut io.Writer) *cobra.Command { options := &GetOptions{} // retrieve a list of handled resources from printer as valid args @@ -125,7 +125,7 @@ func NewCmdGet(f *cmdutil.Factory, out io.Writer, errOut io.Writer) *cobra.Comma // RunGet implements the generic Get command // TODO: convert all direct flag accessors to a struct and pass that instead of cmd -func RunGet(f *cmdutil.Factory, out, errOut io.Writer, cmd *cobra.Command, args []string, options *GetOptions) error { +func RunGet(f cmdutil.Factory, out, errOut io.Writer, cmd *cobra.Command, args []string, options *GetOptions) error { if len(options.Raw) > 0 { restClient, err := f.RESTClient() if err != nil { diff --git a/pkg/kubectl/cmd/help.go b/pkg/kubectl/cmd/help.go index 9a8a922597e..e696b68d8ef 100644 --- a/pkg/kubectl/cmd/help.go +++ b/pkg/kubectl/cmd/help.go @@ -28,7 +28,7 @@ import ( const help_long = `Help provides help for any command in the application. Simply type kubectl help [path to command] for full details.` -func NewCmdHelp(f *cmdutil.Factory, out io.Writer) *cobra.Command { +func NewCmdHelp(f cmdutil.Factory, out io.Writer) *cobra.Command { cmd := &cobra.Command{ Use: "help [command] | STRING_TO_SEARCH", Short: "Help about any command", diff --git a/pkg/kubectl/cmd/label.go b/pkg/kubectl/cmd/label.go index 04029ba6eb9..4a4b53783c3 100644 --- a/pkg/kubectl/cmd/label.go +++ b/pkg/kubectl/cmd/label.go @@ -88,7 +88,7 @@ var ( kubectl label pods foo bar-`) ) -func NewCmdLabel(f *cmdutil.Factory, out io.Writer) *cobra.Command { +func NewCmdLabel(f cmdutil.Factory, out io.Writer) *cobra.Command { options := &LabelOptions{} // retrieve a list of handled resources from printer as valid args @@ -135,7 +135,7 @@ func NewCmdLabel(f *cmdutil.Factory, out io.Writer) *cobra.Command { } // Complete adapts from the command line args and factory to the data required. -func (o *LabelOptions) Complete(f *cmdutil.Factory, out io.Writer, cmd *cobra.Command, args []string) (err error) { +func (o *LabelOptions) Complete(f cmdutil.Factory, out io.Writer, cmd *cobra.Command, args []string) (err error) { o.out = out o.local = cmdutil.GetFlagBool(cmd, "local") o.overwrite = cmdutil.GetFlagBool(cmd, "overwrite") @@ -166,7 +166,7 @@ func (o *LabelOptions) Validate() error { } // RunLabel does the work -func (o *LabelOptions) RunLabel(f *cmdutil.Factory, cmd *cobra.Command) error { +func (o *LabelOptions) RunLabel(f cmdutil.Factory, cmd *cobra.Command) error { cmdNamespace, enforceNamespace, err := f.DefaultNamespace() if err != nil { return err diff --git a/pkg/kubectl/cmd/logs.go b/pkg/kubectl/cmd/logs.go index 5f75160f9d3..cd1d3dc8175 100644 --- a/pkg/kubectl/cmd/logs.go +++ b/pkg/kubectl/cmd/logs.go @@ -74,7 +74,7 @@ type LogsOptions struct { } // NewCmdLog creates a new pod logs command -func NewCmdLogs(f *cmdutil.Factory, out io.Writer) *cobra.Command { +func NewCmdLogs(f cmdutil.Factory, out io.Writer) *cobra.Command { o := &LogsOptions{} cmd := &cobra.Command{ Use: "logs [-f] [-p] POD [-c CONTAINER]", @@ -111,7 +111,7 @@ func NewCmdLogs(f *cmdutil.Factory, out io.Writer) *cobra.Command { return cmd } -func (o *LogsOptions) Complete(f *cmdutil.Factory, out io.Writer, cmd *cobra.Command, args []string) error { +func (o *LogsOptions) Complete(f cmdutil.Factory, out io.Writer, cmd *cobra.Command, args []string) error { containerName := cmdutil.GetFlagString(cmd, "container") switch len(args) { case 0: diff --git a/pkg/kubectl/cmd/patch.go b/pkg/kubectl/cmd/patch.go index fee7a8f6502..7a6ca0a707e 100644 --- a/pkg/kubectl/cmd/patch.go +++ b/pkg/kubectl/cmd/patch.go @@ -69,7 +69,7 @@ var ( kubectl patch pod valid-pod --type='json' -p='[{"op": "replace", "path": "/spec/containers/0/image", "value":"new image"}]'`) ) -func NewCmdPatch(f *cmdutil.Factory, out io.Writer) *cobra.Command { +func NewCmdPatch(f cmdutil.Factory, out io.Writer) *cobra.Command { options := &PatchOptions{} // retrieve a list of handled resources from printer as valid args @@ -111,7 +111,7 @@ func NewCmdPatch(f *cmdutil.Factory, out io.Writer) *cobra.Command { return cmd } -func RunPatch(f *cmdutil.Factory, out io.Writer, cmd *cobra.Command, args []string, options *PatchOptions) error { +func RunPatch(f cmdutil.Factory, out io.Writer, cmd *cobra.Command, args []string, options *PatchOptions) error { switch { case options.Local && len(args) != 0: return fmt.Errorf("cannot specify --local and server resources") diff --git a/pkg/kubectl/cmd/portforward.go b/pkg/kubectl/cmd/portforward.go index c78411464bd..df03d4c0b4b 100644 --- a/pkg/kubectl/cmd/portforward.go +++ b/pkg/kubectl/cmd/portforward.go @@ -62,7 +62,7 @@ var ( kubectl port-forward mypod 0:5000`) ) -func NewCmdPortForward(f *cmdutil.Factory, cmdOut, cmdErr io.Writer) *cobra.Command { +func NewCmdPortForward(f cmdutil.Factory, cmdOut, cmdErr io.Writer) *cobra.Command { opts := &PortForwardOptions{ PortForwarder: &defaultPortForwarder{ cmdOut: cmdOut, @@ -112,7 +112,7 @@ func (f *defaultPortForwarder) ForwardPorts(method string, url *url.URL, opts Po } // Complete completes all the required options for port-forward cmd. -func (o *PortForwardOptions) Complete(f *cmdutil.Factory, cmd *cobra.Command, args []string, cmdOut io.Writer, cmdErr io.Writer) error { +func (o *PortForwardOptions) Complete(f cmdutil.Factory, cmd *cobra.Command, args []string, cmdOut io.Writer, cmdErr io.Writer) error { var err error o.PodName = cmdutil.GetFlagString(cmd, "pod") if len(o.PodName) == 0 && len(args) == 0 { diff --git a/pkg/kubectl/cmd/proxy.go b/pkg/kubectl/cmd/proxy.go index 95a1fa640b4..1714d133f88 100644 --- a/pkg/kubectl/cmd/proxy.go +++ b/pkg/kubectl/cmd/proxy.go @@ -45,7 +45,7 @@ var ( kubectl proxy --api-prefix=/k8s-api`) ) -func NewCmdProxy(f *cmdutil.Factory, out io.Writer) *cobra.Command { +func NewCmdProxy(f cmdutil.Factory, out io.Writer) *cobra.Command { cmd := &cobra.Command{ Use: "proxy [--port=PORT] [--www=static-dir] [--www-prefix=prefix] [--api-prefix=prefix]", Short: "Run a proxy to the Kubernetes API server", @@ -86,7 +86,7 @@ func NewCmdProxy(f *cmdutil.Factory, out io.Writer) *cobra.Command { return cmd } -func RunProxy(f *cmdutil.Factory, out io.Writer, cmd *cobra.Command) error { +func RunProxy(f cmdutil.Factory, out io.Writer, cmd *cobra.Command) error { path := cmdutil.GetFlagString(cmd, "unix-socket") port := cmdutil.GetFlagInt(cmd, "port") address := cmdutil.GetFlagString(cmd, "address") diff --git a/pkg/kubectl/cmd/replace.go b/pkg/kubectl/cmd/replace.go index 12ce0b1257e..91e7d772ff8 100644 --- a/pkg/kubectl/cmd/replace.go +++ b/pkg/kubectl/cmd/replace.go @@ -58,7 +58,7 @@ var ( kubectl replace --force -f ./pod.json`) ) -func NewCmdReplace(f *cmdutil.Factory, out io.Writer) *cobra.Command { +func NewCmdReplace(f cmdutil.Factory, out io.Writer) *cobra.Command { options := &resource.FilenameOptions{} cmd := &cobra.Command{ @@ -90,7 +90,7 @@ func NewCmdReplace(f *cmdutil.Factory, out io.Writer) *cobra.Command { return cmd } -func RunReplace(f *cmdutil.Factory, out io.Writer, cmd *cobra.Command, args []string, options *resource.FilenameOptions) error { +func RunReplace(f cmdutil.Factory, out io.Writer, cmd *cobra.Command, args []string, options *resource.FilenameOptions) error { if len(os.Args) > 1 && os.Args[1] == "update" { printDeprecationWarning("replace", "update") } @@ -163,7 +163,7 @@ func RunReplace(f *cmdutil.Factory, out io.Writer, cmd *cobra.Command, args []st }) } -func forceReplace(f *cmdutil.Factory, out io.Writer, cmd *cobra.Command, args []string, shortOutput bool, options *resource.FilenameOptions) error { +func forceReplace(f cmdutil.Factory, out io.Writer, cmd *cobra.Command, args []string, shortOutput bool, options *resource.FilenameOptions) error { schema, err := f.Validator(cmdutil.GetFlagBool(cmd, "validate"), cmdutil.GetFlagString(cmd, "schema-cache-dir")) if err != nil { return err diff --git a/pkg/kubectl/cmd/rollingupdate.go b/pkg/kubectl/cmd/rollingupdate.go index dc37cc8eefa..1de87ee0d24 100644 --- a/pkg/kubectl/cmd/rollingupdate.go +++ b/pkg/kubectl/cmd/rollingupdate.go @@ -74,7 +74,7 @@ var ( pollInterval, _ = time.ParseDuration("3s") ) -func NewCmdRollingUpdate(f *cmdutil.Factory, out io.Writer) *cobra.Command { +func NewCmdRollingUpdate(f cmdutil.Factory, out io.Writer) *cobra.Command { options := &resource.FilenameOptions{} cmd := &cobra.Command{ @@ -141,7 +141,7 @@ func validateArguments(cmd *cobra.Command, filenames, args []string) error { return utilerrors.NewAggregate(errors) } -func RunRollingUpdate(f *cmdutil.Factory, out io.Writer, cmd *cobra.Command, args []string, options *resource.FilenameOptions) error { +func RunRollingUpdate(f cmdutil.Factory, out io.Writer, cmd *cobra.Command, args []string, options *resource.FilenameOptions) error { if len(os.Args) > 1 && os.Args[1] == "rollingupdate" { printDeprecationWarning("rolling-update", "rollingupdate") } diff --git a/pkg/kubectl/cmd/rollout/rollout.go b/pkg/kubectl/cmd/rollout/rollout.go index a2b0e7946c6..7972478e181 100644 --- a/pkg/kubectl/cmd/rollout/rollout.go +++ b/pkg/kubectl/cmd/rollout/rollout.go @@ -36,7 +36,7 @@ var ( `) ) -func NewCmdRollout(f *cmdutil.Factory, out io.Writer) *cobra.Command { +func NewCmdRollout(f cmdutil.Factory, out io.Writer) *cobra.Command { cmd := &cobra.Command{ Use: "rollout SUBCOMMAND", diff --git a/pkg/kubectl/cmd/rollout/rollout_history.go b/pkg/kubectl/cmd/rollout/rollout_history.go index b759ee92aae..32a6866a3aa 100644 --- a/pkg/kubectl/cmd/rollout/rollout_history.go +++ b/pkg/kubectl/cmd/rollout/rollout_history.go @@ -39,7 +39,7 @@ var ( kubectl rollout history deployment/abc --revision=3`) ) -func NewCmdRolloutHistory(f *cmdutil.Factory, out io.Writer) *cobra.Command { +func NewCmdRolloutHistory(f cmdutil.Factory, out io.Writer) *cobra.Command { options := &resource.FilenameOptions{} validArgs := []string{"deployment"} @@ -63,7 +63,7 @@ func NewCmdRolloutHistory(f *cmdutil.Factory, out io.Writer) *cobra.Command { return cmd } -func RunHistory(f *cmdutil.Factory, cmd *cobra.Command, out io.Writer, args []string, options *resource.FilenameOptions) error { +func RunHistory(f cmdutil.Factory, cmd *cobra.Command, out io.Writer, args []string, options *resource.FilenameOptions) error { if len(args) == 0 && cmdutil.IsFilenameEmpty(options.Filenames) { return cmdutil.UsageError(cmd, "Required resource not specified.") } diff --git a/pkg/kubectl/cmd/rollout/rollout_pause.go b/pkg/kubectl/cmd/rollout/rollout_pause.go index 0a59bc294c2..0eeef58cba3 100644 --- a/pkg/kubectl/cmd/rollout/rollout_pause.go +++ b/pkg/kubectl/cmd/rollout/rollout_pause.go @@ -58,7 +58,7 @@ var ( kubectl rollout pause deployment/nginx`) ) -func NewCmdRolloutPause(f *cmdutil.Factory, out io.Writer) *cobra.Command { +func NewCmdRolloutPause(f cmdutil.Factory, out io.Writer) *cobra.Command { options := &PauseConfig{} validArgs := []string{"deployment"} @@ -90,7 +90,7 @@ func NewCmdRolloutPause(f *cmdutil.Factory, out io.Writer) *cobra.Command { return cmd } -func (o *PauseConfig) CompletePause(f *cmdutil.Factory, cmd *cobra.Command, out io.Writer, args []string) error { +func (o *PauseConfig) CompletePause(f cmdutil.Factory, cmd *cobra.Command, out io.Writer, args []string) error { if len(args) == 0 && cmdutil.IsFilenameEmpty(o.Filenames) { return cmdutil.UsageError(cmd, cmd.Use) } diff --git a/pkg/kubectl/cmd/rollout/rollout_resume.go b/pkg/kubectl/cmd/rollout/rollout_resume.go index 680d2806a41..4e958d5a8e5 100644 --- a/pkg/kubectl/cmd/rollout/rollout_resume.go +++ b/pkg/kubectl/cmd/rollout/rollout_resume.go @@ -56,7 +56,7 @@ var ( kubectl rollout resume deployment/nginx`) ) -func NewCmdRolloutResume(f *cmdutil.Factory, out io.Writer) *cobra.Command { +func NewCmdRolloutResume(f cmdutil.Factory, out io.Writer) *cobra.Command { options := &ResumeConfig{} validArgs := []string{"deployment"} @@ -88,7 +88,7 @@ func NewCmdRolloutResume(f *cmdutil.Factory, out io.Writer) *cobra.Command { return cmd } -func (o *ResumeConfig) CompleteResume(f *cmdutil.Factory, cmd *cobra.Command, out io.Writer, args []string) error { +func (o *ResumeConfig) CompleteResume(f cmdutil.Factory, cmd *cobra.Command, out io.Writer, args []string) error { if len(args) == 0 && cmdutil.IsFilenameEmpty(o.Filenames) { return cmdutil.UsageError(cmd, cmd.Use) } diff --git a/pkg/kubectl/cmd/rollout/rollout_status.go b/pkg/kubectl/cmd/rollout/rollout_status.go index c738face420..13e7bd6d917 100644 --- a/pkg/kubectl/cmd/rollout/rollout_status.go +++ b/pkg/kubectl/cmd/rollout/rollout_status.go @@ -45,7 +45,7 @@ var ( kubectl rollout status deployment/nginx`) ) -func NewCmdRolloutStatus(f *cmdutil.Factory, out io.Writer) *cobra.Command { +func NewCmdRolloutStatus(f cmdutil.Factory, out io.Writer) *cobra.Command { options := &resource.FilenameOptions{} validArgs := []string{"deployment"} @@ -70,7 +70,7 @@ func NewCmdRolloutStatus(f *cmdutil.Factory, out io.Writer) *cobra.Command { return cmd } -func RunStatus(f *cmdutil.Factory, cmd *cobra.Command, out io.Writer, args []string, options *resource.FilenameOptions) error { +func RunStatus(f cmdutil.Factory, cmd *cobra.Command, out io.Writer, args []string, options *resource.FilenameOptions) error { if len(args) == 0 && cmdutil.IsFilenameEmpty(options.Filenames) { return cmdutil.UsageError(cmd, "Required resource not specified.") } diff --git a/pkg/kubectl/cmd/rollout/rollout_undo.go b/pkg/kubectl/cmd/rollout/rollout_undo.go index 55cf0100bb8..2efc999581b 100644 --- a/pkg/kubectl/cmd/rollout/rollout_undo.go +++ b/pkg/kubectl/cmd/rollout/rollout_undo.go @@ -59,7 +59,7 @@ var ( kubectl rollout undo --dry-run=true deployment/abc`) ) -func NewCmdRolloutUndo(f *cmdutil.Factory, out io.Writer) *cobra.Command { +func NewCmdRolloutUndo(f cmdutil.Factory, out io.Writer) *cobra.Command { options := &UndoOptions{} validArgs := []string{"deployment"} @@ -93,7 +93,7 @@ func NewCmdRolloutUndo(f *cmdutil.Factory, out io.Writer) *cobra.Command { return cmd } -func (o *UndoOptions) CompleteUndo(f *cmdutil.Factory, cmd *cobra.Command, out io.Writer, args []string) error { +func (o *UndoOptions) CompleteUndo(f cmdutil.Factory, cmd *cobra.Command, out io.Writer, args []string) error { if len(args) == 0 && cmdutil.IsFilenameEmpty(o.Filenames) { return cmdutil.UsageError(cmd, "Required resource not specified.") } diff --git a/pkg/kubectl/cmd/run.go b/pkg/kubectl/cmd/run.go index c6e0d3e7e64..e4f4f361f3b 100644 --- a/pkg/kubectl/cmd/run.go +++ b/pkg/kubectl/cmd/run.go @@ -83,7 +83,7 @@ var ( kubectl run pi --schedule="0/5 * * * ?" --image=perl --restart=OnFailure -- perl -Mbignum=bpi -wle 'print bpi(2000)'`) ) -func NewCmdRun(f *cmdutil.Factory, cmdIn io.Reader, cmdOut, cmdErr io.Writer) *cobra.Command { +func NewCmdRun(f cmdutil.Factory, cmdIn io.Reader, cmdOut, cmdErr io.Writer) *cobra.Command { cmd := &cobra.Command{ Use: "run NAME --image=image [--env=\"key=value\"] [--port=port] [--replicas=replicas] [--dry-run=bool] [--overrides=inline-json] [--command] -- [COMMAND] [args...]", // run-container is deprecated @@ -133,7 +133,7 @@ func addRunFlags(cmd *cobra.Command) { cmd.Flags().String("schedule", "", "A schedule in the Cron format the job should be run with.") } -func Run(f *cmdutil.Factory, cmdIn io.Reader, cmdOut, cmdErr io.Writer, cmd *cobra.Command, args []string, argsLenAtDash int) error { +func Run(f cmdutil.Factory, cmdIn io.Reader, cmdOut, cmdErr io.Writer, cmd *cobra.Command, args []string, argsLenAtDash int) error { if len(os.Args) > 1 && os.Args[1] == "run-container" { printDeprecationWarning("run", "run-container") } @@ -456,7 +456,7 @@ func waitForPodTerminated(podClient coreclient.PodsGetter, ns, name string, out return pod, err } -func handleAttachPod(f *cmdutil.Factory, podClient coreclient.PodsGetter, ns, name string, opts *AttachOptions, quiet bool) error { +func handleAttachPod(f cmdutil.Factory, podClient coreclient.PodsGetter, ns, name string, opts *AttachOptions, quiet bool) error { pod, err := waitForPodRunning(podClient, ns, name, opts.Out, quiet) if err != nil && err != conditions.ErrPodCompleted { return err @@ -535,7 +535,7 @@ func verifyImagePullPolicy(cmd *cobra.Command) error { } } -func generateService(f *cmdutil.Factory, cmd *cobra.Command, args []string, serviceGenerator string, paramsIn map[string]interface{}, namespace string, out io.Writer) error { +func generateService(f cmdutil.Factory, cmd *cobra.Command, args []string, serviceGenerator string, paramsIn map[string]interface{}, namespace string, out io.Writer) error { generators := f.Generators("expose") generator, found := generators[serviceGenerator] if !found { @@ -583,7 +583,7 @@ func generateService(f *cmdutil.Factory, cmd *cobra.Command, args []string, serv return nil } -func createGeneratedObject(f *cmdutil.Factory, cmd *cobra.Command, generator kubectl.Generator, names []kubectl.GeneratorParam, params map[string]interface{}, overrides, namespace string) (runtime.Object, string, meta.RESTMapper, *meta.RESTMapping, error) { +func createGeneratedObject(f cmdutil.Factory, cmd *cobra.Command, generator kubectl.Generator, names []kubectl.GeneratorParam, params map[string]interface{}, overrides, namespace string) (runtime.Object, string, meta.RESTMapper, *meta.RESTMapping, error) { err := kubectl.ValidateParams(names, params) if err != nil { return nil, "", nil, nil, err diff --git a/pkg/kubectl/cmd/scale.go b/pkg/kubectl/cmd/scale.go index c850d19afdd..e3d03de9bf2 100644 --- a/pkg/kubectl/cmd/scale.go +++ b/pkg/kubectl/cmd/scale.go @@ -56,7 +56,7 @@ var ( ) // NewCmdScale returns a cobra command with the appropriate configuration and flags to run scale -func NewCmdScale(f *cmdutil.Factory, out io.Writer) *cobra.Command { +func NewCmdScale(f cmdutil.Factory, out io.Writer) *cobra.Command { options := &resource.FilenameOptions{} validArgs := []string{"deployment", "replicaset", "replicationcontroller", "job"} @@ -93,7 +93,7 @@ func NewCmdScale(f *cmdutil.Factory, out io.Writer) *cobra.Command { } // RunScale executes the scaling -func RunScale(f *cmdutil.Factory, out io.Writer, cmd *cobra.Command, args []string, shortOutput bool, options *resource.FilenameOptions) error { +func RunScale(f cmdutil.Factory, out io.Writer, cmd *cobra.Command, args []string, shortOutput bool, options *resource.FilenameOptions) error { if len(os.Args) > 1 && os.Args[1] == "resize" { printDeprecationWarning("scale", "resize") } diff --git a/pkg/kubectl/cmd/set/set.go b/pkg/kubectl/cmd/set/set.go index 2dd208f01ff..c377a9b0238 100644 --- a/pkg/kubectl/cmd/set/set.go +++ b/pkg/kubectl/cmd/set/set.go @@ -32,7 +32,7 @@ var ( set_example = dedent.Dedent(``) ) -func NewCmdSet(f *cmdutil.Factory, out io.Writer) *cobra.Command { +func NewCmdSet(f cmdutil.Factory, out io.Writer) *cobra.Command { cmd := &cobra.Command{ Use: "set SUBCOMMAND", diff --git a/pkg/kubectl/cmd/set/set_image.go b/pkg/kubectl/cmd/set/set_image.go index 1e34312ec8b..8f245d295ea 100644 --- a/pkg/kubectl/cmd/set/set_image.go +++ b/pkg/kubectl/cmd/set/set_image.go @@ -78,7 +78,7 @@ var ( kubectl set image -f path/to/file.yaml nginx=nginx:1.9.1 --local -o yaml`) ) -func NewCmdImage(f *cmdutil.Factory, out io.Writer) *cobra.Command { +func NewCmdImage(f cmdutil.Factory, out io.Writer) *cobra.Command { options := &ImageOptions{ Out: out, } @@ -105,7 +105,7 @@ func NewCmdImage(f *cmdutil.Factory, out io.Writer) *cobra.Command { return cmd } -func (o *ImageOptions) Complete(f *cmdutil.Factory, cmd *cobra.Command, args []string) error { +func (o *ImageOptions) Complete(f cmdutil.Factory, cmd *cobra.Command, args []string) error { o.Mapper, o.Typer = f.Object() o.UpdatePodSpecForObject = f.UpdatePodSpecForObject o.Encoder = f.JSONEncoder() diff --git a/pkg/kubectl/cmd/stop.go b/pkg/kubectl/cmd/stop.go index fbfb33fc70b..b3c74f54e88 100644 --- a/pkg/kubectl/cmd/stop.go +++ b/pkg/kubectl/cmd/stop.go @@ -49,7 +49,7 @@ var ( kubectl stop -f path/to/resources`) ) -func NewCmdStop(f *cmdutil.Factory, out io.Writer) *cobra.Command { +func NewCmdStop(f cmdutil.Factory, out io.Writer) *cobra.Command { options := &resource.FilenameOptions{} cmd := &cobra.Command{ @@ -75,7 +75,7 @@ func NewCmdStop(f *cmdutil.Factory, out io.Writer) *cobra.Command { return cmd } -func RunStop(f *cmdutil.Factory, cmd *cobra.Command, args []string, out io.Writer, options *resource.FilenameOptions) error { +func RunStop(f cmdutil.Factory, cmd *cobra.Command, args []string, out io.Writer, options *resource.FilenameOptions) error { cmdNamespace, enforceNamespace, err := f.DefaultNamespace() if err != nil { return err diff --git a/pkg/kubectl/cmd/taint.go b/pkg/kubectl/cmd/taint.go index caa27758d11..6e6cb76ffc2 100644 --- a/pkg/kubectl/cmd/taint.go +++ b/pkg/kubectl/cmd/taint.go @@ -47,7 +47,7 @@ type TaintOptions struct { selector string overwrite bool all bool - f *cmdutil.Factory + f cmdutil.Factory out io.Writer cmd *cobra.Command } @@ -73,7 +73,7 @@ var ( kubectl taint nodes foo dedicated-`) ) -func NewCmdTaint(f *cmdutil.Factory, out io.Writer) *cobra.Command { +func NewCmdTaint(f cmdutil.Factory, out io.Writer) *cobra.Command { options := &TaintOptions{} validArgs := []string{"node"} @@ -220,7 +220,7 @@ func parseTaints(spec []string) ([]api.Taint, []api.Taint, error) { } // Complete adapts from the command line args and factory to the data required. -func (o *TaintOptions) Complete(f *cmdutil.Factory, out io.Writer, cmd *cobra.Command, args []string) (err error) { +func (o *TaintOptions) Complete(f cmdutil.Factory, out io.Writer, cmd *cobra.Command, args []string) (err error) { namespace, _, err := f.DefaultNamespace() if err != nil { return err diff --git a/pkg/kubectl/cmd/top.go b/pkg/kubectl/cmd/top.go index 7d1f3dfdd70..6d2b94a2b5b 100644 --- a/pkg/kubectl/cmd/top.go +++ b/pkg/kubectl/cmd/top.go @@ -35,7 +35,7 @@ var ( The top command allows you to see the resource consumption for nodes or pods.`) ) -func NewCmdTop(f *cmdutil.Factory, out io.Writer) *cobra.Command { +func NewCmdTop(f cmdutil.Factory, out io.Writer) *cobra.Command { options := &TopOptions{} cmd := &cobra.Command{ @@ -55,6 +55,6 @@ func NewCmdTop(f *cmdutil.Factory, out io.Writer) *cobra.Command { return cmd } -func (o TopOptions) RunTop(f *cmdutil.Factory, cmd *cobra.Command, args []string, out io.Writer) error { +func (o TopOptions) RunTop(f cmdutil.Factory, cmd *cobra.Command, args []string, out io.Writer) error { return cmd.Help() } diff --git a/pkg/kubectl/cmd/top_node.go b/pkg/kubectl/cmd/top_node.go index e9e4669d1c6..43f2542415e 100644 --- a/pkg/kubectl/cmd/top_node.go +++ b/pkg/kubectl/cmd/top_node.go @@ -53,7 +53,7 @@ var ( kubectl top node NODE_NAME`) ) -func NewCmdTopNode(f *cmdutil.Factory, out io.Writer) *cobra.Command { +func NewCmdTopNode(f cmdutil.Factory, out io.Writer) *cobra.Command { options := &TopNodeOptions{} cmd := &cobra.Command{ @@ -78,7 +78,7 @@ func NewCmdTopNode(f *cmdutil.Factory, out io.Writer) *cobra.Command { return cmd } -func (o *TopNodeOptions) Complete(f *cmdutil.Factory, cmd *cobra.Command, args []string, out io.Writer) error { +func (o *TopNodeOptions) Complete(f cmdutil.Factory, cmd *cobra.Command, args []string, out io.Writer) error { var err error if len(args) == 1 { o.ResourceName = args[0] diff --git a/pkg/kubectl/cmd/top_pod.go b/pkg/kubectl/cmd/top_pod.go index 1e6fd189ded..d8894de318b 100644 --- a/pkg/kubectl/cmd/top_pod.go +++ b/pkg/kubectl/cmd/top_pod.go @@ -69,7 +69,7 @@ var ( kubectl top pod -l name=myLabel`) ) -func NewCmdTopPod(f *cmdutil.Factory, out io.Writer) *cobra.Command { +func NewCmdTopPod(f cmdutil.Factory, out io.Writer) *cobra.Command { options := &TopPodOptions{} cmd := &cobra.Command{ @@ -96,7 +96,7 @@ func NewCmdTopPod(f *cmdutil.Factory, out io.Writer) *cobra.Command { return cmd } -func (o *TopPodOptions) Complete(f *cmdutil.Factory, cmd *cobra.Command, args []string, out io.Writer) error { +func (o *TopPodOptions) Complete(f cmdutil.Factory, cmd *cobra.Command, args []string, out io.Writer) error { var err error if len(args) == 1 { o.ResourceName = args[0] diff --git a/pkg/kubectl/cmd/version.go b/pkg/kubectl/cmd/version.go index eb785330dab..f128918a3fb 100644 --- a/pkg/kubectl/cmd/version.go +++ b/pkg/kubectl/cmd/version.go @@ -26,7 +26,7 @@ import ( cmdutil "k8s.io/kubernetes/pkg/kubectl/cmd/util" ) -func NewCmdVersion(f *cmdutil.Factory, out io.Writer) *cobra.Command { +func NewCmdVersion(f cmdutil.Factory, out io.Writer) *cobra.Command { cmd := &cobra.Command{ Use: "version", Short: "Print the client and server version information", @@ -40,7 +40,7 @@ func NewCmdVersion(f *cmdutil.Factory, out io.Writer) *cobra.Command { return cmd } -func RunVersion(f *cmdutil.Factory, out io.Writer, cmd *cobra.Command) error { +func RunVersion(f cmdutil.Factory, out io.Writer, cmd *cobra.Command) error { kubectl.GetClientVersion(out) if cmdutil.GetFlagBool(cmd, "client") { return nil