Skip to content

Commit

Permalink
engine: print container restart
Browse files Browse the repository at this point in the history
  • Loading branch information
nicks committed Mar 8, 2021
1 parent a9cc0aa commit 1495a42
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 3 deletions.
41 changes: 39 additions & 2 deletions internal/engine/pod.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,26 @@ func handlePodChangeAction(ctx context.Context, state *store.EngineState, action

prunePods(ms)

podInfo.Containers = podContainers(ctx, pod, pod.Status.ContainerStatuses)
podInfo.InitContainers = podContainers(ctx, pod, pod.Status.InitContainerStatuses)
initContainers := podContainers(ctx, pod, pod.Status.InitContainerStatuses)
if !isNew {
names := restartedContainerNames(podInfo.InitContainers, initContainers)
for _, name := range names {
s := fmt.Sprintf("Detected container restart. Pod: %s. Container: %s.", podInfo.PodID, name)
handleLogAction(state, store.NewLogAction(manifest.Name, podInfo.SpanID, logger.WarnLvl, nil, []byte(s)))
}
}
podInfo.InitContainers = initContainers

containers := podContainers(ctx, pod, pod.Status.ContainerStatuses)
if !isNew {
names := restartedContainerNames(podInfo.Containers, containers)
for _, name := range names {
s := fmt.Sprintf("Detected container restart. Pod: %s. Container: %s.", podInfo.PodID, name)
handleLogAction(state, store.NewLogAction(manifest.Name, podInfo.SpanID, logger.WarnLvl, nil, []byte(s)))
}
}
podInfo.Containers = containers

if isNew {
// This is the first time we've seen this pod.
// Ignore any restarts that happened before Tilt saw it.
Expand Down Expand Up @@ -86,6 +104,25 @@ func handlePodChangeAction(ctx context.Context, state *store.EngineState, action
checkForContainerCrash(ctx, state, mt)
}

func restartedContainerNames(existingContainers []store.Container, newContainers []store.Container) []container.Name {
result := []container.Name{}
for i, c := range newContainers {
if i >= len(existingContainers) {
break
}

existing := existingContainers[i]
if existing.Name != c.Name {
continue
}

if c.Restarts > existing.Restarts {
result = append(result, c.Name)
}
}
return result
}

// Find the ManifestTarget for the PodChangeAction,
// and confirm that it matches what we've deployed.
func matchPodChangeToManifest(state *store.EngineState, action k8swatch.PodChangeAction) *store.ManifestTarget {
Expand Down
5 changes: 4 additions & 1 deletion internal/engine/upper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2123,6 +2123,8 @@ func TestUpperPodLogInCrashLoopThirdInstanceStillUp(t *testing.T) {
assert.Contains(t, es.LogStore.SpanLog(pod.SpanID), "third string\n")
assert.Contains(t, es.LogStore.ManifestLog(name), "second string\n")
assert.Contains(t, es.LogStore.ManifestLog(name), "third string\n")
assert.Contains(t, es.LogStore.ManifestLog(name),
"WARNING: Detected container restart. Pod: fakePodID. Container: sancho.\n")
assert.Contains(t, es.LogStore.SpanLog(pod.SpanID), "third string\n")
})

Expand Down Expand Up @@ -2155,7 +2157,8 @@ func TestUpperPodLogInCrashLoopPodCurrentlyDown(t *testing.T) {
f.withState(func(state store.EngineState) {
ms, _ := state.ManifestState(name)
spanID := ms.MostRecentPod().SpanID
assert.Equal(t, "first string\nsecond string\n", state.LogStore.SpanLog(spanID))
assert.Equal(t, "first string\nWARNING: Detected container restart. Pod: fakePodID. Container: sancho.\nsecond string\n",
state.LogStore.SpanLog(spanID))
})

err := f.Stop()
Expand Down

0 comments on commit 1495a42

Please sign in to comment.