mirror_client.go 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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 pod
  14. import (
  15. "k8s.io/api/core/v1"
  16. "k8s.io/apimachinery/pkg/api/errors"
  17. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  18. "k8s.io/apimachinery/pkg/types"
  19. clientset "k8s.io/client-go/kubernetes"
  20. "k8s.io/klog"
  21. kubecontainer "k8s.io/kubernetes/pkg/kubelet/container"
  22. kubetypes "k8s.io/kubernetes/pkg/kubelet/types"
  23. )
  24. // MirrorClient knows how to create/delete a mirror pod in the API server.
  25. type MirrorClient interface {
  26. // CreateMirrorPod creates a mirror pod in the API server for the given
  27. // pod or returns an error. The mirror pod will have the same annotations
  28. // as the given pod as well as an extra annotation containing the hash of
  29. // the static pod.
  30. CreateMirrorPod(pod *v1.Pod) error
  31. // DeleteMirrorPod deletes the mirror pod with the given full name from
  32. // the API server or returns an error.
  33. DeleteMirrorPod(podFullName string, uid *types.UID) (bool, error)
  34. }
  35. // basicMirrorClient is a functional MirrorClient. Mirror pods are stored in
  36. // the kubelet directly because they need to be in sync with the internal
  37. // pods.
  38. type basicMirrorClient struct {
  39. apiserverClient clientset.Interface
  40. }
  41. // NewBasicMirrorClient returns a new MirrorClient.
  42. func NewBasicMirrorClient(apiserverClient clientset.Interface) MirrorClient {
  43. return &basicMirrorClient{apiserverClient: apiserverClient}
  44. }
  45. func (mc *basicMirrorClient) CreateMirrorPod(pod *v1.Pod) error {
  46. if mc.apiserverClient == nil {
  47. return nil
  48. }
  49. // Make a copy of the pod.
  50. copyPod := *pod
  51. copyPod.Annotations = make(map[string]string)
  52. for k, v := range pod.Annotations {
  53. copyPod.Annotations[k] = v
  54. }
  55. hash := getPodHash(pod)
  56. copyPod.Annotations[kubetypes.ConfigMirrorAnnotationKey] = hash
  57. apiPod, err := mc.apiserverClient.CoreV1().Pods(copyPod.Namespace).Create(&copyPod)
  58. if err != nil && errors.IsAlreadyExists(err) {
  59. // Check if the existing pod is the same as the pod we want to create.
  60. if h, ok := apiPod.Annotations[kubetypes.ConfigMirrorAnnotationKey]; ok && h == hash {
  61. return nil
  62. }
  63. }
  64. return err
  65. }
  66. // DeleteMirrorPod deletes a mirror pod.
  67. // It takes the full name of the pod and optionally a UID. If the UID
  68. // is non-nil, the pod is deleted only if its UID matches the supplied UID.
  69. // It returns whether the pod was actually deleted, and any error returned
  70. // while parsing the name of the pod.
  71. // Non-existence of the pod or UID mismatch is not treated as an error; the
  72. // routine simply returns false in that case.
  73. func (mc *basicMirrorClient) DeleteMirrorPod(podFullName string, uid *types.UID) (bool, error) {
  74. if mc.apiserverClient == nil {
  75. return false, nil
  76. }
  77. name, namespace, err := kubecontainer.ParsePodFullName(podFullName)
  78. if err != nil {
  79. klog.Errorf("Failed to parse a pod full name %q", podFullName)
  80. return false, err
  81. }
  82. klog.V(2).Infof("Deleting a mirror pod %q (uid %#v)", podFullName, uid)
  83. var GracePeriodSeconds int64
  84. GracePeriodSeconds = 0
  85. if err := mc.apiserverClient.CoreV1().Pods(namespace).Delete(name, &metav1.DeleteOptions{GracePeriodSeconds: &GracePeriodSeconds, Preconditions: &metav1.Preconditions{UID: uid}}); err != nil {
  86. // Unfortunately, there's no generic error for failing a precondition
  87. if !(errors.IsNotFound(err) || errors.IsConflict(err)) {
  88. // We should return the error here, but historically this routine does
  89. // not return an error unless it can't parse the pod name
  90. klog.Errorf("Failed deleting a mirror pod %q: %v", podFullName, err)
  91. }
  92. return false, nil
  93. }
  94. return true, nil
  95. }
  96. // IsStaticPod returns true if the pod is a static pod.
  97. func IsStaticPod(pod *v1.Pod) bool {
  98. source, err := kubetypes.GetPodSource(pod)
  99. return err == nil && source != kubetypes.ApiserverSource
  100. }
  101. // IsMirrorPod returns true if the pod is a mirror pod.
  102. func IsMirrorPod(pod *v1.Pod) bool {
  103. _, ok := pod.Annotations[kubetypes.ConfigMirrorAnnotationKey]
  104. return ok
  105. }
  106. func getHashFromMirrorPod(pod *v1.Pod) (string, bool) {
  107. hash, ok := pod.Annotations[kubetypes.ConfigMirrorAnnotationKey]
  108. return hash, ok
  109. }
  110. func getPodHash(pod *v1.Pod) string {
  111. // The annotation exists for all static pods.
  112. return pod.Annotations[kubetypes.ConfigHashAnnotationKey]
  113. }