Skip to content

Commit cd4f91b

Browse files
author
gurghet
committed
fix: better error handling
1 parent 7a080b2 commit cd4f91b

1 file changed

Lines changed: 12 additions & 17 deletions

File tree

operator.py

Lines changed: 12 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -19,30 +19,25 @@ def __init__(self, logger):
1919

2020
def _get_github_token(self):
2121
"""Retrieve GitHub token from secret."""
22+
current_namespace = "operators" # Set default namespace
23+
2224
try:
23-
self.logger.info("Checking Kubernetes contexts...")
24-
contexts = kubernetes.config.list_kube_config_contexts()
25-
if not contexts:
26-
self.logger.error("No Kubernetes context found. This usually means the operator is not properly configured with cluster access.")
27-
raise kopf.PermanentError("No Kubernetes context found. Please ensure the operator has proper KUBECONFIG or in-cluster configuration.")
28-
29-
# Get namespace from service account token
30-
self.logger.info("Attempting to determine current namespace...")
25+
self.logger.info("Running in-cluster, attempting to determine current namespace...")
3126
try:
3227
namespace_file = "/var/run/secrets/kubernetes.io/serviceaccount/namespace"
3328
self.logger.debug(f"Reading namespace from {namespace_file}")
3429
with open(namespace_file, "r") as f:
35-
current_namespace = f.read().strip()
36-
self.logger.info(f"Successfully determined current namespace: {current_namespace}")
37-
except FileNotFoundError:
30+
ns = f.read().strip()
31+
if ns: # Only use the namespace if we got a non-empty value
32+
current_namespace = ns
33+
self.logger.info(f"Successfully determined current namespace: {current_namespace}")
34+
else:
35+
self.logger.warning("Empty namespace found in service account token, using default 'operators'")
36+
except (FileNotFoundError, PermissionError) as e:
3837
self.logger.warning(
39-
"Could not read namespace from service account token. "
40-
"This typically means either: \n"
41-
"1. The pod is not running in a Kubernetes cluster\n"
42-
"2. The service account token is not mounted\n"
43-
"Falling back to 'default' namespace."
38+
f"Could not read namespace from service account token ({str(e)}). "
39+
"Falling back to default namespace 'operators'"
4440
)
45-
current_namespace = "default"
4641

4742
self.logger.info(f"Attempting to read 'github-token' secret from namespace '{current_namespace}'")
4843
try:

0 commit comments

Comments
 (0)