Skip to content

Commit 23abfd8

Browse files
author
ikethecoder
authored
Upd to work with restish command line (#17)
1 parent b3738ae commit 23abfd8

File tree

2 files changed

+126
-116
lines changed

2 files changed

+126
-116
lines changed

microservices/gatewayApi/v1/routes/gateway.py

Lines changed: 126 additions & 115 deletions
Original file line numberDiff line numberDiff line change
@@ -129,145 +129,156 @@ def write_config(namespace: str) -> object:
129129
reserved_hosts.append(transform_host(host))
130130
reserved_hosts = list(set(reserved_hosts))
131131

132+
133+
dfile = None
134+
132135
if 'configFile' in request.files:
133136
log.debug("[%s] %s" % (namespace, request.files['configFile']))
134137
dfile = request.files['configFile']
138+
dry_run = request.values['dryRun']
139+
elif request.content_type.startswith("application/json"):
140+
dfile = request.json['configFile']
141+
dry_run = request.json['dryRun']
142+
else:
143+
log.error("Missing input")
144+
log.error(request.get_data())
145+
log.error(request.form)
146+
log.error(request.content_type)
147+
log.error(request.headers)
148+
abort(make_response(jsonify(error="Missing input"), 400))
149+
150+
tempFolder = "%s/%s/%s" % ('/tmp', uuid.uuid4(), outFolder)
151+
os.makedirs (tempFolder, exist_ok=False)
135152

136-
tempFolder = "%s/%s/%s" % ('/tmp', uuid.uuid4(), outFolder)
137-
os.makedirs (tempFolder, exist_ok=False)
153+
# dfile.save("%s/%s" % (tempFolder, 'config.yaml'))
154+
155+
# log.debug("Saved to %s" % tempFolder)
156+
yaml_documents_iter = yaml.load_all(dfile, Loader=yaml.FullLoader)
138157

139-
# dfile.save("%s/%s" % (tempFolder, 'config.yaml'))
140-
141-
# log.debug("Saved to %s" % tempFolder)
142-
yaml_documents_iter = yaml.load_all(dfile, Loader=yaml.FullLoader)
158+
yaml_documents = []
159+
for doc in yaml_documents_iter:
160+
yaml_documents.append(doc)
161+
162+
selectTag = "ns.%s" % namespace
163+
ns_qualifier = None
143164

144-
yaml_documents = []
145-
for doc in yaml_documents_iter:
146-
yaml_documents.append(doc)
165+
orig_config = prep_submitted_config (yaml_documents)
147166

148-
selectTag = "ns.%s" % namespace
149-
ns_qualifier = None
167+
for index, gw_config in enumerate(yaml_documents):
168+
log.info("[%s] Parsing file %s" % (namespace, index))
150169

151-
orig_config = prep_submitted_config (yaml_documents)
170+
if gw_config is None:
171+
continue
152172

153-
for index, gw_config in enumerate(yaml_documents):
154-
log.info("[%s] Parsing file %s" % (namespace, index))
173+
#######################
174+
# Enrichments
175+
#######################
155176

156-
#######################
157-
# Enrichments
158-
#######################
177+
# Transformation route hosts if in non-prod environment (HOST_TRANSFORM_ENABLED)
178+
host_transformation (namespace, gw_config)
159179

160-
# Transformation route hosts if in non-prod environment (HOST_TRANSFORM_ENABLED)
161-
host_transformation (namespace, gw_config)
180+
# If there is a tag with a pipeline qualifier (i.e./ ns.<namespace>.dev)
181+
# then add to tags automatically the tag: ns.<namespace>
182+
tags_transformation (namespace, gw_config)
162183

163-
# If there is a tag with a pipeline qualifier (i.e./ ns.<namespace>.dev)
164-
# then add to tags automatically the tag: ns.<namespace>
165-
tags_transformation (namespace, gw_config)
184+
#
185+
# Enrich the rate-limiting plugin with the appropriate Redis details
186+
plugins_transformations (namespace, gw_config)
166187

167-
#
168-
# Enrich the rate-limiting plugin with the appropriate Redis details
169-
plugins_transformations (namespace, gw_config)
188+
with open("%s/%s" % (tempFolder, 'config-%02d.yaml' % index), 'w') as file:
189+
yaml.dump(gw_config, file)
170190

171-
with open("%s/%s" % (tempFolder, 'config-%02d.yaml' % index), 'w') as file:
172-
yaml.dump(gw_config, file)
191+
#######################
192+
# Validations
193+
#######################
173194

174-
#######################
175-
# Validations
176-
#######################
195+
# Validate that the every object is tagged with the namespace
196+
try:
197+
validate_tags (gw_config, selectTag)
198+
except Exception as ex:
199+
abort(make_response(jsonify(error="Validation Errors:\n%s" % ex), 400))
177200

178-
# Validate that the every object is tagged with the namespace
179-
try:
180-
validate_tags (gw_config, selectTag)
181-
except Exception as ex:
182-
abort(make_response(jsonify(error="Validation Errors:\n%s" % ex), 400))
201+
# Validate that hosts are valid
202+
try:
203+
validate_hosts (gw_config, reserved_hosts)
204+
except Exception as ex:
205+
abort(make_response(jsonify(error="Validation Errors:\n%s" % ex), 400))
183206

184-
# Validate that hosts are valid
185-
try:
186-
validate_hosts (gw_config, reserved_hosts)
187-
except Exception as ex:
188-
abort(make_response(jsonify(error="Validation Errors:\n%s" % ex), 400))
207+
# Validation #3
208+
# Validate that certain plugins are configured (such as the gwa_gov_endpoint) at the right level
189209

190-
# Validation #3
191-
# Validate that certain plugins are configured (such as the gwa_gov_endpoint) at the right level
210+
# Validate based on DNS 952
211+
212+
nsq = traverse_get_ns_qualifier (gw_config, selectTag)
213+
if nsq is not None:
214+
if ns_qualifier is not None and nsq != ns_qualifier:
215+
abort(make_response(jsonify(error="Validation Errors:\n%s" % ("Conflicting ns qualifiers (%s != %s)" % (ns_qualifier, nsq))), 400))
216+
ns_qualifier = nsq
217+
log.info("[%s] CHANGING ns_qualifier %s" % (namespace, ns_qualifier))
192218

193-
# Validate based on DNS 952
194-
195-
nsq = traverse_get_ns_qualifier (gw_config, selectTag)
196-
if nsq is not None:
197-
if ns_qualifier is not None and nsq != ns_qualifier:
198-
abort(make_response(jsonify(error="Validation Errors:\n%s" % ("Conflicting ns qualifiers (%s != %s)" % (ns_qualifier, nsq))), 400))
199-
ns_qualifier = nsq
200-
log.info("[%s] CHANGING ns_qualifier %s" % (namespace, ns_qualifier))
201-
202-
if ns_qualifier is not None:
203-
selectTag = ns_qualifier
204-
205-
# Call the 'deck' command
206-
cmd = "sync"
207-
if request.values['dryRun'] == 'true':
208-
cmd = "diff"
209-
210-
log.info("[%s] %s action using %s" % (namespace, cmd, selectTag))
211-
args = [
212-
"deck", cmd, "--config", "/tmp/deck.yaml", "--skip-consumers", "--select-tag", selectTag, "--state", tempFolder
213-
]
214-
log.debug("[%s] Running %s" % (namespace, args))
215-
deck_run = Popen(args, stdout=PIPE, stderr=STDOUT)
216-
out, err = deck_run.communicate()
217-
if deck_run.returncode != 0:
218-
cleanup (tempFolder)
219-
log.warn("%s - %s" % (namespace, out.decode('utf-8')))
220-
abort(make_response(jsonify(error="Sync Failed.", results=mask(out.decode('utf-8'))), 400))
221-
222-
elif cmd == "sync":
223-
try:
224-
route_count = prepare_apply_routes (namespace, selectTag, is_host_transform_enabled(), tempFolder)
225-
log.debug("%s - Prepared %d routes" % (namespace, route_count))
226-
if route_count > 0:
227-
apply_routes (tempFolder)
228-
log.debug("%s - Applied %d routes" % (namespace, route_count))
229-
route_count = prepare_delete_routes (namespace, selectTag, tempFolder)
230-
log.debug("%s - Prepared %d deletions" % (namespace, route_count))
231-
if route_count > 0:
232-
delete_routes (tempFolder)
233-
234-
# create Network Security Policies (nsp) for any upstream that
235-
# has the format: <name>.<ocp_ns>.svc
236-
log.debug("%s - Update NSPs" % (namespace))
237-
ocp_ns_list = get_ocp_service_namespaces (tempFolder)
238-
for ocp_ns in ocp_ns_list:
239-
if check_nsp (namespace, ocp_ns) is False:
240-
apply_nsp (namespace, ocp_ns, tempFolder)
241-
242-
# ok all looks good, so update a secret containing the original submitted request
243-
log.debug("%s - Update Original Config" % (namespace))
244-
write_submitted_config (orig_config, tempFolder)
245-
prep_and_apply_secret (namespace, selectTag, tempFolder)
246-
log.debug("%s - Updated Original Config" % (namespace))
247-
except HTTPException as ex:
248-
traceback.print_exc()
249-
log.error("Error updating custom routes, nsps and secrets. %s" % ex)
250-
abort(make_response(jsonify(error="Partially failed."), 400))
251-
except:
252-
traceback.print_exc()
253-
log.error("Error updating custom routes, nsps and secrets. %s" % sys.exc_info()[0])
254-
abort(make_response(jsonify(error="Partially failed."), 400))
219+
if ns_qualifier is not None:
220+
selectTag = ns_qualifier
255221

222+
# Call the 'deck' command
223+
cmd = "sync"
224+
if dry_run == 'true' or dry_run is True:
225+
cmd = "diff"
226+
227+
log.info("[%s] %s action using %s" % (namespace, cmd, selectTag))
228+
args = [
229+
"deck", cmd, "--config", "/tmp/deck.yaml", "--skip-consumers", "--select-tag", selectTag, "--state", tempFolder
230+
]
231+
log.debug("[%s] Running %s" % (namespace, args))
232+
deck_run = Popen(args, stdout=PIPE, stderr=STDOUT)
233+
out, err = deck_run.communicate()
234+
if deck_run.returncode != 0:
256235
cleanup (tempFolder)
236+
log.warn("%s - %s" % (namespace, out.decode('utf-8')))
237+
abort(make_response(jsonify(error="Sync Failed.", results=mask(out.decode('utf-8'))), 400))
257238

258-
log.debug("[%s] The exit code was: %d" % (namespace, deck_run.returncode))
239+
elif cmd == "sync":
240+
try:
241+
route_count = prepare_apply_routes (namespace, selectTag, is_host_transform_enabled(), tempFolder)
242+
log.debug("%s - Prepared %d routes" % (namespace, route_count))
243+
if route_count > 0:
244+
apply_routes (tempFolder)
245+
log.debug("%s - Applied %d routes" % (namespace, route_count))
246+
route_count = prepare_delete_routes (namespace, selectTag, tempFolder)
247+
log.debug("%s - Prepared %d deletions" % (namespace, route_count))
248+
if route_count > 0:
249+
delete_routes (tempFolder)
250+
251+
# create Network Security Policies (nsp) for any upstream that
252+
# has the format: <name>.<ocp_ns>.svc
253+
log.debug("%s - Update NSPs" % (namespace))
254+
ocp_ns_list = get_ocp_service_namespaces (tempFolder)
255+
for ocp_ns in ocp_ns_list:
256+
if check_nsp (namespace, ocp_ns) is False:
257+
apply_nsp (namespace, ocp_ns, tempFolder)
258+
259+
# ok all looks good, so update a secret containing the original submitted request
260+
log.debug("%s - Update Original Config" % (namespace))
261+
write_submitted_config (orig_config, tempFolder)
262+
prep_and_apply_secret (namespace, selectTag, tempFolder)
263+
log.debug("%s - Updated Original Config" % (namespace))
264+
except HTTPException as ex:
265+
traceback.print_exc()
266+
log.error("Error updating custom routes, nsps and secrets. %s" % ex)
267+
abort(make_response(jsonify(error="Partially failed."), 400))
268+
except:
269+
traceback.print_exc()
270+
log.error("Error updating custom routes, nsps and secrets. %s" % sys.exc_info()[0])
271+
abort(make_response(jsonify(error="Partially failed."), 400))
259272

260-
message = "Sync successful."
261-
if cmd == 'diff':
262-
message = "Dry-run. No changes applied."
273+
cleanup (tempFolder)
263274

264-
return make_response(jsonify(message=message, results=mask(out.decode('utf-8'))))
265-
else:
266-
log.error("Missing input")
267-
log.error(request.get_data())
268-
log.error(request.form)
269-
log.error(request.headers)
270-
abort(make_response(jsonify(error="Missing input"), 400))
275+
log.debug("[%s] The exit code was: %d" % (namespace, deck_run.returncode))
276+
277+
message = "Sync successful."
278+
if cmd == 'diff':
279+
message = "Dry-run. No changes applied."
280+
281+
return make_response(jsonify(message=message, results=mask(out.decode('utf-8'))))
271282

272283
def cleanup (dir_path):
273284
log = app.logger

microservices/gatewayApi/v1/spec/v1.yaml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -271,7 +271,6 @@ paths:
271271
'500':
272272
$ref: '#/components/responses/500InternalServerError'
273273

274-
/namespaces/{namespace}/gateway:
275274
delete:
276275
summary: Delete the gateway config
277276
description: Deletes with optional qualifier

0 commit comments

Comments
 (0)