Skip to content
This repository was archived by the owner on Jul 15, 2022. It is now read-only.

Commit e3c24d7

Browse files
author
Leonardo Chaia
committed
chore: allow to query registries not added in settings
1 parent 8044a3b commit e3c24d7

File tree

3 files changed

+77
-5
lines changed

3 files changed

+77
-5
lines changed
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
import { ImageSource, ImageListFilter, ImageListItemData, ImageInfo } from './image-source.model';
2+
import { TimCacheService } from '../tim-cache/tim-cache.service';
3+
import { Observable } from 'rxjs';
4+
import { ImageLayerHistoryV1Compatibility } from '../registry/registry.model';
5+
import { RegistryService } from '../registry/registry.service';
6+
import { RegistryImageSource } from '../registry/registry.image-source';
7+
import { DEFAULT_REGISTRY_CACHE } from '../settings/settings.service';
8+
import { CachingImageSource } from './caching-image-source';
9+
import { DockerRegistrySettings } from '../settings/settings.model';
10+
11+
export class GenericImageSource extends ImageSource {
12+
public readonly name = 'Generic Image Source';
13+
private readonly wrapped: ImageSource;
14+
15+
constructor(
16+
public readonly registryDNS: string,
17+
private readonly registry: RegistryService,
18+
private readonly cache: TimCacheService) {
19+
super();
20+
const registrySettings = {
21+
enableCaching: true,
22+
cacheDurationInMinutes: DEFAULT_REGISTRY_CACHE,
23+
isDockerHub: false,
24+
url: `https://${registryDNS}/`,
25+
username: null,
26+
password: null
27+
} as DockerRegistrySettings;
28+
29+
const registrySource = new RegistryImageSource(registrySettings, this.registry);
30+
31+
this.wrapped = new CachingImageSource(registrySource, this.cache, registrySettings.cacheDurationInMinutes);
32+
}
33+
34+
public loadList(filter?: ImageListFilter): Observable<ImageListItemData[]> {
35+
return this.wrapped.loadList(filter);
36+
}
37+
38+
public loadImageInfo(image: string): Observable<ImageInfo> {
39+
return this.wrapped.loadImageInfo(image);
40+
}
41+
42+
public loadImageHistory(image: string): Observable<ImageLayerHistoryV1Compatibility[]> {
43+
return this.wrapped.loadImageHistory(image);
44+
}
45+
46+
public loadImageTags(image: string): Observable<string[]> {
47+
return this.wrapped.loadImageTags(image);
48+
}
49+
}

src/app/docker-images/image-source.service.ts

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@ import { ImageSourceMultiple, ImageSource } from './image-source.model';
33
import { of, combineLatest, BehaviorSubject } from 'rxjs';
44
import { switchMap, map, take } from 'rxjs/operators';
55
import { flatten } from '../shared/array-tools';
6+
import { explodeImage } from './image-tools';
7+
import { GenericImageSource } from './generic-image-source';
8+
import { RegistryService } from '../registry/registry.service';
9+
import { TimCacheService } from '../tim-cache/tim-cache.service';
610

711
@Injectable({
812
providedIn: 'root'
@@ -17,7 +21,9 @@ export class ImageSourceService {
1721

1822
constructor(
1923
@Inject(ImageSourceMultiple)
20-
sources: ImageSourceMultiple[]) {
24+
sources: ImageSourceMultiple[],
25+
private readonly registry: RegistryService,
26+
private readonly cache: TimCacheService) {
2127

2228
of(sources)
2329
.pipe(
@@ -34,15 +40,27 @@ export class ImageSourceService {
3440
return this.sources
3541
.pipe(
3642
map(sources => sources.find(s => s.isImageOwner(image))),
43+
map(source => !source ? this.createGenericImageSourceForImage(image) : source),
3744
take(1),
3845
);
3946
}
4047

4148
public getForDNS(registryDNS: string) {
4249
return this.sources.pipe(
4350
map(srcs => srcs.find(s => s.isRegistryOwner(registryDNS))),
51+
map(source => !source ? this.createGenericImageSource(registryDNS) : source),
4452
take(1),
4553
);
4654
}
4755

56+
protected createGenericImageSourceForImage(image: string): ImageSource {
57+
const imageInfo = explodeImage(image);
58+
if (imageInfo.registry) {
59+
return this.createGenericImageSource(imageInfo.registry);
60+
}
61+
}
62+
63+
protected createGenericImageSource(registryDNS: string): ImageSource {
64+
return new GenericImageSource(registryDNS, this.registry, this.cache);
65+
}
4866
}

src/app/docker-images/image-tools.ts

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,18 +12,23 @@ export function explodeImage(image: string) {
1212

1313
const matches = image.match(/(.+\/)?([^:]+)(:.+)?/);
1414

15-
let registryOrNamespace = matches[1];
15+
const registryOrNamespace = matches[1];
1616
let registry: string;
1717
let reference = matches[2];
1818
let tag = matches[3];
1919

2020
if (registryOrNamespace) {
21-
registryOrNamespace = registryOrNamespace.replace('/', '');
2221

23-
// Determine registry vs namespace
24-
if (registryOrNamespace.includes('.')) {
22+
if (registryOrNamespace.includes('/')) {
23+
// Registry and namespace
24+
const split = registryOrNamespace.split('/');
25+
registry = split[0];
26+
reference = `${split[1]}/${reference}`;
27+
} else if (registryOrNamespace.includes('.')) {
28+
// Registry only
2529
registry = registryOrNamespace;
2630
} else {
31+
// Namespace only
2732
reference = `${registryOrNamespace}/${reference}`;
2833
}
2934
}

0 commit comments

Comments
 (0)