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
11 changes: 9 additions & 2 deletions testing/it_sidecar/it_sidecar.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ func contains(v []string, item string) bool {
}
return false
}

// listReadyApps converts a list returned from podsInformer.GetStore().List() to a map containing apps with ready status
// app is determined by app label
func listReadyApps(list []interface{}) (readypods, notReady []string) {
Expand Down Expand Up @@ -138,7 +139,7 @@ func listenForEvents(ctx context.Context, clientset *kubernetes.Clientset, onFai
return
}
log.Printf("EVENT %s %s %s %s\n", event.Namespace, event.InvolvedObject.Name, event.Reason, event.Message)
if event.Reason == "Failed" {
if event.Reason == "Failed" || event.Reason == "BackOff" {
onFailure(event)
}
}
Expand Down Expand Up @@ -370,7 +371,13 @@ func main() {
clientset = kubernetes.NewForConfigOrDie(config)
defer cleanup(clientset)

go stern.Run(ctx, *namespace, clientset)
go func() {
err := stern.Run(ctx, *namespace, clientset, allowErrors)
if err != nil {
log.Print(err)
}
cancel()
}()

listenForEvents(ctx, clientset, func(event *v1.Event) {
if !allowErrors {
Expand Down
6 changes: 3 additions & 3 deletions testing/it_sidecar/stern/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,11 @@ import (
)

// Run starts the main run loop
func Run(ctx context.Context, namespace string, clientset *kubernetes.Clientset) error {
func Run(ctx context.Context, namespace string, clientset *kubernetes.Clientset, allowErrors bool) error {

tails := make(map[string]*Tail)

err := Watch(ctx, clientset.CoreV1().Pods(namespace), RUNNING, labels.Everything(), func(p *Target) {
err := Watch(ctx, clientset.CoreV1().Pods(namespace), RUNNING, labels.Everything(), allowErrors, func(p *Target) {
id := p.GetID()
if tails[id] != nil {
return
Expand All @@ -45,7 +45,7 @@ func Run(ctx context.Context, namespace string, clientset *kubernetes.Clientset)
delete(tails, id)
})

Choose a reason for hiding this comment

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

The error handling strategy within the Watch function's callback does not account for handling errors that may occur during the execution of the callback. This could lead to silent failures where errors are not propagated or logged, making debugging difficult. It's recommended to implement a strategy for error handling within the callback, such as logging errors or using a channel to propagate errors back to the caller for handling.

if err != nil {
return fmt.Errorf("failed to set up watch: %v", err)
return fmt.Errorf("watch error: %v", err)
}

return nil
Expand Down
8 changes: 7 additions & 1 deletion testing/it_sidecar/stern/watch.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ func (t *Target) GetID() string {
// Watch starts listening to Kubernetes events and emits modified
// containers/pods. The first result is targets added, the second is targets
// removed
func Watch(ctx context.Context, i v1.PodInterface, containerState ContainerState, labelSelector labels.Selector, onAdded, onRemoved func(*Target)) error {
func Watch(ctx context.Context, i v1.PodInterface, containerState ContainerState, labelSelector labels.Selector, allowErrors bool, onAdded, onRemoved func(*Target)) error {
watcher, err := i.Watch(ctx, metav1.ListOptions{Watch: true, LabelSelector: labelSelector.String()})
if err != nil {
return fmt.Errorf("failed to set up watch: %s", err)
Expand Down Expand Up @@ -83,6 +83,12 @@ func Watch(ctx context.Context, i v1.PodInterface, containerState ContainerState

log.Print("container ", c.Name, " has state ", c.State)

if !allowErrors {
if t := c.State.Terminated; t != nil && t.ExitCode != 0 && t.Reason == "Error" {
return fmt.Errorf("container %s failed with exit code %d and reason '%s'", c.Name, t.ExitCode, t.Reason)
}
}

if containerState.Match(c.State) {
onAdded(&Target{
Namespace: pod.Namespace,
Expand Down