Esempio n. 1
0
// Deployment is created using Deployments of child Entities
func (c *App) Deployment() (*deploy.Deployment, error) {
	d := new(deploy.Deployment)
	err := d.AddDeployment(c.objects)
	if err != nil {
		return nil, err
	}
	for _, entity := range c.children() {
		deploy, err := entity.Deployment()
		if err != nil {
			return nil, err
		}

		err = d.AddDeployment(*deploy)
		if err != nil {
			return nil, err
		}
	}
	return d, nil
}
Esempio n. 2
0
// Deployment is created containing Pod with attached Containers.
func (c *Pod) Deployment() (*deploy.Deployment, error) {
	deployment := deploy.Deployment{}

	// create Pod from tree of Entities
	kubePod, childObj, err := c.data()
	if err != nil {
		return nil, err
	}

	// add Pod to deployment
	err = deployment.Add(kubePod)
	if err != nil {
		return nil, err
	}

	// add child objects
	err = deployment.AddDeployment(childObj)
	if err != nil {
		return nil, err
	}
	return &deployment, nil
}
Esempio n. 3
0
// Deployment is created for RC attached with it's Pod.
func (c *ReplicationController) Deployment() (*deploy.Deployment, error) {
	deployment := new(deploy.Deployment)

	// create RC
	kubeRC, childObj, err := c.data()
	if err != nil {
		return nil, err
	}

	// add RC to deployment
	err = deployment.Add(kubeRC)
	if err != nil {
		return nil, err
	}

	// add child objects
	err = deployment.AddDeployment(childObj)
	if err != nil {
		return nil, err
	}
	return deployment, nil
}
func TestRCAttachPod(t *testing.T) {
	kubeContainer := kube.Container{
		Name:            "redis",
		Image:           "redis",
		ImagePullPolicy: kube.PullAlways,
	}
	selector := map[string]string{
		"app": "db",
	}

	// create ReplicationController
	rcObjects := testRandomObjects(10)
	rc := testNewRC(t, "container-test", selector, rcObjects)

	// create kube.Pod
	// create Pod from created pod
	kubePod := &kube.Pod{
		ObjectMeta: kube.ObjectMeta{Name: "attached"},
		Spec: kube.PodSpec{
			Containers: []kube.Container{
				kubeContainer,
			},
			RestartPolicy: kube.RestartPolicyAlways,
			DNSPolicy:     kube.DNSDefault,
		},
	}
	podObjects := testRandomObjects(10)
	pod, err := NewPod(kubePod, kube.ObjectMeta{}, "", podObjects...)
	assert.NoError(t, err, "should be valid pod")

	// Attach pod to RC
	// Should assume defaults up tree creating necessary components
	err = rc.Attach(pod)
	assert.NoError(t, err, "pod should be able to attach to ")
	children := rc.children()
	assert.Contains(t, children, pod, "should have image as child")

	// Compare internal elements
	assert.NotNil(t, rc.pod, "should of created pod")
	assert.Len(t, rc.pod.containers, 1)

	// Create struct representation for expected
	// Insert into Deployment
	// Create Deployment from RC
	rcMeta := rc.rc.ObjectMeta
	rcMeta.Namespace = kube.NamespaceDefault

	podMeta := pod.pod.ObjectMeta
	podMeta.Labels = selector
	expectedRC := &kube.ReplicationController{
		ObjectMeta: rcMeta,
		Spec: kube.ReplicationControllerSpec{
			Selector: selector,
			Template: &kube.PodTemplateSpec{
				ObjectMeta: kube.ObjectMeta{
					Namespace: kube.NamespaceDefault,
					Labels:    selector,
				},
				Spec: kubePod.Spec,
			},
		},
	}

	// Insert into Deployment
	expected := new(deploy.Deployment)
	err = expected.Add(expectedRC)
	assert.NoError(t, err, "should be valid RC")

	// add objects to deployment
	expected.AddDeployment(pod.objects)
	expected.AddDeployment(rc.objects)

	// Create Deployment from RC
	actual, err := rc.Deployment()
	assert.NoError(t, err, "should produce valid deployment")

	// Compare deployments
	testDeploymentEqual(t, expected, actual)

	// check images
	images := rc.Images()
	assert.Len(t, images, 1)
}
func TestRCAttachContainer(t *testing.T) {
	containerName := "attached"
	imageName := "embedded-image"
	selector := map[string]string{
		"app": "db",
	}

	// create ReplicationController
	rcObjects := testRandomObjects(10)
	rc := testNewRC(t, "container-test", selector, rcObjects)

	// create kube.Container
	// create Container from created container
	containerObjects := testRandomObjects(20)
	kubeContainer := testNewKubeContainer(containerName, imageName)
	container, err := NewContainer(kubeContainer, kube.ObjectMeta{}, "", containerObjects...)
	assert.NoError(t, err, "should be valid container")

	// Attach container to RC
	// Should assume defaults up tree creating necessary components
	err = rc.Attach(container)
	assert.NoError(t, err, "container should be able to attach to rc")

	// Compare internal elements
	assert.NotNil(t, rc.pod, "should of created pod")
	// assert.Len(t, rc.pod.containers, 1)

	// Create struct representation for expected
	// Insert into Deployment
	// Create Deployment from RC
	rcMeta := rc.rc.ObjectMeta
	rcMeta.Namespace = kube.NamespaceDefault
	expectedRC := &kube.ReplicationController{
		ObjectMeta: rcMeta,
		Spec: kube.ReplicationControllerSpec{
			Selector: selector,
			Template: &kube.PodTemplateSpec{
				ObjectMeta: kube.ObjectMeta{
					Labels:    selector,
					Namespace: kube.NamespaceDefault,
				},
				Spec: kube.PodSpec{
					Containers: []kube.Container{
						kubeContainer,
					},
					RestartPolicy: kube.RestartPolicyAlways,
					DNSPolicy:     kube.DNSDefault,
				},
			},
		},
	}

	// Insert into Deployment
	expected := new(deploy.Deployment)
	err = expected.Add(expectedRC)
	assert.NoError(t, err, "should be valid RC")

	// add objects to deployment
	expected.AddDeployment(container.objects)
	expected.AddDeployment(rc.objects)

	// Create Deployment from RC
	actual, err := rc.Deployment()
	assert.NoError(t, err, "should produce valid deployment")

	// Compare deployments
	testDeploymentEqual(t, expected, actual)

	// check images
	images := rc.Images()
	assert.Len(t, images, 1)
}
func TestRCAttachImage(t *testing.T) {
	imageName := "rc-attach-image"
	rcName := "image-test"
	selector := map[string]string{
		"app": "cache",
	}

	// create ReplicationController
	rcObjects := testRandomObjects(10)
	rc := testNewRC(t, rcName, selector, rcObjects)

	// create Image
	imageObjects := testRandomObjects(10)
	imageEntity := testNewImage(t, imageName, kube.ObjectMeta{}, "", imageObjects)

	// Attach image to RC
	// Should assume defaults up tree creating necessary components
	err := rc.Attach(imageEntity)
	assert.NoError(t, err, "attachment should be allowed")

	// Compare internal elements
	assert.NotNil(t, rc.pod, "should of created template")

	// Create struct representation for expected
	expectedRC := &kube.ReplicationController{
		ObjectMeta: kube.ObjectMeta{
			Name:      rcName,
			Namespace: kube.NamespaceDefault,
		},
		Spec: kube.ReplicationControllerSpec{
			Selector: selector,
			Template: &kube.PodTemplateSpec{
				ObjectMeta: kube.ObjectMeta{
					Namespace: kube.NamespaceDefault,
					Labels:    selector,
				},
				Spec: kube.PodSpec{
					Containers: []kube.Container{
						{
							Name:            imageName,
							Image:           imageName,
							ImagePullPolicy: kube.PullAlways,
						},
					},
					RestartPolicy: kube.RestartPolicyAlways,
					DNSPolicy:     kube.DNSDefault,
				},
			},
		},
	}

	// Insert into Deployment
	expected := new(deploy.Deployment)
	err = expected.Add(expectedRC)
	assert.NoError(t, err, "should be valid RC")

	// add objects to deployment
	expected.AddDeployment(imageEntity.objects)
	expected.AddDeployment(rc.objects)

	// Create Deployment from RC
	actual, err := rc.Deployment()
	assert.NoError(t, err, "should produce valid deployment")

	// Compare deployments
	testDeploymentEqual(t, expected, actual)

	// check images
	images := rc.Images()
	expectedImage, _ := image.FromString(imageName)
	assert.Len(t, images, 1)
	for _, v := range images {
		assert.EqualValues(t, expectedImage, v, "should match original image")
	}
}