forked from barak/tarpoon
Add glide.yaml and vendor deps
This commit is contained in:
parent
db918f12ad
commit
5b3d5e81bd
18880 changed files with 5166045 additions and 1 deletions
45
vendor/k8s.io/kubernetes/pkg/kubelet/pod/BUILD
generated
vendored
Normal file
45
vendor/k8s.io/kubernetes/pkg/kubelet/pod/BUILD
generated
vendored
Normal file
|
|
@ -0,0 +1,45 @@
|
|||
package(default_visibility = ["//visibility:public"])
|
||||
|
||||
licenses(["notice"])
|
||||
|
||||
load(
|
||||
"@io_bazel_rules_go//go:def.bzl",
|
||||
"go_binary",
|
||||
"go_library",
|
||||
"go_test",
|
||||
"cgo_library",
|
||||
)
|
||||
|
||||
go_library(
|
||||
name = "go_default_library",
|
||||
srcs = [
|
||||
"mirror_client.go",
|
||||
"pod_manager.go",
|
||||
],
|
||||
tags = ["automanaged"],
|
||||
deps = [
|
||||
"//pkg/api/errors:go_default_library",
|
||||
"//pkg/api/v1:go_default_library",
|
||||
"//pkg/client/clientset_generated/release_1_5:go_default_library",
|
||||
"//pkg/kubelet/container:go_default_library",
|
||||
"//pkg/kubelet/types:go_default_library",
|
||||
"//pkg/types:go_default_library",
|
||||
"//vendor:github.com/golang/glog",
|
||||
],
|
||||
)
|
||||
|
||||
go_test(
|
||||
name = "go_default_test",
|
||||
srcs = [
|
||||
"mirror_client_test.go",
|
||||
"pod_manager_test.go",
|
||||
],
|
||||
library = "go_default_library",
|
||||
tags = ["automanaged"],
|
||||
deps = [
|
||||
"//pkg/api/v1:go_default_library",
|
||||
"//pkg/kubelet/container:go_default_library",
|
||||
"//pkg/kubelet/pod/testing:go_default_library",
|
||||
"//pkg/kubelet/types:go_default_library",
|
||||
],
|
||||
)
|
||||
110
vendor/k8s.io/kubernetes/pkg/kubelet/pod/mirror_client.go
generated
vendored
Normal file
110
vendor/k8s.io/kubernetes/pkg/kubelet/pod/mirror_client.go
generated
vendored
Normal file
|
|
@ -0,0 +1,110 @@
|
|||
/*
|
||||
Copyright 2015 The Kubernetes Authors.
|
||||
|
||||
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.
|
||||
*/
|
||||
|
||||
package pod
|
||||
|
||||
import (
|
||||
"github.com/golang/glog"
|
||||
"k8s.io/kubernetes/pkg/api/errors"
|
||||
"k8s.io/kubernetes/pkg/api/v1"
|
||||
clientset "k8s.io/kubernetes/pkg/client/clientset_generated/release_1_5"
|
||||
kubecontainer "k8s.io/kubernetes/pkg/kubelet/container"
|
||||
kubetypes "k8s.io/kubernetes/pkg/kubelet/types"
|
||||
)
|
||||
|
||||
// MirrorClient knows how to create/delete a mirror pod in the API server.
|
||||
type MirrorClient interface {
|
||||
// CreateMirrorPod creates a mirror pod in the API server for the given
|
||||
// pod or returns an error. The mirror pod will have the same annotations
|
||||
// as the given pod as well as an extra annotation containing the hash of
|
||||
// the static pod.
|
||||
CreateMirrorPod(pod *v1.Pod) error
|
||||
// DeleteMirrorPod deletes the mirror pod with the given full name from
|
||||
// the API server or returns an error.
|
||||
DeleteMirrorPod(podFullName string) error
|
||||
}
|
||||
|
||||
// basicMirrorClient is a functional MirrorClient. Mirror pods are stored in
|
||||
// the kubelet directly because they need to be in sync with the internal
|
||||
// pods.
|
||||
type basicMirrorClient struct {
|
||||
apiserverClient clientset.Interface
|
||||
}
|
||||
|
||||
// NewBasicMirrorClient returns a new MirrorClient.
|
||||
func NewBasicMirrorClient(apiserverClient clientset.Interface) MirrorClient {
|
||||
return &basicMirrorClient{apiserverClient: apiserverClient}
|
||||
}
|
||||
|
||||
func (mc *basicMirrorClient) CreateMirrorPod(pod *v1.Pod) error {
|
||||
if mc.apiserverClient == nil {
|
||||
return nil
|
||||
}
|
||||
// Make a copy of the pod.
|
||||
copyPod := *pod
|
||||
copyPod.Annotations = make(map[string]string)
|
||||
|
||||
for k, v := range pod.Annotations {
|
||||
copyPod.Annotations[k] = v
|
||||
}
|
||||
hash := getPodHash(pod)
|
||||
copyPod.Annotations[kubetypes.ConfigMirrorAnnotationKey] = hash
|
||||
apiPod, err := mc.apiserverClient.Core().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
|
||||
}
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
func (mc *basicMirrorClient) DeleteMirrorPod(podFullName string) error {
|
||||
if mc.apiserverClient == nil {
|
||||
return nil
|
||||
}
|
||||
name, namespace, err := kubecontainer.ParsePodFullName(podFullName)
|
||||
if err != nil {
|
||||
glog.Errorf("Failed to parse a pod full name %q", podFullName)
|
||||
return err
|
||||
}
|
||||
glog.V(2).Infof("Deleting a mirror pod %q", podFullName)
|
||||
// TODO(random-liu): Delete the mirror pod with uid precondition in mirror pod manager
|
||||
if err := mc.apiserverClient.Core().Pods(namespace).Delete(name, v1.NewDeleteOptions(0)); err != nil && !errors.IsNotFound(err) {
|
||||
glog.Errorf("Failed deleting a mirror pod %q: %v", podFullName, err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func IsStaticPod(pod *v1.Pod) bool {
|
||||
source, err := kubetypes.GetPodSource(pod)
|
||||
return err == nil && source != kubetypes.ApiserverSource
|
||||
}
|
||||
|
||||
func IsMirrorPod(pod *v1.Pod) bool {
|
||||
_, ok := pod.Annotations[kubetypes.ConfigMirrorAnnotationKey]
|
||||
return ok
|
||||
}
|
||||
|
||||
func getHashFromMirrorPod(pod *v1.Pod) (string, bool) {
|
||||
hash, ok := pod.Annotations[kubetypes.ConfigMirrorAnnotationKey]
|
||||
return hash, ok
|
||||
}
|
||||
|
||||
func getPodHash(pod *v1.Pod) string {
|
||||
// The annotation exists for all static pods.
|
||||
return pod.Annotations[kubetypes.ConfigHashAnnotationKey]
|
||||
}
|
||||
54
vendor/k8s.io/kubernetes/pkg/kubelet/pod/mirror_client_test.go
generated
vendored
Normal file
54
vendor/k8s.io/kubernetes/pkg/kubelet/pod/mirror_client_test.go
generated
vendored
Normal file
|
|
@ -0,0 +1,54 @@
|
|||
/*
|
||||
Copyright 2015 The Kubernetes Authors.
|
||||
|
||||
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.
|
||||
*/
|
||||
|
||||
package pod
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
kubecontainer "k8s.io/kubernetes/pkg/kubelet/container"
|
||||
)
|
||||
|
||||
func TestParsePodFullName(t *testing.T) {
|
||||
type nameTuple struct {
|
||||
Name string
|
||||
Namespace string
|
||||
}
|
||||
successfulCases := map[string]nameTuple{
|
||||
"bar_foo": {Name: "bar", Namespace: "foo"},
|
||||
"bar.org_foo.com": {Name: "bar.org", Namespace: "foo.com"},
|
||||
"bar-bar_foo": {Name: "bar-bar", Namespace: "foo"},
|
||||
}
|
||||
failedCases := []string{"barfoo", "bar_foo_foo", ""}
|
||||
|
||||
for podFullName, expected := range successfulCases {
|
||||
name, namespace, err := kubecontainer.ParsePodFullName(podFullName)
|
||||
if err != nil {
|
||||
t.Errorf("unexpected error when parsing the full name: %v", err)
|
||||
continue
|
||||
}
|
||||
if name != expected.Name || namespace != expected.Namespace {
|
||||
t.Errorf("expected name %q, namespace %q; got name %q, namespace %q",
|
||||
expected.Name, expected.Namespace, name, namespace)
|
||||
}
|
||||
}
|
||||
for _, podFullName := range failedCases {
|
||||
_, _, err := kubecontainer.ParsePodFullName(podFullName)
|
||||
if err == nil {
|
||||
t.Errorf("expected error when parsing the full name, got none")
|
||||
}
|
||||
}
|
||||
}
|
||||
308
vendor/k8s.io/kubernetes/pkg/kubelet/pod/pod_manager.go
generated
vendored
Normal file
308
vendor/k8s.io/kubernetes/pkg/kubelet/pod/pod_manager.go
generated
vendored
Normal file
|
|
@ -0,0 +1,308 @@
|
|||
/*
|
||||
Copyright 2015 The Kubernetes Authors.
|
||||
|
||||
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.
|
||||
*/
|
||||
|
||||
package pod
|
||||
|
||||
import (
|
||||
"sync"
|
||||
|
||||
"k8s.io/kubernetes/pkg/api/v1"
|
||||
kubecontainer "k8s.io/kubernetes/pkg/kubelet/container"
|
||||
"k8s.io/kubernetes/pkg/types"
|
||||
)
|
||||
|
||||
// Manager stores and manages access to pods, maintaining the mappings
|
||||
// between static pods and mirror pods.
|
||||
//
|
||||
// The kubelet discovers pod updates from 3 sources: file, http, and
|
||||
// apiserver. Pods from non-apiserver sources are called static pods, and API
|
||||
// server is not aware of the existence of static pods. In order to monitor
|
||||
// the status of such pods, the kubelet creates a mirror pod for each static
|
||||
// pod via the API server.
|
||||
//
|
||||
// A mirror pod has the same pod full name (name and namespace) as its static
|
||||
// counterpart (albeit different metadata such as UID, etc). By leveraging the
|
||||
// fact that the kubelet reports the pod status using the pod full name, the
|
||||
// status of the mirror pod always reflects the actual status of the static
|
||||
// pod. When a static pod gets deleted, the associated orphaned mirror pod
|
||||
// will also be removed.
|
||||
type Manager interface {
|
||||
// GetPods returns the regular pods bound to the kubelet and their spec.
|
||||
GetPods() []*v1.Pod
|
||||
// GetPodByName returns the (non-mirror) pod that matches full name, as well as
|
||||
// whether the pod was found.
|
||||
GetPodByFullName(podFullName string) (*v1.Pod, bool)
|
||||
// GetPodByName provides the (non-mirror) pod that matches namespace and
|
||||
// name, as well as whether the pod was found.
|
||||
GetPodByName(namespace, name string) (*v1.Pod, bool)
|
||||
// GetPodByUID provides the (non-mirror) pod that matches pod UID, as well as
|
||||
// whether the pod is found.
|
||||
GetPodByUID(types.UID) (*v1.Pod, bool)
|
||||
// GetPodByMirrorPod returns the static pod for the given mirror pod and
|
||||
// whether it was known to the pod manger.
|
||||
GetPodByMirrorPod(*v1.Pod) (*v1.Pod, bool)
|
||||
// GetMirrorPodByPod returns the mirror pod for the given static pod and
|
||||
// whether it was known to the pod manager.
|
||||
GetMirrorPodByPod(*v1.Pod) (*v1.Pod, bool)
|
||||
// GetPodsAndMirrorPods returns the both regular and mirror pods.
|
||||
GetPodsAndMirrorPods() ([]*v1.Pod, []*v1.Pod)
|
||||
// SetPods replaces the internal pods with the new pods.
|
||||
// It is currently only used for testing.
|
||||
SetPods(pods []*v1.Pod)
|
||||
// AddPod adds the given pod to the manager.
|
||||
AddPod(pod *v1.Pod)
|
||||
// UpdatePod updates the given pod in the manager.
|
||||
UpdatePod(pod *v1.Pod)
|
||||
// DeletePod deletes the given pod from the manager. For mirror pods,
|
||||
// this means deleting the mappings related to mirror pods. For non-
|
||||
// mirror pods, this means deleting from indexes for all non-mirror pods.
|
||||
DeletePod(pod *v1.Pod)
|
||||
// DeleteOrphanedMirrorPods deletes all mirror pods which do not have
|
||||
// associated static pods. This method sends deletion requests to the API
|
||||
// server, but does NOT modify the internal pod storage in basicManager.
|
||||
DeleteOrphanedMirrorPods()
|
||||
// TranslatePodUID returns the actual UID of a pod. If the UID belongs to
|
||||
// a mirror pod, returns the UID of its static pod. Otherwise, returns the
|
||||
// original UID.
|
||||
//
|
||||
// All public-facing functions should perform this translation for UIDs
|
||||
// because user may provide a mirror pod UID, which is not recognized by
|
||||
// internal Kubelet functions.
|
||||
TranslatePodUID(uid types.UID) types.UID
|
||||
// GetUIDTranslations returns the mappings of static pod UIDs to mirror pod
|
||||
// UIDs and mirror pod UIDs to static pod UIDs.
|
||||
GetUIDTranslations() (podToMirror, mirrorToPod map[types.UID]types.UID)
|
||||
// IsMirrorPodOf returns true if mirrorPod is a correct representation of
|
||||
// pod; false otherwise.
|
||||
IsMirrorPodOf(mirrorPod, pod *v1.Pod) bool
|
||||
|
||||
MirrorClient
|
||||
}
|
||||
|
||||
// basicManager is a functional Manger.
|
||||
//
|
||||
// All fields in basicManager are read-only and are updated calling SetPods,
|
||||
// AddPod, UpdatePod, or DeletePod.
|
||||
type basicManager struct {
|
||||
// Protects all internal maps.
|
||||
lock sync.RWMutex
|
||||
|
||||
// Regular pods indexed by UID.
|
||||
podByUID map[types.UID]*v1.Pod
|
||||
// Mirror pods indexed by UID.
|
||||
mirrorPodByUID map[types.UID]*v1.Pod
|
||||
|
||||
// Pods indexed by full name for easy access.
|
||||
podByFullName map[string]*v1.Pod
|
||||
mirrorPodByFullName map[string]*v1.Pod
|
||||
|
||||
// Mirror pod UID to pod UID map.
|
||||
translationByUID map[types.UID]types.UID
|
||||
|
||||
// A mirror pod client to create/delete mirror pods.
|
||||
MirrorClient
|
||||
}
|
||||
|
||||
// NewBasicPodManager returns a functional Manager.
|
||||
func NewBasicPodManager(client MirrorClient) Manager {
|
||||
pm := &basicManager{}
|
||||
pm.MirrorClient = client
|
||||
pm.SetPods(nil)
|
||||
return pm
|
||||
}
|
||||
|
||||
// Set the internal pods based on the new pods.
|
||||
func (pm *basicManager) SetPods(newPods []*v1.Pod) {
|
||||
pm.lock.Lock()
|
||||
defer pm.lock.Unlock()
|
||||
|
||||
pm.podByUID = make(map[types.UID]*v1.Pod)
|
||||
pm.podByFullName = make(map[string]*v1.Pod)
|
||||
pm.mirrorPodByUID = make(map[types.UID]*v1.Pod)
|
||||
pm.mirrorPodByFullName = make(map[string]*v1.Pod)
|
||||
pm.translationByUID = make(map[types.UID]types.UID)
|
||||
|
||||
pm.updatePodsInternal(newPods...)
|
||||
}
|
||||
|
||||
func (pm *basicManager) AddPod(pod *v1.Pod) {
|
||||
pm.UpdatePod(pod)
|
||||
}
|
||||
|
||||
func (pm *basicManager) UpdatePod(pod *v1.Pod) {
|
||||
pm.lock.Lock()
|
||||
defer pm.lock.Unlock()
|
||||
pm.updatePodsInternal(pod)
|
||||
}
|
||||
|
||||
// updatePodsInternal replaces the given pods in the current state of the
|
||||
// manager, updating the various indices. The caller is assumed to hold the
|
||||
// lock.
|
||||
func (pm *basicManager) updatePodsInternal(pods ...*v1.Pod) {
|
||||
for _, pod := range pods {
|
||||
podFullName := kubecontainer.GetPodFullName(pod)
|
||||
if IsMirrorPod(pod) {
|
||||
pm.mirrorPodByUID[pod.UID] = pod
|
||||
pm.mirrorPodByFullName[podFullName] = pod
|
||||
if p, ok := pm.podByFullName[podFullName]; ok {
|
||||
pm.translationByUID[pod.UID] = p.UID
|
||||
}
|
||||
} else {
|
||||
pm.podByUID[pod.UID] = pod
|
||||
pm.podByFullName[podFullName] = pod
|
||||
if mirror, ok := pm.mirrorPodByFullName[podFullName]; ok {
|
||||
pm.translationByUID[mirror.UID] = pod.UID
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (pm *basicManager) DeletePod(pod *v1.Pod) {
|
||||
pm.lock.Lock()
|
||||
defer pm.lock.Unlock()
|
||||
podFullName := kubecontainer.GetPodFullName(pod)
|
||||
if IsMirrorPod(pod) {
|
||||
delete(pm.mirrorPodByUID, pod.UID)
|
||||
delete(pm.mirrorPodByFullName, podFullName)
|
||||
delete(pm.translationByUID, pod.UID)
|
||||
} else {
|
||||
delete(pm.podByUID, pod.UID)
|
||||
delete(pm.podByFullName, podFullName)
|
||||
}
|
||||
}
|
||||
|
||||
func (pm *basicManager) GetPods() []*v1.Pod {
|
||||
pm.lock.RLock()
|
||||
defer pm.lock.RUnlock()
|
||||
return podsMapToPods(pm.podByUID)
|
||||
}
|
||||
|
||||
func (pm *basicManager) GetPodsAndMirrorPods() ([]*v1.Pod, []*v1.Pod) {
|
||||
pm.lock.RLock()
|
||||
defer pm.lock.RUnlock()
|
||||
pods := podsMapToPods(pm.podByUID)
|
||||
mirrorPods := podsMapToPods(pm.mirrorPodByUID)
|
||||
return pods, mirrorPods
|
||||
}
|
||||
|
||||
func (pm *basicManager) GetPodByUID(uid types.UID) (*v1.Pod, bool) {
|
||||
pm.lock.RLock()
|
||||
defer pm.lock.RUnlock()
|
||||
pod, ok := pm.podByUID[uid]
|
||||
return pod, ok
|
||||
}
|
||||
|
||||
func (pm *basicManager) GetPodByName(namespace, name string) (*v1.Pod, bool) {
|
||||
podFullName := kubecontainer.BuildPodFullName(name, namespace)
|
||||
return pm.GetPodByFullName(podFullName)
|
||||
}
|
||||
|
||||
func (pm *basicManager) GetPodByFullName(podFullName string) (*v1.Pod, bool) {
|
||||
pm.lock.RLock()
|
||||
defer pm.lock.RUnlock()
|
||||
pod, ok := pm.podByFullName[podFullName]
|
||||
return pod, ok
|
||||
}
|
||||
|
||||
func (pm *basicManager) TranslatePodUID(uid types.UID) types.UID {
|
||||
if uid == "" {
|
||||
return uid
|
||||
}
|
||||
|
||||
pm.lock.RLock()
|
||||
defer pm.lock.RUnlock()
|
||||
if translated, ok := pm.translationByUID[uid]; ok {
|
||||
return translated
|
||||
}
|
||||
return uid
|
||||
}
|
||||
|
||||
func (pm *basicManager) GetUIDTranslations() (podToMirror, mirrorToPod map[types.UID]types.UID) {
|
||||
pm.lock.RLock()
|
||||
defer pm.lock.RUnlock()
|
||||
|
||||
podToMirror = make(map[types.UID]types.UID, len(pm.translationByUID))
|
||||
mirrorToPod = make(map[types.UID]types.UID, len(pm.translationByUID))
|
||||
// Insert empty translation mapping for all static pods.
|
||||
for uid, pod := range pm.podByUID {
|
||||
if !IsStaticPod(pod) {
|
||||
continue
|
||||
}
|
||||
podToMirror[uid] = ""
|
||||
}
|
||||
// Fill in translations. Notice that if there is no mirror pod for a
|
||||
// static pod, its uid will be translated into empty string "". This
|
||||
// is WAI, from the caller side we can know that the static pod doesn't
|
||||
// have a corresponding mirror pod instead of using static pod uid directly.
|
||||
for k, v := range pm.translationByUID {
|
||||
mirrorToPod[k] = v
|
||||
podToMirror[v] = k
|
||||
}
|
||||
return podToMirror, mirrorToPod
|
||||
}
|
||||
|
||||
func (pm *basicManager) getOrphanedMirrorPodNames() []string {
|
||||
pm.lock.RLock()
|
||||
defer pm.lock.RUnlock()
|
||||
var podFullNames []string
|
||||
for podFullName := range pm.mirrorPodByFullName {
|
||||
if _, ok := pm.podByFullName[podFullName]; !ok {
|
||||
podFullNames = append(podFullNames, podFullName)
|
||||
}
|
||||
}
|
||||
return podFullNames
|
||||
}
|
||||
|
||||
func (pm *basicManager) DeleteOrphanedMirrorPods() {
|
||||
podFullNames := pm.getOrphanedMirrorPodNames()
|
||||
for _, podFullName := range podFullNames {
|
||||
pm.MirrorClient.DeleteMirrorPod(podFullName)
|
||||
}
|
||||
}
|
||||
|
||||
func (pm *basicManager) IsMirrorPodOf(mirrorPod, pod *v1.Pod) bool {
|
||||
// Check name and namespace first.
|
||||
if pod.Name != mirrorPod.Name || pod.Namespace != mirrorPod.Namespace {
|
||||
return false
|
||||
}
|
||||
hash, ok := getHashFromMirrorPod(mirrorPod)
|
||||
if !ok {
|
||||
return false
|
||||
}
|
||||
return hash == getPodHash(pod)
|
||||
}
|
||||
|
||||
func podsMapToPods(UIDMap map[types.UID]*v1.Pod) []*v1.Pod {
|
||||
pods := make([]*v1.Pod, 0, len(UIDMap))
|
||||
for _, pod := range UIDMap {
|
||||
pods = append(pods, pod)
|
||||
}
|
||||
return pods
|
||||
}
|
||||
|
||||
func (pm *basicManager) GetMirrorPodByPod(pod *v1.Pod) (*v1.Pod, bool) {
|
||||
pm.lock.RLock()
|
||||
defer pm.lock.RUnlock()
|
||||
mirrorPod, ok := pm.mirrorPodByFullName[kubecontainer.GetPodFullName(pod)]
|
||||
return mirrorPod, ok
|
||||
}
|
||||
|
||||
func (pm *basicManager) GetPodByMirrorPod(mirrorPod *v1.Pod) (*v1.Pod, bool) {
|
||||
pm.lock.RLock()
|
||||
defer pm.lock.RUnlock()
|
||||
pod, ok := pm.podByFullName[kubecontainer.GetPodFullName(mirrorPod)]
|
||||
return pod, ok
|
||||
}
|
||||
110
vendor/k8s.io/kubernetes/pkg/kubelet/pod/pod_manager_test.go
generated
vendored
Normal file
110
vendor/k8s.io/kubernetes/pkg/kubelet/pod/pod_manager_test.go
generated
vendored
Normal file
|
|
@ -0,0 +1,110 @@
|
|||
/*
|
||||
Copyright 2015 The Kubernetes Authors.
|
||||
|
||||
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.
|
||||
*/
|
||||
|
||||
package pod
|
||||
|
||||
import (
|
||||
"reflect"
|
||||
"testing"
|
||||
|
||||
"k8s.io/kubernetes/pkg/api/v1"
|
||||
podtest "k8s.io/kubernetes/pkg/kubelet/pod/testing"
|
||||
kubetypes "k8s.io/kubernetes/pkg/kubelet/types"
|
||||
)
|
||||
|
||||
// Stub out mirror client for testing purpose.
|
||||
func newTestManager() (*basicManager, *podtest.FakeMirrorClient) {
|
||||
fakeMirrorClient := podtest.NewFakeMirrorClient()
|
||||
manager := NewBasicPodManager(fakeMirrorClient).(*basicManager)
|
||||
return manager, fakeMirrorClient
|
||||
}
|
||||
|
||||
// Tests that pods/maps are properly set after the pod update, and the basic
|
||||
// methods work correctly.
|
||||
func TestGetSetPods(t *testing.T) {
|
||||
mirrorPod := &v1.Pod{
|
||||
ObjectMeta: v1.ObjectMeta{
|
||||
UID: "987654321",
|
||||
Name: "bar",
|
||||
Namespace: "default",
|
||||
Annotations: map[string]string{
|
||||
kubetypes.ConfigSourceAnnotationKey: "api",
|
||||
kubetypes.ConfigMirrorAnnotationKey: "mirror",
|
||||
},
|
||||
},
|
||||
}
|
||||
staticPod := &v1.Pod{
|
||||
ObjectMeta: v1.ObjectMeta{
|
||||
UID: "123456789",
|
||||
Name: "bar",
|
||||
Namespace: "default",
|
||||
Annotations: map[string]string{kubetypes.ConfigSourceAnnotationKey: "file"},
|
||||
},
|
||||
}
|
||||
|
||||
expectedPods := []*v1.Pod{
|
||||
{
|
||||
ObjectMeta: v1.ObjectMeta{
|
||||
UID: "999999999",
|
||||
Name: "taco",
|
||||
Namespace: "default",
|
||||
Annotations: map[string]string{kubetypes.ConfigSourceAnnotationKey: "api"},
|
||||
},
|
||||
},
|
||||
staticPod,
|
||||
}
|
||||
updates := append(expectedPods, mirrorPod)
|
||||
podManager, _ := newTestManager()
|
||||
podManager.SetPods(updates)
|
||||
|
||||
// Tests that all regular pods are recorded correctly.
|
||||
actualPods := podManager.GetPods()
|
||||
if len(actualPods) != len(expectedPods) {
|
||||
t.Errorf("expected %d pods, got %d pods; expected pods %#v, got pods %#v", len(expectedPods), len(actualPods),
|
||||
expectedPods, actualPods)
|
||||
}
|
||||
for _, expected := range expectedPods {
|
||||
found := false
|
||||
for _, actual := range actualPods {
|
||||
if actual.UID == expected.UID {
|
||||
if !reflect.DeepEqual(&expected, &actual) {
|
||||
t.Errorf("pod was recorded incorrectly. expect: %#v, got: %#v", expected, actual)
|
||||
}
|
||||
found = true
|
||||
break
|
||||
}
|
||||
}
|
||||
if !found {
|
||||
t.Errorf("pod %q was not found in %#v", expected.UID, actualPods)
|
||||
}
|
||||
}
|
||||
// Tests UID translation works as expected.
|
||||
if uid := podManager.TranslatePodUID(mirrorPod.UID); uid != staticPod.UID {
|
||||
t.Errorf("unable to translate UID %q to the static POD's UID %q; %#v",
|
||||
mirrorPod.UID, staticPod.UID, podManager.mirrorPodByUID)
|
||||
}
|
||||
|
||||
// Test the basic Get methods.
|
||||
actualPod, ok := podManager.GetPodByFullName("bar_default")
|
||||
if !ok || !reflect.DeepEqual(actualPod, staticPod) {
|
||||
t.Errorf("unable to get pod by full name; expected: %#v, got: %#v", staticPod, actualPod)
|
||||
}
|
||||
actualPod, ok = podManager.GetPodByName("default", "bar")
|
||||
if !ok || !reflect.DeepEqual(actualPod, staticPod) {
|
||||
t.Errorf("unable to get pod by name; expected: %#v, got: %#v", staticPod, actualPod)
|
||||
}
|
||||
|
||||
}
|
||||
22
vendor/k8s.io/kubernetes/pkg/kubelet/pod/testing/BUILD
generated
vendored
Normal file
22
vendor/k8s.io/kubernetes/pkg/kubelet/pod/testing/BUILD
generated
vendored
Normal file
|
|
@ -0,0 +1,22 @@
|
|||
package(default_visibility = ["//visibility:public"])
|
||||
|
||||
licenses(["notice"])
|
||||
|
||||
load(
|
||||
"@io_bazel_rules_go//go:def.bzl",
|
||||
"go_binary",
|
||||
"go_library",
|
||||
"go_test",
|
||||
"cgo_library",
|
||||
)
|
||||
|
||||
go_library(
|
||||
name = "go_default_library",
|
||||
srcs = ["fake_mirror_client.go"],
|
||||
tags = ["automanaged"],
|
||||
deps = [
|
||||
"//pkg/api/v1:go_default_library",
|
||||
"//pkg/kubelet/container:go_default_library",
|
||||
"//pkg/util/sets:go_default_library",
|
||||
],
|
||||
)
|
||||
83
vendor/k8s.io/kubernetes/pkg/kubelet/pod/testing/fake_mirror_client.go
generated
vendored
Normal file
83
vendor/k8s.io/kubernetes/pkg/kubelet/pod/testing/fake_mirror_client.go
generated
vendored
Normal file
|
|
@ -0,0 +1,83 @@
|
|||
/*
|
||||
Copyright 2015 The Kubernetes Authors.
|
||||
|
||||
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.
|
||||
*/
|
||||
|
||||
package testing
|
||||
|
||||
import (
|
||||
"sync"
|
||||
|
||||
"k8s.io/kubernetes/pkg/api/v1"
|
||||
kubecontainer "k8s.io/kubernetes/pkg/kubelet/container"
|
||||
"k8s.io/kubernetes/pkg/util/sets"
|
||||
)
|
||||
|
||||
type FakeMirrorClient struct {
|
||||
mirrorPodLock sync.RWMutex
|
||||
// Note that a real mirror manager does not store the mirror pods in
|
||||
// itself. This fake manager does this to track calls.
|
||||
mirrorPods sets.String
|
||||
createCounts map[string]int
|
||||
deleteCounts map[string]int
|
||||
}
|
||||
|
||||
func NewFakeMirrorClient() *FakeMirrorClient {
|
||||
m := FakeMirrorClient{}
|
||||
m.mirrorPods = sets.NewString()
|
||||
m.createCounts = make(map[string]int)
|
||||
m.deleteCounts = make(map[string]int)
|
||||
return &m
|
||||
}
|
||||
|
||||
func (fmc *FakeMirrorClient) CreateMirrorPod(pod *v1.Pod) error {
|
||||
fmc.mirrorPodLock.Lock()
|
||||
defer fmc.mirrorPodLock.Unlock()
|
||||
podFullName := kubecontainer.GetPodFullName(pod)
|
||||
fmc.mirrorPods.Insert(podFullName)
|
||||
fmc.createCounts[podFullName]++
|
||||
return nil
|
||||
}
|
||||
|
||||
func (fmc *FakeMirrorClient) DeleteMirrorPod(podFullName string) error {
|
||||
fmc.mirrorPodLock.Lock()
|
||||
defer fmc.mirrorPodLock.Unlock()
|
||||
fmc.mirrorPods.Delete(podFullName)
|
||||
fmc.deleteCounts[podFullName]++
|
||||
return nil
|
||||
}
|
||||
|
||||
func (fmc *FakeMirrorClient) HasPod(podFullName string) bool {
|
||||
fmc.mirrorPodLock.RLock()
|
||||
defer fmc.mirrorPodLock.RUnlock()
|
||||
return fmc.mirrorPods.Has(podFullName)
|
||||
}
|
||||
|
||||
func (fmc *FakeMirrorClient) NumOfPods() int {
|
||||
fmc.mirrorPodLock.RLock()
|
||||
defer fmc.mirrorPodLock.RUnlock()
|
||||
return fmc.mirrorPods.Len()
|
||||
}
|
||||
|
||||
func (fmc *FakeMirrorClient) GetPods() []string {
|
||||
fmc.mirrorPodLock.RLock()
|
||||
defer fmc.mirrorPodLock.RUnlock()
|
||||
return fmc.mirrorPods.List()
|
||||
}
|
||||
|
||||
func (fmc *FakeMirrorClient) GetCounts(podFullName string) (int, int) {
|
||||
fmc.mirrorPodLock.RLock()
|
||||
defer fmc.mirrorPodLock.RUnlock()
|
||||
return fmc.createCounts[podFullName], fmc.deleteCounts[podFullName]
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue