-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1708 from DSD-DBS/allow-communication-between-use…
…r-sessions feat: Allow communication between sessions of same user
- Loading branch information
Showing
7 changed files
with
163 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
# SPDX-FileCopyrightText: Copyright DB InfraGO AG and contributors | ||
# SPDX-License-Identifier: Apache-2.0 | ||
|
||
|
||
from capellacollab.sessions import operators | ||
from capellacollab.users import models as users_models | ||
|
||
from .. import models as sessions_models | ||
from . import interface | ||
|
||
|
||
class NetworkingIntegration(interface.HookRegistration): | ||
"""Allow sessions of the same user to talk to each other.""" | ||
|
||
def post_session_creation_hook( # type: ignore | ||
self, | ||
session_id: str, | ||
operator: operators.KubernetesOperator, | ||
user: users_models.DatabaseUser, | ||
**kwargs, | ||
) -> interface.PostSessionCreationHookResult: | ||
"""Allow sessions of the user to talk to each other.""" | ||
|
||
operator.create_network_policy_from_pod_to_label( | ||
session_id, | ||
{"capellacollab/session-id": session_id}, | ||
{"capellacollab/owner-id": str(user.id)}, | ||
) | ||
|
||
return interface.PostSessionCreationHookResult() | ||
|
||
def pre_session_termination_hook( # type: ignore | ||
self, | ||
operator: operators.KubernetesOperator, | ||
session: sessions_models.DatabaseSession, | ||
**kwargs, | ||
): | ||
operator.delete_network_policy(session.id) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
# SPDX-FileCopyrightText: Copyright DB InfraGO AG and contributors | ||
# SPDX-License-Identifier: Apache-2.0 | ||
|
||
|
||
import kubernetes.client | ||
import pytest | ||
|
||
from capellacollab.sessions import models as sessions_models | ||
from capellacollab.sessions import operators | ||
from capellacollab.sessions.hooks import networking as networking_hook | ||
from capellacollab.users import models as users_models | ||
|
||
|
||
def test_network_policy_created( | ||
user: users_models.DatabaseUser, monkeypatch: pytest.MonkeyPatch | ||
): | ||
network_policy_counter = 0 | ||
|
||
def mock_create_namespaced_network_policy( | ||
self, | ||
namespace: str, | ||
network_policy: kubernetes.client.V1PersistentVolumeClaim, | ||
): | ||
nonlocal network_policy_counter | ||
network_policy_counter += 1 | ||
|
||
monkeypatch.setattr( | ||
kubernetes.client.NetworkingV1Api, | ||
"create_namespaced_network_policy", | ||
mock_create_namespaced_network_policy, | ||
) | ||
|
||
networking_hook.NetworkingIntegration().post_session_creation_hook( | ||
session_id="test", | ||
operator=operators.KubernetesOperator(), | ||
user=user, | ||
) | ||
|
||
assert network_policy_counter == 1 | ||
|
||
|
||
def test_network_policy_deleted( | ||
session: sessions_models.DatabaseSession, monkeypatch: pytest.MonkeyPatch | ||
): | ||
network_policy_del_counter = 0 | ||
|
||
def mock_delete_namespaced_network_policy( | ||
self, | ||
name: str, | ||
namespace: str, | ||
): | ||
nonlocal network_policy_del_counter | ||
network_policy_del_counter += 1 | ||
|
||
monkeypatch.setattr( | ||
kubernetes.client.NetworkingV1Api, | ||
"delete_namespaced_network_policy", | ||
mock_delete_namespaced_network_policy, | ||
) | ||
|
||
networking_hook.NetworkingIntegration().pre_session_termination_hook( | ||
operator=operators.KubernetesOperator(), | ||
session=session, | ||
) | ||
|
||
assert network_policy_del_counter == 1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters