// RecordConfigEvent records an event for the deployment config referenced by the // deployment. func RecordConfigEvent(client kclient.EventNamespacer, deployment *kapi.ReplicationController, decoder runtime.Decoder, eventType, reason, msg string) { t := unversioned.Time{Time: time.Now()} var obj runtime.Object = deployment if config, err := deployutil.DecodeDeploymentConfig(deployment, decoder); err == nil { obj = config } else { glog.Errorf("Unable to decode deployment config from %s/%s: %v", deployment.Namespace, deployment.Name, err) } ref, err := kapi.GetReference(obj) if err != nil { glog.Errorf("Unable to get reference for %#v: %v", obj, err) return } event := &kapi.Event{ ObjectMeta: kapi.ObjectMeta{ Name: fmt.Sprintf("%v.%x", ref.Name, t.UnixNano()), Namespace: ref.Namespace, }, InvolvedObject: *ref, Reason: reason, Message: msg, Source: kapi.EventSource{ Component: deployutil.DeployerPodNameFor(deployment), }, FirstTimestamp: t, LastTimestamp: t, Count: 1, Type: eventType, } if _, err := client.Events(ref.Namespace).Create(event); err != nil { glog.Errorf("Could not create event '%#v': %v", event, err) } }
func (e *HookExecutor) emitEvent(deployment *kapi.ReplicationController, eventType, reason, msg string) { t := unversioned.Time{Time: time.Now()} var ref *kapi.ObjectReference if config, err := deployutil.DecodeDeploymentConfig(deployment, e.decoder); err != nil { glog.Errorf("Unable to decode deployment %s/%s to replication contoller: %v", deployment.Namespace, deployment.Name, err) if ref, err = kapi.GetReference(deployment); err != nil { glog.Errorf("Unable to get reference for %#v: %v", deployment, err) return } } else { if ref, err = kapi.GetReference(config); err != nil { glog.Errorf("Unable to get reference for %#v: %v", config, err) return } } event := &kapi.Event{ ObjectMeta: kapi.ObjectMeta{ Name: fmt.Sprintf("%v.%x", ref.Name, t.UnixNano()), Namespace: ref.Namespace, }, InvolvedObject: *ref, Reason: reason, Message: msg, Source: kapi.EventSource{ Component: deployutil.DeployerPodNameFor(deployment), }, FirstTimestamp: t, LastTimestamp: t, Count: 1, Type: eventType, } if _, err := e.events.Create(event); err != nil { glog.Errorf("Could not send event '%#v': %v", event, err) } }
// TestHandle_createPodOk ensures that a the deployer pod created in response // to a new deployment is valid. func TestHandle_createPodOk(t *testing.T) { var ( updatedDeployment *kapi.ReplicationController createdPod *kapi.Pod expectedContainer = okContainer() ) fake := &ktestclient.Fake{} fake.AddReactor("create", "pods", func(action ktestclient.Action) (handled bool, ret runtime.Object, err error) { pod := action.(ktestclient.CreateAction).GetObject().(*kapi.Pod) createdPod = pod return true, pod, nil }) fake.AddReactor("update", "replicationcontrollers", func(action ktestclient.Action) (handled bool, ret runtime.Object, err error) { rc := action.(ktestclient.UpdateAction).GetObject().(*kapi.ReplicationController) updatedDeployment = rc return true, rc, nil }) // Verify new -> pending config := deploytest.OkDeploymentConfig(1) config.Spec.Strategy = deploytest.OkCustomStrategy() deployment, _ := deployutil.MakeDeployment(config, codec) deployment.Annotations[deployapi.DeploymentStatusAnnotation] = string(deployapi.DeploymentStatusNew) deployment.Spec.Template.Spec.NodeSelector = map[string]string{"labelKey1": "labelValue1", "labelKey2": "labelValue2"} controller := okDeploymentController(fake, nil, nil, true) if err := controller.Handle(deployment); err != nil { t.Fatalf("unexpected error: %v", err) } if updatedDeployment == nil { t.Fatalf("expected an updated deployment") } if e, a := deployapi.DeploymentStatusPending, deployutil.DeploymentStatusFor(updatedDeployment); e != a { t.Fatalf("expected updated deployment status %s, got %s", e, a) } if createdPod == nil { t.Fatalf("expected a pod to be created") } if e := deployutil.DeployerPodNameFor(updatedDeployment); len(e) == 0 { t.Fatalf("missing deployment pod annotation") } if e, a := createdPod.Name, deployutil.DeployerPodNameFor(updatedDeployment); e != a { t.Fatalf("expected deployment pod annotation %s, got %s", e, a) } if e := deployutil.DeploymentNameFor(createdPod); len(e) == 0 { t.Fatalf("missing deployment annotation") } if e, a := updatedDeployment.Name, deployutil.DeploymentNameFor(createdPod); e != a { t.Fatalf("expected pod deployment annotation %s, got %s", e, a) } if e, a := deployment.Spec.Template.Spec.NodeSelector, createdPod.Spec.NodeSelector; !reflect.DeepEqual(e, a) { t.Fatalf("expected pod NodeSelector %v, got %v", e, a) } if createdPod.Spec.ActiveDeadlineSeconds == nil { t.Fatalf("expected ActiveDeadlineSeconds to be set on the deployer pod") } if *createdPod.Spec.ActiveDeadlineSeconds != deployapi.MaxDeploymentDurationSeconds { t.Fatalf("expected ActiveDeadlineSeconds on the deployer pod to be set to %d; found: %d", deployapi.MaxDeploymentDurationSeconds, *createdPod.Spec.ActiveDeadlineSeconds) } actualContainer := createdPod.Spec.Containers[0] if e, a := expectedContainer.Image, actualContainer.Image; e != a { t.Fatalf("expected container image %s, got %s", expectedContainer.Image, actualContainer.Image) } if e, a := expectedContainer.Command[0], actualContainer.Command[0]; e != a { t.Fatalf("expected container command %s, got %s", expectedContainer.Command[0], actualContainer.Command[0]) } if e, a := expectedContainer.Env[0].Name, actualContainer.Env[0].Name; e != a { t.Fatalf("expected container env name %s, got %s", expectedContainer.Env[0].Name, actualContainer.Env[0].Name) } if e, a := expectedContainer.Env[0].Value, actualContainer.Env[0].Value; e != a { t.Fatalf("expected container env value %s, got %s", expectedContainer.Env[0].Value, actualContainer.Env[0].Value) } if e, a := expectedContainer.Resources, actualContainer.Resources; !kapi.Semantic.DeepEqual(e, a) { t.Fatalf("expected container resources %v, got %v", expectedContainer.Resources, actualContainer.Resources) } }
// TestHandle_createPodOk ensures that a the deployer pod created in response // to a new deployment is valid. func TestHandle_createPodOk(t *testing.T) { var ( updatedDeployment *kapi.ReplicationController createdPod *kapi.Pod expectedContainer = okContainer() ) controller := &DeploymentController{ decodeConfig: func(deployment *kapi.ReplicationController) (*deployapi.DeploymentConfig, error) { return deployutil.DecodeDeploymentConfig(deployment, api.Codec) }, deploymentClient: &deploymentClientImpl{ updateDeploymentFunc: func(namespace string, deployment *kapi.ReplicationController) (*kapi.ReplicationController, error) { updatedDeployment = deployment return updatedDeployment, nil }, }, podClient: &podClientImpl{ createPodFunc: func(namespace string, pod *kapi.Pod) (*kapi.Pod, error) { createdPod = pod return pod, nil }, }, makeContainer: func(strategy *deployapi.DeploymentStrategy) (*kapi.Container, error) { return expectedContainer, nil }, recorder: &record.FakeRecorder{}, } // Verify new -> pending config := deploytest.OkDeploymentConfig(1) deployment, _ := deployutil.MakeDeployment(config, kapi.Codec) deployment.Annotations[deployapi.DeploymentStatusAnnotation] = string(deployapi.DeploymentStatusNew) deployment.Spec.Template.Spec.NodeSelector = map[string]string{"labelKey1": "labelValue1", "labelKey2": "labelValue2"} err := controller.Handle(deployment) if err != nil { t.Fatalf("unexpected error: %v", err) } if updatedDeployment == nil { t.Fatalf("expected an updated deployment") } if e, a := deployapi.DeploymentStatusPending, deployutil.DeploymentStatusFor(updatedDeployment); e != a { t.Fatalf("expected updated deployment status %s, got %s", e, a) } if createdPod == nil { t.Fatalf("expected a pod to be created") } if e := deployutil.DeployerPodNameFor(updatedDeployment); len(e) == 0 { t.Fatalf("missing deployment pod annotation") } if e, a := createdPod.Name, deployutil.DeployerPodNameFor(updatedDeployment); e != a { t.Fatalf("expected deployment pod annotation %s, got %s", e, a) } if e := deployutil.DeploymentNameFor(createdPod); len(e) == 0 { t.Fatalf("missing deployment annotation") } if e, a := updatedDeployment.Name, deployutil.DeploymentNameFor(createdPod); e != a { t.Fatalf("expected pod deployment annotation %s, got %s", e, a) } if e, a := deployment.Spec.Template.Spec.NodeSelector, createdPod.Spec.NodeSelector; !reflect.DeepEqual(e, a) { t.Fatalf("expected pod NodeSelector %v, got %v", e, a) } if createdPod.Spec.ActiveDeadlineSeconds == nil { t.Fatalf("expected ActiveDeadlineSeconds to be set on the deployer pod") } if *createdPod.Spec.ActiveDeadlineSeconds != deployapi.MaxDeploymentDurationSeconds { t.Fatalf("expected ActiveDeadlineSeconds on the deployer pod to be set to %d; found: %d", deployapi.MaxDeploymentDurationSeconds, *createdPod.Spec.ActiveDeadlineSeconds) } actualContainer := createdPod.Spec.Containers[0] if e, a := expectedContainer.Image, actualContainer.Image; e != a { t.Fatalf("expected container image %s, got %s", expectedContainer.Image, actualContainer.Image) } if e, a := expectedContainer.Command[0], actualContainer.Command[0]; e != a { t.Fatalf("expected container command %s, got %s", expectedContainer.Command[0], actualContainer.Command[0]) } if e, a := expectedContainer.Env[0].Name, actualContainer.Env[0].Name; e != a { t.Fatalf("expected container env name %s, got %s", expectedContainer.Env[0].Name, actualContainer.Env[0].Name) } if e, a := expectedContainer.Env[0].Value, actualContainer.Env[0].Value; e != a { t.Fatalf("expected container env value %s, got %s", expectedContainer.Env[0].Value, actualContainer.Env[0].Value) } if e, a := expectedContainer.Resources, actualContainer.Resources; !kapi.Semantic.DeepEqual(e, a) { t.Fatalf("expected container resources %v, got %v", expectedContainer.Resources, actualContainer.Resources) } }