Skip to content

Commit 534b47f

Browse files
committed
chore: remove duplication of logic
Signed-off-by: Oleksander Piskun <oleksandr2088@icloud.com>
1 parent 5266854 commit 534b47f

3 files changed

Lines changed: 23 additions & 28 deletions

File tree

lib/Command/ExApp/Register.php

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
217217
$output->writeln(sprintf('ExApp %s K8s deployment failed. Error: %s', $appId, $deployResult));
218218
}
219219
$this->exAppService->setStatusError($exApp, $deployResult);
220-
$this->_removeK8sResources($harpK8sUrl, $appId, $k8sRoles);
220+
$this->kubernetesActions->cleanupResources($harpK8sUrl, $appId, $k8sRoles);
221221
$this->_unregisterExApp($appId, $isTestDeployMode);
222222
return 1;
223223
}
@@ -239,7 +239,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
239239
$output->writeln(sprintf('ExApp %s K8s expose failed. Error: %s', $appId, $exposeResult['error']));
240240
}
241241
$this->exAppService->setStatusError($exApp, $exposeResult['error']);
242-
$this->_removeK8sResources($harpK8sUrl, $appId, $k8sRoles);
242+
$this->kubernetesActions->cleanupResources($harpK8sUrl, $appId, $k8sRoles);
243243
$this->_unregisterExApp($appId, $isTestDeployMode);
244244
return 1;
245245
}
@@ -271,7 +271,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
271271
}
272272
$this->exAppService->setStatusError($exApp, 'Heartbeat check failed');
273273
if ($harpK8sUrl !== null) {
274-
$this->_removeK8sResources($harpK8sUrl, $appId, $k8sRoles);
274+
$this->kubernetesActions->cleanupResources($harpK8sUrl, $appId, $k8sRoles);
275275
$this->_unregisterExApp($appId, $isTestDeployMode);
276276
}
277277
return 1;
@@ -296,17 +296,6 @@ protected function execute(InputInterface $input, OutputInterface $output): int
296296
return 0;
297297
}
298298

299-
private function _removeK8sResources(string $harpK8sUrl, string $appId, array $roles): void {
300-
if (!empty($roles)) {
301-
$error = $this->kubernetesActions->removeAllRoles($harpK8sUrl, $appId, $roles);
302-
} else {
303-
$error = $this->kubernetesActions->removeExApp($harpK8sUrl, $appId);
304-
}
305-
if ($error) {
306-
$this->logger->warning(sprintf('Failed to clean up K8s resources for %s: %s', $appId, $error));
307-
}
308-
}
309-
310299
private function _unregisterExApp(string $appId, bool $testDeployMode = false): void {
311300
if ($testDeployMode) {
312301
return;

lib/Command/ExApp/Update.php

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -253,7 +253,7 @@ private function updateExApp(InputInterface $input, OutputInterface $output, str
253253
$output->writeln(sprintf('ExApp %s K8s deployment update failed. Error: %s', $appId, $deployResult));
254254
}
255255
$this->exAppService->setStatusError($exApp, 'K8s deployment update failed');
256-
$this->_removeK8sResources($harpK8sUrl, $appId, $k8sRoles);
256+
$this->kubernetesActions->cleanupResources($harpK8sUrl, $appId, $k8sRoles);
257257
return 1;
258258
}
259259

@@ -273,7 +273,7 @@ private function updateExApp(InputInterface $input, OutputInterface $output, str
273273
$output->writeln(sprintf('ExApp %s K8s expose failed. Error: %s', $appId, $exposeResult['error']));
274274
}
275275
$this->exAppService->setStatusError($exApp, $exposeResult['error']);
276-
$this->_removeK8sResources($harpK8sUrl, $appId, $k8sRoles);
276+
$this->kubernetesActions->cleanupResources($harpK8sUrl, $appId, $k8sRoles);
277277
return 1;
278278
}
279279

@@ -304,7 +304,7 @@ private function updateExApp(InputInterface $input, OutputInterface $output, str
304304
}
305305
$this->exAppService->setStatusError($exApp, 'Heartbeat check failed');
306306
if ($harpK8sUrl !== null) {
307-
$this->_removeK8sResources($harpK8sUrl, $appId, $k8sRoles);
307+
$this->kubernetesActions->cleanupResources($harpK8sUrl, $appId, $k8sRoles);
308308
}
309309
return 1;
310310
}
@@ -342,14 +342,4 @@ private function updateExApp(InputInterface $input, OutputInterface $output, str
342342
return 0;
343343
}
344344

345-
private function _removeK8sResources(string $harpK8sUrl, string $appId, array $roles): void {
346-
if (!empty($roles)) {
347-
$error = $this->kubernetesActions->removeAllRoles($harpK8sUrl, $appId, $roles);
348-
} else {
349-
$error = $this->kubernetesActions->removeExApp($harpK8sUrl, $appId);
350-
}
351-
if ($error) {
352-
$this->logger->warning(sprintf('Failed to clean up K8s resources for %s: %s', $appId, $error));
353-
}
354-
}
355345
}

lib/DeployActions/KubernetesActions.php

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
declare(strict_types=1);
44

55
/**
6-
* SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors
6+
* SPDX-FileCopyrightText: 2026 Nextcloud GmbH and Nextcloud contributors
77
* SPDX-License-Identifier: AGPL-3.0-or-later
88
*/
99

@@ -232,6 +232,22 @@ private function rollbackDeployedRoles(string $harpUrl, string $exAppName, array
232232
}
233233
}
234234

235+
/**
236+
* Clean up K8s resources for an ExApp (Deployments, Services, etc.).
237+
*
238+
* Dispatches to removeAllRoles() or removeExApp() depending on whether roles are defined.
239+
*/
240+
public function cleanupResources(string $harpUrl, string $appId, array $roles): void {
241+
if (!empty($roles)) {
242+
$error = $this->removeAllRoles($harpUrl, $appId, $roles);
243+
} else {
244+
$error = $this->removeExApp($harpUrl, $appId);
245+
}
246+
if ($error) {
247+
$this->logger->warning(sprintf('Failed to clean up K8s resources for %s: %s', $appId, $error));
248+
}
249+
}
250+
235251
/**
236252
* Expose the single entry-point role for a multi-role ExApp.
237253
*

0 commit comments

Comments
 (0)