Skip to content
Draft
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
2929949
feat(k8s): add eventInformer to podTracker
cognifloyd Apr 28, 2022
be144f4
feat(k8s): ignore event deletion
cognifloyd Apr 28, 2022
7602600
feat(k8s): begin handling event stream
cognifloyd Apr 28, 2022
457a664
refactor: create eventInformer from eventInformerFactory
cognifloyd Apr 29, 2022
7d674e8
refactor: rename selector=>labelSelector
cognifloyd Apr 29, 2022
71c9b34
enhance: register eventInformerFactory on podTracker
cognifloyd Apr 29, 2022
c3e4b50
enhance: add podTracker.inspectContainerEvent
cognifloyd Apr 29, 2022
3e89df6
enhance: add signal for running container
cognifloyd Apr 29, 2022
8ba7ad5
enhance: only mark containers as running/terminated if it is the corr…
cognifloyd Apr 29, 2022
038bf97
enhance(k8s): exit WaitContainer if build is canceled
cognifloyd Apr 29, 2022
b5ab8eb
enhance(k8s): add containerTracker.Events() function
cognifloyd Apr 29, 2022
57011c1
tests: fix inspectContainerStatuses test
cognifloyd Apr 29, 2022
639b219
bugfix(k8s): Make sure image pull errors are detected
cognifloyd Apr 29, 2022
092840b
refactor(k8s): use channels to signal imagePull success/errors
cognifloyd Apr 29, 2022
e3d6d93
fix: comment typos
cognifloyd Apr 29, 2022
aa1078e
enhance: capture ImagePull errors from ContainerStatuses as well
cognifloyd Apr 29, 2022
9c64abd
enhance(k8s): handle more image pull event types
cognifloyd Apr 29, 2022
9686c23
tests(k8s): fix tests for RunContainer
cognifloyd May 3, 2022
dd189e0
tests(k8s): test RunContainer and WaitContainer with canceled build
cognifloyd May 3, 2022
ca14e32
tests(k8s): test AssembleBuild with canceled build
cognifloyd May 4, 2022
2a6d109
tests(k8s): test RunContainer with PodTracker failure (increase cover…
cognifloyd May 4, 2022
08c219c
tests(k8s): test inspectContainerStatuses with Running or ImagePullError
cognifloyd May 4, 2022
40da3f8
chore: prune some comments
cognifloyd May 4, 2022
4726072
tests: fix inspectContainerStatuses test with an errgroup
cognifloyd May 4, 2022
0d2f315
tests: test RunContainer with an ImagePullError
cognifloyd May 4, 2022
ccf9fb2
tests: test getTrackedPodEvent
cognifloyd May 4, 2022
f76339d
tests: test podTracker.HandleEventAdd, podTracker.HandleEventUpdate
cognifloyd May 4, 2022
e6876d1
refactor: drop unused Events function
cognifloyd May 4, 2022
ecceeac
tests: test inspectContainerEvent image pull events
cognifloyd May 4, 2022
b945f33
tests: test inspectContainerEvent edge cases
cognifloyd May 4, 2022
9ec81fa
tests: test more pull policies in SetupContainer
cognifloyd May 4, 2022
65cbe59
chore: delete dead code.
cognifloyd May 5, 2022
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
Prev Previous commit
Next Next commit
refactor(k8s): use channels to signal imagePull success/errors
  • Loading branch information
cognifloyd committed May 4, 2022
commit 092840bfbe21357777a0031991f989f1119621fe
104 changes: 47 additions & 57 deletions runtime/kubernetes/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,14 @@ import (
"k8s.io/apimachinery/pkg/util/wait"
)

const (
// kubernetes event reasons.
reasonBackOff = "BackOff"
reasonFailed = "Failed"
reasonPulled = "Pulled"
reasonPulling = "Pulling"
)

// InspectContainer inspects the pipeline container.
func (c *client) InspectContainer(ctx context.Context, ctn *pipeline.Container) error {
c.Logger.Tracef("inspecting container %s", ctn.ID)
Expand Down Expand Up @@ -104,11 +112,6 @@ func (c *client) RunContainer(ctx context.Context, ctn *pipeline.Container, b *p
return err
}

var (
events []*v1.Event
imagePulled bool
)

// make sure the container starts (watch for image pull errors or similar)
for {
select {
Expand All @@ -118,57 +121,16 @@ func (c *client) RunContainer(ctx context.Context, ctn *pipeline.Container, b *p
case <-ctnTracker.Running:
// hooray it is running
return nil
default:
}

// no need to search for image pull events
if imagePulled {
continue
}

events, err = ctnTracker.Events()
if err != nil {
return err
}

for _, event := range events {
// check if the event mentions the target image
if !(strings.Contains(event.Message, ctn.Image) || strings.Contains(event.Message, _image)) {
// if the relevant messages does not include our image
// it is probably for "kubernetes/pause:latest"
// or it is a generic message that is basically useless like:
// event.Reason => event.Message
// Failed => Error: ErrImagePull
// BackOff => Error: ImagePullBackOff
continue
}

switch event.Reason {
// examples: event.Reason => event.Message
case "Failed", "BackOff":
// Failed => Failed to pull image "image:tag": <containerd message>
// BackOff => Back-off pulling image "image:tag"
return fmt.Errorf("failed to run container %s in %s: %s", ctn.ID, c.Pod.ObjectMeta.Name, event.Message)
case "Pulled":
// Pulled => Successfully pulled image "image:tag" in <time>
imagePulled = true
case "Pulling":
// Pulling => Pulling image "image:tag"
continue
default:
continue
}

if imagePulled {
// found the event we care about, stop checking.
break
}
case event := <-ctnTracker.ImagePullErrors:
return fmt.Errorf(
"failed to run container %s in %s: [%s] %s",
ctn.ID,
c.Pod.ObjectMeta.Name,
event.Reason,
event.Message,
)
}

break
}

return nil
}

// SetupContainer prepares the image for the pipeline container.
Expand Down Expand Up @@ -487,8 +449,36 @@ func (p *podTracker) inspectContainerEvent(event *v1.Event) {
return
}

// TODO: save the event on the tracker somehow,
// or send a signal that triggers reloading the events
// for this container
p.Logger.Tracef("container event for %s: [%s] %s", tracker.Name, event.Reason, event.Message)

// check if the event mentions the target image
// if the relevant messages does not include our image
// it is probably for "kubernetes/pause:latest"
// or it is a generic message that is basically useless like:
// event.Reason => event.Message
// Failed => Error: ErrImagePull
// BackOff => Error: ImagePullBackOff
if strings.Contains(event.Message, tracker.Image) {
switch event.Reason {
// examples: event.Reason => event.Message
case reasonFailed, reasonBackOff:
// Failed => Failed to pull image "image:tag": <containerd message>
// BackOff => Back-off pulling image "image:tag"
tracker.ImagePullErrors <- event
return
case reasonPulled:
// Pulled => Successfully pulled image "image:tag" in <time>
tracker.imagePulledOnce.Do(func() {
p.Logger.Debugf("container image pulled: %s in pod %s, %v", tracker.Name, p.TrackedPod, event.Message)

// let RunContainer know the container image was pulled
close(tracker.ImagePulled)
})
case reasonPulling:
// Pulling => Pulling image "image:tag"
return
default:
return
}
}
}
6 changes: 6 additions & 0 deletions runtime/kubernetes/pod_tracker.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,12 @@ type containerTracker struct {
// Image is the final image of the container
Image string

// imagePulledOnce ensures that the ImagePulled channel only gets closed once.
imagePulledOnce sync.Once
// ImagePulled will be closed once the container's image has been pulled.
ImagePulled chan struct{}
// ImagePullErrors collects any image pull errors.
ImagePullErrors chan *v1.Event
// runningOnce ensures that the Terminated channel only gets closed once.
runningOnce sync.Once
// Running will be closed once the container reaches a terminal state.
Expand Down