Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
94 changes: 83 additions & 11 deletions pkg/build/controller/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ type BuildStrategy interface {
type podManager interface {
CreatePod(namespace string, pod *kapi.Pod) (*kapi.Pod, error)
DeletePod(namespace string, pod *kapi.Pod) error
GetPod(namespace, name string) (*kapi.Pod, error)
}

type imageStreamClient interface {
Expand Down Expand Up @@ -138,20 +139,19 @@ type BuildPodController struct {
}

func (bc *BuildPodController) HandlePod(pod *kapi.Pod) error {
// Find the build for this pod
var build *buildapi.Build
for _, obj := range bc.BuildStore.List() {
b := obj.(*buildapi.Build)
if buildutil.GetBuildPodName(b) == pod.Name {
build = b
break
}
// pod and build have the same name, so look up the build by the pod's name.
obj, exists, err := bc.BuildStore.Get(pod)
if err != nil {
glog.V(4).Infof("Error getting build for pod %s/%s: %v", pod.Namespace, pod.Name, err)
return err
}

if build == nil {
if !exists || obj == nil {
glog.V(5).Infof("No build found for pod %s/%s", pod.Namespace, pod.Name)
return nil
}

build := obj.(*buildapi.Build)

// A cancelling event was triggered for the build, delete its pod and update build status.
if build.Cancelled {
glog.V(2).Infof("Cancelling build %s.", build.Name)
Expand Down Expand Up @@ -182,7 +182,7 @@ func (bc *BuildPodController) HandlePod(pod *kapi.Pod) error {
if build.Status != nextStatus {
glog.V(4).Infof("Updating build %s status %s -> %s", build.Name, build.Status, nextStatus)
build.Status = nextStatus
if build.Status == buildapi.BuildStatusComplete || build.Status == buildapi.BuildStatusFailed || build.Status == buildapi.BuildStatusCancelled {
if buildutil.IsBuildComplete(build) {
now := util.Now()
build.CompletionTimestamp = &now
}
Expand Down Expand Up @@ -224,3 +224,75 @@ func (bc *BuildPodController) CancelBuild(build *buildapi.Build, pod *kapi.Pod)
func isBuildCancellable(build *buildapi.Build) bool {
return build.Status == buildapi.BuildStatusNew || build.Status == buildapi.BuildStatusPending || build.Status == buildapi.BuildStatusRunning
}

// BuildPodDeleteController watches pods running builds and updates the build if the pod is deleted
type BuildPodDeleteController struct {
BuildStore cache.Store
BuildUpdater buildclient.BuildUpdater
}

func (bc *BuildPodDeleteController) HandleBuildPodDeletion(pod *kapi.Pod) error {
glog.V(4).Infof("Handling deletion of build pod %s/%s", pod.Namespace, pod.Name)
// pod and build use the same name and same key function, so we can look up the
// build by the pod object. Doing this "right" is actually uglier since
// we would ideally want to pass the build object to the keyFunc, but that
// would require us having the build. Constructing the key by hand is
// also not ideal since the keyFunc could change.
obj, exists, err := bc.BuildStore.Get(pod)
if err != nil {
glog.V(4).Infof("Error getting build for pod %s/%s", pod.Namespace, pod.Name)
return err
}
if !exists || obj == nil {
glog.V(5).Infof("No build found for deleted pod %s/%s", pod.Namespace, pod.Name)
return nil
}
build := obj.(*buildapi.Build)

if buildutil.IsBuildComplete(build) {
glog.V(4).Infof("Pod was deleted but Build %s/%s is already completed, so no need to update it.", build.Namespace, build.Name)
return nil
}

nextStatus := buildapi.BuildStatusError
if build.Status != nextStatus {
glog.V(4).Infof("Updating build %s/%s status %s -> %s", build.Namespace, build.Name, build.Status, nextStatus)
build.Status = nextStatus
build.Message = "The Pod for this Build was deleted before the Build completed."
now := util.Now()
build.CompletionTimestamp = &now
if err := bc.BuildUpdater.Update(build.Namespace, build); err != nil {
return fmt.Errorf("Failed to update build %s: %v", build.Name, err)
}
}
return nil
}

// BuildDeleteController watches for builds being deleted and cleans up associated pods
type BuildDeleteController struct {
PodManager podManager
}

func (bc *BuildDeleteController) HandleBuildDeletion(build *buildapi.Build) error {
glog.V(4).Infof("Handling deletion of build %s", build.Name)
podName := buildutil.GetBuildPodName(build)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Either we should remove that method entirely or create a similar one to get build name from pod. With this approach we're not consistent IMHO, because on one side we're stating the pod name is the same as build and we just use that to get the build, but here we're using that helper method.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

adding one to get build name from pod.

pod, err := bc.PodManager.GetPod(build.Namespace, podName)
if err != nil {
glog.V(2).Infof("Failed to find pod with name %s for build %s in namespace %s due to error: %v", podName, build.Name, build.Namespace, err)
return err
}
if pod == nil {
glog.V(2).Infof("Did not find pod with name %s for build %s in namespace %s", podName, build.Name, build.Namespace)
return nil
}
if pod.Labels[buildapi.BuildLabel] != build.Name {
glog.V(2).Infof("Not deleting pod %s/%s because the build label %s does not match the build name %s", pod.Namespace, podName, pod.Labels[buildapi.BuildLabel], build.Name)
return nil
}
err = bc.PodManager.DeletePod(build.Namespace, pod)
if err != nil {
glog.V(2).Infof("Failed to delete pod %s/%s for build %s due to error: %v", build.Namespace, podName, build.Name, err)
return err
}
return nil
}
12 changes: 12 additions & 0 deletions pkg/build/controller/controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,10 @@ func (*okPodManager) DeletePod(namespace string, pod *kapi.Pod) error {
return nil
}

func (*okPodManager) GetPod(namespace, name string) (*kapi.Pod, error) {
return &kapi.Pod{}, nil
}

type errPodManager struct{}

func (*errPodManager) CreatePod(namespace string, pod *kapi.Pod) (*kapi.Pod, error) {
Expand All @@ -63,6 +67,10 @@ func (*errPodManager) DeletePod(namespace string, pod *kapi.Pod) error {
return errors.New("DeletePod error!")
}

func (*errPodManager) GetPod(namespace, name string) (*kapi.Pod, error) {
return nil, errors.New("GetPod error!")
}

type errExistsPodManager struct{}

func (*errExistsPodManager) CreatePod(namespace string, pod *kapi.Pod) (*kapi.Pod, error) {
Expand All @@ -73,6 +81,10 @@ func (*errExistsPodManager) DeletePod(namespace string, pod *kapi.Pod) error {
return kerrors.NewNotFound("kind", "name")
}

func (*errExistsPodManager) GetPod(namespace, name string) (*kapi.Pod, error) {
return nil, kerrors.NewNotFound("kind", "name")
}

type okImageStreamClient struct{}

func (*okImageStreamClient) GetImageStream(namespace, name string) (*imageapi.ImageStream, error) {
Expand Down
Loading