Skip to content

Commit

Permalink
Add !Snippets, see Use traefik labels as label prefix #105
Browse files Browse the repository at this point in the history
  • Loading branch information
BigBoot committed Jan 21, 2025
1 parent fbbd4d3 commit 3188db2
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 6 deletions.
17 changes: 17 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -311,6 +311,23 @@ Keyword monitor with custom status_codes:
kuma.__web: '{ "name": "Example HTTP", "url": "https://example.com", "keyword": "Example Domain", "status_codes": ["200"] }'
```

#### !Snippets
There's a special case for snippets starting with a `!`, these snippets will apply to labels without requiring the prefix (i.e. `kuma.__`). The purpose of these is to be able to reuse existing labels from other tools. (Note: Due to this !Snippets will always receive a single string argument containing the label value instead of a structured list).

For example you could create a snippet to reuse traefik labels by defining a snippet called `!traefik.enable`:
```jinja
{# Only apply if value is "true" #}
{% if args[0] == "true" %}
{# Extract some information from existing labels, note that this will likely not fit your setup and you will need to adjust this to get the required data #}
{% set traefik_service = container_name %}
{% set domain = container_name + ".example.com" %}
{% set port = container["Labels"]["traefik.http.services." + traefik_service + ".loadbalancer.server.port"] %}
{{ container_name }}_http.http.name: {{ container_name }}
{{ container_name }}_http.http.url: https://{{ domain }}:{{ port }}
{% endif %}
```


### Static Monitors 📊
In addition to reading Monitors from Docker labels, AutoKuma can create Monitors from files. This can be usefull if you have want AutoKuma to manage monitors which aren't directly related to a container.
Expand Down
16 changes: 10 additions & 6 deletions autokuma/src/entity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -181,11 +181,15 @@ pub fn get_entities_from_labels(
format!("Snippet '{}' not found!", key)
});

let args = serde_json::from_str::<Vec<serde_json::Value>>(&format!("[{}]", value))
.log_warn(std::module_path!(), |e| {
format!("Error while parsing snippet arguments: {}", e.to_string())
})
.ok();
let args = if key.starts_with("__!") {
Some(vec![serde_json::Value::String(value.to_owned())])
} else {
serde_json::from_str::<Vec<serde_json::Value>>(&format!("[{}]", value))
.log_warn(std::module_path!(), |e| {
format!("Error while parsing snippet arguments: {}", e.to_string())
})
.ok()
};

if let (Some(snippet), Some(args)) = (snippet, args) {
let mut template_values = template_values.clone();
Expand Down Expand Up @@ -248,7 +252,7 @@ pub fn get_entities_from_labels(
match result {
Err(Error::NameNotFound(name)) => {
warn!(
"Cannot create monitor {} because referenced {} with {} is not found",
"Cannot create monitor {} because referenced {} with name {} is not found",
id,
name.type_name(),
name.name()
Expand Down
2 changes: 2 additions & 0 deletions autokuma/src/sources/docker_source.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ async fn get_kuma_containers(
|labels| {
labels.keys().any(|key| {
key.starts_with(&format!("{}.", state.config.docker.label_prefix))
|| state.config.snippets.contains_key(&format!("!{}", key))
})
},
)
Expand Down Expand Up @@ -69,6 +70,7 @@ async fn get_kuma_services(state: Arc<AppState>, docker: &Docker) -> Result<Vec<
|labels| {
labels.keys().any(|key| {
key.starts_with(&format!("{}.", state.config.docker.label_prefix))
|| state.config.snippets.contains_key(&format!("!{}", key))
})
},
)
Expand Down

0 comments on commit 3188db2

Please sign in to comment.