mirror of
				https://github.com/optim-enterprises-bv/kubernetes.git
				synced 2025-11-04 04:08:16 +00:00 
			
		
		
		
	Write BoundPods to etcd instead of ContainerManifestList
Rename ManifestFactory -> BoundPodFactory and change the general structure of the call to focus on BoundPod.
This commit is contained in:
		
				
					committed by
					
						
						Eric Paris
					
				
			
			
				
	
			
			
			
						parent
						
							892942af8f
						
					
				
				
					commit
					6ae611aedd
				
			@@ -61,5 +61,20 @@ func init() {
 | 
				
			|||||||
			}
 | 
								}
 | 
				
			||||||
			out.ResourceVersion = in.ResourceVersion
 | 
								out.ResourceVersion = in.ResourceVersion
 | 
				
			||||||
			return nil
 | 
								return nil
 | 
				
			||||||
		})
 | 
							},
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							// Convert Pod to BoundPod
 | 
				
			||||||
 | 
							func(in *Pod, out *BoundPod, s conversion.Scope) error {
 | 
				
			||||||
 | 
								if err := s.Convert(&in.DesiredState.Manifest, out, 0); err != nil {
 | 
				
			||||||
 | 
									return err
 | 
				
			||||||
 | 
								}
 | 
				
			||||||
 | 
								// Only copy a subset of fields, and override manifest attributes with the pod
 | 
				
			||||||
 | 
								// metadata
 | 
				
			||||||
 | 
								out.UID = in.UID
 | 
				
			||||||
 | 
								out.ID = in.ID
 | 
				
			||||||
 | 
								out.Namespace = in.Namespace
 | 
				
			||||||
 | 
								out.CreationTimestamp = in.CreationTimestamp
 | 
				
			||||||
 | 
								return nil
 | 
				
			||||||
 | 
							},
 | 
				
			||||||
 | 
						)
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -33,10 +33,12 @@ import (
 | 
				
			|||||||
)
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func NewTestEtcdRegistry(client tools.EtcdClient) *etcdregistry.Registry {
 | 
					func NewTestEtcdRegistry(client tools.EtcdClient) *etcdregistry.Registry {
 | 
				
			||||||
	registry := etcdregistry.NewRegistry(tools.EtcdHelper{client, latest.Codec, tools.RuntimeVersionAdapter{latest.ResourceVersioner}},
 | 
						registry := etcdregistry.NewRegistry(
 | 
				
			||||||
		&pod.BasicManifestFactory{
 | 
							tools.EtcdHelper{client, latest.Codec, tools.RuntimeVersionAdapter{latest.ResourceVersioner}},
 | 
				
			||||||
 | 
							&pod.BasicBoundPodFactory{
 | 
				
			||||||
			ServiceRegistry: ®istrytest.ServiceRegistry{},
 | 
								ServiceRegistry: ®istrytest.ServiceRegistry{},
 | 
				
			||||||
		})
 | 
							},
 | 
				
			||||||
 | 
						)
 | 
				
			||||||
	return registry
 | 
						return registry
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -20,8 +20,8 @@ import (
 | 
				
			|||||||
	"github.com/GoogleCloudPlatform/kubernetes/pkg/api"
 | 
						"github.com/GoogleCloudPlatform/kubernetes/pkg/api"
 | 
				
			||||||
)
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// Allowed returns true if manifests is a collection of manifests
 | 
					// Allowed returns true if pods is a collection of bound pods
 | 
				
			||||||
// which can run without conflict on a single minion.
 | 
					// which can run without conflict on a single minion.
 | 
				
			||||||
func Allowed(manifests []api.ContainerManifest) bool {
 | 
					func Allowed(pods []api.BoundPod) bool {
 | 
				
			||||||
	return !PortsConflict(manifests)
 | 
						return !PortsConflict(pods)
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -30,10 +30,10 @@ func containerWithHostPorts(ports ...int) api.Container {
 | 
				
			|||||||
	return c
 | 
						return c
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func manifestWithContainers(containers ...api.Container) api.ContainerManifest {
 | 
					func podWithContainers(containers ...api.Container) api.BoundPod {
 | 
				
			||||||
	m := api.ContainerManifest{}
 | 
						m := api.BoundPod{}
 | 
				
			||||||
	for _, c := range containers {
 | 
						for _, c := range containers {
 | 
				
			||||||
		m.Containers = append(m.Containers, c)
 | 
							m.Spec.Containers = append(m.Spec.Containers, c)
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	return m
 | 
						return m
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
@@ -41,16 +41,16 @@ func manifestWithContainers(containers ...api.Container) api.ContainerManifest {
 | 
				
			|||||||
func TestAllowed(t *testing.T) {
 | 
					func TestAllowed(t *testing.T) {
 | 
				
			||||||
	table := []struct {
 | 
						table := []struct {
 | 
				
			||||||
		allowed bool
 | 
							allowed bool
 | 
				
			||||||
		manifests []api.ContainerManifest
 | 
							pods    []api.BoundPod
 | 
				
			||||||
	}{
 | 
						}{
 | 
				
			||||||
		{
 | 
							{
 | 
				
			||||||
			allowed: true,
 | 
								allowed: true,
 | 
				
			||||||
			manifests: []api.ContainerManifest{
 | 
								pods: []api.BoundPod{
 | 
				
			||||||
				manifestWithContainers(
 | 
									podWithContainers(
 | 
				
			||||||
					containerWithHostPorts(1, 2, 3),
 | 
										containerWithHostPorts(1, 2, 3),
 | 
				
			||||||
					containerWithHostPorts(4, 5, 6),
 | 
										containerWithHostPorts(4, 5, 6),
 | 
				
			||||||
				),
 | 
									),
 | 
				
			||||||
				manifestWithContainers(
 | 
									podWithContainers(
 | 
				
			||||||
					containerWithHostPorts(7, 8, 9),
 | 
										containerWithHostPorts(7, 8, 9),
 | 
				
			||||||
					containerWithHostPorts(10, 11, 12),
 | 
										containerWithHostPorts(10, 11, 12),
 | 
				
			||||||
				),
 | 
									),
 | 
				
			||||||
@@ -58,12 +58,12 @@ func TestAllowed(t *testing.T) {
 | 
				
			|||||||
		},
 | 
							},
 | 
				
			||||||
		{
 | 
							{
 | 
				
			||||||
			allowed: true,
 | 
								allowed: true,
 | 
				
			||||||
			manifests: []api.ContainerManifest{
 | 
								pods: []api.BoundPod{
 | 
				
			||||||
				manifestWithContainers(
 | 
									podWithContainers(
 | 
				
			||||||
					containerWithHostPorts(0, 0),
 | 
										containerWithHostPorts(0, 0),
 | 
				
			||||||
					containerWithHostPorts(0, 0),
 | 
										containerWithHostPorts(0, 0),
 | 
				
			||||||
				),
 | 
									),
 | 
				
			||||||
				manifestWithContainers(
 | 
									podWithContainers(
 | 
				
			||||||
					containerWithHostPorts(0, 0),
 | 
										containerWithHostPorts(0, 0),
 | 
				
			||||||
					containerWithHostPorts(0, 0),
 | 
										containerWithHostPorts(0, 0),
 | 
				
			||||||
				),
 | 
									),
 | 
				
			||||||
@@ -71,19 +71,19 @@ func TestAllowed(t *testing.T) {
 | 
				
			|||||||
		},
 | 
							},
 | 
				
			||||||
		{
 | 
							{
 | 
				
			||||||
			allowed: false,
 | 
								allowed: false,
 | 
				
			||||||
			manifests: []api.ContainerManifest{
 | 
								pods: []api.BoundPod{
 | 
				
			||||||
				manifestWithContainers(
 | 
									podWithContainers(
 | 
				
			||||||
					containerWithHostPorts(3, 3),
 | 
										containerWithHostPorts(3, 3),
 | 
				
			||||||
				),
 | 
									),
 | 
				
			||||||
			},
 | 
								},
 | 
				
			||||||
		},
 | 
							},
 | 
				
			||||||
		{
 | 
							{
 | 
				
			||||||
			allowed: false,
 | 
								allowed: false,
 | 
				
			||||||
			manifests: []api.ContainerManifest{
 | 
								pods: []api.BoundPod{
 | 
				
			||||||
				manifestWithContainers(
 | 
									podWithContainers(
 | 
				
			||||||
					containerWithHostPorts(6),
 | 
										containerWithHostPorts(6),
 | 
				
			||||||
				),
 | 
									),
 | 
				
			||||||
				manifestWithContainers(
 | 
									podWithContainers(
 | 
				
			||||||
					containerWithHostPorts(6),
 | 
										containerWithHostPorts(6),
 | 
				
			||||||
				),
 | 
									),
 | 
				
			||||||
			},
 | 
								},
 | 
				
			||||||
@@ -91,8 +91,8 @@ func TestAllowed(t *testing.T) {
 | 
				
			|||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	for _, item := range table {
 | 
						for _, item := range table {
 | 
				
			||||||
		if e, a := item.allowed, Allowed(item.manifests); e != a {
 | 
							if e, a := item.allowed, Allowed(item.pods); e != a {
 | 
				
			||||||
			t.Errorf("Expected %v, got %v: \n%v\v", e, a, item.manifests)
 | 
								t.Errorf("Expected %v, got %v: \n%v\v", e, a, item.pods)
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -22,10 +22,10 @@ import (
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
// PortsConflict returns true iff two containers attempt to expose
 | 
					// PortsConflict returns true iff two containers attempt to expose
 | 
				
			||||||
// the same host port.
 | 
					// the same host port.
 | 
				
			||||||
func PortsConflict(manifests []api.ContainerManifest) bool {
 | 
					func PortsConflict(pods []api.BoundPod) bool {
 | 
				
			||||||
	hostPorts := map[int]struct{}{}
 | 
						hostPorts := map[int]struct{}{}
 | 
				
			||||||
	for _, manifest := range manifests {
 | 
						for _, pod := range pods {
 | 
				
			||||||
		for _, container := range manifest.Containers {
 | 
							for _, container := range pod.Spec.Containers {
 | 
				
			||||||
			for _, port := range container.Ports {
 | 
								for _, port := range container.Ports {
 | 
				
			||||||
				if port.HostPort == 0 {
 | 
									if port.HostPort == 0 {
 | 
				
			||||||
					continue
 | 
										continue
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -89,15 +89,15 @@ func NewEtcdHelper(client tools.EtcdGetSet, version string) (helper tools.EtcdHe
 | 
				
			|||||||
func New(c *Config) *Master {
 | 
					func New(c *Config) *Master {
 | 
				
			||||||
	minionRegistry := makeMinionRegistry(c)
 | 
						minionRegistry := makeMinionRegistry(c)
 | 
				
			||||||
	serviceRegistry := etcd.NewRegistry(c.EtcdHelper, nil)
 | 
						serviceRegistry := etcd.NewRegistry(c.EtcdHelper, nil)
 | 
				
			||||||
	manifestFactory := &pod.BasicManifestFactory{
 | 
						boundPodFactory := &pod.BasicBoundPodFactory{
 | 
				
			||||||
		ServiceRegistry: serviceRegistry,
 | 
							ServiceRegistry: serviceRegistry,
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	m := &Master{
 | 
						m := &Master{
 | 
				
			||||||
		podRegistry:        etcd.NewRegistry(c.EtcdHelper, manifestFactory),
 | 
							podRegistry:        etcd.NewRegistry(c.EtcdHelper, boundPodFactory),
 | 
				
			||||||
		controllerRegistry: etcd.NewRegistry(c.EtcdHelper, nil),
 | 
							controllerRegistry: etcd.NewRegistry(c.EtcdHelper, nil),
 | 
				
			||||||
		serviceRegistry:    serviceRegistry,
 | 
							serviceRegistry:    serviceRegistry,
 | 
				
			||||||
		endpointRegistry:   etcd.NewRegistry(c.EtcdHelper, nil),
 | 
							endpointRegistry:   etcd.NewRegistry(c.EtcdHelper, nil),
 | 
				
			||||||
		bindingRegistry:    etcd.NewRegistry(c.EtcdHelper, manifestFactory),
 | 
							bindingRegistry:    etcd.NewRegistry(c.EtcdHelper, boundPodFactory),
 | 
				
			||||||
		eventRegistry:      event.NewEtcdRegistry(c.EtcdHelper, uint64(c.EventTTL.Seconds())),
 | 
							eventRegistry:      event.NewEtcdRegistry(c.EtcdHelper, uint64(c.EventTTL.Seconds())),
 | 
				
			||||||
		minionRegistry:     minionRegistry,
 | 
							minionRegistry:     minionRegistry,
 | 
				
			||||||
		client:             c.Client,
 | 
							client:             c.Client,
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -51,15 +51,15 @@ const (
 | 
				
			|||||||
// Registry implements PodRegistry, ControllerRegistry, ServiceRegistry and MinionRegistry, backed by etcd.
 | 
					// Registry implements PodRegistry, ControllerRegistry, ServiceRegistry and MinionRegistry, backed by etcd.
 | 
				
			||||||
type Registry struct {
 | 
					type Registry struct {
 | 
				
			||||||
	tools.EtcdHelper
 | 
						tools.EtcdHelper
 | 
				
			||||||
	manifestFactory pod.ManifestFactory
 | 
						boundPodFactory pod.BoundPodFactory
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// NewRegistry creates an etcd registry.
 | 
					// NewRegistry creates an etcd registry.
 | 
				
			||||||
func NewRegistry(helper tools.EtcdHelper, manifestFactory pod.ManifestFactory) *Registry {
 | 
					func NewRegistry(helper tools.EtcdHelper, boundPodFactory pod.BoundPodFactory) *Registry {
 | 
				
			||||||
	registry := &Registry{
 | 
						registry := &Registry{
 | 
				
			||||||
		EtcdHelper: helper,
 | 
							EtcdHelper: helper,
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	registry.manifestFactory = manifestFactory
 | 
						registry.boundPodFactory = boundPodFactory
 | 
				
			||||||
	return registry
 | 
						return registry
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -230,18 +230,18 @@ func (r *Registry) assignPod(ctx api.Context, podID string, machine string) erro
 | 
				
			|||||||
		return err
 | 
							return err
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	// TODO: move this to a watch/rectification loop.
 | 
						// TODO: move this to a watch/rectification loop.
 | 
				
			||||||
	manifest, err := r.manifestFactory.MakeManifest(machine, *finalPod)
 | 
						pod, err := r.boundPodFactory.MakeBoundPod(machine, finalPod)
 | 
				
			||||||
	if err != nil {
 | 
						if err != nil {
 | 
				
			||||||
		return err
 | 
							return err
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	contKey := makeContainerKey(machine)
 | 
						contKey := makeContainerKey(machine)
 | 
				
			||||||
	err = r.AtomicUpdate(contKey, &api.ContainerManifestList{}, func(in runtime.Object) (runtime.Object, error) {
 | 
						err = r.AtomicUpdate(contKey, &api.BoundPods{}, func(in runtime.Object) (runtime.Object, error) {
 | 
				
			||||||
		manifests := *in.(*api.ContainerManifestList)
 | 
							pods := *in.(*api.BoundPods)
 | 
				
			||||||
		manifests.Items = append(manifests.Items, manifest)
 | 
							pods.Items = append(pods.Items, *pod)
 | 
				
			||||||
		if !constraint.Allowed(manifests.Items) {
 | 
							if !constraint.Allowed(pods.Items) {
 | 
				
			||||||
			return nil, fmt.Errorf("The assignment would cause a constraint violation")
 | 
								return nil, fmt.Errorf("The assignment would cause a constraint violation")
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
		return &manifests, nil
 | 
							return &pods, nil
 | 
				
			||||||
	})
 | 
						})
 | 
				
			||||||
	if err != nil {
 | 
						if err != nil {
 | 
				
			||||||
		// Put the pod's host back the way it was. This is a terrible hack that
 | 
							// Put the pod's host back the way it was. This is a terrible hack that
 | 
				
			||||||
@@ -321,13 +321,13 @@ func (r *Registry) DeletePod(ctx api.Context, podID string) error {
 | 
				
			|||||||
	}
 | 
						}
 | 
				
			||||||
	// Next, remove the pod from the machine atomically.
 | 
						// Next, remove the pod from the machine atomically.
 | 
				
			||||||
	contKey := makeContainerKey(machine)
 | 
						contKey := makeContainerKey(machine)
 | 
				
			||||||
	return r.AtomicUpdate(contKey, &api.ContainerManifestList{}, func(in runtime.Object) (runtime.Object, error) {
 | 
						return r.AtomicUpdate(contKey, &api.BoundPods{}, func(in runtime.Object) (runtime.Object, error) {
 | 
				
			||||||
		manifests := in.(*api.ContainerManifestList)
 | 
							pods := in.(*api.BoundPods)
 | 
				
			||||||
		newManifests := make([]api.ContainerManifest, 0, len(manifests.Items))
 | 
							newPods := make([]api.BoundPod, 0, len(pods.Items))
 | 
				
			||||||
		found := false
 | 
							found := false
 | 
				
			||||||
		for _, manifest := range manifests.Items {
 | 
							for _, pod := range pods.Items {
 | 
				
			||||||
			if manifest.ID != podID {
 | 
								if pod.ID != podID {
 | 
				
			||||||
				newManifests = append(newManifests, manifest)
 | 
									newPods = append(newPods, pod)
 | 
				
			||||||
			} else {
 | 
								} else {
 | 
				
			||||||
				found = true
 | 
									found = true
 | 
				
			||||||
			}
 | 
								}
 | 
				
			||||||
@@ -336,10 +336,10 @@ func (r *Registry) DeletePod(ctx api.Context, podID string) error {
 | 
				
			|||||||
			// This really shouldn't happen, it indicates something is broken, and likely
 | 
								// This really shouldn't happen, it indicates something is broken, and likely
 | 
				
			||||||
			// there is a lost pod somewhere.
 | 
								// there is a lost pod somewhere.
 | 
				
			||||||
			// However it is "deleted" so log it and move on
 | 
								// However it is "deleted" so log it and move on
 | 
				
			||||||
			glog.Warningf("Couldn't find: %s in %#v", podID, manifests)
 | 
								glog.Warningf("Couldn't find: %s in %#v", podID, pods)
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
		manifests.Items = newManifests
 | 
							pods.Items = newPods
 | 
				
			||||||
		return manifests, nil
 | 
							return pods, nil
 | 
				
			||||||
	})
 | 
						})
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -36,7 +36,7 @@ import (
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
func NewTestEtcdRegistry(client tools.EtcdClient) *Registry {
 | 
					func NewTestEtcdRegistry(client tools.EtcdClient) *Registry {
 | 
				
			||||||
	registry := NewRegistry(tools.EtcdHelper{client, latest.Codec, tools.RuntimeVersionAdapter{latest.ResourceVersioner}},
 | 
						registry := NewRegistry(tools.EtcdHelper{client, latest.Codec, tools.RuntimeVersionAdapter{latest.ResourceVersioner}},
 | 
				
			||||||
		&pod.BasicManifestFactory{
 | 
							&pod.BasicBoundPodFactory{
 | 
				
			||||||
			ServiceRegistry: ®istrytest.ServiceRegistry{},
 | 
								ServiceRegistry: ®istrytest.ServiceRegistry{},
 | 
				
			||||||
		})
 | 
							})
 | 
				
			||||||
	return registry
 | 
						return registry
 | 
				
			||||||
@@ -160,7 +160,7 @@ func TestEtcdCreatePod(t *testing.T) {
 | 
				
			|||||||
		},
 | 
							},
 | 
				
			||||||
		E: tools.EtcdErrorNotFound,
 | 
							E: tools.EtcdErrorNotFound,
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	fakeClient.Set("/registry/hosts/machine/kubelet", runtime.EncodeOrDie(latest.Codec, &api.ContainerManifestList{}), 0)
 | 
						fakeClient.Set("/registry/hosts/machine/kubelet", runtime.EncodeOrDie(latest.Codec, &api.BoundPods{}), 0)
 | 
				
			||||||
	registry := NewTestEtcdRegistry(fakeClient)
 | 
						registry := NewTestEtcdRegistry(fakeClient)
 | 
				
			||||||
	err := registry.CreatePod(ctx, &api.Pod{
 | 
						err := registry.CreatePod(ctx, &api.Pod{
 | 
				
			||||||
		TypeMeta: api.TypeMeta{
 | 
							TypeMeta: api.TypeMeta{
 | 
				
			||||||
@@ -199,15 +199,15 @@ func TestEtcdCreatePod(t *testing.T) {
 | 
				
			|||||||
	if pod.ID != "foo" {
 | 
						if pod.ID != "foo" {
 | 
				
			||||||
		t.Errorf("Unexpected pod: %#v %s", pod, resp.Node.Value)
 | 
							t.Errorf("Unexpected pod: %#v %s", pod, resp.Node.Value)
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	var manifests api.ContainerManifestList
 | 
						var boundPods api.BoundPods
 | 
				
			||||||
	resp, err = fakeClient.Get("/registry/hosts/machine/kubelet", false, false)
 | 
						resp, err = fakeClient.Get("/registry/hosts/machine/kubelet", false, false)
 | 
				
			||||||
	if err != nil {
 | 
						if err != nil {
 | 
				
			||||||
		t.Errorf("unexpected error: %v", err)
 | 
							t.Errorf("unexpected error: %v", err)
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	err = latest.Codec.DecodeInto([]byte(resp.Node.Value), &manifests)
 | 
						err = latest.Codec.DecodeInto([]byte(resp.Node.Value), &boundPods)
 | 
				
			||||||
	if len(manifests.Items) != 1 || manifests.Items[0].ID != "foo" {
 | 
						if len(boundPods.Items) != 1 || boundPods.Items[0].ID != "foo" {
 | 
				
			||||||
		t.Errorf("Unexpected manifest list: %#v", manifests)
 | 
							t.Errorf("Unexpected boundPod list: %#v", boundPods)
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -355,15 +355,15 @@ func TestEtcdCreatePodWithContainersNotFound(t *testing.T) {
 | 
				
			|||||||
	if pod.ID != "foo" {
 | 
						if pod.ID != "foo" {
 | 
				
			||||||
		t.Errorf("Unexpected pod: %#v %s", pod, resp.Node.Value)
 | 
							t.Errorf("Unexpected pod: %#v %s", pod, resp.Node.Value)
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	var manifests api.ContainerManifestList
 | 
						var boundPods api.BoundPods
 | 
				
			||||||
	resp, err = fakeClient.Get("/registry/hosts/machine/kubelet", false, false)
 | 
						resp, err = fakeClient.Get("/registry/hosts/machine/kubelet", false, false)
 | 
				
			||||||
	if err != nil {
 | 
						if err != nil {
 | 
				
			||||||
		t.Errorf("unexpected error: %v", err)
 | 
							t.Errorf("unexpected error: %v", err)
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	err = latest.Codec.DecodeInto([]byte(resp.Node.Value), &manifests)
 | 
						err = latest.Codec.DecodeInto([]byte(resp.Node.Value), &boundPods)
 | 
				
			||||||
	if len(manifests.Items) != 1 || manifests.Items[0].ID != "foo" {
 | 
						if len(boundPods.Items) != 1 || boundPods.Items[0].ID != "foo" {
 | 
				
			||||||
		t.Errorf("Unexpected manifest list: %#v", manifests)
 | 
							t.Errorf("Unexpected boundPod list: %#v", boundPods)
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -378,9 +378,9 @@ func TestEtcdCreatePodWithExistingContainers(t *testing.T) {
 | 
				
			|||||||
		},
 | 
							},
 | 
				
			||||||
		E: tools.EtcdErrorNotFound,
 | 
							E: tools.EtcdErrorNotFound,
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	fakeClient.Set("/registry/hosts/machine/kubelet", runtime.EncodeOrDie(latest.Codec, &api.ContainerManifestList{
 | 
						fakeClient.Set("/registry/hosts/machine/kubelet", runtime.EncodeOrDie(latest.Codec, &api.BoundPods{
 | 
				
			||||||
		Items: []api.ContainerManifest{
 | 
							Items: []api.BoundPod{
 | 
				
			||||||
			{ID: "bar"},
 | 
								{TypeMeta: api.TypeMeta{ID: "bar"}},
 | 
				
			||||||
		},
 | 
							},
 | 
				
			||||||
	}), 0)
 | 
						}), 0)
 | 
				
			||||||
	registry := NewTestEtcdRegistry(fakeClient)
 | 
						registry := NewTestEtcdRegistry(fakeClient)
 | 
				
			||||||
@@ -422,15 +422,15 @@ func TestEtcdCreatePodWithExistingContainers(t *testing.T) {
 | 
				
			|||||||
	if pod.ID != "foo" {
 | 
						if pod.ID != "foo" {
 | 
				
			||||||
		t.Errorf("Unexpected pod: %#v %s", pod, resp.Node.Value)
 | 
							t.Errorf("Unexpected pod: %#v %s", pod, resp.Node.Value)
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	var manifests api.ContainerManifestList
 | 
						var boundPods api.BoundPods
 | 
				
			||||||
	resp, err = fakeClient.Get("/registry/hosts/machine/kubelet", false, false)
 | 
						resp, err = fakeClient.Get("/registry/hosts/machine/kubelet", false, false)
 | 
				
			||||||
	if err != nil {
 | 
						if err != nil {
 | 
				
			||||||
		t.Errorf("unexpected error: %v", err)
 | 
							t.Errorf("unexpected error: %v", err)
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	err = latest.Codec.DecodeInto([]byte(resp.Node.Value), &manifests)
 | 
						err = latest.Codec.DecodeInto([]byte(resp.Node.Value), &boundPods)
 | 
				
			||||||
	if len(manifests.Items) != 2 || manifests.Items[1].ID != "foo" {
 | 
						if len(boundPods.Items) != 2 || boundPods.Items[1].ID != "foo" {
 | 
				
			||||||
		t.Errorf("Unexpected manifest list: %#v", manifests)
 | 
							t.Errorf("Unexpected boundPod list: %#v", boundPods)
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -586,9 +586,9 @@ func TestEtcdDeletePod(t *testing.T) {
 | 
				
			|||||||
		TypeMeta:     api.TypeMeta{ID: "foo"},
 | 
							TypeMeta:     api.TypeMeta{ID: "foo"},
 | 
				
			||||||
		DesiredState: api.PodState{Host: "machine"},
 | 
							DesiredState: api.PodState{Host: "machine"},
 | 
				
			||||||
	}), 0)
 | 
						}), 0)
 | 
				
			||||||
	fakeClient.Set("/registry/hosts/machine/kubelet", runtime.EncodeOrDie(latest.Codec, &api.ContainerManifestList{
 | 
						fakeClient.Set("/registry/hosts/machine/kubelet", runtime.EncodeOrDie(latest.Codec, &api.BoundPods{
 | 
				
			||||||
		Items: []api.ContainerManifest{
 | 
							Items: []api.BoundPod{
 | 
				
			||||||
			{ID: "foo"},
 | 
								{TypeMeta: api.TypeMeta{ID: "foo"}},
 | 
				
			||||||
		},
 | 
							},
 | 
				
			||||||
	}), 0)
 | 
						}), 0)
 | 
				
			||||||
	registry := NewTestEtcdRegistry(fakeClient)
 | 
						registry := NewTestEtcdRegistry(fakeClient)
 | 
				
			||||||
@@ -606,9 +606,9 @@ func TestEtcdDeletePod(t *testing.T) {
 | 
				
			|||||||
	if err != nil {
 | 
						if err != nil {
 | 
				
			||||||
		t.Fatalf("Unexpected error %v", err)
 | 
							t.Fatalf("Unexpected error %v", err)
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	var manifests api.ContainerManifestList
 | 
						var boundPods api.BoundPods
 | 
				
			||||||
	latest.Codec.DecodeInto([]byte(response.Node.Value), &manifests)
 | 
						latest.Codec.DecodeInto([]byte(response.Node.Value), &boundPods)
 | 
				
			||||||
	if len(manifests.Items) != 0 {
 | 
						if len(boundPods.Items) != 0 {
 | 
				
			||||||
		t.Errorf("Unexpected container set: %s, expected empty", response.Node.Value)
 | 
							t.Errorf("Unexpected container set: %s, expected empty", response.Node.Value)
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
@@ -622,10 +622,10 @@ func TestEtcdDeletePodMultipleContainers(t *testing.T) {
 | 
				
			|||||||
		TypeMeta:     api.TypeMeta{ID: "foo"},
 | 
							TypeMeta:     api.TypeMeta{ID: "foo"},
 | 
				
			||||||
		DesiredState: api.PodState{Host: "machine"},
 | 
							DesiredState: api.PodState{Host: "machine"},
 | 
				
			||||||
	}), 0)
 | 
						}), 0)
 | 
				
			||||||
	fakeClient.Set("/registry/hosts/machine/kubelet", runtime.EncodeOrDie(latest.Codec, &api.ContainerManifestList{
 | 
						fakeClient.Set("/registry/hosts/machine/kubelet", runtime.EncodeOrDie(latest.Codec, &api.BoundPods{
 | 
				
			||||||
		Items: []api.ContainerManifest{
 | 
							Items: []api.BoundPod{
 | 
				
			||||||
			{ID: "foo"},
 | 
								{TypeMeta: api.TypeMeta{ID: "foo"}},
 | 
				
			||||||
			{ID: "bar"},
 | 
								{TypeMeta: api.TypeMeta{ID: "bar"}},
 | 
				
			||||||
		},
 | 
							},
 | 
				
			||||||
	}), 0)
 | 
						}), 0)
 | 
				
			||||||
	registry := NewTestEtcdRegistry(fakeClient)
 | 
						registry := NewTestEtcdRegistry(fakeClient)
 | 
				
			||||||
@@ -644,13 +644,13 @@ func TestEtcdDeletePodMultipleContainers(t *testing.T) {
 | 
				
			|||||||
	if err != nil {
 | 
						if err != nil {
 | 
				
			||||||
		t.Fatalf("Unexpected error %v", err)
 | 
							t.Fatalf("Unexpected error %v", err)
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	var manifests api.ContainerManifestList
 | 
						var boundPods api.BoundPods
 | 
				
			||||||
	latest.Codec.DecodeInto([]byte(response.Node.Value), &manifests)
 | 
						latest.Codec.DecodeInto([]byte(response.Node.Value), &boundPods)
 | 
				
			||||||
	if len(manifests.Items) != 1 {
 | 
						if len(boundPods.Items) != 1 {
 | 
				
			||||||
		t.Fatalf("Unexpected manifest set: %#v, expected empty", manifests)
 | 
							t.Fatalf("Unexpected boundPod set: %#v, expected empty", boundPods)
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	if manifests.Items[0].ID != "bar" {
 | 
						if boundPods.Items[0].ID != "bar" {
 | 
				
			||||||
		t.Errorf("Deleted wrong manifest: %#v", manifests)
 | 
							t.Errorf("Deleted wrong boundPod: %#v", boundPods)
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -21,24 +21,27 @@ import (
 | 
				
			|||||||
	"github.com/GoogleCloudPlatform/kubernetes/pkg/registry/service"
 | 
						"github.com/GoogleCloudPlatform/kubernetes/pkg/registry/service"
 | 
				
			||||||
)
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
type ManifestFactory interface {
 | 
					type BoundPodFactory interface {
 | 
				
			||||||
	// Make a container object for a given pod, given the machine that the pod is running on.
 | 
						// Make a container object for a given pod, given the machine that the pod is running on.
 | 
				
			||||||
	MakeManifest(machine string, pod api.Pod) (api.ContainerManifest, error)
 | 
						MakeBoundPod(machine string, pod *api.Pod) (*api.BoundPod, error)
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
type BasicManifestFactory struct {
 | 
					type BasicBoundPodFactory struct {
 | 
				
			||||||
	// TODO: this should really point at the API rather than a registry
 | 
						// TODO: this should really point at the API rather than a registry
 | 
				
			||||||
	ServiceRegistry service.Registry
 | 
						ServiceRegistry service.Registry
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func (b *BasicManifestFactory) MakeManifest(machine string, pod api.Pod) (api.ContainerManifest, error) {
 | 
					func (b *BasicBoundPodFactory) MakeBoundPod(machine string, pod *api.Pod) (*api.BoundPod, error) {
 | 
				
			||||||
	envVars, err := service.GetServiceEnvironmentVariables(api.NewContext(), b.ServiceRegistry, machine)
 | 
						envVars, err := service.GetServiceEnvironmentVariables(api.NewContext(), b.ServiceRegistry, machine)
 | 
				
			||||||
	if err != nil {
 | 
						if err != nil {
 | 
				
			||||||
		return api.ContainerManifest{}, err
 | 
							return nil, err
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	for ix, container := range pod.DesiredState.Manifest.Containers {
 | 
						boundPod := &api.BoundPod{}
 | 
				
			||||||
		pod.DesiredState.Manifest.ID = pod.ID
 | 
						if err := api.Scheme.Convert(pod, boundPod); err != nil {
 | 
				
			||||||
		pod.DesiredState.Manifest.Containers[ix].Env = append(container.Env, envVars...)
 | 
							return nil, err
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	return pod.DesiredState.Manifest, nil
 | 
						for ix, container := range boundPod.Spec.Containers {
 | 
				
			||||||
 | 
							boundPod.Spec.Containers[ix].Env = append(container.Env, envVars...)
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						return boundPod, nil
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
@@ -25,13 +25,13 @@ import (
 | 
				
			|||||||
	"github.com/GoogleCloudPlatform/kubernetes/pkg/util"
 | 
						"github.com/GoogleCloudPlatform/kubernetes/pkg/util"
 | 
				
			||||||
)
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func TestMakeManifestNoServices(t *testing.T) {
 | 
					func TestMakeBoundPodNoServices(t *testing.T) {
 | 
				
			||||||
	registry := registrytest.ServiceRegistry{}
 | 
						registry := registrytest.ServiceRegistry{}
 | 
				
			||||||
	factory := &BasicManifestFactory{
 | 
						factory := &BasicBoundPodFactory{
 | 
				
			||||||
		ServiceRegistry: ®istry,
 | 
							ServiceRegistry: ®istry,
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	manifest, err := factory.MakeManifest("machine", api.Pod{
 | 
						pod, err := factory.MakeBoundPod("machine", &api.Pod{
 | 
				
			||||||
		TypeMeta: api.TypeMeta{ID: "foobar"},
 | 
							TypeMeta: api.TypeMeta{ID: "foobar"},
 | 
				
			||||||
		DesiredState: api.PodState{
 | 
							DesiredState: api.PodState{
 | 
				
			||||||
			Manifest: api.ContainerManifest{
 | 
								Manifest: api.ContainerManifest{
 | 
				
			||||||
@@ -44,19 +44,19 @@ func TestMakeManifestNoServices(t *testing.T) {
 | 
				
			|||||||
		},
 | 
							},
 | 
				
			||||||
	})
 | 
						})
 | 
				
			||||||
	if err != nil {
 | 
						if err != nil {
 | 
				
			||||||
		t.Errorf("unexpected error: %v", err)
 | 
							t.Fatalf("unexpected error: %v", err)
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	container := manifest.Containers[0]
 | 
						container := pod.Spec.Containers[0]
 | 
				
			||||||
	if len(container.Env) != 0 {
 | 
						if len(container.Env) != 0 {
 | 
				
			||||||
		t.Errorf("Expected zero env vars, got: %#v", manifest)
 | 
							t.Errorf("Expected zero env vars, got: %#v", pod)
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	if manifest.ID != "foobar" {
 | 
						if pod.ID != "foobar" {
 | 
				
			||||||
		t.Errorf("Failed to assign ID to manifest: %#v", manifest.ID)
 | 
							t.Errorf("Failed to assign ID to pod: %#v", pod.ID)
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func TestMakeManifestServices(t *testing.T) {
 | 
					func TestMakeBoundPodServices(t *testing.T) {
 | 
				
			||||||
	registry := registrytest.ServiceRegistry{
 | 
						registry := registrytest.ServiceRegistry{
 | 
				
			||||||
		List: api.ServiceList{
 | 
							List: api.ServiceList{
 | 
				
			||||||
			Items: []api.Service{
 | 
								Items: []api.Service{
 | 
				
			||||||
@@ -72,11 +72,11 @@ func TestMakeManifestServices(t *testing.T) {
 | 
				
			|||||||
			},
 | 
								},
 | 
				
			||||||
		},
 | 
							},
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	factory := &BasicManifestFactory{
 | 
						factory := &BasicBoundPodFactory{
 | 
				
			||||||
		ServiceRegistry: ®istry,
 | 
							ServiceRegistry: ®istry,
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	manifest, err := factory.MakeManifest("machine", api.Pod{
 | 
						pod, err := factory.MakeBoundPod("machine", &api.Pod{
 | 
				
			||||||
		DesiredState: api.PodState{
 | 
							DesiredState: api.PodState{
 | 
				
			||||||
			Manifest: api.ContainerManifest{
 | 
								Manifest: api.ContainerManifest{
 | 
				
			||||||
				Containers: []api.Container{
 | 
									Containers: []api.Container{
 | 
				
			||||||
@@ -88,10 +88,10 @@ func TestMakeManifestServices(t *testing.T) {
 | 
				
			|||||||
		},
 | 
							},
 | 
				
			||||||
	})
 | 
						})
 | 
				
			||||||
	if err != nil {
 | 
						if err != nil {
 | 
				
			||||||
		t.Errorf("unexpected error: %v", err)
 | 
							t.Fatalf("unexpected error: %v", err)
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	container := manifest.Containers[0]
 | 
						container := pod.Spec.Containers[0]
 | 
				
			||||||
	envs := []api.EnvVar{
 | 
						envs := []api.EnvVar{
 | 
				
			||||||
		{
 | 
							{
 | 
				
			||||||
			Name:  "TEST_SERVICE_HOST",
 | 
								Name:  "TEST_SERVICE_HOST",
 | 
				
			||||||
@@ -123,8 +123,7 @@ func TestMakeManifestServices(t *testing.T) {
 | 
				
			|||||||
		},
 | 
							},
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	if len(container.Env) != len(envs) {
 | 
						if len(container.Env) != len(envs) {
 | 
				
			||||||
		t.Errorf("Expected %d env vars, got %d: %#v", len(envs), len(container.Env), manifest)
 | 
							t.Fatalf("Expected %d env vars, got %d: %#v", len(envs), len(container.Env), pod)
 | 
				
			||||||
		return
 | 
					 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	for ix := range container.Env {
 | 
						for ix := range container.Env {
 | 
				
			||||||
		if !reflect.DeepEqual(envs[ix], container.Env[ix]) {
 | 
							if !reflect.DeepEqual(envs[ix], container.Env[ix]) {
 | 
				
			||||||
@@ -133,7 +132,7 @@ func TestMakeManifestServices(t *testing.T) {
 | 
				
			|||||||
	}
 | 
						}
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func TestMakeManifestServicesExistingEnvVar(t *testing.T) {
 | 
					func TestMakeBoundPodServicesExistingEnvVar(t *testing.T) {
 | 
				
			||||||
	registry := registrytest.ServiceRegistry{
 | 
						registry := registrytest.ServiceRegistry{
 | 
				
			||||||
		List: api.ServiceList{
 | 
							List: api.ServiceList{
 | 
				
			||||||
			Items: []api.Service{
 | 
								Items: []api.Service{
 | 
				
			||||||
@@ -149,11 +148,11 @@ func TestMakeManifestServicesExistingEnvVar(t *testing.T) {
 | 
				
			|||||||
			},
 | 
								},
 | 
				
			||||||
		},
 | 
							},
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	factory := &BasicManifestFactory{
 | 
						factory := &BasicBoundPodFactory{
 | 
				
			||||||
		ServiceRegistry: ®istry,
 | 
							ServiceRegistry: ®istry,
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	manifest, err := factory.MakeManifest("machine", api.Pod{
 | 
						pod, err := factory.MakeBoundPod("machine", &api.Pod{
 | 
				
			||||||
		DesiredState: api.PodState{
 | 
							DesiredState: api.PodState{
 | 
				
			||||||
			Manifest: api.ContainerManifest{
 | 
								Manifest: api.ContainerManifest{
 | 
				
			||||||
				Containers: []api.Container{
 | 
									Containers: []api.Container{
 | 
				
			||||||
@@ -170,10 +169,10 @@ func TestMakeManifestServicesExistingEnvVar(t *testing.T) {
 | 
				
			|||||||
		},
 | 
							},
 | 
				
			||||||
	})
 | 
						})
 | 
				
			||||||
	if err != nil {
 | 
						if err != nil {
 | 
				
			||||||
		t.Errorf("unexpected error: %v", err)
 | 
							t.Fatalf("unexpected error: %v", err)
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	container := manifest.Containers[0]
 | 
						container := pod.Spec.Containers[0]
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	envs := []api.EnvVar{
 | 
						envs := []api.EnvVar{
 | 
				
			||||||
		{
 | 
							{
 | 
				
			||||||
@@ -210,8 +209,7 @@ func TestMakeManifestServicesExistingEnvVar(t *testing.T) {
 | 
				
			|||||||
		},
 | 
							},
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	if len(container.Env) != len(envs) {
 | 
						if len(container.Env) != len(envs) {
 | 
				
			||||||
		t.Errorf("Expected %d env vars, got: %#v", len(envs), manifest)
 | 
							t.Fatalf("Expected %d env vars, got: %#v", len(envs), pod)
 | 
				
			||||||
		return
 | 
					 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	for ix := range container.Env {
 | 
						for ix := range container.Env {
 | 
				
			||||||
		if !reflect.DeepEqual(envs[ix], container.Env[ix]) {
 | 
							if !reflect.DeepEqual(envs[ix], container.Env[ix]) {
 | 
				
			||||||
		Reference in New Issue
	
	Block a user