Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: git interactor for remote instance registration #125

Closed
wants to merge 9 commits into from
Closed
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
118 changes: 69 additions & 49 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 MD037 -->

- [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,42 +216,51 @@ 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. Options: "gitlab | git"
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
# Configuration options for the Git target manager
git:
# The URL of your Git repository
repoUrl: https://my-git-instance.sparrow.com/my-repo.git
# Your Git Personal Access Token
# You can also set this value through the SPARROW_TARGETMANAGER_GIT_TOKEN environment variable
token: gitpat-xxxxxxxxxxxx
```

#### Loader
Expand All @@ -259,14 +271,12 @@ You select which loader is used by setting the `loaderType` parameter.

Available loaders:

- `http` (default): Retrieves the checks' configuration from a remote endpoint during runtime. Additional configuration
parameters are set in the `loader.http` section.
- `http` (default): Retrieves the checks' configuration from a remote endpoint during runtime. Additional configuration parameters are set in the `loader.http` section.

- `file`: Loads the checks' configuration from a local file during runtime. Additional configuration
parameters are set in the `loader.file` section.

If you want to retrieve the checks' configuration only once, you can set `loader.interval` to 0.
The target manager is currently not functional in combination with this configuration.
If you want to retrieve the checks' configuration only once, you can set `loader.interval` to 0. The target manager is currently not functional in combination with this configuration.

#### Logging Configuration

Expand All @@ -279,11 +289,9 @@ You can configure the logging behavior of the sparrow instance by setting the fo

### Checks

In addition to the technical startup configuration, the `sparrow` checks' configuration can be dynamically loaded during runtime.
The `loader` is capable of dynamically loading and configuring checks.
In addition to the technical startup configuration, the `sparrow` checks' configuration can be dynamically loaded during runtime. The `loader` is capable of dynamically loading and configuring checks.

For detailed information on available loader configuration options, please refer
to [this documentation](docs/sparrow_run.md).
For detailed information on available loader configuration options, please refer to [this documentation](docs/sparrow_run.md).

Example format of a configuration file for the checks:

Expand All @@ -294,26 +302,30 @@ health:

### Target Manager

The `sparrow` can optionally manage targets for checks and register itself as a target on a (remote) backend through
the `TargetManager` interface. This feature is optional; if the startup configuration does not include
the `targetManager`, it will not be used. When configured, it offers various settings, detailed below, which can be set
in the startup YAML configuration file as shown in the [example configuration](#example-startup-configuration).
The `sparrow` allows to optionally manage targets for checks and register itself as a target on a (remote) backend through the `TargetManager` interface. You can set the configuration settings for the `targetManager` in the startup YAML configuration file, as shown in the [example startup configuration](#example-startup-configuration). If the startup configuration does not include the `targetManager`, it will not be used.

The `TargetManager` supports both GitLab and Git as remote state backends, allowing `sparrow` instances to register themselves, discover new targets, and update their status. This functionality ensures dynamic instance management across various platforms and environments.

The following table details the configuration options available for the `TargetManager`:

| Type | Description |
| ------------------------------------ | -------------------------------------------------------------------------------------------- |
| `targetManager.type` | Type of the target manager. Options: `gitlab` or `git` |
| `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.git.repoUrl` | URL of the Git repository used as a remote state backend. |
| `targetManager.git.token` | Token for authenticating with the Git repository. |
| `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
backend. The various `sparrow` instances can register themselves as targets in the project.
The `sparrow` instances will also check the project for new targets and add them to the local state.
The registration is done by committing a "state" file in the main branch of the repository,
which is named after the DNS name of the `sparrow`. The state file contains the following information:
By configuring the `TargetManager`, `sparrow` instances can seamlessly integrate with your chosen Git-based remote repository, whether hosted on GitLab or another Git service. This enables efficient synchronization and management of global targets across your distributed `sparrow` instances.

The registration of a `sparrow` instance is accomplished by committing a "state" file in the `main` branch of the repository, named after the DNS name of the `sparrow`. The state file contains essential information such as the instance's URL and the last seen timestamp, allowing for effective target management and health checks.

Example state file content for a registered `sparrow` instance:

```json
{
Expand All @@ -322,6 +334,8 @@ which is named after the DNS name of the `sparrow`. The state file contains the
}
```

This configuration enables dynamic and scalable management of `sparrow` instances across diverse operational environments, facilitating robust health checks and target management.

### Check: Health

Available configuration options:
Expand Down Expand Up @@ -367,7 +381,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 +426,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 +476,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 +531,9 @@ 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> |
| 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> |

## 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)

6 changes: 5 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,10 +165,14 @@ sparrowConfig:
# file:
# path: /config/checks.yaml
# targetManager:
# type: git | gitlab
# checkInterval: 300s
# unhealthyThreshold: 600s
# registrationInterval: 300s
# updateInterval: 900s
# git:
# repoUrl: https://mygit.example.com/myrepo.git
# token: ""
# gitlab:
# token: ""
# baseUrl: https://gitlab.com
Expand Down
26 changes: 22 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,32 @@ 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() {
if err := s.Run(ctx); err != nil {
cErr <- fmt.Errorf("error while running sparrow: %w", err)
}
}()

select {
case <-sigChan:
log.Info("Signal received, shutting down")
cancel()
<-cErr
case err := <-cErr:
return err
}
lvlcn-t marked this conversation as resolved.
Show resolved Hide resolved

Expand Down
Loading
Loading