Skip to content

Commit

Permalink
Add API function TryUpdatingCache
Browse files Browse the repository at this point in the history
The function can be used to reload the registries without harming the
cache if it fails. On success, the cache will be updated as intended.
This function can be reused by the current `GetRegistries` API.

Signed-off-by: Sascha Grunert <sgrunert@suse.com>
  • Loading branch information
saschagrunert committed Aug 2, 2019
1 parent e1aacd9 commit 09d8ac0
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 1 deletion.
15 changes: 14 additions & 1 deletion pkg/sysregistriesv2/system_registries_v2.go
Original file line number Diff line number Diff line change
Expand Up @@ -338,11 +338,24 @@ func getConfig(ctx *types.SystemContext) (*V2RegistriesConf, error) {
configPath := ConfigPath(ctx)

configMutex.Lock()
defer configMutex.Unlock()
// if the config has already been loaded, return the cached registries
if config, inCache := configCache[configPath]; inCache {
configMutex.Unlock()
return config, nil
}
configMutex.Unlock()

return TryUpdatingCache(ctx)
}

// TryUpdatingCache loads the configuration from the provided `SystemContext`
// without using the internal cache. On success, the loaded configuration will
// be added into the internal registry cache.
func TryUpdatingCache(ctx *types.SystemContext) (*V2RegistriesConf, error) {
configPath := ConfigPath(ctx)

configMutex.Lock()
defer configMutex.Unlock()

// load the config
config, err := loadRegistryConf(configPath)
Expand Down
19 changes: 19 additions & 0 deletions pkg/sysregistriesv2/system_registries_v2_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -445,3 +445,22 @@ func TestPullSourcesFromReference(t *testing.T) {
assert.Nil(t, err)
assert.Equal(t, 1, len(pullSources))
}

func TestTryUpdatingCache(t *testing.T) {
ctx := &types.SystemContext{
SystemRegistriesConfPath: "testdata/try-update-cache-valid.conf",
}
configCache = make(map[string]*V2RegistriesConf)
registries, err := TryUpdatingCache(ctx)
assert.Nil(t, err)
assert.Equal(t, 1, len(registries.Registries))
assert.Equal(t, 1, len(configCache))

ctxInvalid := &types.SystemContext{
SystemRegistriesConfPath: "testdata/try-update-cache-invalid.conf",
}
registries, err = TryUpdatingCache(ctxInvalid)
assert.NotNil(t, err)
assert.Nil(t, registries)
assert.Equal(t, 1, len(configCache))
}
1 change: 1 addition & 0 deletions pkg/sysregistriesv2/testdata/try-update-cache-invalid.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
invalid
2 changes: 2 additions & 0 deletions pkg/sysregistriesv2/testdata/try-update-cache-valid.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[[registry]]
location = "registry.com"

0 comments on commit 09d8ac0

Please sign in to comment.