-
Notifications
You must be signed in to change notification settings - Fork 26
fix(k8s): apply daemon registry mappings #1046
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
6a7858b
5435e02
af4a36b
e4f9a6d
cb6817d
fc14a86
80688ef
c08c5a5
1b66957
04955fc
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -24,6 +24,9 @@ | |
| * Daemon configuration (daemons) | ||
| */ | ||
| readonly class DaemonConfigService { | ||
| /** Registry mapping target that keeps the image name and tells the daemon not to pull the image. */ | ||
| public const LOCAL_REGISTRY = 'local'; | ||
|
|
||
| public function __construct( | ||
| private LoggerInterface $logger, | ||
| private DaemonConfigMapper $mapper, | ||
|
|
@@ -173,30 +176,31 @@ public function updateDaemonConfig(DaemonConfig $daemonConfig): ?DaemonConfig { | |
|
|
||
| public function addDockerRegistry(DaemonConfig $daemonConfig, array $registryMap): DaemonConfig|array|null { | ||
| try { | ||
| $from = $registryMap['from'] ?? null; | ||
| $to = $registryMap['to'] ?? null; | ||
| if (!is_string($from) || !is_string($to)) { | ||
| return ['error' => 'The source and target registry cannot be empty']; | ||
| } | ||
| $from = rtrim(trim($from), '/'); | ||
| $to = rtrim(trim($to), '/'); | ||
| if ($from === '' || $to === '') { | ||
| return ['error' => 'The source and target registry cannot be empty']; | ||
| } | ||
|
|
||
| $deployConfig = $daemonConfig->getDeployConfig(); | ||
|
|
||
| if (!isset($deployConfig['registries'])) { | ||
| $deployConfig['registries'] = []; | ||
| } | ||
|
|
||
| $fromExists = false; | ||
| foreach ($deployConfig['registries'] as $registry) { | ||
| if ($registry['from'] === $registryMap['from']) { | ||
| $fromExists = true; | ||
| break; | ||
| } | ||
| } | ||
| if ($fromExists) { | ||
| return ['error' => sprintf('This Docker registry map from "%s" already exists', $registryMap['from'])]; | ||
| if (self::resolveRegistryTarget($deployConfig, $from) !== null) { | ||
| return ['error' => sprintf('This Docker registry map from "%s" already exists', $from)]; | ||
| } | ||
| if ($registryMap['from'] === $registryMap['to']) { | ||
| if ($from === $to) { | ||
| return ['error' => 'The source and target registry cannot be the same']; | ||
| } | ||
| if (empty($registryMap['from']) || empty($registryMap['to'])) { | ||
| return ['error' => 'The source and target registry cannot be empty']; | ||
| } | ||
|
|
||
| $deployConfig['registries'][] = $registryMap; | ||
| $deployConfig['registries'] = [...array_values($deployConfig['registries']), ['from' => $from, 'to' => $to]]; | ||
| $daemonConfig->setDeployConfig($deployConfig); | ||
|
|
||
| return $this->mapper->update($daemonConfig); | ||
|
|
@@ -210,12 +214,12 @@ public function removeDockerRegistry(DaemonConfig $daemonConfig, array $registry | |
| try { | ||
| $deployConfig = $daemonConfig->getDeployConfig(); | ||
|
|
||
| if (!in_array($registryMap, $deployConfig['registries'])) { | ||
| if (!in_array($registryMap, $deployConfig['registries'] ?? [])) { | ||
| return ['error' => 'This Docker registry map does not exist']; | ||
| } | ||
| $deployConfig['registries'] = array_filter($deployConfig['registries'], function ($registry) use ($registryMap) { | ||
| return !($registry['from'] === $registryMap['from'] && $registry['to'] === $registryMap['to']); | ||
| }); | ||
| $deployConfig['registries'] = array_values(array_filter($deployConfig['registries'], function ($registry) use ($registryMap) { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🩺 Stability & Availability | 🟠 Major | ⚡ Quick win 🔎 Supported by static analysis🏁 Script executed: sed -n '150,270p' lib/Service/DaemonConfigService.php
rg -n 'removeDockerRegistry|addDockerRegistry|registries' lib tests/phpRepository: nextcloud/app_api Length of output: 11740 🏁 Script executed: sed -n '1,155p' tests/php/Service/DaemonConfigServiceTest.php
printf '\n--- DaemonConfig references and storage/config handling ---\n'
rg -n -C 3 'class DaemonConfig|function getDeployConfig|function setDeployConfig|deploy_config|json_decode|json_encode|registr' lib tests/php migrations appinfo README.md 2>/dev/null | head -n 260Repository: nextcloud/app_api Length of output: 28299 🏁 Script executed: fd -i 'DaemonConfig*' .
printf '\n--- model and mapper candidates ---\n'
for f in $(fd -i 'DaemonConfig*' . | head -n 12); do
printf '\n### %s\n' "$f"
rg -n -C 3 'class DaemonConfig|deploy_config|getDeployConfig|setDeployConfig|json|serialize|column' "$f" 2>/dev/null | head -n 100
doneRepository: nextcloud/app_api Length of output: 31750 Handle malformed stored entries during removal. If Guard non-array entries and use null-coalescing for missing fields. Proposed fix $deployConfig['registries'] = array_values(array_filter($deployConfig['registries'], function ($registry) use ($registryMap) {
- return !($registry['from'] === $registryMap['from'] && $registry['to'] === $registryMap['to']);
+ if (!is_array($registry)) {
+ return true;
+ }
+ return !(($registry['from'] ?? null) === $registryMap['from']
+ && ($registry['to'] ?? null) === $registryMap['to']);
})); |
||
| return !(($registry['from'] ?? null) === ($registryMap['from'] ?? null) && ($registry['to'] ?? null) === ($registryMap['to'] ?? null)); | ||
| })); | ||
| $daemonConfig->setDeployConfig($deployConfig); | ||
|
|
||
| return $this->mapper->update($daemonConfig); | ||
|
|
@@ -224,4 +228,33 @@ public function removeDockerRegistry(DaemonConfig $daemonConfig, array $registry | |
| return null; | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Effective target of the daemon's registry mappings for the registry an ExApp image comes from: | ||
| * the registry to take the image from instead, LOCAL_REGISTRY, or null when no usable mapping exists. | ||
| * | ||
| * The first mapping of the registry with a usable target wins. A target is usable when it is a non-empty | ||
| * string once surrounding whitespace and trailing slashes are dropped, so "local/" is LOCAL_REGISTRY as well. | ||
| */ | ||
| public static function resolveRegistryTarget(array $deployConfig, string $imageRegistry): ?string { | ||
| foreach ($deployConfig['registries'] ?? [] as $registry) { | ||
| $target = $registry['to'] ?? null; | ||
| if (($registry['from'] ?? null) !== $imageRegistry || !is_string($target)) { | ||
| continue; | ||
| } | ||
| $target = rtrim(trim($target), '/'); | ||
| if ($target !== '') { | ||
| return $target; | ||
| } | ||
| } | ||
| return null; | ||
| } | ||
|
|
||
| /** | ||
| * Registry to take an ExApp image from, after the registry mappings of the daemon are applied. | ||
| */ | ||
| public static function resolveImageRegistry(array $deployConfig, string $imageRegistry): string { | ||
| $target = self::resolveRegistryTarget($deployConfig, $imageRegistry); | ||
| return $target === null || $target === self::LOCAL_REGISTRY ? $imageRegistry : $target; | ||
| } | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.