Skip to content

Commit 2511caa

Browse files
committed
added Full Discoveries to give the endpoint a complete Update
1 parent a93f67a commit 2511caa

4 files changed

Lines changed: 100 additions & 16 deletions

File tree

.github/templates/README.template.md

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,22 @@ If no Key is provided ServDiscovery will leave out the Authorization Header.
5656

5757
### DISCOVERY_INTERVAL
5858

59-
The Discovery Interval sets the Interval of which ServDiscovery will update the Endpoint, etc.
59+
The Discovery Interval sets the Interval (in seconds) of which ServDiscovery will update the provided Endpoint.
60+
61+
Default: `60`
62+
63+
### FULL_DISCOVERY_INTERVAL
64+
65+
Sets the Interval for Full Disoveries.
66+
This tells ServDiscovery to send a Full Update of all the activate Containers in the `added` JSON Key.
67+
68+
> [!IMPORTANT]
69+
> Must be set to a **fraction of** DISCOVERY_INTERVAL.
70+
> (example: `DISCOVERY_INTERVAL * 2`)
71+
> `FULL_DISCOVERY_INTERVAL / DISCOVERY_INTERVAL` must result in an int.
72+
> `FULL_DISCOVERY_INTERVAL` must be bigger than `DISCOVERY_INTERVAL`.
73+
74+
Default: **Disabled**
6075

6176
## Contributing
6277

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
.env
2-
.venv
2+
.venv
3+
*.lua

app.py

Lines changed: 81 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,9 @@
2929
ENDPOINT_KEY = os.getenv("ENDPOINT_KEY")
3030

3131
DISCOVERY_INTERVAL = os.getenv("DISCOVERY_INTERVAL")
32+
FULL_DISCOVERY_INTERVAL = os.getenv("FULL_DISCOVERY_INTERVAL")
33+
34+
fullDiscoveryRatio = 0
3235

3336
dockerClient = DockerClient(base_url="unix://var/run/docker.sock")
3437

@@ -60,19 +63,25 @@ def getHostsFromLabels(labels):
6063

6164
return hosts
6265

63-
def updateEnabledContainers():
66+
def getEnabledContainers():
67+
newContainers = []
68+
69+
try:
70+
newContainers = dockerClient.containers.list(filters={"label": "discovery.enable=true"})
71+
except Exception as e:
72+
logger.error(f"Error fetching containers: {str(e)}")
73+
74+
return newContainers
75+
76+
def getDiscovery():
6477
diffs = {
6578
"removed": [],
6679
"added": []
6780
}
6881

6982
global containers
70-
newContainers = []
7183

72-
try:
73-
newContainers = dockerClient.containers.list(filters={"label": "discovery.enable=true"})
74-
except Exception as e:
75-
logger.error(f"Error fetching containers: {str(e)}")
84+
newContainers = getEnabledContainers()
7685

7786
containerDiff = getDiff(containers, newContainers)
7887

@@ -124,6 +133,23 @@ def updateEnabledContainers():
124133

125134
return diffs
126135

136+
def getFullDiscovery():
137+
diffs = {
138+
"removed": [],
139+
"added": []
140+
}
141+
142+
newContainers = getEnabledContainers()
143+
144+
logger.info(f"Found {newContainers.__len__()} Containers")
145+
146+
for container in newContainers:
147+
hosts = getHostsFromLabels(container.labels)
148+
149+
diffs.get("added").extend(hosts)
150+
151+
return diffs
152+
127153
def cleanDiff(diff):
128154
removed, added = set(diff.get("removed")), set(diff.get("added"))
129155

@@ -163,21 +189,49 @@ def sendDiffToEndpoint(diff):
163189
# thread = threading.Thread(target=main, daemon=True)
164190
# thread.start()
165191

192+
def doDiscovery():
193+
logger.debug("Starting Discovery")
194+
195+
globalDiff = getDiscovery()
196+
197+
logger.debug("Cleaning Diff")
198+
199+
return cleanDiff(globalDiff)
200+
201+
def doFullDiscovery():
202+
logger.debug("Starting Full Discovery")
203+
204+
globalDiff = getFullDiscovery()
205+
206+
return globalDiff
207+
166208
def main():
209+
i = 0
167210
while True:
168-
logger.info(f"Starting Discover in {DISCOVERY_INTERVAL}...")
211+
isFullDiscovery = False
169212

170-
sleep(DISCOVERY_INTERVAL)
171-
172-
logger.info("Starting Discovery")
213+
if i == fullDiscoveryRatio:
214+
isFullDiscovery = True
215+
i = 0
173216

174-
globalDiff = updateEnabledContainers()
217+
if not isFullDiscovery:
218+
logger.info(f"Starting Discover in {DISCOVERY_INTERVAL}...")
219+
else:
220+
logger.info(f"Starting Full Discover in {DISCOVERY_INTERVAL}...")
175221

176-
logger.debug("Cleaning Diff")
222+
sleep(DISCOVERY_INTERVAL)
177223

178-
globalDiff = cleanDiff(globalDiff)
224+
globalDiff = {
225+
"removed": [],
226+
"added": []
227+
}
179228

180-
# Check if there is actually any Diff
229+
if not isFullDiscovery:
230+
globalDiff = doDiscovery()
231+
else:
232+
globalDiff = doFullDiscovery()
233+
234+
# Check if there are any Changes
181235
if globalDiff.get("removed").__len__() + globalDiff.get("added").__len__() <= 0:
182236
logger.debug("No Changes were made, skipping...")
183237
continue
@@ -188,6 +242,8 @@ def main():
188242

189243
logger.debug(f"Endpoint responded with {response.text} {response.status_code} {"OK" if response.ok else "NOT OK"}")
190244

245+
i += 1
246+
191247
if __name__ == '__main__':
192248
logger.setLevel(level=LOG_LEVEL)
193249

@@ -203,6 +259,17 @@ def main():
203259
logger.warning(f"No DISCOVERY_INTERVAL set, using 30sec as default")
204260
DISCOVERY_INTERVAL = 30
205261

262+
if not FULL_DISCOVERY_INTERVAL:
263+
logger.warning(f"No FULL_DISCOVERY_INTERVAL set, disregarding Full Discoveries")
264+
FULL_DISCOVERY_INTERVAL = 0
265+
elif FULL_DISCOVERY_INTERVAL % DISCOVERY_INTERVAL != 0:
266+
logger.warning(f"FULL_DISCOVERY_INTERVAL is not a valid Integer, rounding to nearest Int")
267+
FULL_DISCOVERY_INTERVAL = round(FULL_DISCOVERY_INTERVAL)
268+
269+
if FULL_DISCOVERY_INTERVAL != 0:
270+
fullDiscoveryRatio = FULL_DISCOVERY_INTERVAL / DISCOVERY_INTERVAL
271+
logger.debug(f"FULL_DISCOVERY_INTERVAL ratio set to {fullDiscoveryRatio}")
272+
206273
if not ENDPOINT_KEY or ENDPOINT_KEY == "":
207274
logger.warning(f"No ENDPOINT_KEY set, requests may be denied")
208275

docker-compose.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ services:
55
environment:
66
ENDPOINT: https://mydomain.com/ENDPOINT
77
ENDPOINT_KEY: MY_VERY_SECURE_KEY
8+
ALIVE_UPDATE_INTERVAL: 60
89
SERVER_NAME: server-1
910
volumes:
1011
- /var/run/docker.sock:/var/run/docker.sock

0 commit comments

Comments
 (0)