priorityclass.go 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. /*
  2. Copyright 2017 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. apiv1 "k8s.io/api/core/v1"
  17. scheduling "k8s.io/api/scheduling/v1"
  18. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  19. "k8s.io/apimachinery/pkg/runtime"
  20. "k8s.io/kubernetes/pkg/kubectl/generate"
  21. )
  22. // PriorityClassV1Generator supports stable generation of a priorityClass.
  23. type PriorityClassV1Generator struct {
  24. Name string
  25. Value int32
  26. GlobalDefault bool
  27. Description string
  28. PreemptionPolicy apiv1.PreemptionPolicy
  29. }
  30. // Ensure it supports the generator pattern that uses parameters specified during construction.
  31. var _ generate.StructuredGenerator = &PriorityClassV1Generator{}
  32. func (PriorityClassV1Generator) ParamNames() []generate.GeneratorParam {
  33. return []generate.GeneratorParam{
  34. {Name: "name", Required: true},
  35. {Name: "value", Required: true},
  36. {Name: "global-default", Required: false},
  37. {Name: "description", Required: false},
  38. {Name: "preemption-policy", Required: false},
  39. }
  40. }
  41. func (s PriorityClassV1Generator) Generate(params map[string]interface{}) (runtime.Object, error) {
  42. if err := generate.ValidateParams(s.ParamNames(), params); err != nil {
  43. return nil, err
  44. }
  45. name, found := params["name"].(string)
  46. if !found {
  47. return nil, fmt.Errorf("expected string, saw %v for 'name'", name)
  48. }
  49. value, found := params["value"].(int32)
  50. if !found {
  51. return nil, fmt.Errorf("expected int32, found %v", value)
  52. }
  53. globalDefault, found := params["global-default"].(bool)
  54. if !found {
  55. return nil, fmt.Errorf("expected bool, found %v", globalDefault)
  56. }
  57. description, found := params["description"].(string)
  58. if !found {
  59. return nil, fmt.Errorf("expected string, found %v", description)
  60. }
  61. preemptionPolicy := apiv1.PreemptionPolicy(params["preemption-policy"].(string))
  62. delegate := &PriorityClassV1Generator{Name: name, Value: value, GlobalDefault: globalDefault, Description: description, PreemptionPolicy: preemptionPolicy}
  63. return delegate.StructuredGenerate()
  64. }
  65. // StructuredGenerate outputs a priorityClass object using the configured fields.
  66. func (s *PriorityClassV1Generator) StructuredGenerate() (runtime.Object, error) {
  67. return &scheduling.PriorityClass{
  68. ObjectMeta: metav1.ObjectMeta{
  69. Name: s.Name,
  70. },
  71. Value: s.Value,
  72. GlobalDefault: s.GlobalDefault,
  73. Description: s.Description,
  74. PreemptionPolicy: &s.PreemptionPolicy,
  75. }, nil
  76. }