generate.go 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. /*
  2. Copyright 2014 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. "reflect"
  17. "strconv"
  18. "strings"
  19. "github.com/spf13/cobra"
  20. "github.com/spf13/pflag"
  21. "k8s.io/kubernetes/pkg/runtime"
  22. utilerrors "k8s.io/kubernetes/pkg/util/errors"
  23. )
  24. // GeneratorParam is a parameter for a generator
  25. // TODO: facilitate structured json generator input schemes
  26. type GeneratorParam struct {
  27. Name string
  28. Required bool
  29. }
  30. // Generator is an interface for things that can generate API objects from input parameters.
  31. type Generator interface {
  32. // Generate creates an API object given a set of parameters
  33. Generate(params map[string]interface{}) (runtime.Object, error)
  34. // ParamNames returns the list of parameters that this generator uses
  35. ParamNames() []GeneratorParam
  36. }
  37. // StructuredGenerator is an interface for things that can generate API objects not using parameter injection
  38. type StructuredGenerator interface {
  39. // StructuredGenerator creates an API object using pre-configured parameters
  40. StructuredGenerate() (runtime.Object, error)
  41. }
  42. func IsZero(i interface{}) bool {
  43. if i == nil {
  44. return true
  45. }
  46. return reflect.DeepEqual(i, reflect.Zero(reflect.TypeOf(i)).Interface())
  47. }
  48. // ValidateParams ensures that all required params are present in the params map
  49. func ValidateParams(paramSpec []GeneratorParam, params map[string]interface{}) error {
  50. allErrs := []error{}
  51. for ix := range paramSpec {
  52. if paramSpec[ix].Required {
  53. value, found := params[paramSpec[ix].Name]
  54. if !found || IsZero(value) {
  55. allErrs = append(allErrs, fmt.Errorf("Parameter: %s is required", paramSpec[ix].Name))
  56. }
  57. }
  58. }
  59. return utilerrors.NewAggregate(allErrs)
  60. }
  61. // AnnotateFlags annotates all flags that are used by generators.
  62. func AnnotateFlags(cmd *cobra.Command, generators map[string]Generator) {
  63. // Iterate over all generators and mark any flags used by them.
  64. for name, generator := range generators {
  65. generatorParams := map[string]struct{}{}
  66. for _, param := range generator.ParamNames() {
  67. generatorParams[param.Name] = struct{}{}
  68. }
  69. cmd.Flags().VisitAll(func(flag *pflag.Flag) {
  70. if _, found := generatorParams[flag.Name]; !found {
  71. // This flag is not used by the current generator
  72. // so skip it.
  73. return
  74. }
  75. if flag.Annotations == nil {
  76. flag.Annotations = map[string][]string{}
  77. }
  78. if annotations := flag.Annotations["generator"]; annotations == nil {
  79. flag.Annotations["generator"] = []string{}
  80. }
  81. flag.Annotations["generator"] = append(flag.Annotations["generator"], name)
  82. })
  83. }
  84. }
  85. // EnsureFlagsValid ensures that no invalid flags are being used against a generator.
  86. func EnsureFlagsValid(cmd *cobra.Command, generators map[string]Generator, generatorInUse string) error {
  87. AnnotateFlags(cmd, generators)
  88. allErrs := []error{}
  89. cmd.Flags().VisitAll(func(flag *pflag.Flag) {
  90. // If the flag hasn't changed, don't validate it.
  91. if !flag.Changed {
  92. return
  93. }
  94. // Look into the flag annotations for the generators that can use it.
  95. if annotations := flag.Annotations["generator"]; len(annotations) > 0 {
  96. annotationMap := map[string]struct{}{}
  97. for _, ann := range annotations {
  98. annotationMap[ann] = struct{}{}
  99. }
  100. // If the current generator is not annotated, then this flag shouldn't
  101. // be used with it.
  102. if _, found := annotationMap[generatorInUse]; !found {
  103. allErrs = append(allErrs, fmt.Errorf("cannot use --%s with --generator=%s", flag.Name, generatorInUse))
  104. }
  105. }
  106. })
  107. return utilerrors.NewAggregate(allErrs)
  108. }
  109. // MakeParams is a utility that creates generator parameters from a command line
  110. func MakeParams(cmd *cobra.Command, params []GeneratorParam) map[string]interface{} {
  111. result := map[string]interface{}{}
  112. for ix := range params {
  113. f := cmd.Flags().Lookup(params[ix].Name)
  114. if f != nil {
  115. result[params[ix].Name] = f.Value.String()
  116. }
  117. }
  118. return result
  119. }
  120. func MakeProtocols(protocols map[string]string) string {
  121. out := []string{}
  122. for key, value := range protocols {
  123. out = append(out, fmt.Sprintf("%s/%s", key, value))
  124. }
  125. return strings.Join(out, ",")
  126. }
  127. func ParseProtocols(protocols interface{}) (map[string]string, error) {
  128. protocolsString, isString := protocols.(string)
  129. if !isString {
  130. return nil, fmt.Errorf("expected string, found %v", protocols)
  131. }
  132. if len(protocolsString) == 0 {
  133. return nil, fmt.Errorf("no protocols passed")
  134. }
  135. portProtocolMap := map[string]string{}
  136. protocolsSlice := strings.Split(protocolsString, ",")
  137. for ix := range protocolsSlice {
  138. portProtocol := strings.Split(protocolsSlice[ix], "/")
  139. if len(portProtocol) != 2 {
  140. return nil, fmt.Errorf("unexpected port protocol mapping: %s", protocolsSlice[ix])
  141. }
  142. portProtocolMap[portProtocol[0]] = portProtocol[1]
  143. }
  144. return portProtocolMap, nil
  145. }
  146. func MakeLabels(labels map[string]string) string {
  147. out := []string{}
  148. for key, value := range labels {
  149. out = append(out, fmt.Sprintf("%s=%s", key, value))
  150. }
  151. return strings.Join(out, ",")
  152. }
  153. // ParseLabels turns a string representation of a label set into a map[string]string
  154. func ParseLabels(labelSpec interface{}) (map[string]string, error) {
  155. labelString, isString := labelSpec.(string)
  156. if !isString {
  157. return nil, fmt.Errorf("expected string, found %v", labelSpec)
  158. }
  159. if len(labelString) == 0 {
  160. return nil, fmt.Errorf("no label spec passed")
  161. }
  162. labels := map[string]string{}
  163. labelSpecs := strings.Split(labelString, ",")
  164. for ix := range labelSpecs {
  165. labelSpec := strings.Split(labelSpecs[ix], "=")
  166. if len(labelSpec) != 2 {
  167. return nil, fmt.Errorf("unexpected label spec: %s", labelSpecs[ix])
  168. }
  169. labels[labelSpec[0]] = labelSpec[1]
  170. }
  171. return labels, nil
  172. }
  173. func GetBool(params map[string]string, key string, defValue bool) (bool, error) {
  174. if val, found := params[key]; !found {
  175. return defValue, nil
  176. } else {
  177. return strconv.ParseBool(val)
  178. }
  179. }