fix(registry) EE-1861 improve registry selection (#5899)

* fix(registry) EE-1861 hide anonymous dockerhub registry if user has an authenticated one

* fix(registry) EE-1861 pick up a best match dockerhub registry

* fix(registry) EE-1861 set the anonymous registry as default if it is shown

* fix(registry) EE-1861 refactor how to match registry

Co-authored-by: Simon Meng <simon.meng@portainer.io>
pull/5917/head
cong meng 2021-10-15 21:42:46 +13:00 committed by GitHub
parent dfe0b3f69d
commit 9dcd5651e8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 46 additions and 12 deletions

View File

@ -69,13 +69,19 @@ class porImageRegistryController {
async reloadRegistries() {
return this.$async(async () => {
try {
const registries = await this.EndpointService.registries(this.endpoint.Id, this.namespace);
this.registries = _.concat(this.defaultRegistry, registries);
let showDefaultRegistry = false;
this.registries = await this.EndpointService.registries(this.endpoint.Id, this.namespace);
// hide default(anonymous) dockerhub registry if user has an authenticated one
if (!this.registries.some((registry) => registry.Type === RegistryTypes.DOCKERHUB)) {
showDefaultRegistry = true;
this.registries.push(this.defaultRegistry);
}
const id = this.model.Registry.Id;
const registry = _.find(this.registries, { Id: id });
if (!registry) {
this.model.Registry = this.defaultRegistry;
this.model.Registry = showDefaultRegistry ? this.defaultRegistry : this.registries[0];
}
} catch (err) {
this.Notifications.error('Failure', err, 'Unable to retrieve registries');

View File

@ -107,17 +107,45 @@ angular.module('portainer.app').factory('RegistryService', [
return url;
}
// findBestMatchRegistry finds out the best match registry for repository
// matching precedence:
// 1. registryId matched
// 2. both domain name and username matched (for dockerhub only)
// 3. only URL matched
// 4. pick up the first dockerhub registry
function findBestMatchRegistry(repository, registries, registryId) {
let highMatch, lowMatch;
for (const registry of registries) {
if (registry.Id == registryId) {
return registry;
}
if (registry.Type === RegistryTypes.DOCKERHUB) {
// try to match repository examples:
// <USERNAME>/nginx:latest
// docker.io/<USERNAME>/nginx:latest
if (repository.startsWith(registry.Username + '/') || repository.startsWith(getURL(registry) + '/' + registry.Username + '/')) {
highMatch = registry;
}
// try to match repository examples:
// portainer/portainer-ee:latest
// <NON-USERNAME>/portainer-ee:latest
lowMatch = lowMatch || registry;
}
if (_.includes(repository, getURL(registry))) {
lowMatch = registry;
}
}
return highMatch || lowMatch;
}
function retrievePorRegistryModelFromRepositoryWithRegistries(repository, registries, registryId) {
const model = new PorImageRegistryModel();
const registry = registries.find((reg) => {
if (registryId) {
return reg.Id === registryId;
}
if (reg.Type === RegistryTypes.DOCKERHUB) {
return _.includes(repository, reg.Username);
}
return _.includes(repository, getURL(reg));
});
const registry = findBestMatchRegistry(repository, registries, registryId);
if (registry) {
const url = getURL(registry);
let lastIndex = repository.lastIndexOf(url);