diff --git a/CHANGELOG.md b/CHANGELOG.md index d8893722..242616e0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -27,6 +27,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/). ### Fixed +- Uninstalling an ExApp in the app management and removing its deploy daemon also remove the ExApp's Kubernetes resources. #1210 - Registry mappings of a deploy daemon (`occ app_api:daemon:registry:add`) are applied by the Kubernetes backend too; before, ExApp images were always taken from the registry in `info.xml`. A `local` mapping sets `imagePullPolicy: Never` with HaRP newer than 0.4.5, so the image must be present on every node. #1046 - Updating a deploy daemon that does not exist answers `success: false` instead of a server error. #1082 - The ExApp proxy also refuses `.` path segments and `..` segments followed by `;`, `%3F` or `%23`. #1163 diff --git a/lib/Command/ExApp/Unregister.php b/lib/Command/ExApp/Unregister.php index 000ce296..c8de14e0 100644 --- a/lib/Command/ExApp/Unregister.php +++ b/lib/Command/ExApp/Unregister.php @@ -13,7 +13,6 @@ use OCA\AppAPI\DeployActions\KubernetesActions; use OCA\AppAPI\Service\AppAPIService; use OCA\AppAPI\Service\DaemonConfigService; -use OCA\AppAPI\Service\ExAppDeployOptionsService; use OCA\AppAPI\Service\ExAppService; use Symfony\Component\Console\Command\Command; use Symfony\Component\Console\Input\InputArgument; @@ -29,7 +28,6 @@ public function __construct( private readonly DockerActions $dockerActions, private readonly KubernetesActions $kubernetesActions, private readonly ExAppService $exAppService, - private readonly ExAppDeployOptionsService $exAppDeployOptionsService, ) { parent::__construct(); } @@ -153,22 +151,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int } } } elseif ($daemonConfig->getAcceptsDeployId() === $this->kubernetesActions->getAcceptsDeployId()) { - $this->kubernetesActions->initGuzzleClient($daemonConfig); - $harpK8sUrl = $this->kubernetesActions->buildHarpK8sUrl($daemonConfig); - - // Check for stored multi-role configuration - $rolesOption = $this->exAppDeployOptionsService->getDeployOption($exApp->getAppid(), 'k8s_service_roles'); - $roles = $rolesOption !== null ? $rolesOption->getValue() : []; - - if (!empty($roles) && is_array($roles)) { - $removeResult = $this->kubernetesActions->removeAllRoles( - $harpK8sUrl, $exApp->getAppid(), $roles, removeData: $rmData - ); - } else { - $removeResult = $this->kubernetesActions->removeExApp( - $harpK8sUrl, $exApp->getAppid(), removeData: $rmData - ); - } + $removeResult = $this->kubernetesActions->removeExAppResources($daemonConfig, $exApp->getAppid(), removeData: $rmData); if ($removeResult) { if (!$silent) { $output->writeln(sprintf('Failed to remove K8s ExApp %s: %s', $appId, $removeResult)); diff --git a/lib/Controller/ExAppsPageController.php b/lib/Controller/ExAppsPageController.php index fc467e97..b2edd1a4 100644 --- a/lib/Controller/ExAppsPageController.php +++ b/lib/Controller/ExAppsPageController.php @@ -17,6 +17,7 @@ use OC\App\Platform; use OCA\AppAPI\AppInfo\Application; use OCA\AppAPI\DeployActions\DockerActions; +use OCA\AppAPI\DeployActions\KubernetesActions; use OCA\AppAPI\Fetcher\ExAppFetcher; use OCA\AppAPI\ResponseDefinitions; use OCA\AppAPI\Service\AppAPIService; @@ -52,6 +53,7 @@ public function __construct( private readonly AppAPIService $service, private readonly DaemonConfigService $daemonConfigService, private readonly DockerActions $dockerActions, + private readonly KubernetesActions $kubernetesActions, private readonly CategoryFetcher $categoryFetcher, private readonly IFactory $l10nFactory, private readonly ExAppFetcher $exAppFetcher, @@ -512,6 +514,11 @@ public function uninstallApp(string $appId, bool $removeContainer = true, bool $ } } } + } elseif ($daemonConfig->getAcceptsDeployId() === $this->kubernetesActions->getAcceptsDeployId()) { + $removeError = $this->kubernetesActions->removeExAppResources($daemonConfig, $exApp->getAppid(), removeData: $removeData); + if ($removeData && $removeError === '') { + $this->exAppService->logExAppDataDeleted($exApp); + } } $this->exAppService->unregisterExApp($appId); } diff --git a/lib/DeployActions/KubernetesActions.php b/lib/DeployActions/KubernetesActions.php index fd109bd4..e04ab95b 100644 --- a/lib/DeployActions/KubernetesActions.php +++ b/lib/DeployActions/KubernetesActions.php @@ -287,14 +287,35 @@ public function stopAllRoles(string $harpUrl, string $exAppName, array $roles): return ''; } + /** + * Remove the deployment of every role, also after a failed one + * + * @return string the first error, empty on success + */ public function removeAllRoles(string $harpUrl, string $exAppName, array $roles, bool $removeData = false): string { + $firstError = ''; foreach ($roles as $role) { $error = $this->removeExApp($harpUrl, $exAppName, removeData: $removeData, roleSuffix: $role['name']); - if ($error) { - return $error; + if ($firstError === '') { + $firstError = $error; } } - return ''; + return $firstError; + } + + /** + * Remove the Kubernetes resources of an ExApp, of every role for a multi-role ExApp + * + * @return string the error, empty on success + */ + public function removeExAppResources(DaemonConfig $daemonConfig, string $exAppName, bool $removeData = false): string { + $this->initGuzzleClient($daemonConfig); + $harpK8sUrl = $this->buildHarpK8sUrl($daemonConfig); + $roles = $this->exAppDeployOptionsService->getDeployOption($exAppName, 'k8s_service_roles')?->getValue() ?? []; + if ($roles !== []) { + return $this->removeAllRoles($harpK8sUrl, $exAppName, $roles, removeData: $removeData); + } + return $this->removeExApp($harpK8sUrl, $exAppName, removeData: $removeData); } private function buildNamePayload(string $exAppName, string $instanceId = '', string $roleSuffix = ''): array { diff --git a/lib/Service/AppAPIService.php b/lib/Service/AppAPIService.php index 88088cf8..646e5c51 100644 --- a/lib/Service/AppAPIService.php +++ b/lib/Service/AppAPIService.php @@ -867,6 +867,10 @@ public function removeExAppsByDaemonConfigName(DaemonConfig $daemonConfig): void if ($dataDeleted) { $this->exAppService->logExAppDataDeleted($exApp); } + } elseif ($daemonConfig->getAcceptsDeployId() === $this->kubernetesActions->getAcceptsDeployId()) { + if ($this->kubernetesActions->removeExAppResources($daemonConfig, $exApp->getAppid(), true) === '') { + $this->exAppService->logExAppDataDeleted($exApp); + } } $this->exAppService->unregisterExApp($exApp->getAppid()); } diff --git a/tests/php/Command/ExApp/UnregisterTest.php b/tests/php/Command/ExApp/UnregisterTest.php index 8e884c06..74cd4625 100644 --- a/tests/php/Command/ExApp/UnregisterTest.php +++ b/tests/php/Command/ExApp/UnregisterTest.php @@ -16,7 +16,6 @@ use OCA\AppAPI\DeployActions\KubernetesActions; use OCA\AppAPI\Service\AppAPIService; use OCA\AppAPI\Service\DaemonConfigService; -use OCA\AppAPI\Service\ExAppDeployOptionsService; use OCA\AppAPI\Service\ExAppService; use PHPUnit\Framework\Attributes\DataProvider; use PHPUnit\Framework\MockObject\MockObject; @@ -47,7 +46,6 @@ protected function setUp(): void { $this->dockerActions, $this->kubernetesActions, $this->exAppService, - $this->createMock(ExAppDeployOptionsService::class), )); } @@ -118,7 +116,8 @@ public static function kubernetesProvider(): array { #[DataProvider('kubernetesProvider')] public function testKubernetesExAppDataDeletionIsLogged(bool $rmData, string $removeError, bool $logged): void { $exApp = $this->registerExApp('kubernetes-install', true); - $this->kubernetesActions->method('removeExApp')->willReturn($removeError); + $this->kubernetesActions->expects(self::once())->method('removeExAppResources') + ->with(self::isInstanceOf(DaemonConfig::class), 'test_app', $rmData)->willReturn($removeError); $this->exAppService->expects($logged ? self::once() : self::never())->method('logExAppDataDeleted')->with($exApp); $this->commandTester->execute(['appid' => 'test_app', '--rm-data' => $rmData, '--force' => true, '--silent' => true]); diff --git a/tests/php/Controller/ExAppsPageControllerTest.php b/tests/php/Controller/ExAppsPageControllerTest.php index 801f5c60..001b0fc1 100644 --- a/tests/php/Controller/ExAppsPageControllerTest.php +++ b/tests/php/Controller/ExAppsPageControllerTest.php @@ -14,6 +14,7 @@ use OCA\AppAPI\Db\DaemonConfig; use OCA\AppAPI\Db\ExApp; use OCA\AppAPI\DeployActions\DockerActions; +use OCA\AppAPI\DeployActions\KubernetesActions; use OCA\AppAPI\Fetcher\ExAppFetcher; use OCA\AppAPI\Service\AppAPIService; use OCA\AppAPI\Service\DaemonConfigService; @@ -40,6 +41,7 @@ class ExAppsPageControllerTest extends TestCase { private IConfig&MockObject $config; private IAppManager&MockObject $appManager; private DockerActions&MockObject $dockerActions; + private KubernetesActions&MockObject $kubernetesActions; private AppAPIService&MockObject $service; protected function setUp(): void { @@ -50,6 +52,8 @@ protected function setUp(): void { $this->service = $this->createMock(AppAPIService::class); $this->daemonConfigService = $this->createMock(DaemonConfigService::class); $this->dockerActions = $this->createMock(DockerActions::class); + $this->kubernetesActions = $this->createMock(KubernetesActions::class); + $this->kubernetesActions->method('getAcceptsDeployId')->willReturn('kubernetes-install'); $categoryFetcher = $this->createMock(CategoryFetcher::class); $this->l10nFactory = $this->createMock(IFactory::class); $this->exAppFetcher = $this->createMock(ExAppFetcher::class); @@ -65,6 +69,7 @@ protected function setUp(): void { $this->service, $this->daemonConfigService, $this->dockerActions, + $this->kubernetesActions, $categoryFetcher, $this->l10nFactory, $this->exAppFetcher, @@ -266,6 +271,30 @@ public function testUninstallAppLogsTheDeletedData(bool $isHarp, bool $removeDat $this->controller->uninstallApp('fake_app', true, $removeData); } + public static function uninstallKubernetesProvider(): array { + return [ + 'data removed' => [true, '', true], + 'data kept' => [false, '', false], + 'removal failed' => [true, 'Failed to remove K8s ExApp', false], + ]; + } + + #[DataProvider('uninstallKubernetesProvider')] + public function testUninstallAppRemovesTheKubernetesResources(bool $removeData, string $removeError, bool $logged): void { + $exApp = new ExApp(['appid' => 'fake_app', 'daemon_config_name' => 'k8s_daemon', 'enabled' => 0]); + $daemonConfig = new DaemonConfig(['name' => 'k8s_daemon', 'accepts_deploy_id' => 'kubernetes-install', 'deploy_config' => ['harp' => ['exapp_direct' => true]]]); + $this->exAppService->method('getExApp')->with('fake_app')->willReturn($exApp); + $this->daemonConfigService->method('getDaemonConfigByName')->willReturn($daemonConfig); + $this->dockerActions->method('getAcceptsDeployId')->willReturn('docker-install'); + $this->dockerActions->expects(self::never())->method('removeExApp'); + $this->kubernetesActions->expects(self::once())->method('removeExAppResources') + ->with($daemonConfig, 'fake_app', $removeData)->willReturn($removeError); + $this->exAppService->expects($logged ? self::once() : self::never())->method('logExAppDataDeleted')->with($exApp); + $this->exAppService->expects(self::once())->method('unregisterExApp')->with('fake_app'); + + $this->controller->uninstallApp('fake_app', true, $removeData); + } + public function testEnableAppLogsTheInstallationWithoutTheValuesOfTheEnvironmentVariables(): void { $this->exAppService->method('getExApp')->with('fake_app')->willReturn(null); $this->exAppService->expects(self::once())->method('logExAppInstallRequested') diff --git a/tests/php/DeployActions/KubernetesActionsTest.php b/tests/php/DeployActions/KubernetesActionsTest.php index 844dffb2..2ad0c6c4 100644 --- a/tests/php/DeployActions/KubernetesActionsTest.php +++ b/tests/php/DeployActions/KubernetesActionsTest.php @@ -16,6 +16,7 @@ use GuzzleHttp\Psr7\Response; use OCA\AppAPI\Db\DaemonConfig; use OCA\AppAPI\Db\ExApp; +use OCA\AppAPI\Db\ExAppDeployOption; use OCA\AppAPI\DeployActions\KubernetesActions; use OCA\AppAPI\Service\AppAPICommonService; use OCA\AppAPI\Service\ExAppDeployOptionsService; @@ -237,4 +238,70 @@ public function testRemoveExAppRemovesTheDataOfAGoneDeployment(bool $removeData, self::assertTrue(json_decode((string)$removes[0]['request']->getBody(), true)['remove_data']); } } + + public static function removeExAppResourcesProvider(): array { + return [ + 'single deployment' => [null, ['']], + 'multi-role ExApp' => [[['name' => 'web'], ['name' => 'worker']], ['web', 'worker']], + ]; + } + + #[DataProvider('removeExAppResourcesProvider')] + public function testRemoveExAppResourcesRemovesEveryDeploymentOfTheExApp(?array $roles, array $roleSuffixes): void { + $exAppDeployOptionsService = $this->createMock(ExAppDeployOptionsService::class); + $exAppDeployOptionsService->method('getDeployOption')->with('test-deploy', 'k8s_service_roles') + ->willReturn($roles === null ? null : new ExAppDeployOption(['appid' => 'test-deploy', 'type' => 'k8s_service_roles', 'value' => $roles])); + $kubernetesActions = $this->getMockBuilder(KubernetesActions::class) + ->setConstructorArgs([ + $this->createMock(LoggerInterface::class), + $this->createMock(IConfig::class), + $this->createMock(ICertificateManager::class), + $this->createMock(IAppManager::class), + $this->createMock(IURLGenerator::class), + $this->createMock(AppAPICommonService::class), + $this->createMock(ExAppService::class), + $this->createMock(ICrypto::class), + $exAppDeployOptionsService, + ]) + ->onlyMethods(['initGuzzleClient', 'removeExApp']) + ->getMock(); + $removed = []; + $kubernetesActions->expects(self::exactly(count($roleSuffixes)))->method('removeExApp') + ->willReturnCallback(static function (string $harpUrl, string $exAppName, bool $removeData, string $roleSuffix = '') use (&$removed): string { + $removed[] = [$exAppName, $removeData, $roleSuffix]; + return ''; + }); + $daemonConfig = new DaemonConfig(['accepts_deploy_id' => KubernetesActions::DEPLOY_ID, 'protocol' => 'http', 'host' => 'harp:8780', 'deploy_config' => []]); + + self::assertSame('', $kubernetesActions->removeExAppResources($daemonConfig, 'test-deploy', removeData: true)); + self::assertSame(array_map(static fn (string $roleSuffix): array => ['test-deploy', true, $roleSuffix], $roleSuffixes), $removed); + } + + public function testRemoveAllRolesRemovesTheOtherRolesAfterAFailure(): void { + $kubernetesActions = $this->getMockBuilder(KubernetesActions::class) + ->setConstructorArgs([ + $this->createMock(LoggerInterface::class), + $this->createMock(IConfig::class), + $this->createMock(ICertificateManager::class), + $this->createMock(IAppManager::class), + $this->createMock(IURLGenerator::class), + $this->createMock(AppAPICommonService::class), + $this->createMock(ExAppService::class), + $this->createMock(ICrypto::class), + $this->createMock(ExAppDeployOptionsService::class), + ]) + ->onlyMethods(['removeExApp']) + ->getMock(); + $removed = []; + $kubernetesActions->method('removeExApp') + ->willReturnCallback(static function (string $harpUrl, string $exAppName, bool $removeData, string $roleSuffix = '') use (&$removed): string { + $removed[] = $roleSuffix; + return $roleSuffix === 'worker' ? '' : sprintf('Failed to remove role %s', $roleSuffix); + }); + + $error = $kubernetesActions->removeAllRoles('http://harp:8780/exapps/app_api/k8s', 'test-deploy', [['name' => 'api'], ['name' => 'worker'], ['name' => 'cron']]); + + self::assertSame('Failed to remove role api', $error); + self::assertSame(['api', 'worker', 'cron'], $removed); + } } diff --git a/tests/php/Service/AppAPIServiceTest.php b/tests/php/Service/AppAPIServiceTest.php index 736e9416..2e8366fd 100644 --- a/tests/php/Service/AppAPIServiceTest.php +++ b/tests/php/Service/AppAPIServiceTest.php @@ -478,6 +478,20 @@ public function testRemoveExAppsByDaemonConfigNameLogsTheDeletedData(bool $isHar $this->service->removeExAppsByDaemonConfigName($daemonConfig); } + public function testRemoveExAppsByDaemonConfigNameRemovesTheKubernetesResources(): void { + $exApp = $this->createExApp(); + $exApp->setAcceptsDeployId('kubernetes-install'); + $daemonConfig = new DaemonConfig(['name' => 'test_daemon', 'accepts_deploy_id' => 'kubernetes-install', 'deploy_config' => ['harp' => ['exapp_direct' => true]]]); + $this->setupReachableManualExApp($exApp, '{}'); + $this->exAppService->method('getExAppsByDaemonName')->with('test_daemon')->willReturn([$exApp]); + $this->exAppService->method('disableExAppInternal')->willReturn(true); + $this->kubernetesActions->expects(self::once())->method('removeExAppResources')->with($daemonConfig, 'test_app', true)->willReturn(''); + $this->exAppService->expects(self::once())->method('logExAppDataDeleted')->with($exApp); + $this->exAppService->expects(self::once())->method('unregisterExApp')->with('test_app'); + + $this->service->removeExAppsByDaemonConfigName($daemonConfig); + } + private function createRequestForUser(string $appId, string $userId, string $secret): MockObject&\OCP\IRequest { $request = $this->createMock(\OCP\IRequest::class); $request->method('getRemoteAddress')->willReturn('127.0.0.1'); diff --git a/tests/psalm-baseline.xml b/tests/psalm-baseline.xml index bf6df4a2..00a202e1 100644 --- a/tests/psalm-baseline.xml +++ b/tests/psalm-baseline.xml @@ -149,12 +149,6 @@ - - - - - -