Skip to content

fix(cli): flaky gateway configuration after redeploying #35

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jun 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

- Added `deploy` command to deploy all gateway components in a single command
- Added `--http-methods` flag to `add-route` command to specify http methods

## [0.3.1] - 2023-06-27

### Fixed

- Fixed gateway manager failing after deploying due to token refresh desync
26 changes: 23 additions & 3 deletions cli/cli/gateway.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,14 @@
import click
import requests
from loguru import logger
from requests.adapters import HTTPAdapter
from urllib3 import Retry

from cli import conf
from cli.model import Route

MAX_RETRIES = 5


def response_hook(response: requests.Response, *_args, **_kwargs):
"""Response hook to log request and call raise_for_status."""
Expand All @@ -16,9 +20,12 @@ def response_hook(response: requests.Response, *_args, **_kwargs):
try:
response.raise_for_status()
except requests.exceptions.HTTPError as err:
body_json = err.response.json()
logger.error(f"Error: \n{json.dumps(body_json, indent=2)}")
raise
try: # Try to parse the body as JSON
body_json = err.response.json()
logger.error(f"Error: \n{json.dumps(body_json, indent=2)}")
except json.decoder.JSONDecodeError:
logger.error(f"Error: \n{err.response.text}")
raise err


class GatewayManager:
Expand Down Expand Up @@ -59,12 +66,25 @@ def __init__(self):
self.services_url = self.admin_url + "/services"
self.token = self.config.gw_admin_token

self._create_session()

def _create_session(self):
self.session = requests.Session()
if self.token:
self.session.headers["X-Auth-Token"] = self.token
self.session.headers["Content-Type"] = "application/json"
self.session.hooks["response"].append(response_hook)

retries = Retry(
total=MAX_RETRIES,
backoff_factor=2,
status=MAX_RETRIES, # Status max retries
# 403: retry for token refresh
# 404: retry for Envoy service not found
status_forcelist=[500, 404, 403],
)
self.session.mount("https://", HTTPAdapter(max_retries=retries))

def add_route(self, route: Route):
service_url = f"{self.services_url}/{route.name}"
route_url = f"{self.routes_url}/{route.name}"
Expand Down
2 changes: 1 addition & 1 deletion cli/pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "scw-gateway"
version = "0.3.0"
version = "0.3.1"
description = "CLI to deploy and manage a self-hosted Kong gateway on Scaleway Serverless Ecosystem"
authors = ["Simon Shillaker <sshillaker@scaleway.com>"]
readme = "README.md"
Expand Down