Skip to content

Commit bc4c402

Browse files
authored
Merge branch 'master' into feature/use-namespace
2 parents 78ebebc + 9505bcb commit bc4c402

File tree

2 files changed

+50
-3
lines changed

2 files changed

+50
-3
lines changed

README.md

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
# What?
66

7-
This is a docker container intended to run inside a kubernetes cluster to collect config maps with a specified label and store the included files in an local folder. The main target is to be run as a sidecar container to supply an application with information from the cluster. The contained python script is working with the Kubernetes API 1.10
7+
This is a docker container intended to run inside a kubernetes cluster to collect config maps with a specified label and store the included files in an local folder. It can also send a html request to a specified URL after a configmap change. The main target is to be run as a sidecar container to supply an application with information from the cluster. The contained python script is working with the Kubernetes API 1.10
88

99
# Why?
1010

@@ -13,6 +13,7 @@ Currently (April 2018) there is no simple way to hand files in configmaps to a s
1313
# How?
1414

1515
Run the container created by this repo together you application in an single pod with a shared volume. Specify which label should be monitored and where the files should be stored.
16+
By adding additional env variables the container can send a html request to specified URL.
1617

1718
# Features
1819

@@ -40,3 +41,18 @@ Example for a simple deployment can be found in `example.yaml`. Depending on the
4041
- description: If specified, the sidecar will search for config-maps inside this namespace
4142
- required: false
4243
- type: string
44+
45+
- `REQ_URL`
46+
- description: URL to which send a request after a configmap got reloaded
47+
- required: false
48+
- type: URI
49+
50+
- `REQ_METHOD`
51+
- description: Request method GET(default) or POST
52+
- required: false
53+
- type: string
54+
55+
- `REQ_PAYLOAD`
56+
- description: If you use POST you can also provide json payload
57+
- required: false
58+
- type: json

sidecar/sidecar.py

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
from kubernetes import client, config, watch
22
import os
33
import sys
4+
import requests
5+
from requests.packages.urllib3.util.retry import Retry
6+
from requests.adapters import HTTPAdapter
47

58

69
def writeTextToFile(folder, filename, data):
@@ -9,6 +12,25 @@ def writeTextToFile(folder, filename, data):
912
f.close()
1013

1114

15+
def request(url, method, payload):
16+
r = requests.Session()
17+
retries = Retry(total = 5,
18+
connect = 5,
19+
backoff_factor = 0.2,
20+
status_forcelist = [ 500, 502, 503, 504 ])
21+
r.mount('http://', HTTPAdapter(max_retries=retries))
22+
r.mount('https://', HTTPAdapter(max_retries=retries))
23+
if url is None:
24+
print("No url provided. Doing nothing.")
25+
# If method is not provided use GET as default
26+
elif method == "GET" or method is None:
27+
res = r.get("%s" % url, timeout=10)
28+
print ("%s request sent to %s. Response: %d %s" % (method, url, res.status_code, res.reason))
29+
elif method == "POST":
30+
res = r.post("%s" % url, json=payload, timeout=10)
31+
print ("%s request sent to %s. Response: %d %s" % (method, url, res.status_code, res.reason))
32+
33+
1234
def removeFile(folder, filename):
1335
completeFile = folder +"/"+filename
1436
if os.path.isfile(completeFile):
@@ -17,7 +39,7 @@ def removeFile(folder, filename):
1739
print("Error: %s file not found" % completeFile)
1840

1941

20-
def watchForChanges(label, targetFolder):
42+
def watchForChanges(label, targetFolder, url, method, payload):
2143
v1 = client.CoreV1Api()
2244
w = watch.Watch()
2345
stream = None
@@ -42,8 +64,12 @@ def watchForChanges(label, targetFolder):
4264
print("File in configmap %s %s" % (filename, eventType))
4365
if (eventType == "ADDED") or (eventType == "MODIFIED"):
4466
writeTextToFile(targetFolder, filename, dataMap[filename])
67+
if url is not None:
68+
request(url, method, payload)
4569
else:
4670
removeFile(targetFolder, filename)
71+
if url is not None:
72+
request(url, method, payload)
4773

4874

4975
def main():
@@ -56,9 +82,14 @@ def main():
5682
if targetFolder is None:
5783
print("Should have added FOLDER as environment variable! Exit")
5884
return -1
85+
86+
method = os.getenv('REQ_METHOD')
87+
url = os.getenv('REQ_URL')
88+
payload = os.getenv('REQ_PAYLOAD')
89+
5990
config.load_incluster_config()
6091
print("Config for cluster api loaded...")
61-
watchForChanges(label, targetFolder)
92+
watchForChanges(label, targetFolder, url, method, payload)
6293

6394

6495
if __name__ == '__main__':

0 commit comments

Comments
 (0)