kubectl commands must not use the factory out of Run

This commit is contained in:
Fabiano Franz 2016-10-24 11:17:55 -02:00
parent 5423eaf431
commit 60fc7b87be
3 changed files with 28 additions and 24 deletions

View File

@ -19,6 +19,7 @@ package set
import ( import (
"fmt" "fmt"
"io" "io"
"strings"
"github.com/spf13/cobra" "github.com/spf13/cobra"
"k8s.io/kubernetes/pkg/api" "k8s.io/kubernetes/pkg/api"
@ -37,8 +38,8 @@ var (
Specify compute resource requirements (cpu, memory) for any resource that defines a pod template. If a pod is successfully scheduled, it is guaranteed the amount of resource requested, but may burst up to its specified limits. Specify compute resource requirements (cpu, memory) for any resource that defines a pod template. If a pod is successfully scheduled, it is guaranteed the amount of resource requested, but may burst up to its specified limits.
for each compute resource, if a limit is specified and a request is omitted, the request will default to the limit. for each compute resource, if a limit is specified and a request is omitted, the request will default to the limit.
Possible resources include (case insensitive):`) Possible resources include (case insensitive): %s.`)
resources_example = templates.Examples(` resources_example = templates.Examples(`
# Set a deployments nginx container cpu limits to "200m" and memory to "512Mi" # Set a deployments nginx container cpu limits to "200m" and memory to "512Mi"
@ -87,17 +88,16 @@ func NewCmdResources(f cmdutil.Factory, out io.Writer, errOut io.Writer) *cobra.
Out: out, Out: out,
Err: errOut, Err: errOut,
} }
var pod_specs string
RESTMappings := cmdutil.ResourcesWithPodSpecs()
for _, Map := range RESTMappings {
pod_specs = pod_specs + ", " + Map.Resource
resourceTypesWithPodTemplate := []string{}
for _, resource := range f.SuggestedPodTemplateResources() {
resourceTypesWithPodTemplate = append(resourceTypesWithPodTemplate, resource.Resource)
} }
cmd := &cobra.Command{ cmd := &cobra.Command{
Use: "resources (-f FILENAME | TYPE NAME) ([--limits=LIMITS & --requests=REQUESTS]", Use: "resources (-f FILENAME | TYPE NAME) ([--limits=LIMITS & --requests=REQUESTS]",
Short: "update resource requests/limits on objects with pod templates", Short: "update resource requests/limits on objects with pod templates",
Long: resources_long + " " + pod_specs[2:], Long: fmt.Sprintf(resources_long, strings.Join(resourceTypesWithPodTemplate, ", ")),
Example: resources_example, Example: resources_example,
Run: func(cmd *cobra.Command, args []string) { Run: func(cmd *cobra.Command, args []string) {
cmdutil.CheckErr(options.Complete(f, cmd, args)) cmdutil.CheckErr(options.Complete(f, cmd, args))

View File

@ -340,6 +340,10 @@ func (f *FakeFactory) DefaultResourceFilterFunc() kubectl.Filters {
return nil return nil
} }
func (f *FakeFactory) SuggestedPodTemplateResources() []unversioned.GroupResource {
return []unversioned.GroupResource{}
}
type fakeMixedFactory struct { type fakeMixedFactory struct {
cmdutil.Factory cmdutil.Factory
tf *TestFactory tf *TestFactory
@ -507,6 +511,10 @@ func (f *fakeAPIFactory) NewBuilder() *resource.Builder {
return resource.NewBuilder(mapper, typer, resource.ClientMapperFunc(f.ClientForMapping), f.Decoder(true)) return resource.NewBuilder(mapper, typer, resource.ClientMapperFunc(f.ClientForMapping), f.Decoder(true))
} }
func (f *fakeAPIFactory) SuggestedPodTemplateResources() []unversioned.GroupResource {
return []unversioned.GroupResource{}
}
func NewAPIFactory() (cmdutil.Factory, *TestFactory, runtime.Codec, runtime.NegotiatedSerializer) { func NewAPIFactory() (cmdutil.Factory, *TestFactory, runtime.Codec, runtime.NegotiatedSerializer) {
t := &TestFactory{ t := &TestFactory{
Validator: validation.NullSchema{}, Validator: validation.NullSchema{},

View File

@ -178,6 +178,9 @@ type Factory interface {
PrinterForMapping(cmd *cobra.Command, mapping *meta.RESTMapping, withNamespace bool) (kubectl.ResourcePrinter, error) PrinterForMapping(cmd *cobra.Command, mapping *meta.RESTMapping, withNamespace bool) (kubectl.ResourcePrinter, error)
// One stop shopping for a Builder // One stop shopping for a Builder
NewBuilder() *resource.Builder NewBuilder() *resource.Builder
// SuggestedPodTemplateResources returns a list of resource types that declare a pod template
SuggestedPodTemplateResources() []unversioned.GroupResource
} }
const ( const (
@ -1333,6 +1336,16 @@ func (f *factory) NewBuilder() *resource.Builder {
return resource.NewBuilder(mapper, typer, resource.ClientMapperFunc(f.ClientForMapping), f.Decoder(true)) return resource.NewBuilder(mapper, typer, resource.ClientMapperFunc(f.ClientForMapping), f.Decoder(true))
} }
func (f *factory) SuggestedPodTemplateResources() []unversioned.GroupResource {
return []unversioned.GroupResource{
{Resource: "replicationcontroller"},
{Resource: "deployment"},
{Resource: "daemonset"},
{Resource: "job"},
{Resource: "replicaset"},
}
}
// registerThirdPartyResources inspects the discovery endpoint to find thirdpartyresources in the discovery doc // registerThirdPartyResources inspects the discovery endpoint to find thirdpartyresources in the discovery doc
// and then registers them with the apimachinery code. I think this is done so that scheme/codec stuff works, // and then registers them with the apimachinery code. I think this is done so that scheme/codec stuff works,
// but I really don't know. Feels like this code should go away once kubectl is completely generic for generic // but I really don't know. Feels like this code should go away once kubectl is completely generic for generic
@ -1383,20 +1396,3 @@ func registerThirdPartyResources(discoveryClient discovery.DiscoveryInterface) e
return nil return nil
} }
func ResourcesWithPodSpecs() []*meta.RESTMapping {
restMaps := []*meta.RESTMapping{}
resourcesWithTemplates := []string{"ReplicationController", "Deployment", "DaemonSet", "Job", "ReplicaSet"}
mapper, _ := NewFactory(nil).Object()
for _, resource := range resourcesWithTemplates {
restmap, err := mapper.RESTMapping(unversioned.GroupKind{Kind: resource})
if err == nil {
restMaps = append(restMaps, restmap)
} else {
mapping, _ := mapper.RESTMapping(extensions.Kind(resource))
restMaps = append(restMaps, mapping)
}
}
return restMaps
}