Compare commits
6
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
69f14549c6 | ||
|
|
6ab10f6b2a | ||
|
|
35fee8e82b | ||
|
|
8686ba58c0 | ||
|
|
437a3e92d3 | ||
|
|
4796cc1aa1 |
@@ -1,5 +1,5 @@
|
||||
# Alpine :: Traefik Labels
|
||||
    
|
||||
    
|
||||
|
||||
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,6 +1,5 @@
|
||||
# :: Header
|
||||
FROM 11notes/node:stable
|
||||
ENV APP_VERSION=0.1.1
|
||||
ENV APP_ROOT=/labels
|
||||
|
||||
# :: Run
|
||||
|
||||
+19
-9
@@ -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)});
|
||||
|
||||
@@ -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),
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user