deployment.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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/api/unversioned"
  19. "k8s.io/kubernetes/pkg/apis/extensions"
  20. "k8s.io/kubernetes/pkg/runtime"
  21. )
  22. // DeploymentGeneratorV1 supports stable generation of a deployment
  23. type DeploymentBasicGeneratorV1 struct {
  24. Name string
  25. Images []string
  26. }
  27. // Ensure it supports the generator pattern that uses parameters specified during construction
  28. var _ StructuredGenerator = &DeploymentBasicGeneratorV1{}
  29. func (DeploymentBasicGeneratorV1) ParamNames() []GeneratorParam {
  30. return []GeneratorParam{
  31. {"name", true},
  32. {"image", true},
  33. }
  34. }
  35. func (s DeploymentBasicGeneratorV1) Generate(params map[string]interface{}) (runtime.Object, error) {
  36. err := ValidateParams(s.ParamNames(), params)
  37. if err != nil {
  38. return nil, err
  39. }
  40. name, isString := params["name"].(string)
  41. if !isString {
  42. return nil, fmt.Errorf("expected string, saw %v for 'name'", name)
  43. }
  44. imageStrings, isArray := params["image"].([]string)
  45. if !isArray {
  46. return nil, fmt.Errorf("expected []string, found :%v", imageStrings)
  47. }
  48. delegate := &DeploymentBasicGeneratorV1{Name: name, Images: imageStrings}
  49. return delegate.StructuredGenerate()
  50. }
  51. // StructuredGenerate outputs a deployment object using the configured fields
  52. func (s *DeploymentBasicGeneratorV1) StructuredGenerate() (runtime.Object, error) {
  53. if err := s.validate(); err != nil {
  54. return nil, err
  55. }
  56. podSpec := api.PodSpec{Containers: []api.Container{}}
  57. for _, imageString := range s.Images {
  58. imageSplit := strings.Split(imageString, "/")
  59. name := imageSplit[len(imageSplit)-1]
  60. podSpec.Containers = append(podSpec.Containers, api.Container{Name: name, Image: imageString})
  61. }
  62. // setup default label and selector
  63. labels := map[string]string{}
  64. labels["app"] = s.Name
  65. selector := unversioned.LabelSelector{MatchLabels: labels}
  66. deployment := extensions.Deployment{
  67. ObjectMeta: api.ObjectMeta{
  68. Name: s.Name,
  69. Labels: labels,
  70. },
  71. Spec: extensions.DeploymentSpec{
  72. Replicas: 1,
  73. Selector: &selector,
  74. Template: api.PodTemplateSpec{
  75. ObjectMeta: api.ObjectMeta{
  76. Labels: labels,
  77. },
  78. Spec: podSpec,
  79. },
  80. },
  81. }
  82. return &deployment, nil
  83. }
  84. // validate validates required fields are set to support structured generation
  85. func (s *DeploymentBasicGeneratorV1) validate() error {
  86. if len(s.Name) == 0 {
  87. return fmt.Errorf("name must be specified")
  88. }
  89. if len(s.Images) == 0 {
  90. return fmt.Errorf("at least one image must be specified")
  91. }
  92. return nil
  93. }