Compare commits

...
Author SHA1 Message Date
ElevenNotes 69f14549c6 rediss://localhost:6379/0 2023-12-04 15:16:53 +01:00
ElevenNotes 6ab10f6b2a sanitize container name 2023-12-04 14:43:43 +01:00
ElevenNotes 35fee8e82b status:null for poll 2023-12-04 14:42:57 +01:00
ElevenNotes 8686ba58c0 log for container event only if traefik label 2023-12-04 14:28:23 +01:00
ElevenNotes 437a3e92d3 event?.status 2023-12-04 14:09:20 +01:00
ElevenNotes 4796cc1aa1 update 0.1.3 2023-12-04 14:03:26 +01:00
4 changed files with 22 additions and 13 deletions
+2 -2
View File
@@ -1,5 +1,5 @@
# Alpine :: Traefik Labels
![size](https://img.shields.io/docker/image-size/11notes/traefik-labels/0.1.2?color=0eb305) ![version](https://img.shields.io/docker/v/11notes/traefik-labels?color=eb7a09) ![pulls](https://img.shields.io/docker/pulls/11notes/traefik-labels?color=2b75d6) ![activity](https://img.shields.io/github/commit-activity/m/11notes/docker-traefik-labels?color=c91cb8) ![commit-last](https://img.shields.io/github/last-commit/11notes/docker-traefik-labels?color=c91cb8)
![size](https://img.shields.io/docker/image-size/11notes/traefik-labels/0.1.3?color=0eb305) ![version](https://img.shields.io/docker/v/11notes/traefik-labels?color=eb7a09) ![pulls](https://img.shields.io/docker/pulls/11notes/traefik-labels?color=2b75d6) ![activity](https://img.shields.io/github/commit-activity/m/11notes/docker-traefik-labels?color=c91cb8) ![commit-last](https://img.shields.io/github/last-commit/11notes/docker-traefik-labels?color=c91cb8)
Run Traefik Labels based on Alpine Linux. Small, lightweight, secure and fast 🏔️
@@ -39,7 +39,7 @@ docker run --name demo \
## Environment
| Parameter | Value | Default |
| --- | --- | --- |
| `LABELS_REDIS_URL` | the redis URL to connect, use rediss:// for SSL | redis:://localhost:6379/0 |
| `LABELS_REDIS_URL` | the redis URL to connect, use rediss:// for SSL | rediss://localhost:6379/0 |
| `LABELS_INTERVAL` | in what interval container information is pulled | 300 |
| `LABELS_TIMEOUT` | how many seconds after an interval the keys should stay till they expire | 30 |
-1
View File
@@ -1,6 +1,5 @@
# :: Header
FROM 11notes/node:stable
ENV APP_VERSION=0.1.1
ENV APP_ROOT=/labels
# :: Run
+19 -9
View File
@@ -30,7 +30,7 @@ class Labels{
this.#redis.connect();
this.#redis.on('ready', ()=>{
this.#log('connected to Redis');
this.#log('successfully connected to redis database');
(async() => {
await this.dockerPoll();
})();
@@ -48,13 +48,16 @@ class Labels{
dockerEvents(){
this.#docker.getEvents({}, (error, data) => {
data.on('data', async(chunk) => {
const event = JSON.parse(chunk.toString('utf8'));
if(/Container/i.test(event?.Type) && /start|stop|restart|kill|die|destroy/i.test(event?.status)){
this.#log(`new docker event for container ${event.id}`);
await this.dockerInspect(event.id, event.status);
}
});
if(error){
this.#log(error, ERROR);
}else{
data.on('data', async(chunk) => {
const event = JSON.parse(chunk.toString('utf8'));
if(/Container/i.test(event?.Type) && /^(start|stop|restart|kill|die|destroy)$/i.test(event?.status)){
await this.dockerInspect(event.id, event.status);
}
});
}
});
}
@@ -65,7 +68,7 @@ class Labels{
this.#log(`poll start (interval:${process.env.LABELS_INTERVAL})}`);
this.#docker.listContainers((error, containers) => {
containers.forEach(async(container) => {
await this.dockerInspect(container.Id, 'start');
await this.dockerInspect(container.Id);
});
});
}catch(e){
@@ -79,6 +82,7 @@ class Labels{
dockerInspect(id, status = null){
return(new Promise((resolve, reject) => {
let log = false;
const container = this.#docker.getContainer(id);
container.inspect(async(error, data) => {
if(error){
@@ -86,6 +90,12 @@ class Labels{
}
for(const label in data?.Config?.Labels){
if(/traefik\//i.test(label)){
if(!log){
this.#log(`inspect container ${data.Name.replace(/^\//i, '')}${(
(null === status) ? '' : ` event[${status}]`
)}`);
log = true;
}
switch(true){
case /start|restart/i.test(status):
await this.#redis.set(label, data?.Config?.Labels[label], {EX:parseInt(process.env.LABELS_INTERVAL) + parseInt(process.env.LABELS_TIMEOUT)});
+1 -1
View File
@@ -3,7 +3,7 @@ process.once('SIGINT', () => process.exit(0));
const { fork } = require('node:child_process');
const child = fork(`${__dirname}/app.js`, [], {
env:{
LABELS_REDIS_URL:process.env?.LABELS_REDIS_URL || 'redis:://localhost:6379/0',
LABELS_REDIS_URL:process.env?.LABELS_REDIS_URL || 'rediss://localhost:6379/0',
LABELS_INTERVAL:parseInt(process.env?.LABELS_INTERVAL || 300),
LABELS_TIMEOUT:parseInt(process.env?.LABELS_TIMEOUT|| 30),
}