From 36afe2a43a6477753380736d41afc643d6116d58 Mon Sep 17 00:00:00 2001 From: "Dr. Stefan Schimanski" Date: Mon, 4 Jul 2016 11:39:05 +0200 Subject: [PATCH 1/3] Fix nsFlag scope in kubectl-run e2e test --- test/e2e/kubectl.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/test/e2e/kubectl.go b/test/e2e/kubectl.go index 8ca7aa95b2e..1b4bac72661 100644 --- a/test/e2e/kubectl.go +++ b/test/e2e/kubectl.go @@ -1089,10 +1089,11 @@ var _ = framework.KubeDescribe("Kubectl client", func() { }) framework.KubeDescribe("Kubectl run --rm job", func() { - nsFlag := fmt.Sprintf("--namespace=%v", ns) jobName := "e2e-test-rm-busybox-job" It("should create a job from an image, then delete the job [Conformance]", func() { + nsFlag := fmt.Sprintf("--namespace=%v", ns) + // The rkt runtime doesn't support attach, see #23335 framework.SkipIfContainerRuntimeIs("rkt") framework.SkipUnlessServerVersionGTE(jobsVersion, c) From 5b95524d650461fe3e1bcdbd6ff3e86caf5d7ac8 Mon Sep 17 00:00:00 2001 From: maaz khan Date: Wed, 13 Jan 2016 15:49:11 -0800 Subject: [PATCH 2/3] Add support for kubectl create quota command --- hack/verify-flags/known-flags.txt | 1 + pkg/kubectl/cmd/create.go | 1 + pkg/kubectl/cmd/create_quota.go | 87 +++++++++++++++++++++ pkg/kubectl/cmd/create_quota_test.go | 55 +++++++++++++ pkg/kubectl/cmd/util/factory.go | 6 ++ pkg/kubectl/quota.go | 113 +++++++++++++++++++++++++++ pkg/kubectl/quota_test.go | 81 +++++++++++++++++++ 7 files changed, 344 insertions(+) create mode 100644 pkg/kubectl/cmd/create_quota.go create mode 100644 pkg/kubectl/cmd/create_quota_test.go create mode 100644 pkg/kubectl/quota.go create mode 100644 pkg/kubectl/quota_test.go diff --git a/hack/verify-flags/known-flags.txt b/hack/verify-flags/known-flags.txt index f7b86694d46..678db563bf6 100644 --- a/hack/verify-flags/known-flags.txt +++ b/hack/verify-flags/known-flags.txt @@ -196,6 +196,7 @@ grace-period ha-domain hairpin-mode hard-pod-affinity-symmetric-weight +hard healthz-bind-address healthz-port horizontal-pod-autoscaler-sync-period diff --git a/pkg/kubectl/cmd/create.go b/pkg/kubectl/cmd/create.go index a2638dcf960..53cb68bd5a4 100644 --- a/pkg/kubectl/cmd/create.go +++ b/pkg/kubectl/cmd/create.go @@ -80,6 +80,7 @@ func NewCmdCreate(f *cmdutil.Factory, out io.Writer) *cobra.Command { // create subcommands cmd.AddCommand(NewCmdCreateNamespace(f, out)) + cmd.AddCommand(NewCmdCreateQuota(f, out)) cmd.AddCommand(NewCmdCreateSecret(f, out)) cmd.AddCommand(NewCmdCreateConfigMap(f, out)) cmd.AddCommand(NewCmdCreateServiceAccount(f, out)) diff --git a/pkg/kubectl/cmd/create_quota.go b/pkg/kubectl/cmd/create_quota.go new file mode 100644 index 00000000000..26ba236b44e --- /dev/null +++ b/pkg/kubectl/cmd/create_quota.go @@ -0,0 +1,87 @@ +/* +Copyright 2015 The Kubernetes Authors All rights reserved. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package cmd + +import ( + "fmt" + "io" + + "github.com/spf13/cobra" + + "k8s.io/kubernetes/pkg/kubectl" + cmdutil "k8s.io/kubernetes/pkg/kubectl/cmd/util" +) + +const ( + quotaLong = ` +Create a resourcequota with the specified name and hard limits` + + quotaExample = ` // Create a new resourcequota named my-quota + $ kubectl create quota my-quota --hard=cpu=1,memory=1G,pods=2,services=3,replicationcontrollers=2,resourcequotas=1,secrets=5,persistentvolumeclaims=10` +) + +// NewCmdCreateQuota is a macro command to create a new quota +func NewCmdCreateQuota(f *cmdutil.Factory, cmdOut io.Writer) *cobra.Command { + cmd := &cobra.Command{ + Use: "quota NAME [--hard=key1=value1,key2=value2] [--dry-run=bool]", + Aliases: []string{"q"}, + Short: "Create a quota with the specified name.", + Long: quotaLong, + Example: quotaExample, + Run: func(cmd *cobra.Command, args []string) { + err := CreateQuota(f, cmdOut, cmd, args) + cmdutil.CheckErr(err) + }, + } + + cmdutil.AddApplyAnnotationFlags(cmd) + cmdutil.AddValidateFlags(cmd) + cmdutil.AddGeneratorFlags(cmd, cmdutil.ResourceQuotaV1GeneratorName) + cmd.Flags().String("hard", "", "Specify multiple key/value pair to insert in resourcequota (i.e. --hard=cpu=1,memory=1G,pods=2,services=3,replicationcontrollers=2,resourcequotas=1,secrets=5,persistentvolumeclaims=10)") + cmd.MarkFlagRequired("hard") + return cmd +} + +// CreateQuota implements the behavior to run the create quota command +func CreateQuota(f *cmdutil.Factory, cmdOut io.Writer, cmd *cobra.Command, args []string) error { + name, err := NameFromCommandArgs(cmd, args) + if err != nil { + return err + } + requiredFlags := []string{"hard"} + for _, requiredFlag := range requiredFlags { + if value := cmdutil.GetFlagString(cmd, requiredFlag); len(value) == 0 { + return cmdutil.UsageError(cmd, "flag %s is required", requiredFlag) + } + } + var generator kubectl.StructuredGenerator + switch generatorName := cmdutil.GetFlagString(cmd, "generator"); generatorName { + case cmdutil.ResourceQuotaV1GeneratorName: + generator = &kubectl.ResourceQuotaGeneratorV1{ + Name: name, + Hard: cmdutil.GetFlagString(cmd, "hard"), + } + default: + return cmdutil.UsageError(cmd, fmt.Sprintf("Generator: %s not supported.", generatorName)) + } + return RunCreateSubcommand(f, cmd, cmdOut, &CreateSubcommandOptions{ + Name: name, + StructuredGenerator: generator, + DryRun: cmdutil.GetFlagBool(cmd, "dry-run"), + OutputFormat: cmdutil.GetFlagString(cmd, "output"), + }) +} diff --git a/pkg/kubectl/cmd/create_quota_test.go b/pkg/kubectl/cmd/create_quota_test.go new file mode 100644 index 00000000000..cbaf779e575 --- /dev/null +++ b/pkg/kubectl/cmd/create_quota_test.go @@ -0,0 +1,55 @@ +/* +Copyright 2014 The Kubernetes Authors All rights reserved. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package cmd + +import ( + "bytes" + "net/http" + "testing" + + "k8s.io/kubernetes/pkg/api" + "k8s.io/kubernetes/pkg/client/unversioned/fake" +) + +func TestCreateQuota(t *testing.T) { + resourceQuotaObject := &api.ResourceQuota{} + resourceQuotaObject.Name = "my-quota" + f, tf, codec := NewAPIFactory() + tf.Printer = &testPrinter{} + tf.Client = &fake.RESTClient{ + Codec: codec, + Client: fake.CreateHTTPClient(func(req *http.Request) (*http.Response, error) { + switch p, m := req.URL.Path, req.Method; { + case p == "/namespaces/test/resourcequotas" && m == "POST": + return &http.Response{StatusCode: 201, Body: objBody(codec, resourceQuotaObject)}, nil + default: + t.Fatalf("unexpected request: %#v\n%#v", req.URL, req) + return nil, nil + } + }), + } + tf.Namespace = "test" + buf := bytes.NewBuffer([]byte{}) + cmd := NewCmdCreateQuota(f, buf) + cmd.Flags().Set("hard", "cpu=1") + cmd.Flags().Set("output", "name") + cmd.Run(cmd, []string{resourceQuotaObject.Name}) + expectedOutput := "resourcequota/" + resourceQuotaObject.Name + "\n" + if buf.String() != expectedOutput { + t.Errorf("expected output: %s, but got: %s", expectedOutput, buf.String()) + } +} diff --git a/pkg/kubectl/cmd/util/factory.go b/pkg/kubectl/cmd/util/factory.go index 3bcf7bf54f4..f40335e308d 100644 --- a/pkg/kubectl/cmd/util/factory.go +++ b/pkg/kubectl/cmd/util/factory.go @@ -165,6 +165,7 @@ const ( JobV1Beta1GeneratorName = "job/v1beta1" JobV1GeneratorName = "job/v1" NamespaceV1GeneratorName = "namespace/v1" + ResourceQuotaV1GeneratorName = "resourcequotas/v1" SecretV1GeneratorName = "secret/v1" SecretForDockerRegistryV1GeneratorName = "secret-for-docker-registry/v1" SecretForTLSV1GeneratorName = "secret-for-tls/v1" @@ -192,6 +193,11 @@ func DefaultGenerators(cmdName string) map[string]kubectl.Generator { generators["namespace"] = map[string]kubectl.Generator{ NamespaceV1GeneratorName: kubectl.NamespaceGeneratorV1{}, } + + generators["quota"] = map[string]kubectl.Generator{ + ResourceQuotaV1GeneratorName: kubectl.ResourceQuotaGeneratorV1{}, + } + generators["secret"] = map[string]kubectl.Generator{ SecretV1GeneratorName: kubectl.SecretGeneratorV1{}, } diff --git a/pkg/kubectl/quota.go b/pkg/kubectl/quota.go new file mode 100644 index 00000000000..e6409905b52 --- /dev/null +++ b/pkg/kubectl/quota.go @@ -0,0 +1,113 @@ +/* +Copyright 2015 The Kubernetes Authors All rights reserved. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package kubectl + +import ( + "fmt" + + "k8s.io/kubernetes/pkg/api" + "k8s.io/kubernetes/pkg/api/resource" + "k8s.io/kubernetes/pkg/runtime" + "strings" +) + +// ResourceQuotaGeneratorV1 supports stable generation of a namespace +type ResourceQuotaGeneratorV1 struct { + Name string + Hard string +} + +// ParamNames returns the set of supported input parameters when using the parameter injection generator pattern +func (g ResourceQuotaGeneratorV1) ParamNames() []GeneratorParam { + return []GeneratorParam{ + {"name", true}, + {"hard", true}, + } +} + +// Ensure it supports the generator pattern that uses parameter injection +var _ Generator = &ResourceQuotaGeneratorV1{} + +// Ensure it supports the generator pattern that uses parameters specified during construction +var _ StructuredGenerator = &ResourceQuotaGeneratorV1{} + +func (g ResourceQuotaGeneratorV1) Generate(genericParams map[string]interface{}) (runtime.Object, error) { + err := ValidateParams(g.ParamNames(), genericParams) + if err != nil { + return nil, err + } + + params := map[string]string{} + for key, value := range genericParams { + strVal, isString := value.(string) + if !isString { + return nil, fmt.Errorf("expected string, saw %v for '%s'", value, key) + } + params[key] = strVal + } + + delegate := &ResourceQuotaGeneratorV1{} + delegate.Name = params["name"] + delegate.Hard = params["hard"] + return delegate.StructuredGenerate() +} + +// StructuredGenerate outputs a ResourceQuota object using the configured fields +func (g *ResourceQuotaGeneratorV1) StructuredGenerate() (runtime.Object, error) { + if err := g.validate(); err != nil { + return nil, err + } + + resourceQuotaSpec, err := generateResourceQuotaSpecList(g.Hard) + if err != nil { + return nil, err + } + + resourceQuota := &api.ResourceQuota{} + resourceQuota.Name = g.Name + resourceQuota.Spec.Hard = resourceQuotaSpec + return resourceQuota, nil +} + +func generateResourceQuotaSpecList(hard string) (resourceList api.ResourceList, err error) { + + defer func() { + if p := recover(); p != nil { + resourceList = nil + err = fmt.Errorf("Invalid input %v", p) + } + }() + + resourceList = make(api.ResourceList) + for _, keyValue := range strings.Split(hard, ",") { + items := strings.Split(keyValue, "=") + if len(items) != 2 { + return nil, fmt.Errorf("invalid input %v, expected key=value", keyValue) + } + + resourceList[api.ResourceName(items[0])] = resource.MustParse(items[1]) + } + return +} + +// validate validates required fields are set to support structured generation +func (r *ResourceQuotaGeneratorV1) validate() error { + if len(r.Name) == 0 { + return fmt.Errorf("name must be specified") + } + return nil +} diff --git a/pkg/kubectl/quota_test.go b/pkg/kubectl/quota_test.go new file mode 100644 index 00000000000..07075f3bc44 --- /dev/null +++ b/pkg/kubectl/quota_test.go @@ -0,0 +1,81 @@ +/* +Copyright 2015 The Kubernetes Authors All rights reserved. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package kubectl + +import ( + "reflect" + "testing" + + "k8s.io/kubernetes/pkg/api" +) + +func TestQuotaGenerate(t *testing.T) { + hard := "cpu=10,memory=5G,pods=10,services=7" + resourceQuotaSpecList, err := generateResourceQuotaSpecList(hard) + if err != nil { + t.Errorf("unexpected error: %v", err) + } + + tests := map[string]struct { + params map[string]interface{} + expected *api.ResourceQuota + expectErr bool + }{ + "test-valid-use": { + params: map[string]interface{}{ + "name": "foo", + "hard": hard, + }, + expected: &api.ResourceQuota{ + ObjectMeta: api.ObjectMeta{ + Name: "foo", + }, + Spec: api.ResourceQuotaSpec{Hard: resourceQuotaSpecList}, + }, + expectErr: false, + }, + "test-missing-required-param": { + params: map[string]interface{}{ + "name": "foo", + }, + expectErr: true, + }, + } + + generator := ResourceQuotaGeneratorV1{} + for _, test := range tests { + obj, err := generator.Generate(test.params) + if !test.expectErr && err != nil { + t.Errorf("unexpected error: %v", err) + } + if test.expectErr && err != nil { + continue + } + if !reflect.DeepEqual(obj.(*api.ResourceQuota), test.expected) { + t.Errorf("\nexpected:\n%#v\nsaw:\n%#v", test.expected, obj.(*api.ResourceQuota)) + } + } +} + +func TestGenerateResourceQuotaSpecList(t *testing.T) { + hardInvalidValue := "cpu=23foo,memory=5G,pods=10bar,services=7" + + resourceList, err := generateResourceQuotaSpecList(hardInvalidValue) + if err != nil && resourceList != nil { + t.Errorf("\nexpected:\n%#v\nsaw:\n%#v", nil, resourceList) + } +} From 199f991f6aa6e5df0ccd903573376a40acca3974 Mon Sep 17 00:00:00 2001 From: "Dr. Stefan Schimanski" Date: Fri, 1 Jul 2016 15:25:52 +0200 Subject: [PATCH 3/3] Add --scopes to kubectl-create-quota and add tests --- .generated_docs | 2 + docs/man/man1/kubectl-create-quota.1 | 3 + .../kubectl/kubectl_create_quota.md | 36 ++++++++++ hack/verify-flags/known-flags.txt | 1 + pkg/kubectl/cmd/create_quota.go | 27 ++++--- pkg/kubectl/cmd/create_quota_test.go | 48 +++++++++---- pkg/kubectl/quota.go | 66 ++++++++++------- pkg/kubectl/quota_test.go | 61 ++++++++++++---- test/e2e/kubectl.go | 71 +++++++++++++++++++ 9 files changed, 248 insertions(+), 67 deletions(-) create mode 100644 docs/man/man1/kubectl-create-quota.1 create mode 100644 docs/user-guide/kubectl/kubectl_create_quota.md diff --git a/.generated_docs b/.generated_docs index f36b59e130d..1bcefdf9d55 100644 --- a/.generated_docs +++ b/.generated_docs @@ -28,6 +28,7 @@ docs/man/man1/kubectl-convert.1 docs/man/man1/kubectl-cordon.1 docs/man/man1/kubectl-create-configmap.1 docs/man/man1/kubectl-create-namespace.1 +docs/man/man1/kubectl-create-quota.1 docs/man/man1/kubectl-create-secret-docker-registry.1 docs/man/man1/kubectl-create-secret-generic.1 docs/man/man1/kubectl-create-secret-tls.1 @@ -89,6 +90,7 @@ docs/user-guide/kubectl/kubectl_cordon.md docs/user-guide/kubectl/kubectl_create.md docs/user-guide/kubectl/kubectl_create_configmap.md docs/user-guide/kubectl/kubectl_create_namespace.md +docs/user-guide/kubectl/kubectl_create_quota.md docs/user-guide/kubectl/kubectl_create_secret.md docs/user-guide/kubectl/kubectl_create_secret_docker-registry.md docs/user-guide/kubectl/kubectl_create_secret_generic.md diff --git a/docs/man/man1/kubectl-create-quota.1 b/docs/man/man1/kubectl-create-quota.1 new file mode 100644 index 00000000000..b6fd7a0f989 --- /dev/null +++ b/docs/man/man1/kubectl-create-quota.1 @@ -0,0 +1,3 @@ +This file is autogenerated, but we've stopped checking such files into the +repository to reduce the need for rebases. Please run hack/generate-docs.sh to +populate this file. diff --git a/docs/user-guide/kubectl/kubectl_create_quota.md b/docs/user-guide/kubectl/kubectl_create_quota.md new file mode 100644 index 00000000000..185d3bea1e9 --- /dev/null +++ b/docs/user-guide/kubectl/kubectl_create_quota.md @@ -0,0 +1,36 @@ + + + + +WARNING +WARNING +WARNING +WARNING +WARNING + +

PLEASE NOTE: This document applies to the HEAD of the source tree

+ +If you are using a released version of Kubernetes, you should +refer to the docs that go with that version. + +Documentation for other releases can be found at +[releases.k8s.io](http://releases.k8s.io). + +-- + + + + + +This file is autogenerated, but we've stopped checking such files into the +repository to reduce the need for rebases. Please run hack/generate-docs.sh to +populate this file. + + +[![Analytics](https://kubernetes-site.appspot.com/UA-36037335-10/GitHub/docs/user-guide/kubectl/kubectl_create_quota.md?pixel)]() + diff --git a/hack/verify-flags/known-flags.txt b/hack/verify-flags/known-flags.txt index 678db563bf6..5e041bd1515 100644 --- a/hack/verify-flags/known-flags.txt +++ b/hack/verify-flags/known-flags.txt @@ -419,6 +419,7 @@ save-config scheduler-config scheduler-name schema-cache-dir +scopes seccomp-profile-root secure-port serialize-image-pulls diff --git a/pkg/kubectl/cmd/create_quota.go b/pkg/kubectl/cmd/create_quota.go index 26ba236b44e..2be38d436e5 100644 --- a/pkg/kubectl/cmd/create_quota.go +++ b/pkg/kubectl/cmd/create_quota.go @@ -1,5 +1,5 @@ /* -Copyright 2015 The Kubernetes Authors All rights reserved. +Copyright 2016 The Kubernetes Authors. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. @@ -28,16 +28,19 @@ import ( const ( quotaLong = ` -Create a resourcequota with the specified name and hard limits` +Create a resourcequota with the specified name, hard limits and optional scopes` quotaExample = ` // Create a new resourcequota named my-quota - $ kubectl create quota my-quota --hard=cpu=1,memory=1G,pods=2,services=3,replicationcontrollers=2,resourcequotas=1,secrets=5,persistentvolumeclaims=10` + $ kubectl create quota my-quota --hard=cpu=1,memory=1G,pods=2,services=3,replicationcontrollers=2,resourcequotas=1,secrets=5,persistentvolumeclaims=10 + + // Create a new resourcequota named best-effort + $ kubectl create quota best-effort --hard=pods=100 --scopes=BestEffort` ) // NewCmdCreateQuota is a macro command to create a new quota func NewCmdCreateQuota(f *cmdutil.Factory, cmdOut io.Writer) *cobra.Command { cmd := &cobra.Command{ - Use: "quota NAME [--hard=key1=value1,key2=value2] [--dry-run=bool]", + Use: "quota NAME [--hard=key1=value1,key2=value2] [--scopes=Scope1,Scope2] [--dry-run=bool]", Aliases: []string{"q"}, Short: "Create a quota with the specified name.", Long: quotaLong, @@ -50,9 +53,10 @@ func NewCmdCreateQuota(f *cmdutil.Factory, cmdOut io.Writer) *cobra.Command { cmdutil.AddApplyAnnotationFlags(cmd) cmdutil.AddValidateFlags(cmd) + cmdutil.AddPrinterFlags(cmd) cmdutil.AddGeneratorFlags(cmd, cmdutil.ResourceQuotaV1GeneratorName) - cmd.Flags().String("hard", "", "Specify multiple key/value pair to insert in resourcequota (i.e. --hard=cpu=1,memory=1G,pods=2,services=3,replicationcontrollers=2,resourcequotas=1,secrets=5,persistentvolumeclaims=10)") - cmd.MarkFlagRequired("hard") + cmd.Flags().String("hard", "", "A comma-delimited set of resource=quantity pairs that define a hard limit.") + cmd.Flags().String("scopes", "", "A comma-delimited set of quota scopes that must all match each object tracked by the quota.") return cmd } @@ -62,18 +66,13 @@ func CreateQuota(f *cmdutil.Factory, cmdOut io.Writer, cmd *cobra.Command, args if err != nil { return err } - requiredFlags := []string{"hard"} - for _, requiredFlag := range requiredFlags { - if value := cmdutil.GetFlagString(cmd, requiredFlag); len(value) == 0 { - return cmdutil.UsageError(cmd, "flag %s is required", requiredFlag) - } - } var generator kubectl.StructuredGenerator switch generatorName := cmdutil.GetFlagString(cmd, "generator"); generatorName { case cmdutil.ResourceQuotaV1GeneratorName: generator = &kubectl.ResourceQuotaGeneratorV1{ - Name: name, - Hard: cmdutil.GetFlagString(cmd, "hard"), + Name: name, + Hard: cmdutil.GetFlagString(cmd, "hard"), + Scopes: cmdutil.GetFlagString(cmd, "scopes"), } default: return cmdutil.UsageError(cmd, fmt.Sprintf("Generator: %s not supported.", generatorName)) diff --git a/pkg/kubectl/cmd/create_quota_test.go b/pkg/kubectl/cmd/create_quota_test.go index cbaf779e575..270e4fce224 100644 --- a/pkg/kubectl/cmd/create_quota_test.go +++ b/pkg/kubectl/cmd/create_quota_test.go @@ -1,5 +1,5 @@ /* -Copyright 2014 The Kubernetes Authors All rights reserved. +Copyright 2016 The Kubernetes Authors. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. @@ -28,14 +28,14 @@ import ( func TestCreateQuota(t *testing.T) { resourceQuotaObject := &api.ResourceQuota{} resourceQuotaObject.Name = "my-quota" - f, tf, codec := NewAPIFactory() + f, tf, codec, ns := NewAPIFactory() tf.Printer = &testPrinter{} tf.Client = &fake.RESTClient{ - Codec: codec, + NegotiatedSerializer: ns, Client: fake.CreateHTTPClient(func(req *http.Request) (*http.Response, error) { switch p, m := req.URL.Path, req.Method; { case p == "/namespaces/test/resourcequotas" && m == "POST": - return &http.Response{StatusCode: 201, Body: objBody(codec, resourceQuotaObject)}, nil + return &http.Response{StatusCode: 201, Header: defaultHeader(), Body: objBody(codec, resourceQuotaObject)}, nil default: t.Fatalf("unexpected request: %#v\n%#v", req.URL, req) return nil, nil @@ -43,13 +43,37 @@ func TestCreateQuota(t *testing.T) { }), } tf.Namespace = "test" - buf := bytes.NewBuffer([]byte{}) - cmd := NewCmdCreateQuota(f, buf) - cmd.Flags().Set("hard", "cpu=1") - cmd.Flags().Set("output", "name") - cmd.Run(cmd, []string{resourceQuotaObject.Name}) - expectedOutput := "resourcequota/" + resourceQuotaObject.Name + "\n" - if buf.String() != expectedOutput { - t.Errorf("expected output: %s, but got: %s", expectedOutput, buf.String()) + + tests := map[string]struct { + flags map[string]string + expectedOutput string + }{ + "single resource": { + flags: map[string]string{"hard": "cpu=1", "output": "name"}, + expectedOutput: "resourcequota/" + resourceQuotaObject.Name + "\n", + }, + "single resource with a scope": { + flags: map[string]string{"hard": "cpu=1", "output": "name", "scopes": "BestEffort"}, + expectedOutput: "resourcequota/" + resourceQuotaObject.Name + "\n", + }, + "multiple resources": { + flags: map[string]string{"hard": "cpu=1,pods=42", "output": "name", "scopes": "BestEffort"}, + expectedOutput: "resourcequota/" + resourceQuotaObject.Name + "\n", + }, + "single resource with multiple scopes": { + flags: map[string]string{"hard": "cpu=1", "output": "name", "scopes": "BestEffort,NotTerminating"}, + expectedOutput: "resourcequota/" + resourceQuotaObject.Name + "\n", + }, + } + for name, test := range tests { + buf := bytes.NewBuffer([]byte{}) + cmd := NewCmdCreateQuota(f, buf) + cmd.Flags().Set("hard", "cpu=1") + cmd.Flags().Set("output", "name") + cmd.Run(cmd, []string{resourceQuotaObject.Name}) + + if buf.String() != test.expectedOutput { + t.Errorf("%s: expected output: %s, but got: %s", name, test.expectedOutput, buf.String()) + } } } diff --git a/pkg/kubectl/quota.go b/pkg/kubectl/quota.go index e6409905b52..1261aba20b5 100644 --- a/pkg/kubectl/quota.go +++ b/pkg/kubectl/quota.go @@ -1,5 +1,5 @@ /* -Copyright 2015 The Kubernetes Authors All rights reserved. +Copyright 2016 The Kubernetes Authors. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. @@ -18,17 +18,22 @@ package kubectl import ( "fmt" + "strings" "k8s.io/kubernetes/pkg/api" - "k8s.io/kubernetes/pkg/api/resource" "k8s.io/kubernetes/pkg/runtime" - "strings" ) -// ResourceQuotaGeneratorV1 supports stable generation of a namespace +// ResourceQuotaGeneratorV1 supports stable generation of a resource quota type ResourceQuotaGeneratorV1 struct { + // The name of a quota object. Name string + + // The hard resource limit string before parsing. Hard string + + // The scopes of a quota object before parsing. + Scopes string } // ParamNames returns the set of supported input parameters when using the parameter injection generator pattern @@ -36,6 +41,7 @@ func (g ResourceQuotaGeneratorV1) ParamNames() []GeneratorParam { return []GeneratorParam{ {"name", true}, {"hard", true}, + {"scopes", false}, } } @@ -63,6 +69,7 @@ func (g ResourceQuotaGeneratorV1) Generate(genericParams map[string]interface{}) delegate := &ResourceQuotaGeneratorV1{} delegate.Name = params["name"] delegate.Hard = params["hard"] + delegate.Scopes = params["scopes"] return delegate.StructuredGenerate() } @@ -72,38 +79,23 @@ func (g *ResourceQuotaGeneratorV1) StructuredGenerate() (runtime.Object, error) return nil, err } - resourceQuotaSpec, err := generateResourceQuotaSpecList(g.Hard) + resourceList, err := populateResourceList(g.Hard) + if err != nil { + return nil, err + } + + scopes, err := parseScopes(g.Scopes) if err != nil { return nil, err } resourceQuota := &api.ResourceQuota{} resourceQuota.Name = g.Name - resourceQuota.Spec.Hard = resourceQuotaSpec + resourceQuota.Spec.Hard = resourceList + resourceQuota.Spec.Scopes = scopes return resourceQuota, nil } -func generateResourceQuotaSpecList(hard string) (resourceList api.ResourceList, err error) { - - defer func() { - if p := recover(); p != nil { - resourceList = nil - err = fmt.Errorf("Invalid input %v", p) - } - }() - - resourceList = make(api.ResourceList) - for _, keyValue := range strings.Split(hard, ",") { - items := strings.Split(keyValue, "=") - if len(items) != 2 { - return nil, fmt.Errorf("invalid input %v, expected key=value", keyValue) - } - - resourceList[api.ResourceName(items[0])] = resource.MustParse(items[1]) - } - return -} - // validate validates required fields are set to support structured generation func (r *ResourceQuotaGeneratorV1) validate() error { if len(r.Name) == 0 { @@ -111,3 +103,23 @@ func (r *ResourceQuotaGeneratorV1) validate() error { } return nil } + +func parseScopes(spec string) ([]api.ResourceQuotaScope, error) { + // empty input gets a nil response to preserve generator test expected behaviors + if spec == "" { + return nil, nil + } + + scopes := strings.Split(spec, ",") + result := make([]api.ResourceQuotaScope, 0, len(scopes)) + for _, scope := range scopes { + // intentionally do not verify the scope against the valid scope list. This is done by the apiserver anyway. + + if scope == "" { + return nil, fmt.Errorf("invalid resource quota scope \"\"") + } + + result = append(result, api.ResourceQuotaScope(scope)) + } + return result, nil +} diff --git a/pkg/kubectl/quota_test.go b/pkg/kubectl/quota_test.go index 07075f3bc44..930ded039b7 100644 --- a/pkg/kubectl/quota_test.go +++ b/pkg/kubectl/quota_test.go @@ -1,5 +1,5 @@ /* -Copyright 2015 The Kubernetes Authors All rights reserved. +Copyright 2016 The Kubernetes Authors. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. @@ -25,7 +25,7 @@ import ( func TestQuotaGenerate(t *testing.T) { hard := "cpu=10,memory=5G,pods=10,services=7" - resourceQuotaSpecList, err := generateResourceQuotaSpecList(hard) + resourceQuotaSpecList, err := populateResourceList(hard) if err != nil { t.Errorf("unexpected error: %v", err) } @@ -54,28 +54,61 @@ func TestQuotaGenerate(t *testing.T) { }, expectErr: true, }, + "test-valid-scopes": { + params: map[string]interface{}{ + "name": "foo", + "hard": hard, + "scopes": "BestEffort,NotTerminating", + }, + expected: &api.ResourceQuota{ + ObjectMeta: api.ObjectMeta{ + Name: "foo", + }, + Spec: api.ResourceQuotaSpec{ + Hard: resourceQuotaSpecList, + Scopes: []api.ResourceQuotaScope{ + api.ResourceQuotaScopeBestEffort, + api.ResourceQuotaScopeNotTerminating, + }, + }, + }, + expectErr: false, + }, + "test-empty-scopes": { + params: map[string]interface{}{ + "name": "foo", + "hard": hard, + "scopes": "", + }, + expected: &api.ResourceQuota{ + ObjectMeta: api.ObjectMeta{ + Name: "foo", + }, + Spec: api.ResourceQuotaSpec{Hard: resourceQuotaSpecList}, + }, + expectErr: false, + }, + "test-invalid-scopes": { + params: map[string]interface{}{ + "name": "foo", + "hard": hard, + "scopes": "abc,", + }, + expectErr: true, + }, } generator := ResourceQuotaGeneratorV1{} - for _, test := range tests { + for name, test := range tests { obj, err := generator.Generate(test.params) if !test.expectErr && err != nil { - t.Errorf("unexpected error: %v", err) + t.Errorf("%s: unexpected error: %v", name, err) } if test.expectErr && err != nil { continue } if !reflect.DeepEqual(obj.(*api.ResourceQuota), test.expected) { - t.Errorf("\nexpected:\n%#v\nsaw:\n%#v", test.expected, obj.(*api.ResourceQuota)) + t.Errorf("%s:\nexpected:\n%#v\nsaw:\n%#v", name, test.expected, obj.(*api.ResourceQuota)) } } } - -func TestGenerateResourceQuotaSpecList(t *testing.T) { - hardInvalidValue := "cpu=23foo,memory=5G,pods=10bar,services=7" - - resourceList, err := generateResourceQuotaSpecList(hardInvalidValue) - if err != nil && resourceList != nil { - t.Errorf("\nexpected:\n%#v\nsaw:\n%#v", nil, resourceList) - } -} diff --git a/test/e2e/kubectl.go b/test/e2e/kubectl.go index 1b4bac72661..89b7d949c37 100644 --- a/test/e2e/kubectl.go +++ b/test/e2e/kubectl.go @@ -44,6 +44,7 @@ import ( "k8s.io/kubernetes/pkg/api" "k8s.io/kubernetes/pkg/api/annotations" apierrs "k8s.io/kubernetes/pkg/api/errors" + "k8s.io/kubernetes/pkg/api/resource" "k8s.io/kubernetes/pkg/api/unversioned" client "k8s.io/kubernetes/pkg/client/unversioned" "k8s.io/kubernetes/pkg/controller" @@ -1198,6 +1199,76 @@ var _ = framework.KubeDescribe("Kubectl client", func() { }) }) + + framework.KubeDescribe("Kubectl create quota", func() { + It("should create a quota without scopes", func() { + nsFlag := fmt.Sprintf("--namespace=%v", ns) + quotaName := "million" + + By("calling kubectl quota") + framework.RunKubectlOrDie("create", "quota", quotaName, "--hard=pods=1000000,services=1000000", nsFlag) + + By("verifying that the quota was created") + quota, err := c.ResourceQuotas(ns).Get(quotaName) + if err != nil { + framework.Failf("Failed getting quota %s: %v", quotaName, err) + } + + if len(quota.Spec.Scopes) != 0 { + framework.Failf("Expected empty scopes, got %v", quota.Spec.Scopes) + } + if len(quota.Spec.Hard) != 2 { + framework.Failf("Expected two resources, got %v", quota.Spec.Hard) + } + r, found := quota.Spec.Hard[api.ResourcePods] + if expected := resource.MustParse("1000000"); !found || (&r).Cmp(expected) != 0 { + framework.Failf("Expected pods=1000000, got %v", r) + } + r, found = quota.Spec.Hard[api.ResourceServices] + if expected := resource.MustParse("1000000"); !found || (&r).Cmp(expected) != 0 { + framework.Failf("Expected services=1000000, got %v", r) + } + }) + + It("should create a quota with scopes", func() { + nsFlag := fmt.Sprintf("--namespace=%v", ns) + quotaName := "scopes" + + By("calling kubectl quota") + framework.RunKubectlOrDie("create", "quota", quotaName, "--hard=pods=1000000", "--scopes=BestEffort,NotTerminating", nsFlag) + + By("verifying that the quota was created") + quota, err := c.ResourceQuotas(ns).Get(quotaName) + if err != nil { + framework.Failf("Failed getting quota %s: %v", quotaName, err) + } + + if len(quota.Spec.Scopes) != 2 { + framework.Failf("Expected two scopes, got %v", quota.Spec.Scopes) + } + scopes := make(map[api.ResourceQuotaScope]struct{}) + for _, scope := range quota.Spec.Scopes { + scopes[scope] = struct{}{} + } + if _, found := scopes[api.ResourceQuotaScopeBestEffort]; !found { + framework.Failf("Expected BestEffort scope, got %v", quota.Spec.Scopes) + } + if _, found := scopes[api.ResourceQuotaScopeNotTerminating]; !found { + framework.Failf("Expected NotTerminating scope, got %v", quota.Spec.Scopes) + } + }) + + It("should reject quota with invalid scopes", func() { + nsFlag := fmt.Sprintf("--namespace=%v", ns) + quotaName := "scopes" + + By("calling kubectl quota") + out, err := framework.RunKubectl("create", "quota", quotaName, "--hard=hard=pods=1000000", "--scopes=Foo", nsFlag) + if err == nil { + framework.Failf("Expected kubectl to fail, but it succeeded: %s", out) + } + }) + }) }) // Checks whether the output split by line contains the required elements.