namespace.go 2.3 KB

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