common.go 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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. // Common logic used by both http and file channels.
  14. package config
  15. import (
  16. "crypto/md5"
  17. "encoding/hex"
  18. "fmt"
  19. "strings"
  20. "k8s.io/api/core/v1"
  21. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  22. "k8s.io/apimachinery/pkg/runtime"
  23. "k8s.io/apimachinery/pkg/types"
  24. utilyaml "k8s.io/apimachinery/pkg/util/yaml"
  25. api "k8s.io/kubernetes/pkg/apis/core"
  26. "k8s.io/kubernetes/pkg/apis/core/helper"
  27. // TODO: remove this import if
  28. // api.Registry.GroupOrDie(v1.GroupName).GroupVersion.String() is changed
  29. // to "v1"?
  30. "k8s.io/kubernetes/pkg/api/legacyscheme"
  31. _ "k8s.io/kubernetes/pkg/apis/core/install"
  32. k8s_api_v1 "k8s.io/kubernetes/pkg/apis/core/v1"
  33. "k8s.io/kubernetes/pkg/apis/core/validation"
  34. kubetypes "k8s.io/kubernetes/pkg/kubelet/types"
  35. "k8s.io/kubernetes/pkg/util/hash"
  36. "k8s.io/klog"
  37. )
  38. // Generate a pod name that is unique among nodes by appending the nodeName.
  39. func generatePodName(name string, nodeName types.NodeName) string {
  40. return fmt.Sprintf("%s-%s", name, strings.ToLower(string(nodeName)))
  41. }
  42. func applyDefaults(pod *api.Pod, source string, isFile bool, nodeName types.NodeName) error {
  43. if len(pod.UID) == 0 {
  44. hasher := md5.New()
  45. if isFile {
  46. fmt.Fprintf(hasher, "host:%s", nodeName)
  47. fmt.Fprintf(hasher, "file:%s", source)
  48. } else {
  49. fmt.Fprintf(hasher, "url:%s", source)
  50. }
  51. hash.DeepHashObject(hasher, pod)
  52. pod.UID = types.UID(hex.EncodeToString(hasher.Sum(nil)[0:]))
  53. klog.V(5).Infof("Generated UID %q pod %q from %s", pod.UID, pod.Name, source)
  54. }
  55. pod.Name = generatePodName(pod.Name, nodeName)
  56. klog.V(5).Infof("Generated Name %q for UID %q from URL %s", pod.Name, pod.UID, source)
  57. if pod.Namespace == "" {
  58. pod.Namespace = metav1.NamespaceDefault
  59. }
  60. klog.V(5).Infof("Using namespace %q for pod %q from %s", pod.Namespace, pod.Name, source)
  61. // Set the Host field to indicate this pod is scheduled on the current node.
  62. pod.Spec.NodeName = string(nodeName)
  63. pod.ObjectMeta.SelfLink = getSelfLink(pod.Name, pod.Namespace)
  64. if pod.Annotations == nil {
  65. pod.Annotations = make(map[string]string)
  66. }
  67. // The generated UID is the hash of the file.
  68. pod.Annotations[kubetypes.ConfigHashAnnotationKey] = string(pod.UID)
  69. if isFile {
  70. // Applying the default Taint tolerations to static pods,
  71. // so they are not evicted when there are node problems.
  72. helper.AddOrUpdateTolerationInPod(pod, &api.Toleration{
  73. Operator: "Exists",
  74. Effect: api.TaintEffectNoExecute,
  75. })
  76. }
  77. // Set the default status to pending.
  78. pod.Status.Phase = api.PodPending
  79. return nil
  80. }
  81. func getSelfLink(name, namespace string) string {
  82. var selfLink string
  83. if len(namespace) == 0 {
  84. namespace = metav1.NamespaceDefault
  85. }
  86. selfLink = fmt.Sprintf("/api/v1/namespaces/%s/pods/%s", namespace, name)
  87. return selfLink
  88. }
  89. type defaultFunc func(pod *api.Pod) error
  90. func tryDecodeSinglePod(data []byte, defaultFn defaultFunc) (parsed bool, pod *v1.Pod, err error) {
  91. // JSON is valid YAML, so this should work for everything.
  92. json, err := utilyaml.ToJSON(data)
  93. if err != nil {
  94. return false, nil, err
  95. }
  96. obj, err := runtime.Decode(legacyscheme.Codecs.UniversalDecoder(), json)
  97. if err != nil {
  98. return false, pod, err
  99. }
  100. newPod, ok := obj.(*api.Pod)
  101. // Check whether the object could be converted to single pod.
  102. if !ok {
  103. return false, pod, fmt.Errorf("invalid pod: %#v", obj)
  104. }
  105. // Apply default values and validate the pod.
  106. if err = defaultFn(newPod); err != nil {
  107. return true, pod, err
  108. }
  109. if errs := validation.ValidatePod(newPod); len(errs) > 0 {
  110. return true, pod, fmt.Errorf("invalid pod: %v", errs)
  111. }
  112. v1Pod := &v1.Pod{}
  113. if err := k8s_api_v1.Convert_core_Pod_To_v1_Pod(newPod, v1Pod, nil); err != nil {
  114. klog.Errorf("Pod %q failed to convert to v1", newPod.Name)
  115. return true, nil, err
  116. }
  117. return true, v1Pod, nil
  118. }
  119. func tryDecodePodList(data []byte, defaultFn defaultFunc) (parsed bool, pods v1.PodList, err error) {
  120. obj, err := runtime.Decode(legacyscheme.Codecs.UniversalDecoder(), data)
  121. if err != nil {
  122. return false, pods, err
  123. }
  124. newPods, ok := obj.(*api.PodList)
  125. // Check whether the object could be converted to list of pods.
  126. if !ok {
  127. err = fmt.Errorf("invalid pods list: %#v", obj)
  128. return false, pods, err
  129. }
  130. // Apply default values and validate pods.
  131. for i := range newPods.Items {
  132. newPod := &newPods.Items[i]
  133. if err = defaultFn(newPod); err != nil {
  134. return true, pods, err
  135. }
  136. if errs := validation.ValidatePod(newPod); len(errs) > 0 {
  137. err = fmt.Errorf("invalid pod: %v", errs)
  138. return true, pods, err
  139. }
  140. }
  141. v1Pods := &v1.PodList{}
  142. if err := k8s_api_v1.Convert_core_PodList_To_v1_PodList(newPods, v1Pods, nil); err != nil {
  143. return true, pods, err
  144. }
  145. return true, *v1Pods, err
  146. }