2015-03-23 19:17:12 +00:00
|
|
|
/*
|
2015-05-01 16:19:44 +00:00
|
|
|
Copyright 2015 The Kubernetes Authors All rights reserved.
|
2015-03-23 19:17:12 +00:00
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2015-10-12 23:28:23 +00:00
|
|
|
package pod
|
2015-03-23 19:17:12 +00:00
|
|
|
|
|
|
|
import (
|
2015-08-05 22:05:17 +00:00
|
|
|
"github.com/golang/glog"
|
2015-08-05 22:03:47 +00:00
|
|
|
"k8s.io/kubernetes/pkg/api"
|
2016-01-21 19:55:37 +00:00
|
|
|
"k8s.io/kubernetes/pkg/api/errors"
|
2015-08-13 19:01:50 +00:00
|
|
|
client "k8s.io/kubernetes/pkg/client/unversioned"
|
2015-08-05 22:03:47 +00:00
|
|
|
kubecontainer "k8s.io/kubernetes/pkg/kubelet/container"
|
2015-10-09 17:24:31 +00:00
|
|
|
kubetypes "k8s.io/kubernetes/pkg/kubelet/types"
|
2015-03-23 19:17:12 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// Mirror client is used to create/delete a mirror pod.
|
2015-10-12 23:28:23 +00:00
|
|
|
type MirrorClient interface {
|
2015-04-20 18:20:53 +00:00
|
|
|
CreateMirrorPod(*api.Pod) error
|
2015-03-23 19:17:12 +00:00
|
|
|
DeleteMirrorPod(string) error
|
|
|
|
}
|
|
|
|
|
|
|
|
type basicMirrorClient struct {
|
|
|
|
// mirror pods are stored in the kubelet directly because they need to be
|
|
|
|
// in sync with the internal pods.
|
|
|
|
apiserverClient client.Interface
|
|
|
|
}
|
|
|
|
|
2015-10-12 23:28:23 +00:00
|
|
|
func NewBasicMirrorClient(apiserverClient client.Interface) MirrorClient {
|
2015-03-23 19:17:12 +00:00
|
|
|
return &basicMirrorClient{apiserverClient: apiserverClient}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Creates a mirror pod.
|
2015-04-20 18:20:53 +00:00
|
|
|
func (mc *basicMirrorClient) CreateMirrorPod(pod *api.Pod) error {
|
2015-04-20 03:26:07 +00:00
|
|
|
if mc.apiserverClient == nil {
|
2015-03-23 19:17:12 +00:00
|
|
|
return nil
|
|
|
|
}
|
2015-10-06 21:43:23 +00:00
|
|
|
// Make a copy of the pod.
|
|
|
|
copyPod := *pod
|
|
|
|
copyPod.Annotations = make(map[string]string)
|
2015-03-23 19:17:12 +00:00
|
|
|
|
2015-10-06 21:43:23 +00:00
|
|
|
for k, v := range pod.Annotations {
|
|
|
|
copyPod.Annotations[k] = v
|
|
|
|
}
|
2016-01-21 19:55:37 +00:00
|
|
|
hash := getPodHash(pod)
|
|
|
|
copyPod.Annotations[kubetypes.ConfigMirrorAnnotationKey] = hash
|
|
|
|
apiPod, err := mc.apiserverClient.Pods(copyPod.Namespace).Create(©Pod)
|
|
|
|
if err != nil && errors.IsAlreadyExists(err) {
|
|
|
|
// Check if the existing pod is the same as the pod we want to create.
|
|
|
|
if h, ok := apiPod.Annotations[kubetypes.ConfigMirrorAnnotationKey]; ok && h == hash {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
2015-03-23 19:17:12 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Deletes a mirror pod.
|
2015-04-20 03:26:07 +00:00
|
|
|
func (mc *basicMirrorClient) DeleteMirrorPod(podFullName string) error {
|
|
|
|
if mc.apiserverClient == nil {
|
2015-03-23 19:17:12 +00:00
|
|
|
return nil
|
|
|
|
}
|
2015-03-23 17:14:30 +00:00
|
|
|
name, namespace, err := kubecontainer.ParsePodFullName(podFullName)
|
2015-03-23 19:17:12 +00:00
|
|
|
if err != nil {
|
|
|
|
glog.Errorf("Failed to parse a pod full name %q", podFullName)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
glog.V(4).Infof("Deleting a mirror pod %q", podFullName)
|
2016-01-21 19:55:37 +00:00
|
|
|
if err := mc.apiserverClient.Pods(namespace).Delete(name, api.NewDeleteOptions(0)); err != nil && !errors.IsNotFound(err) {
|
2015-03-23 19:17:12 +00:00
|
|
|
glog.Errorf("Failed deleting a mirror pod %q: %v", podFullName, err)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-10-12 23:28:23 +00:00
|
|
|
func IsStaticPod(pod *api.Pod) bool {
|
|
|
|
source, err := kubetypes.GetPodSource(pod)
|
2015-10-09 17:24:31 +00:00
|
|
|
return err == nil && source != kubetypes.ApiserverSource
|
2015-03-23 19:17:12 +00:00
|
|
|
}
|
|
|
|
|
2015-10-12 23:28:23 +00:00
|
|
|
func IsMirrorPod(pod *api.Pod) bool {
|
2015-10-20 20:51:54 +00:00
|
|
|
_, ok := pod.Annotations[kubetypes.ConfigMirrorAnnotationKey]
|
|
|
|
return ok
|
|
|
|
}
|
|
|
|
|
|
|
|
func getHashFromMirrorPod(pod *api.Pod) (string, bool) {
|
|
|
|
hash, ok := pod.Annotations[kubetypes.ConfigMirrorAnnotationKey]
|
|
|
|
return hash, ok
|
|
|
|
}
|
|
|
|
|
|
|
|
func getPodHash(pod *api.Pod) string {
|
|
|
|
// The annotation exists for all static pods.
|
|
|
|
return pod.Annotations[kubetypes.ConfigHashAnnotationKey]
|
2015-03-23 19:17:12 +00:00
|
|
|
}
|