Skip to content

Commit

Permalink
log context cancellation reasons (#33)
Browse files Browse the repository at this point in the history
* log context cancellation reasons
* update required go version to 1.21
  • Loading branch information
apesternikov authored May 17, 2024
1 parent 26af984 commit 526acc1
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 22 deletions.
8 changes: 6 additions & 2 deletions .fasterci/config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,17 @@ workflows:
steps:
- name: Build & test
bazel:
build_flags:
- --enable_bzlmod=false
build_targets:
- //...
test_targets:
- //...
- name: Build & test e2e
working-directory: e2e
bazel:
build_flags:
- --enable_bzlmod=false
build_targets:
- //...
test_targets:
Expand All @@ -31,11 +35,11 @@ workflows:
build_targets:
- //...
build_flags:
- --config=bzlmod
- --enable_bzlmod
test_targets:
- //...
test_flags:
- --config=bzlmod
- --enable_bzlmod
- --test_size_filters=-large,-enormous

- <<: *build_workflow
Expand Down
12 changes: 6 additions & 6 deletions WORKSPACE
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,12 @@ http_archive(
],
)

load("@io_bazel_rules_go//go:deps.bzl", "go_register_toolchains", "go_rules_dependencies")

go_rules_dependencies()

go_register_toolchains(version = "1.21.6")

http_archive(
name = "buildifier_prebuilt",
sha256 = "8ada9d88e51ebf5a1fdff37d75ed41d51f5e677cdbeafb0a22dda54747d6e07e",
Expand All @@ -42,12 +48,6 @@ load("@buildifier_prebuilt//:defs.bzl", "buildifier_prebuilt_register_toolchains

buildifier_prebuilt_register_toolchains()

load("@io_bazel_rules_go//go:deps.bzl", "go_register_toolchains", "go_rules_dependencies")

go_rules_dependencies()

go_register_toolchains(version = "1.21.6")

#
# Self dependencies
#
Expand Down
8 changes: 4 additions & 4 deletions e2e/WORKSPACE
Original file line number Diff line number Diff line change
Expand Up @@ -22,18 +22,18 @@ load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")

http_archive(
name = "io_bazel_rules_go",
sha256 = "278b7ff5a826f3dc10f04feaf0b70d48b68748ccd512d7f98bf442077f043fe3",
sha256 = "6734a719993b1ba4ebe9806e853864395a8d3968ad27f9dd759c196b3eb3abe8",
urls = [
"https://mirror.bazel.build/github.com/bazelbuild/rules_go/releases/download/v0.41.0/rules_go-v0.41.0.zip",
"https://github.com/bazelbuild/rules_go/releases/download/v0.41.0/rules_go-v0.41.0.zip",
"https://mirror.bazel.build/github.com/bazelbuild/rules_go/releases/download/v0.45.1/rules_go-v0.45.1.zip",
"https://github.com/bazelbuild/rules_go/releases/download/v0.45.1/rules_go-v0.45.1.zip",
],
)

load("@io_bazel_rules_go//go:deps.bzl", "go_register_toolchains", "go_rules_dependencies")

go_rules_dependencies()

go_register_toolchains(version = "1.20.6")
go_register_toolchains(version = "1.21.6")

load("@rules_gitops//gitops:deps.bzl", "rules_gitops_dependencies")

Expand Down
2 changes: 1 addition & 1 deletion gitops/deps.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ def rules_gitops_dependencies():
name = "aspect_bazel_lib",
sha256 = "b554eb7942a5ab44c90077df6a0c76fc67c5874c9446a007e9ba68be82bd4796",
strip_prefix = "bazel-lib-2.7.1",
url = "https://github.com/aspect-build/bazel-lib/releases/download/v2.4.1/bazel-lib-v2.7.1.tar.gz",
url = "https://github.com/aspect-build/bazel-lib/releases/download/v2.7.1/bazel-lib-v2.7.1.tar.gz",
)

maybe(
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module github.com/fasterci/rules_gitops

go 1.19
go 1.21

require (
github.com/ghodss/yaml v1.0.0
Expand Down
35 changes: 27 additions & 8 deletions testing/it_sidecar/it_sidecar.go
Original file line number Diff line number Diff line change
Expand Up @@ -340,20 +340,31 @@ func cleanup(clientset *kubernetes.Clientset) {
}
}

var ErrTimedOut = errors.New("timed out")
var ErrStdinClosed = errors.New("stdin closed")
var ErrTermSignalReceived = errors.New("TERM signal received")

func main() {
flag.Parse()
log.SetOutput(os.Stdout)
ctx, cancel := context.WithTimeout(context.Background(), *timeout)
defer cancel()
ctx, stopSignal := signal.NotifyContext(ctx, os.Interrupt, syscall.SIGTERM)
defer stopSignal()
ctx, timeoutCancel := context.WithTimeoutCause(context.Background(), *timeout, ErrTimedOut)
defer timeoutCancel()
ctx, cancel := context.WithCancelCause(ctx)
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt, syscall.SIGTERM)
go func() {
<-c
log.Print("First TERM signal, stopping...")
cancel(ErrTermSignalReceived)
signal.Stop(c)
}()
// cancel context if stdin is closed
go func() {
reader := bufio.NewReader(os.Stdin)
for {
_, _, err := reader.ReadRune()
if err != nil && err == io.EOF {
cancel()
cancel(ErrStdinClosed)
break
}
}
Expand All @@ -378,13 +389,12 @@ func main() {
if err != nil {
log.Print(err)
}
cancel()
cancel(fmt.Errorf("terminate due to kubernetes listening failure: %w", err))
}()

listenForEvents(ctx, clientset, func(event *v1.Event) {
if !allowErrors {
log.Println("Terminate due to failure")
cancel()
cancel(fmt.Errorf("terminate due to event %s/%s %s %s", event.Namespace, event.InvolvedObject.Name, event.Reason, event.Message))
}
})

Expand All @@ -405,4 +415,13 @@ func main() {

fmt.Println("READY")
<-ctx.Done()
switch ctx.Err() {
case context.DeadlineExceeded:
log.Print("Deadline exceeded")
case context.Canceled:
log.Print("Context Canceled")
default:
log.Print(ctx.Err())
}

}

0 comments on commit 526acc1

Please sign in to comment.