Skip to content

Commit

Permalink
feat: handle os signals (#126)
Browse files Browse the repository at this point in the history
* feat: add os signal handling for graceful shutdown

* chore: housekeeping
  • Loading branch information
lvlcn-t authored Apr 2, 2024
1 parent d786d77 commit cf97baa
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 41 deletions.
78 changes: 46 additions & 32 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
# Sparrow - Infrastructure Monitoring<!-- omit from toc -->

<!-- markdownlint-disable MD033 -->
<p align="center">
<a href="/../../commits/" title="Last Commit"><img src="https://img.shields.io/github/last-commit/caas-team/sparrow?style=flat"></a>
<a href="/../../issues" title="Open Issues"><img src="https://img.shields.io/github/issues/caas-team/sparrow?style=flat"></a>
<a href="./LICENSE" title="License"><img src="https://img.shields.io/badge/License-Apache%202.0-green.svg?style=flat"></a>
<a href="/../../commits/" title="Last Commit"><img alt="Last Commit" src="https://img.shields.io/github/last-commit/caas-team/sparrow?style=flat"></a>
<a href="/../../issues" title="Open Issues"><img alt="Open Issues" src="https://img.shields.io/github/issues/caas-team/sparrow?style=flat"></a>
<a href="./LICENSE" title="License"><img alt="License" src="https://img.shields.io/badge/License-Apache%202.0-green.svg?style=flat"></a>
</p>
<!-- markdownlint-enable MD033 -->

- [About this component](#about-this-component)
- [Installation](#installation)
Expand All @@ -15,7 +17,7 @@
- [Image](#image)
- [Configuration](#configuration)
- [Startup](#startup)
- [Example startup configuration](#example-startup-configuration)
- [Example Startup Configuration](#example-startup-configuration)
- [Loader](#loader)
- [Logging Configuration](#logging-configuration)
- [Checks](#checks)
Expand Down Expand Up @@ -187,23 +189,24 @@ export SPARROW_ANY_OTHER_OPTION="Some value"

Just write out the path to the attribute, delimited by `_`.

#### Example startup configuration
#### Example Startup Configuration

```yaml
# DNS sparrow is exposed on
name: sparrow.example.com
# Selects and configures a loader to continuously fetch the checks' configuration at runtime
loader:
# defines which loader to use. Options: "file | http"
# Defines which loader to use. Options: "file | http"
type: http
# the interval in which sparrow tries to fetch a new configuration
# if this isn't set or set to 0, the loader will only retrieve the configuration once
# The interval in which sparrow tries to fetch a new configuration
# If this isn't set or set to 0, the loader will only retrieve the configuration once
interval: 30s
# config specific to the http loader
# Config specific to the http loader
http:
# The url where the config is located
# The URL where the config is located
url: https://myconfig.example.com/config.yaml
# This token is passed in the Authorization header, when refreshing the config
# This token is passed in the Authorization header when refreshing the config
token: xxxxxxx
# A timeout for the config refresh
timeout: 30s
Expand All @@ -213,41 +216,43 @@ loader:
# How many times to retry
count: 3
# config specific to the file loader
# Config specific to the file loader
# The file loader is not intended for production use
file:
# location of the file in the local filesystem
# Location of the file in the local filesystem
path: ./config.yaml
# Configures the api
# Configures the API
api:
# Which address to expose sparrows rest api on
# Which address to expose Sparrow's REST API on
address: :8080
# Configures the target manager
# Omitting this section will disable the target manager
targetManager:
# Defines which target manager to use.
type: gitlab
# The interval for the target reconciliation process
checkInterval: 1m
# How often the instance should register itself as a global target.
# A duration of 0 means no registration.
# How often the instance should register itself as a global target
# A duration of 0 means no registration
registrationInterval: 1m
# How often the instance should update its registration as a global target.
# A duration of 0 means no update.
# How often the instance should update its registration as a global target
# A duration of 0 means no update
updateInterval: 120m
# The amount of time a target can be unhealthy
# before it is removed from the global target list.
# A duration of 0 means no removal.
# before it is removed from the global target list
# A duration of 0 means no removal
unhealthyThreshold: 360m
# Configuration options for the gitlab target manager
# Configuration options for the GitLab target manager
gitlab:
# The url of your gitlab host
# The URL of your GitLab host
baseUrl: https://gitlab.com
# Your gitlab api token
# you can also set this value through the
# SPARROW_TARGETMANAGER_GITLAB_TOKEN environment variable
# Your GitLab API token
# You can also set this value through the SPARROW_TARGETMANAGER_GITLAB_TOKEN environment variable
token: glpat-xxxxxxxx
# the id of your gitlab project. This is where sparrow will register itself
# and grab the list of other sparrows from
# The ID of your GitLab project. This is where Sparrow will register itself
# and grab the list of other Sparrows from
projectId: 18923
```

Expand Down Expand Up @@ -301,12 +306,13 @@ in the startup YAML configuration file as shown in the [example configuration](#

| Type | Description |
| ------------------------------------ | -------------------------------------------------------------------------------------------- |
| `targetManager.type` | Type of the target manager. Options: `gitlab` |
| `targetManager.checkInterval` | Interval for checking new targets. |
| `targetManager.unhealthyThreshold` | Threshold for marking a target as unhealthy. 0 means no cleanup. |
| `targetManager.registrationInterval` | Interval for registering the current sparrow at the target backend. 0 means no registration. |
| `targetManager.updateInterval` | Interval for updating the registration of the current sparrow. 0 means no update. |
| `targetManager.gitlab.token` | Token for authenticating with the GitLab instance. |
| `targetManager.gitlab.baseUrl` | Base URL of the GitLab instance. |
| `targetManager.gitlab.token` | Token for authenticating with the GitLab instance. |
| `targetManager.gitlab.projectId` | Project ID for the GitLab project used as a remote state backend. |

Currently, only one target manager exists: the Gitlab target manager. It uses a gitlab project as the remote state
Expand Down Expand Up @@ -367,7 +373,9 @@ Available configuration options:
| `retry.delay` | `duration` | Initial delay between retries for the latency check. |
| `targets` | `list of strings` | List of targets to send latency probe. Needs to be a valid URL. Can be another `sparrow` instance. Automatically updated when a targetManager is configured. |

<!-- markdownlint-disable MD024 -->
#### Example configuration
<!-- markdownlint-enable MD024 -->

```yaml
latency:
Expand Down Expand Up @@ -410,7 +418,9 @@ Available configuration options:
| `retry.delay` | `duration` | Initial delay between retries for the DNS check. |
| `targets` | `list of strings` | List of targets to lookup. Needs to be a valid domain or IP. Can be another `sparrow` instance. Automatically updated when a targetManager is configured. |

<!-- markdownlint-disable MD024 -->
#### Example configuration
<!-- markdownlint-enable MD024 -->

```yaml
dns:
Expand Down Expand Up @@ -458,7 +468,9 @@ dns:
| `targets[].addr` | `string` | The address of the target to traceroute to. Can be an IP address or DNS name |
| `targets[].port` | `uint16` | The port of the target to traceroute to. Default is 80 |

<!-- markdownlint-disable MD024 -->
#### Example configuration
<!-- markdownlint-enable MD024 -->

```yaml
traceroute:
Expand Down Expand Up @@ -511,9 +523,11 @@ The application itself and all end-user facing content will be made available in

The following channels are available for discussions, feedback, and support requests:

| Type | Channel |
| ---------- | ------------------------------------------------------------------------------------------------------------------------------------------------------ |
| **Issues** | <a href="/../../issues/new/choose" title="General Discussion"><img src="https://img.shields.io/github/issues/caas-team/sparrow?style=flat-square"></a> |
<!-- markdownlint-disable MD033 -->
| Type | Channel |
| ---------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| **Issues** | <a href="/../../issues/new/choose" title="General Discussion"><img alt="Issues" src="https://img.shields.io/github/issues/caas-team/sparrow?style=flat-square"></a> |
<!-- markdownlint-enable MD033 -->

## How to Contribute

Expand Down
3 changes: 1 addition & 2 deletions chart/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,5 @@ A Helm chart to install Sparrow
| startupProbe | object | `{"enabled":false,"failureThreshold":10,"initialDelaySeconds":10,"path":"/","periodSeconds":5,"successThreshold":1,"timeoutSeconds":1}` | Specifies the configuration for a startup probe to check if the sparrow application is started. Ref: https://kubernetes.io/docs/tasks/configure-pod-container/configure-liveness-readiness-startup-probes/ |
| tolerations | list | `[]` | |


----------------------------------------------
Autogenerated from chart metadata using [helm-docs v1.13.1](https://github.com/norwoodj/helm-docs/releases/v1.13.1)

3 changes: 2 additions & 1 deletion chart/values.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ sparrowConfig:
# type: http | file
# interval: 30s
# http:
# url: ""
# url: https://myconfig.example.com/config.yaml
# token: ""
# timeout: 30s
# retry:
Expand All @@ -165,6 +165,7 @@ sparrowConfig:
# file:
# path: /config/checks.yaml
# targetManager:
# type: gitlab
# checkInterval: 300s
# unhealthyThreshold: 600s
# registrationInterval: 300s
Expand Down
24 changes: 20 additions & 4 deletions cmd/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ package cmd
import (
"context"
"fmt"
"os"
"os/signal"
"syscall"
"time"

"github.com/spf13/cobra"
Expand Down Expand Up @@ -70,17 +73,30 @@ func run() func(cmd *cobra.Command, args []string) error {
return fmt.Errorf("failed to parse config: %w", err)
}

log := logger.NewLogger()
ctx := logger.IntoContext(context.Background(), log)
ctx, cancel := logger.NewContextWithLogger(context.Background())
log := logger.FromContext(ctx)
defer cancel()

if err = cfg.Validate(ctx); err != nil {
return fmt.Errorf("error while validating the config: %w", err)
}

sigChan := make(chan os.Signal, 1)
signal.Notify(sigChan, syscall.SIGINT, syscall.SIGTERM)

s := sparrow.New(cfg)
cErr := make(chan error, 1)
log.Info("Running sparrow")
if err = s.Run(ctx); err != nil {
err = fmt.Errorf("error while running sparrow: %w", err)
go func() {
cErr <- s.Run(ctx)
}()

select {
case <-sigChan:
log.Info("Signal received, shutting down")
cancel()
<-cErr
case err := <-cErr:
return err
}

Expand Down
3 changes: 1 addition & 2 deletions pkg/checks/traceroute/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,12 @@ func (c *Config) Validate() error {

for i, t := range c.Targets {
ip := net.ParseIP(t.Addr)

if ip != nil {
continue
}

_, err := url.Parse(t.Addr)
if err != nil && ip == nil {
if err != nil {
return checks.ErrInvalidConfig{CheckName: CheckName, Field: fmt.Sprintf("traceroute.targets[%d].addr", i), Reason: "invalid url or ip"}
}
}
Expand Down

0 comments on commit cf97baa

Please sign in to comment.