Skip to content
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
34 changes: 22 additions & 12 deletions src/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
from requests.adapters import HTTPAdapter
from requests.auth import HTTPBasicAuth
from requests.packages.urllib3.util.retry import Retry
from types import SimpleNamespace

from logger import get_logger
import argparse
Expand Down Expand Up @@ -166,18 +167,27 @@ def request(url, method, enable_5xx=False, payload=None):
logger.warning(f"No url provided. Doing nothing.")
return

# If method is not provided use GET as default
if method == "GET" or not method:
res = r.get("%s" % url, auth=auth, timeout=REQ_TIMEOUT, verify=REQ_TLS_VERIFY)
logger.info(f"Request sent to {url}." f"Response: {res.status_code} {res.reason} {res.text}")
elif method == "POST":
res = r.post("%s" % url, auth=auth, json=payload, timeout=REQ_TIMEOUT, verify=REQ_TLS_VERIFY)
logger.info(f"{payload} sent to {url}." f"Response: {res.status_code} {res.reason} {res.text}")
else:
logger.warning(f"Invalid REQ_METHOD: '{method}', please use 'GET' or 'POST'. Doing nothing.")
return
return res

try:
# If method is not provided use GET as default
if method == "GET" or not method:
res = r.get("%s" % url, auth=auth, timeout=REQ_TIMEOUT, verify=REQ_TLS_VERIFY)
logger.info(f"Request sent to {url}. Response: {res.status_code} {res.reason} {res.text}")
elif method == "POST":
res = r.post("%s" % url, auth=auth, json=payload, timeout=REQ_TIMEOUT, verify=REQ_TLS_VERIFY)
logger.info(f"{payload} sent to {url}. Response: {res.status_code} {res.reason} {res.text}")
else:
logger.warning(f"Invalid REQ_METHOD: '{method}', please use 'GET' or 'POST'. Doing nothing.")
return
return res
except requests.exceptions.SSLError as e:
logger.error(f"SSL certificate verification failed for URL {url}: {e}")
except requests.exceptions.RetryError as e:
logger.error(f"Max retries exceeded for URL {url}: {e}")
except Exception as e:
logger.error(f"Unexpected error during request to {url}: {e}")
# Return a dummy-object with empty attributes to avoid AttributeError if no response is returned (e.g. MaxRetryError)
logger.debug(f"Returning dummy response for URL {url}")
return SimpleNamespace(text="", content=b"")

def timestamp():
"""Get a timestamp of the current time for logging."""
Expand Down
6 changes: 4 additions & 2 deletions src/resources.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,9 +79,11 @@ def _get_file_data_and_name(full_filename, content, enable_5xx, content_type=CON
filename = full_filename[:-4]
if content_type == CONTENT_TYPE_BASE64_BINARY:
file_url = file_data.decode('utf8')
file_data = request(file_url, "GET", enable_5xx).content
response = request(file_url, "GET", enable_5xx)
file_data = response.content if response else b""
else:
file_data = request(file_data, "GET", enable_5xx).text
response = request(file_data, "GET", enable_5xx)
file_data = response.text if response else ""
else:
filename = full_filename

Expand Down
21 changes: 15 additions & 6 deletions src/sidecar.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#!/usr/bin/env python

import os, sys, re
import ssl

from kubernetes import client, config
from kubernetes.client import ApiException
Expand Down Expand Up @@ -146,12 +147,20 @@ def _initialize_kubeclient_configuration():

# this is where kube_config is going to look for a config file
kube_config = os.path.expanduser(KUBE_CONFIG_DEFAULT_LOCATION)
if os.path.exists(kube_config):
logger.info(f"Loading config from '{kube_config}'...")
config.load_kube_config(kube_config)
else:
logger.info("Loading incluster config ...")
config.load_incluster_config()
try:
if os.path.exists(kube_config):
logger.info(f"Loading config from '{kube_config}'...")
config.load_kube_config(kube_config)
else:
logger.info("Loading incluster config...")
config.load_incluster_config()
except ssl.SSLCertVerificationError as e:
logger.error(f"SSL certificate verification failed when initializing Kubernetes client: {e}")
logger.error("Check if the CA certificate at /var/run/secrets/kubernetes.io/serviceaccount/ca.crt is correct or set SKIP_TLS_VERIFY=true (insecure).")
raise
except Exception as e:
logger.error(f"Unexpected error during Kubernetes client initialization: {e}")
raise

if os.getenv(SKIP_TLS_VERIFY) == "true":
configuration = client.Configuration.get_default_copy()
Expand Down
Loading