namespace.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. /*
  2. Copyright 2015 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. "k8s.io/api/core/v1"
  17. "k8s.io/apimachinery/pkg/runtime"
  18. "k8s.io/kubernetes/pkg/kubectl/generate"
  19. )
  20. // NamespaceGeneratorV1 supports stable generation of a namespace
  21. type NamespaceGeneratorV1 struct {
  22. // Name of namespace
  23. Name string
  24. }
  25. // Ensure it supports the generator pattern that uses parameter injection
  26. var _ generate.Generator = &NamespaceGeneratorV1{}
  27. // Ensure it supports the generator pattern that uses parameters specified during construction
  28. var _ generate.StructuredGenerator = &NamespaceGeneratorV1{}
  29. // Generate returns a namespace using the specified parameters
  30. func (g NamespaceGeneratorV1) Generate(genericParams map[string]interface{}) (runtime.Object, error) {
  31. err := generate.ValidateParams(g.ParamNames(), genericParams)
  32. if err != nil {
  33. return nil, err
  34. }
  35. params := map[string]string{}
  36. for key, value := range genericParams {
  37. strVal, isString := value.(string)
  38. if !isString {
  39. return nil, fmt.Errorf("expected string, saw %v for '%s'", value, key)
  40. }
  41. params[key] = strVal
  42. }
  43. delegate := &NamespaceGeneratorV1{Name: params["name"]}
  44. return delegate.StructuredGenerate()
  45. }
  46. // ParamNames returns the set of supported input parameters when using the parameter injection generator pattern
  47. func (g NamespaceGeneratorV1) ParamNames() []generate.GeneratorParam {
  48. return []generate.GeneratorParam{
  49. {Name: "name", Required: true},
  50. }
  51. }
  52. // StructuredGenerate outputs a namespace object using the configured fields
  53. func (g *NamespaceGeneratorV1) StructuredGenerate() (runtime.Object, error) {
  54. if err := g.validate(); err != nil {
  55. return nil, err
  56. }
  57. namespace := &v1.Namespace{}
  58. namespace.Name = g.Name
  59. return namespace, nil
  60. }
  61. // validate validates required fields are set to support structured generation
  62. func (g *NamespaceGeneratorV1) validate() error {
  63. if len(g.Name) == 0 {
  64. return fmt.Errorf("name must be specified")
  65. }
  66. return nil
  67. }