deployment.go 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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. appsv1 "k8s.io/api/apps/v1"
  18. appsv1beta1 "k8s.io/api/apps/v1beta1"
  19. "k8s.io/api/core/v1"
  20. extensionsv1beta1 "k8s.io/api/extensions/v1beta1"
  21. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  22. "k8s.io/apimachinery/pkg/runtime"
  23. "k8s.io/kubernetes/pkg/kubectl/generate"
  24. )
  25. // BaseDeploymentGenerator implements the common functionality of
  26. // DeploymentBasicGeneratorV1, DeploymentBasicAppsGeneratorV1Beta1 and DeploymentBasicAppsGeneratorV1. To reduce
  27. // confusion, it's best to keep this struct in the same file as those
  28. // generators.
  29. type BaseDeploymentGenerator struct {
  30. Name string
  31. Images []string
  32. }
  33. // validate: check if the caller has forgotten to set one of our fields.
  34. func (b BaseDeploymentGenerator) validate() error {
  35. if len(b.Name) == 0 {
  36. return fmt.Errorf("name must be specified")
  37. }
  38. if len(b.Images) == 0 {
  39. return fmt.Errorf("at least one image must be specified")
  40. }
  41. return nil
  42. }
  43. // structuredGenerate: determine the fields of a deployment. The struct that
  44. // embeds BaseDeploymentGenerator should assemble these pieces into a
  45. // runtime.Object.
  46. func (b BaseDeploymentGenerator) structuredGenerate() (
  47. podSpec v1.PodSpec,
  48. labels map[string]string,
  49. selector metav1.LabelSelector,
  50. err error,
  51. ) {
  52. err = b.validate()
  53. if err != nil {
  54. return
  55. }
  56. podSpec = buildPodSpec(b.Images)
  57. labels = map[string]string{}
  58. labels["app"] = b.Name
  59. selector = metav1.LabelSelector{MatchLabels: labels}
  60. return
  61. }
  62. // buildPodSpec: parse the image strings and assemble them into the Containers
  63. // of a PodSpec. This is all you need to create the PodSpec for a deployment.
  64. func buildPodSpec(images []string) v1.PodSpec {
  65. podSpec := v1.PodSpec{Containers: []v1.Container{}}
  66. for _, imageString := range images {
  67. // Retain just the image name
  68. imageSplit := strings.Split(imageString, "/")
  69. name := imageSplit[len(imageSplit)-1]
  70. // Remove any tag or hash
  71. if strings.Contains(name, ":") {
  72. name = strings.Split(name, ":")[0]
  73. }
  74. if strings.Contains(name, "@") {
  75. name = strings.Split(name, "@")[0]
  76. }
  77. podSpec.Containers = append(podSpec.Containers, v1.Container{Name: name, Image: imageString})
  78. }
  79. return podSpec
  80. }
  81. // DeploymentBasicGeneratorV1 supports stable generation of a deployment
  82. type DeploymentBasicGeneratorV1 struct {
  83. BaseDeploymentGenerator
  84. }
  85. // Ensure it supports the generator pattern that uses parameters specified during construction
  86. var _ generate.StructuredGenerator = &DeploymentBasicGeneratorV1{}
  87. // StructuredGenerate outputs a deployment object using the configured fields
  88. func (s *DeploymentBasicGeneratorV1) StructuredGenerate() (runtime.Object, error) {
  89. podSpec, labels, selector, err := s.structuredGenerate()
  90. one := int32(1)
  91. return &extensionsv1beta1.Deployment{
  92. ObjectMeta: metav1.ObjectMeta{
  93. Name: s.Name,
  94. Labels: labels,
  95. },
  96. Spec: extensionsv1beta1.DeploymentSpec{
  97. Replicas: &one,
  98. Selector: &selector,
  99. Template: v1.PodTemplateSpec{
  100. ObjectMeta: metav1.ObjectMeta{
  101. Labels: labels,
  102. },
  103. Spec: podSpec,
  104. },
  105. },
  106. }, err
  107. }
  108. // DeploymentBasicAppsGeneratorV1Beta1 supports stable generation of a deployment under apps/v1beta1 endpoint
  109. type DeploymentBasicAppsGeneratorV1Beta1 struct {
  110. BaseDeploymentGenerator
  111. }
  112. // Ensure it supports the generator pattern that uses parameters specified during construction
  113. var _ generate.StructuredGenerator = &DeploymentBasicAppsGeneratorV1Beta1{}
  114. // StructuredGenerate outputs a deployment object using the configured fields
  115. func (s *DeploymentBasicAppsGeneratorV1Beta1) StructuredGenerate() (runtime.Object, error) {
  116. podSpec, labels, selector, err := s.structuredGenerate()
  117. one := int32(1)
  118. return &appsv1beta1.Deployment{
  119. ObjectMeta: metav1.ObjectMeta{
  120. Name: s.Name,
  121. Labels: labels,
  122. },
  123. Spec: appsv1beta1.DeploymentSpec{
  124. Replicas: &one,
  125. Selector: &selector,
  126. Template: v1.PodTemplateSpec{
  127. ObjectMeta: metav1.ObjectMeta{
  128. Labels: labels,
  129. },
  130. Spec: podSpec,
  131. },
  132. },
  133. }, err
  134. }
  135. // DeploymentBasicAppsGeneratorV1 supports stable generation of a deployment under apps/v1 endpoint
  136. type DeploymentBasicAppsGeneratorV1 struct {
  137. BaseDeploymentGenerator
  138. }
  139. // Ensure it supports the generator pattern that uses parameters specified during construction
  140. var _ generate.StructuredGenerator = &DeploymentBasicAppsGeneratorV1{}
  141. // StructuredGenerate outputs a deployment object using the configured fields
  142. func (s *DeploymentBasicAppsGeneratorV1) StructuredGenerate() (runtime.Object, error) {
  143. podSpec, labels, selector, err := s.structuredGenerate()
  144. one := int32(1)
  145. return &appsv1.Deployment{
  146. ObjectMeta: metav1.ObjectMeta{
  147. Name: s.Name,
  148. Labels: labels,
  149. },
  150. Spec: appsv1.DeploymentSpec{
  151. Replicas: &one,
  152. Selector: &selector,
  153. Template: v1.PodTemplateSpec{
  154. ObjectMeta: metav1.ObjectMeta{
  155. Labels: labels,
  156. },
  157. Spec: podSpec,
  158. },
  159. },
  160. }, err
  161. }