quota.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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 versioned
  14. import (
  15. "fmt"
  16. "strings"
  17. "k8s.io/api/core/v1"
  18. "k8s.io/apimachinery/pkg/runtime"
  19. "k8s.io/kubernetes/pkg/kubectl/generate"
  20. )
  21. // ResourceQuotaGeneratorV1 supports stable generation of a resource quota
  22. type ResourceQuotaGeneratorV1 struct {
  23. // The name of a quota object.
  24. Name string
  25. // The hard resource limit string before parsing.
  26. Hard string
  27. // The scopes of a quota object before parsing.
  28. Scopes string
  29. }
  30. // ParamNames returns the set of supported input parameters when using the parameter injection generator pattern
  31. func (g ResourceQuotaGeneratorV1) ParamNames() []generate.GeneratorParam {
  32. return []generate.GeneratorParam{
  33. {Name: "name", Required: true},
  34. {Name: "hard", Required: true},
  35. {Name: "scopes", Required: false},
  36. }
  37. }
  38. // Ensure it supports the generator pattern that uses parameter injection
  39. var _ generate.Generator = &ResourceQuotaGeneratorV1{}
  40. // Ensure it supports the generator pattern that uses parameters specified during construction
  41. var _ generate.StructuredGenerator = &ResourceQuotaGeneratorV1{}
  42. func (g ResourceQuotaGeneratorV1) Generate(genericParams map[string]interface{}) (runtime.Object, error) {
  43. err := generate.ValidateParams(g.ParamNames(), genericParams)
  44. if err != nil {
  45. return nil, err
  46. }
  47. params := map[string]string{}
  48. for key, value := range genericParams {
  49. strVal, isString := value.(string)
  50. if !isString {
  51. return nil, fmt.Errorf("expected string, saw %v for '%s'", value, key)
  52. }
  53. params[key] = strVal
  54. }
  55. delegate := &ResourceQuotaGeneratorV1{}
  56. delegate.Name = params["name"]
  57. delegate.Hard = params["hard"]
  58. delegate.Scopes = params["scopes"]
  59. return delegate.StructuredGenerate()
  60. }
  61. // StructuredGenerate outputs a ResourceQuota object using the configured fields
  62. func (g *ResourceQuotaGeneratorV1) StructuredGenerate() (runtime.Object, error) {
  63. if err := g.validate(); err != nil {
  64. return nil, err
  65. }
  66. resourceList, err := populateResourceListV1(g.Hard)
  67. if err != nil {
  68. return nil, err
  69. }
  70. scopes, err := parseScopes(g.Scopes)
  71. if err != nil {
  72. return nil, err
  73. }
  74. resourceQuota := &v1.ResourceQuota{}
  75. resourceQuota.Name = g.Name
  76. resourceQuota.Spec.Hard = resourceList
  77. resourceQuota.Spec.Scopes = scopes
  78. return resourceQuota, nil
  79. }
  80. // validate validates required fields are set to support structured generation
  81. func (r *ResourceQuotaGeneratorV1) validate() error {
  82. if len(r.Name) == 0 {
  83. return fmt.Errorf("name must be specified")
  84. }
  85. return nil
  86. }
  87. func parseScopes(spec string) ([]v1.ResourceQuotaScope, error) {
  88. // empty input gets a nil response to preserve generator test expected behaviors
  89. if spec == "" {
  90. return nil, nil
  91. }
  92. scopes := strings.Split(spec, ",")
  93. result := make([]v1.ResourceQuotaScope, 0, len(scopes))
  94. for _, scope := range scopes {
  95. // intentionally do not verify the scope against the valid scope list. This is done by the apiserver anyway.
  96. if scope == "" {
  97. return nil, fmt.Errorf("invalid resource quota scope \"\"")
  98. }
  99. result = append(result, v1.ResourceQuotaScope(scope))
  100. }
  101. return result, nil
  102. }