quota.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. /*
  2. Copyright 2016 The Kubernetes Authors.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package kubectl
  14. import (
  15. "fmt"
  16. "strings"
  17. "k8s.io/kubernetes/pkg/api"
  18. "k8s.io/kubernetes/pkg/runtime"
  19. )
  20. // ResourceQuotaGeneratorV1 supports stable generation of a resource quota
  21. type ResourceQuotaGeneratorV1 struct {
  22. // The name of a quota object.
  23. Name string
  24. // The hard resource limit string before parsing.
  25. Hard string
  26. // The scopes of a quota object before parsing.
  27. Scopes string
  28. }
  29. // ParamNames returns the set of supported input parameters when using the parameter injection generator pattern
  30. func (g ResourceQuotaGeneratorV1) ParamNames() []GeneratorParam {
  31. return []GeneratorParam{
  32. {"name", true},
  33. {"hard", true},
  34. {"scopes", false},
  35. }
  36. }
  37. // Ensure it supports the generator pattern that uses parameter injection
  38. var _ Generator = &ResourceQuotaGeneratorV1{}
  39. // Ensure it supports the generator pattern that uses parameters specified during construction
  40. var _ StructuredGenerator = &ResourceQuotaGeneratorV1{}
  41. func (g ResourceQuotaGeneratorV1) Generate(genericParams map[string]interface{}) (runtime.Object, error) {
  42. err := ValidateParams(g.ParamNames(), genericParams)
  43. if err != nil {
  44. return nil, err
  45. }
  46. params := map[string]string{}
  47. for key, value := range genericParams {
  48. strVal, isString := value.(string)
  49. if !isString {
  50. return nil, fmt.Errorf("expected string, saw %v for '%s'", value, key)
  51. }
  52. params[key] = strVal
  53. }
  54. delegate := &ResourceQuotaGeneratorV1{}
  55. delegate.Name = params["name"]
  56. delegate.Hard = params["hard"]
  57. delegate.Scopes = params["scopes"]
  58. return delegate.StructuredGenerate()
  59. }
  60. // StructuredGenerate outputs a ResourceQuota object using the configured fields
  61. func (g *ResourceQuotaGeneratorV1) StructuredGenerate() (runtime.Object, error) {
  62. if err := g.validate(); err != nil {
  63. return nil, err
  64. }
  65. resourceList, err := populateResourceList(g.Hard)
  66. if err != nil {
  67. return nil, err
  68. }
  69. scopes, err := parseScopes(g.Scopes)
  70. if err != nil {
  71. return nil, err
  72. }
  73. resourceQuota := &api.ResourceQuota{}
  74. resourceQuota.Name = g.Name
  75. resourceQuota.Spec.Hard = resourceList
  76. resourceQuota.Spec.Scopes = scopes
  77. return resourceQuota, nil
  78. }
  79. // validate validates required fields are set to support structured generation
  80. func (r *ResourceQuotaGeneratorV1) validate() error {
  81. if len(r.Name) == 0 {
  82. return fmt.Errorf("name must be specified")
  83. }
  84. return nil
  85. }
  86. func parseScopes(spec string) ([]api.ResourceQuotaScope, error) {
  87. // empty input gets a nil response to preserve generator test expected behaviors
  88. if spec == "" {
  89. return nil, nil
  90. }
  91. scopes := strings.Split(spec, ",")
  92. result := make([]api.ResourceQuotaScope, 0, len(scopes))
  93. for _, scope := range scopes {
  94. // intentionally do not verify the scope against the valid scope list. This is done by the apiserver anyway.
  95. if scope == "" {
  96. return nil, fmt.Errorf("invalid resource quota scope \"\"")
  97. }
  98. result = append(result, api.ResourceQuotaScope(scope))
  99. }
  100. return result, nil
  101. }