-
Notifications
You must be signed in to change notification settings - Fork 376
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Define gRPC server for liveness probe
[upstream commit 6e17c79] Now, we use tetra status command to report the status of tetragon agent. This comes with some overheads as tetra binary has a lot of additional functionality and it seems like an overkill to use that for status reporting. On the other hand, k8s supports liveness probes by using an gRPC endpoint (i.e. https://kubernetes.io/docs/tasks/configure-pod-container/configure-liveness-readiness-startup-probes/#define-a-grpc-liveness-probe). This patch first creates a dedicated gRPC server to report agent status that can be used for the liveness probe. Signed-off-by: Anastasios Papagiannis <[email protected]>
- Loading branch information
Showing
13 changed files
with
949 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
// SPDX-License-Identifier: Apache-2.0 | ||
// Copyright Authors of Tetragon | ||
|
||
package health | ||
|
||
import ( | ||
"context" | ||
"net" | ||
"time" | ||
|
||
"github.com/cilium/tetragon/api/v1/tetragon" | ||
"github.com/cilium/tetragon/pkg/logger" | ||
"google.golang.org/grpc" | ||
gh "google.golang.org/grpc/health" | ||
"google.golang.org/grpc/health/grpc_health_v1" | ||
) | ||
|
||
var ( | ||
log = logger.GetLogger() | ||
) | ||
|
||
func StartHealthServer(ctx context.Context, address string, interval int) { | ||
// Create a new health server and mark it as serving. | ||
healthServer := gh.NewServer() | ||
healthServer.SetServingStatus("liveness", grpc_health_v1.HealthCheckResponse_SERVING) | ||
|
||
// Create a new gRPC server for health checks and register the healthServer. | ||
grpcHealthServer := grpc.NewServer() | ||
grpc_health_v1.RegisterHealthServer(grpcHealthServer, healthServer) | ||
|
||
// Start the gRPC server for the health checks. | ||
go func() { | ||
// the gRPC server for the health checks listens on port 6789 | ||
listener, err := net.Listen("tcp", address) | ||
if err != nil { | ||
log.WithError(err).Fatal("Failed to listen for gRPC healthserver") | ||
} | ||
|
||
log.WithField("address", address).WithField("interval", interval).Info("Starting gRPC health server") | ||
if err = grpcHealthServer.Serve(listener); err != nil { | ||
log.WithError(err).Fatal("Failed to start gRPC healthserver") | ||
} | ||
}() | ||
|
||
// Check the agent health periodically. To check if our agent is health we call | ||
// health.GetHealth() and we report the status to the healthServer. | ||
go func() { | ||
ticker := time.NewTicker(time.Duration(interval) * time.Second) | ||
for { | ||
select { | ||
case <-ticker.C: | ||
servingStatus := grpc_health_v1.HealthCheckResponse_NOT_SERVING | ||
if response, err := GetHealth(); err == nil { | ||
if st := response.GetHealthStatus(); len(st) > 0 && st[0].Status == tetragon.HealthStatusResult_HEALTH_STATUS_RUNNING { | ||
servingStatus = grpc_health_v1.HealthCheckResponse_SERVING | ||
} | ||
} | ||
healthServer.SetServingStatus("liveness", servingStatus) | ||
case <-ctx.Done(): | ||
ticker.Stop() | ||
healthServer.Shutdown() // set all services to NOT_SERVING | ||
grpcHealthServer.Stop() | ||
return | ||
} | ||
} | ||
}() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.