-
Notifications
You must be signed in to change notification settings - Fork 18
/
ecrupdater.py
181 lines (161 loc) · 6.99 KB
/
ecrupdater.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
import sys
import os
import json
import time
import base64
import boto3
import logging
import logging.config
from kubernetes import client as k8sclient
from kubernetes.client import Configuration, ApiClient, ApiException
def setup_logging(
log_config_file=os.environ.get('LOG_CONFIG', './logging.json'),
# fallback if no logger config file has been configured
default_level=os.environ.get('LOG_LEVEL', 'INFO')
):
if os.path.exists(log_config_file):
print("Loading log configuration file from path '{}'...".format(log_config_file))
with open(log_config_file, 'rt') as f:
config = json.load(f)
logging.config.dictConfig(config)
else:
print("No Log config file provided, loading default configuration.")
logging.basicConfig(level=default_level)
def exception_handler(exception_type, exception, traceback):
# Removes standard python stacktrace in case of exceptions
logger.warning("%s: %s", exception_type.__name__, exception)
# Custom exception hook to hide traceback info from logs
if not os.getenv('CDV2_DEBUG_ENABLED') == 'TRUE':
sys.excepthook = exception_handler
print('Starting ECR-updater ..')
pull_secret_name = os.getenv('K8S_PULL_SECRET_NAME', None)
env_update_interval = os.getenv('ECR_UPDATE_INTERVAL', '3600')
create_missing_pull_secrets_str = os.getenv('ECR_CREATE_MISSING', 'false')
# Allows the kubernetes python clients to talk to k8s via the kubectl-proxy sidecar container
kubernetes_api_endpoint = os.getenv('KUBERNETES_API_ENDPOINT', 'http://localhost:8001')
try:
update_interval = int(env_update_interval)
except:
raise ValueError(f'unable to parse {env_update_interval} into seconds, exiting')
if pull_secret_name is None:
raise ValueError('Specify name of secret in env variable K8S_PULL_SECRET_NAME')
def create_pull_secrets():
if create_missing_pull_secrets_str.lower() != 'true':
return None
k8s_config = Configuration()
k8s_config.host = kubernetes_api_endpoint
k8s_api_client = ApiClient(configuration=k8s_config)
v1 = k8sclient.CoreV1Api(api_client=k8s_api_client)
namespaces = v1.list_namespace()
for namespace in namespaces.items:
ns_secrets = v1.list_namespaced_secret(namespace.metadata.name)
has_ecr_secret = [x for x in ns_secrets.items if x.metadata.name == pull_secret_name]
if not has_ecr_secret:
k8s_secret = {
'server':
{
'username': 'temp',
'password': 'temp'
}
}
b64_k8s_secret = base64.b64encode(json.dumps(k8s_secret).encode('utf-8')).decode('utf-8')
secret_body = {
'kind': 'Secret',
'apiVersion': 'v1',
'metadata': {
'name': pull_secret_name,
'creationTimestamp': None
},
'data': {
'.dockerconfigjson': b64_k8s_secret
},
'type': 'kubernetes.io/dockerconfigjson'
}
logger.info('Creating secret %s in namespace %s', pull_secret_name, namespace.metadata.name)
try:
v1.create_namespaced_secret(namespace.metadata.name, secret_body)
except ApiException:
logger.exception('Could not create secret %s in namespace %s',
pull_secret_name,
namespace.metadata.name)
def update_ecr():
logger.info('Starting ECR secret update loop ..')
client = boto3.client('ecr')
response = client.get_authorization_token()
token = response['authorizationData'][0]['authorizationToken']
server = response['authorizationData'][0]['proxyEndpoint']
bare_server = server.replace('https://', '')
decoded_token = base64.b64decode(token).decode('utf-8')
registry_username = decoded_token.split(':')[0]
registry_password = decoded_token.split(':')[1]
k8s_config = Configuration()
k8s_config.host = kubernetes_api_endpoint
k8s_api_client = ApiClient(configuration=k8s_config)
v1 = k8sclient.CoreV1Api(api_client=k8s_api_client)
secrets = v1.list_secret_for_all_namespaces()
registry_secrets = [x for x in secrets.items if x.metadata.name == pull_secret_name]
logger.info('Found %s registry_secrets matching name %s', len(registry_secrets), pull_secret_name)
for secret in registry_secrets:
secret_name = secret.metadata.name
if secret.type == 'kubernetes.io/dockercfg':
logger.info('Updating secret %s (type kubernetes.io/dockercfg) in namespace %s',
secret_name,
secret.metadata.namespace)
k8s_secret = {
server:
{
'username': registry_username,
'password': registry_password
}
}
b64_k8s_secret = base64.b64encode(json.dumps(k8s_secret).encode('utf-8')).decode('utf-8')
body = {
'kind': 'Secret',
'apiVersion': 'v1',
'metadata': {
'name': pull_secret_name,
'creationTimestamp': None
},
'data': {
'.dockercfg': b64_k8s_secret
},
'type': 'kubernetes.io/dockercfg'
}
res = v1.patch_namespaced_secret(secret.metadata.name, secret.metadata.namespace, body)
elif secret.type == 'kubernetes.io/dockerconfigjson':
logger.info('Updating secret %s (type kubernetes.io/dockerconfigjson) in namespace %s',
secret_name,
secret.metadata.namespace)
k8s_secret = {
'auths': {
bare_server: {
'username': registry_username,
'password': registry_password
}
}
}
b64_k8s_secret = base64.b64encode(json.dumps(k8s_secret).encode('utf-8')).decode('utf-8')
body = {
'kind': 'Secret',
'apiVersion': 'v1',
'metadata': {
'name': pull_secret_name,
'creationTimestamp': None
},
'data': {
'.dockerconfigjson': b64_k8s_secret
},
'type': 'kubernetes.io/dockerconfigjson'
}
res = v1.patch_namespaced_secret(secret.metadata.name, secret.metadata.namespace, body)
else:
logger.warning('Unknown secret type for secret name %s: %s'.format(secret_name, secret.type))
if __name__ == '__main__':
setup_logging()
logger = logging.getLogger(__name__)
while True:
logger.info('Running credentials update loop ..')
create_pull_secrets()
update_ecr()
logger.info(f'...done. Waiting %s seconds', update_interval)
time.sleep(update_interval)