-
Notifications
You must be signed in to change notification settings - Fork 2.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(docker): Long-term cache for Docker Hub tags (#28489)
Co-authored-by: Michael Kriese <[email protected]>
- Loading branch information
Showing
6 changed files
with
327 additions
and
51 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,176 @@ | ||
import { mocked } from '../../../../test/util'; | ||
import * as _packageCache from '../../../util/cache/package'; | ||
import { DockerHubCache, DockerHubCacheData } from './dockerhub-cache'; | ||
import type { DockerHubTag } from './schema'; | ||
|
||
jest.mock('../../../util/cache/package'); | ||
const packageCache = mocked(_packageCache); | ||
|
||
function oldCacheData(): DockerHubCacheData { | ||
return { | ||
items: { | ||
1: { | ||
id: 1, | ||
last_updated: '2022-01-01', | ||
name: '1', | ||
tag_last_pushed: '2022-01-01', | ||
digest: 'sha256:111', | ||
}, | ||
2: { | ||
id: 2, | ||
last_updated: '2022-01-02', | ||
name: '2', | ||
tag_last_pushed: '2022-01-02', | ||
digest: 'sha256:222', | ||
}, | ||
3: { | ||
id: 3, | ||
last_updated: '2022-01-03', | ||
name: '3', | ||
tag_last_pushed: '2022-01-03', | ||
digest: 'sha256:333', | ||
}, | ||
}, | ||
updatedAt: '2022-01-01', | ||
}; | ||
} | ||
|
||
function newItem(): DockerHubTag { | ||
return { | ||
id: 4, | ||
last_updated: '2022-01-04', | ||
name: '4', | ||
tag_last_pushed: '2022-01-04', | ||
digest: 'sha256:444', | ||
}; | ||
} | ||
|
||
function newCacheData(): DockerHubCacheData { | ||
const { items } = oldCacheData(); | ||
const item = newItem(); | ||
return { | ||
items: { | ||
...items, | ||
[item.id]: item, | ||
}, | ||
updatedAt: '2022-01-04', | ||
}; | ||
} | ||
|
||
describe('modules/datasource/docker/dockerhub-cache', () => { | ||
beforeEach(() => { | ||
jest.resetAllMocks(); | ||
}); | ||
|
||
const dockerRepository = 'foo/bar'; | ||
|
||
it('initializes empty cache', async () => { | ||
packageCache.get.mockResolvedValue(undefined); | ||
|
||
const res = await DockerHubCache.init(dockerRepository); | ||
|
||
expect(res).toEqual({ | ||
dockerRepository, | ||
cache: { | ||
items: {}, | ||
updatedAt: null, | ||
}, | ||
isChanged: false, | ||
}); | ||
}); | ||
|
||
it('initializes cache with data', async () => { | ||
const oldCache = oldCacheData(); | ||
packageCache.get.mockResolvedValue(oldCache); | ||
|
||
const res = await DockerHubCache.init(dockerRepository); | ||
|
||
expect(res).toEqual({ | ||
dockerRepository, | ||
cache: oldCache, | ||
isChanged: false, | ||
}); | ||
}); | ||
|
||
it('reconciles new items', async () => { | ||
const oldCache = oldCacheData(); | ||
const newCache = newCacheData(); | ||
|
||
packageCache.get.mockResolvedValue(oldCache); | ||
const cache = await DockerHubCache.init(dockerRepository); | ||
const newItems: DockerHubTag[] = [newItem()]; | ||
|
||
const needNextPage = cache.reconcile(newItems); | ||
|
||
expect(needNextPage).toBe(true); | ||
expect(cache).toEqual({ | ||
cache: newCache, | ||
dockerRepository: 'foo/bar', | ||
isChanged: true, | ||
}); | ||
|
||
const res = cache.getItems(); | ||
expect(res).toEqual(Object.values(newCache.items)); | ||
|
||
await cache.save(); | ||
expect(packageCache.set).toHaveBeenCalledWith( | ||
'datasource-docker-hub-cache', | ||
'foo/bar', | ||
newCache, | ||
3 * 60 * 24 * 30, | ||
); | ||
}); | ||
|
||
it('reconciles existing items', async () => { | ||
const oldCache = oldCacheData(); | ||
|
||
packageCache.get.mockResolvedValue(oldCache); | ||
const cache = await DockerHubCache.init(dockerRepository); | ||
const items: DockerHubTag[] = Object.values(oldCache.items); | ||
|
||
const needNextPage = cache.reconcile(items); | ||
|
||
expect(needNextPage).toBe(false); | ||
expect(cache).toEqual({ | ||
cache: oldCache, | ||
dockerRepository: 'foo/bar', | ||
isChanged: false, | ||
}); | ||
|
||
const res = cache.getItems(); | ||
expect(res).toEqual(items); | ||
|
||
await cache.save(); | ||
expect(packageCache.set).not.toHaveBeenCalled(); | ||
}); | ||
|
||
it('reconciles from empty cache', async () => { | ||
const item = newItem(); | ||
const expectedCache = { | ||
items: { | ||
[item.id]: item, | ||
}, | ||
updatedAt: item.last_updated, | ||
}; | ||
const cache = await DockerHubCache.init(dockerRepository); | ||
|
||
const needNextPage = cache.reconcile([item]); | ||
expect(needNextPage).toBe(true); | ||
expect(cache).toEqual({ | ||
cache: expectedCache, | ||
dockerRepository: 'foo/bar', | ||
isChanged: true, | ||
}); | ||
|
||
const res = cache.getItems(); | ||
expect(res).toEqual([item]); | ||
|
||
await cache.save(); | ||
expect(packageCache.set).toHaveBeenCalledWith( | ||
'datasource-docker-hub-cache', | ||
'foo/bar', | ||
expectedCache, | ||
3 * 60 * 24 * 30, | ||
); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
import { dequal } from 'dequal'; | ||
import { DateTime } from 'luxon'; | ||
import * as packageCache from '../../../util/cache/package'; | ||
import type { DockerHubTag } from './schema'; | ||
|
||
export interface DockerHubCacheData { | ||
items: Record<number, DockerHubTag>; | ||
updatedAt: string | null; | ||
} | ||
|
||
const cacheNamespace = 'datasource-docker-hub-cache'; | ||
|
||
export class DockerHubCache { | ||
private isChanged = false; | ||
|
||
private constructor( | ||
private dockerRepository: string, | ||
private cache: DockerHubCacheData, | ||
) {} | ||
|
||
static async init(dockerRepository: string): Promise<DockerHubCache> { | ||
let repoCache = await packageCache.get<DockerHubCacheData>( | ||
cacheNamespace, | ||
dockerRepository, | ||
); | ||
|
||
repoCache ??= { | ||
items: {}, | ||
updatedAt: null, | ||
}; | ||
|
||
return new DockerHubCache(dockerRepository, repoCache); | ||
} | ||
|
||
reconcile(items: DockerHubTag[]): boolean { | ||
let needNextPage = true; | ||
|
||
let { updatedAt } = this.cache; | ||
let latestDate = updatedAt ? DateTime.fromISO(updatedAt) : null; | ||
|
||
for (const newItem of items) { | ||
const id = newItem.id; | ||
const oldItem = this.cache.items[id]; | ||
|
||
if (dequal(oldItem, newItem)) { | ||
needNextPage = false; | ||
continue; | ||
} | ||
|
||
this.cache.items[newItem.id] = newItem; | ||
const newItemDate = DateTime.fromISO(newItem.last_updated); | ||
if (!latestDate || latestDate < newItemDate) { | ||
updatedAt = newItem.last_updated; | ||
latestDate = newItemDate; | ||
} | ||
|
||
this.isChanged = true; | ||
} | ||
|
||
this.cache.updatedAt = updatedAt; | ||
return needNextPage; | ||
} | ||
|
||
async save(): Promise<void> { | ||
if (this.isChanged) { | ||
await packageCache.set( | ||
cacheNamespace, | ||
this.dockerRepository, | ||
this.cache, | ||
3 * 60 * 24 * 30, | ||
); | ||
} | ||
} | ||
|
||
getItems(): DockerHubTag[] { | ||
return Object.values(this.cache.items); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.