Kubelet: refactor pod manager

This change cleans up the pod manager extensively so that
 * Mirror pods are actually stored in the pod manager.
 * Both (non-mirror) pods and mirror pods are indexed by UID and full name for
   easy lookup and mapping. This is required for the next change to send
   full pod along with the pod status update.

This change also renames mirrorManager as mirrorClient since it is merely a
client to contact the API server and create/delete mirror pods.
This commit is contained in:
Yu-Ju Hong
2015-03-23 12:17:12 -07:00
parent ed68c8e82b
commit 08e4a883b6
9 changed files with 374 additions and 375 deletions

View File

@@ -16,10 +16,84 @@ limitations under the License.
package kubelet
// Stub out mirror manager for testing purpose.
func newFakePodManager() (*basicPodManager, *fakeMirrorManager) {
import (
"reflect"
"testing"
"github.com/GoogleCloudPlatform/kubernetes/pkg/api"
)
// Stub out mirror client for testing purpose.
func newFakePodManager() (*basicPodManager, *fakeMirrorClient) {
podManager := newBasicPodManager(nil)
fakeMirrorManager := newFakeMirrorMananger()
podManager.mirrorManager = fakeMirrorManager
return podManager, fakeMirrorManager
fakeMirrorClient := newFakeMirrorClient()
podManager.mirrorClient = fakeMirrorClient
return podManager, fakeMirrorClient
}
// Tests that pods/maps are properly set after the pod update, and the basic
// methods work correctly.
func TestGetSetPods(t *testing.T) {
mirrorPod := api.Pod{
ObjectMeta: api.ObjectMeta{
UID: "987654321",
Name: "bar",
Namespace: "default",
Annotations: map[string]string{
ConfigSourceAnnotationKey: "api",
ConfigMirrorAnnotationKey: "mirror",
},
},
}
staticPod := api.Pod{
ObjectMeta: api.ObjectMeta{
UID: "123456789",
Name: "bar",
Namespace: "default",
Annotations: map[string]string{ConfigSourceAnnotationKey: "file"},
},
}
expectedPods := []api.Pod{
{
ObjectMeta: api.ObjectMeta{
UID: "999999999",
Name: "taco",
Namespace: "default",
Annotations: map[string]string{ConfigSourceAnnotationKey: "api"},
},
},
staticPod,
}
updates := append(expectedPods, mirrorPod)
podManager, _ := newFakePodManager()
podManager.SetPods(updates)
actualPods := podManager.GetPods()
if !reflect.DeepEqual(expectedPods, actualPods) {
t.Errorf("pods are not set correctly; expected %#v, got %#v", expectedPods, actualPods)
}
actualPod, ok := podManager.mirrorPodByUID[mirrorPod.UID]
if !ok {
t.Errorf("mirror pod %q is not found in the mirror pod map by UID", mirrorPod.UID)
} else if !reflect.DeepEqual(&mirrorPod, actualPod) {
t.Errorf("mirror pod is recorded incorrectly. expect: %v, got: %v", mirrorPod, actualPod)
}
actualPod, ok = podManager.mirrorPodByFullName[GetPodFullName(&mirrorPod)]
if !ok {
t.Errorf("mirror pod %q is not found in the mirror pod map by full name", GetPodFullName(&mirrorPod))
} else if !reflect.DeepEqual(&mirrorPod, actualPod) {
t.Errorf("mirror pod is recorded incorrectly. expect: %v, got: %v", mirrorPod, actualPod)
}
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)
}
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)
}
}