Skip to content

Commit d008c3a

Browse files
committed
Update service handler to use certificateAuthorityCertSecretRef
1 parent 66ae0e4 commit d008c3a

File tree

2 files changed

+16
-50
lines changed

2 files changed

+16
-50
lines changed

app/handlers/handlers_services.py

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88

99
from app.crds import ResourceType
1010
from app.utils import to_bool
11-
from app.utils_k8s import get_ca_cert, k8s_get_secret
1211

1312

1413
def k8s_get_twingate_resource(
@@ -93,11 +92,6 @@ def service_to_twingate_resource(service_body: Body, namespace: str) -> dict:
9392
f"{TLS_OBJECT_ANNOTATION} annotation is not provided."
9493
)
9594

96-
if not (tls_secret := k8s_get_secret(namespace, tls_secret_name)):
97-
raise kopf.PermanentError(
98-
f"Kubernetes Secret object: {tls_secret_name} is missing."
99-
)
100-
10195
result["spec"] |= {
10296
"address": "kubernetes.default.svc.cluster.local",
10397
"proxy": {
@@ -106,7 +100,10 @@ def service_to_twingate_resource(service_body: Body, namespace: str) -> dict:
106100
if spec["type"] == ServiceType.LOAD_BALANCER
107101
else f"{service_name}.{namespace}.svc.cluster.local"
108102
),
109-
"certificateAuthorityCert": get_ca_cert(tls_secret),
103+
"certificateAuthorityCertSecretRef": {
104+
"name": tls_secret_name,
105+
"namespace": namespace,
106+
},
110107
},
111108
}
112109

app/handlers/tests/test_handlers_services.py

Lines changed: 12 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
import yaml
77
from kopf._core.intents.causes import Reason
88

9-
from app.api.tests.factories import BASE64_OF_VALID_CA_CERT
109
from app.crds import ResourceType
1110
from app.handlers.handlers_services import (
1211
ALLOWED_EXTRA_ANNOTATIONS,
@@ -15,7 +14,6 @@
1514
service_to_twingate_resource,
1615
twingate_service_create,
1716
)
18-
from app.utils_k8s import get_ca_cert
1917

2018
# Ignore the fact we use _cogs here
2119

@@ -173,25 +171,13 @@ def test_with_extra_annotation(
173171
assert result == expected
174172

175173
def test_kubernetes_resource_type_annotation(
176-
self,
177-
example_cluster_ip_gateway_service_body,
178-
k8s_core_client_mock,
179-
k8s_secret_mock,
174+
self, example_cluster_ip_gateway_service_body
180175
):
181176
tls_object_name = "gateway-tls"
182177
namespace = "custom-namespace"
183-
k8s_core_client_mock.read_namespaced_secret.return_value = k8s_secret_mock
184-
185-
with patch(
186-
"app.handlers.handlers_services.get_ca_cert", wraps=get_ca_cert
187-
) as get_ca_cert_mock:
188-
result = service_to_twingate_resource(
189-
example_cluster_ip_gateway_service_body, namespace
190-
)
191178

192-
get_ca_cert_mock.assert_called_once_with(k8s_secret_mock)
193-
k8s_core_client_mock.read_namespaced_secret.assert_called_once_with(
194-
namespace=namespace, name=tls_object_name
179+
result = service_to_twingate_resource(
180+
example_cluster_ip_gateway_service_body, namespace
195181
)
196182

197183
assert result["spec"] == {
@@ -200,7 +186,10 @@ def test_kubernetes_resource_type_annotation(
200186
"alias": "alias.int",
201187
"proxy": {
202188
"address": "kubernetes-gateway.custom-namespace.svc.cluster.local",
203-
"certificateAuthorityCert": BASE64_OF_VALID_CA_CERT,
189+
"certificateAuthorityCertSecretRef": {
190+
"name": tls_object_name,
191+
"namespace": namespace,
192+
},
204193
},
205194
"protocols": {
206195
"allowIcmp": False,
@@ -231,19 +220,6 @@ def test_kubernetes_resource_type_annotation_without_tls_secret_annotation(
231220
example_cluster_ip_gateway_service_body, "default"
232221
)
233222

234-
def test_kubernetes_resource_type_annotation_without_k8s_secret_object(
235-
self, example_cluster_ip_gateway_service_body, k8s_core_client_mock
236-
):
237-
k8s_core_client_mock.read_namespaced_secret.return_value = None
238-
239-
with pytest.raises(
240-
kopf.PermanentError,
241-
match=r"Kubernetes Secret object: gateway-tls is missing.",
242-
):
243-
service_to_twingate_resource(
244-
example_cluster_ip_gateway_service_body, "default"
245-
)
246-
247223
@pytest.mark.parametrize(
248224
("status", "expected"),
249225
[
@@ -255,16 +231,10 @@ def test_kubernetes_resource_type_annotation_without_k8s_secret_object(
255231
],
256232
)
257233
def test_kubernetes_resource_with_load_balancer_service_type(
258-
self,
259-
example_load_balancer_gateway_service_body,
260-
k8s_core_client_mock,
261-
k8s_secret_mock,
262-
status,
263-
expected,
234+
self, example_load_balancer_gateway_service_body, status, expected
264235
):
265236
tls_object_name = "gateway-tls"
266237
namespace = "default"
267-
k8s_core_client_mock.read_namespaced_secret.return_value = k8s_secret_mock
268238

269239
with patch(
270240
"kopf._cogs.structs.bodies.Body.status",
@@ -275,17 +245,16 @@ def test_kubernetes_resource_with_load_balancer_service_type(
275245
example_load_balancer_gateway_service_body, namespace
276246
)
277247

278-
k8s_core_client_mock.read_namespaced_secret.assert_called_once_with(
279-
namespace=namespace, name=tls_object_name
280-
)
281-
282248
assert result["spec"] == {
283249
"name": "kubernetes-gateway-resource",
284250
"address": "kubernetes.default.svc.cluster.local",
285251
"alias": "alias.int",
286252
"proxy": {
287253
"address": expected,
288-
"certificateAuthorityCert": BASE64_OF_VALID_CA_CERT,
254+
"certificateAuthorityCertSecretRef": {
255+
"name": tls_object_name,
256+
"namespace": namespace,
257+
},
289258
},
290259
"protocols": {
291260
"allowIcmp": False,

0 commit comments

Comments
 (0)