Skip to content

Commit be4df84

Browse files
committed
feat(#739): unsupported probes
- refactor exceptions - refactor unit tests - add post event for on impair
1 parent cc79f16 commit be4df84

File tree

7 files changed

+45
-82
lines changed

7 files changed

+45
-82
lines changed

operator/gefyra/bridge/carrier/__init__.py

+7-13
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import json
22
from typing import Any, Dict, List, Optional
3-
from gefyra.utils import BridgeException, exec_command_pod
3+
from gefyra.bridge.exceptions import BridgeInstallException
4+
from gefyra.utils import exec_command_pod
45
import kubernetes as k8s
56

67
from gefyra.bridge.abstract import AbstractGefyraBridgeProvider
@@ -33,12 +34,7 @@ def __init__(
3334

3435
def install(self, parameters: Optional[Dict[Any, Any]] = None):
3536
parameters = parameters or {}
36-
try:
37-
self._patch_pod_with_carrier(
38-
handle_probes=parameters.get("handleProbes", True)
39-
)
40-
except BridgeException as be:
41-
raise BridgeException from be
37+
self._patch_pod_with_carrier(handle_probes=parameters.get("handleProbes", True))
4238

4339
def _ensure_probes(self, container: k8s.client.V1Container) -> bool:
4440
probes = self._get_all_probes(container)
@@ -144,11 +140,9 @@ def _patch_pod_with_carrier(
144140
self._get_all_probes(container),
145141
)
146142
):
147-
self.logger.error(
148-
"Not all of the probes to be handled are currently"
149-
" supported by Gefyra"
143+
raise BridgeInstallException(
144+
message="Not all of the probes to be handled are currently supported by Gefyra"
150145
)
151-
raise BridgeException()
152146
if (
153147
container.image
154148
== f"{self.configuration.CARRIER_IMAGE}:{self.configuration.CARRIER_IMAGE_TAG}"
@@ -162,8 +156,8 @@ def _patch_pod_with_carrier(
162156
container.image = f"{self.configuration.CARRIER_IMAGE}:{self.configuration.CARRIER_IMAGE_TAG}"
163157
break
164158
else:
165-
raise RuntimeError(
166-
f"Could not found container {self.container} in Pod {self.pod}"
159+
raise BridgeInstallException(
160+
message=f"Could not found container {self.container} in Pod {self.pod}"
167161
)
168162
self.logger.info(
169163
f"Now patching Pod {self.pod}; container {self.container} with Carrier"

operator/gefyra/bridge/exceptions.py

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
from gefyra.exceptions import BridgeException
2+
3+
4+
class BridgeInstallException(BridgeException):
5+
pass

operator/gefyra/bridgestate.py

+14-4
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@
1010
from gefyra.base import GefyraStateObject, StateControllerMixin
1111
from gefyra.configuration import OperatorConfiguration
1212

13-
from gefyra.utils import BridgeException
13+
from gefyra.bridge.exceptions import BridgeInstallException
14+
from gefyra.exceptions import BridgeException
1415

1516

1617
class GefyraBridgeObject(GefyraStateObject):
@@ -38,7 +39,7 @@ class GefyraBridge(StateMachine, StateControllerMixin):
3839

3940
install = (
4041
requested.to(installing, on="_install_provider")
41-
| installing.to(error)
42+
| error.to(installing)
4243
| installing.to.itself(on="_wait_for_provider")
4344
)
4445
set_installed = (
@@ -124,8 +125,9 @@ def _install_provider(self):
124125
"""
125126
try:
126127
self.bridge_provider.install()
127-
except BridgeException:
128-
self.send("impair")
128+
except BridgeInstallException as be:
129+
self.logger.debug(f"Encountered: {be}")
130+
self.send("impair", exception=be)
129131

130132
def _wait_for_provider(self):
131133
if not self.bridge_provider.ready():
@@ -193,3 +195,11 @@ def on_remove(self):
193195
def on_restore(self):
194196
self.bridge_provider.uninstall()
195197
self.send("terminate")
198+
199+
def on_impair(self, exception: BridgeException | None):
200+
self.logger.error(f"Failed from {self.current_state}")
201+
self.post_event(
202+
reason=f"Failed from {self.current_state}",
203+
message=exception.message,
204+
_type="Warning",
205+
)

operator/gefyra/exceptions.py

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
class BridgeException(Exception):
2+
message: str
3+
4+
def __init__(self, message: str):
5+
self.message = message

operator/gefyra/utils.py

-4
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,6 @@
1313
logger = logging.getLogger("gefyra.utils")
1414

1515

16-
class BridgeException(Exception):
17-
pass
18-
19-
2016
def get_label_selector(labels: dict[str, str]) -> str:
2117
return ",".join(["{0}={1}".format(*label) for label in list(labels.items())])
2218

operator/tests/e2e/test_create_bridge.py

+7-11
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
import json
22
import logging
3-
import subprocess
4-
import pytest
53
from pytest_kubernetes.providers import AClusterManager
64
from .utils import GefyraDockerClient
75

@@ -148,7 +146,7 @@ def test_c_fail_create_not_supported_bridges(
148146
k3d.wait("ns/demo-failing", "jsonpath='{.status.phase}'=Active")
149147
k3d.apply("tests/fixtures/demo_pods_not_supported.yaml")
150148
k3d.wait(
151-
"pod/backend",
149+
"pod/frontend",
152150
"condition=ready",
153151
namespace="demo-failing",
154152
timeout=60,
@@ -164,11 +162,9 @@ def test_c_fail_create_not_supported_bridges(
164162
)
165163

166164
# applying the bridge shouldn't have worked
167-
with pytest.raises(subprocess.TimeoutExpired):
168-
k3d.wait(
169-
"pod/frontend",
170-
"jsonpath=.status.containerStatuses[0].image=docker.io/library/"
171-
+ carrier_image,
172-
namespace="demo-failing",
173-
timeout=60,
174-
)
165+
k3d.wait(
166+
"pod/frontend",
167+
"condition=ready",
168+
namespace="demo-failing",
169+
timeout=60,
170+
)
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,7 @@
11
apiVersion: v1
2-
kind: Pod
2+
kind: Namespace
33
metadata:
4-
name: backend
5-
namespace: demo-failing
6-
labels:
7-
app: backend
8-
spec:
9-
securityContext:
10-
runAsUser: 1000
11-
runAsGroup: 1000
12-
fsGroup: 1000
13-
containers:
14-
- name: backend
15-
image: quay.io/gefyra/gefyra-demo-backend
16-
imagePullPolicy: IfNotPresent
17-
ports:
18-
- name: web
19-
containerPort: 5002
20-
protocol: TCP
4+
name: demo-failing
215
---
226
apiVersion: v1
237
kind: Pod
@@ -41,41 +25,14 @@ spec:
4125
livenessProbe:
4226
exec:
4327
command:
44-
- cat
45-
- /tmp/healthy
28+
- ls
29+
- /tmp
4630
initialDelaySeconds: 30
4731
periodSeconds: 10
4832
readinessProbe:
4933
exec:
5034
command:
51-
- cat
52-
- /tmp/ready
35+
- ls
36+
- /tmp
5337
initialDelaySeconds: 5
54-
periodSeconds: 5
55-
---
56-
apiVersion: v1
57-
kind: Service
58-
metadata:
59-
name: backend
60-
namespace: demo-failing
61-
spec:
62-
selector:
63-
app: backend
64-
ports:
65-
- protocol: TCP
66-
port: 5002
67-
targetPort: 5002
68-
---
69-
apiVersion: v1
70-
kind: Service
71-
metadata:
72-
name: frontend
73-
namespace: demo-failing
74-
spec:
75-
selector:
76-
app: frontend
77-
ports:
78-
- protocol: "TCP"
79-
port: 80
80-
targetPort: 5003
81-
type: LoadBalancer
38+
periodSeconds: 5

0 commit comments

Comments
 (0)