Skip to content

Commit f2eb9f3

Browse files
committed
[registry] Disable token refresh thread when prefetch is enabled
Add a new RegistryConfig field called `enable_token_refresh` that will decide whether the registry refresh token thread is started on not. It will be set to true by default but will be disabled when prefetch is enabled. The refresh thread is disabled in this case because it becomes useless once everything has been prefetched and it's unlikely that tokens would expire during the time it takes for the prefetch to run (as it would mean that regular image pulls would be likely to fail as well because they download the layers in a similar time to the prefetch) Signed-off-by: Baptiste Girard-Carrabin <baptiste.girardcarrabin@datadoghq.com>
1 parent c100686 commit f2eb9f3

2 files changed

Lines changed: 46 additions & 5 deletions

File tree

api/src/config.rs

Lines changed: 40 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -626,6 +626,10 @@ pub struct RegistryConfig {
626626
/// Enable HTTP proxy for the read request.
627627
#[serde(default)]
628628
pub proxy: ProxyConfig,
629+
/// Enable background token refresh thread. Defaults to true.
630+
/// When prefetch is enabled, this can be disabled as prefetch traffic will naturally refresh tokens.
631+
#[serde(default = "default_enable_token_refresh")]
632+
pub enable_token_refresh: bool,
629633
}
630634

631635
/// Configuration information for blob cache manager.
@@ -1157,6 +1161,10 @@ fn default_http_timeout() -> u32 {
11571161
5
11581162
}
11591163

1164+
fn default_enable_token_refresh() -> bool {
1165+
true
1166+
}
1167+
11601168
fn default_check_interval() -> u64 {
11611169
5
11621170
}
@@ -1364,7 +1372,7 @@ impl TryFrom<RafsConfig> for ConfigV2 {
13641372
type Error = std::io::Error;
13651373

13661374
fn try_from(v: RafsConfig) -> std::result::Result<Self, Self::Error> {
1367-
let backend: BackendConfigV2 = (&v.device.backend).try_into()?;
1375+
let mut backend: BackendConfigV2 = (&v.device.backend).try_into()?;
13681376
let mut cache: CacheConfigV2 = (&v.device.cache).try_into()?;
13691377
let rafs = RafsConfigV2 {
13701378
mode: v.mode,
@@ -1380,6 +1388,13 @@ impl TryFrom<RafsConfig> for ConfigV2 {
13801388
cache.prefetch = rafs.prefetch.clone();
13811389
}
13821390

1391+
// If prefetch is enabled, disable token refresh by default
1392+
if cache.prefetch.enable {
1393+
if let Some(registry) = backend.registry.as_mut() {
1394+
registry.enable_token_refresh = false;
1395+
}
1396+
}
1397+
13831398
Ok(ConfigV2 {
13841399
version: 2,
13851400
id: v.device.id,
@@ -1509,10 +1524,19 @@ impl TryFrom<&BlobCacheEntryConfig> for BlobCacheEntryConfigV2 {
15091524
cache_validate: false,
15101525
prefetch_config: v.prefetch_config.clone(),
15111526
};
1527+
let mut backend: BackendConfigV2 = (&backend_config).try_into()?;
1528+
1529+
// If prefetch is enabled, disable token refresh by default
1530+
if cache_config.prefetch_config.enable {
1531+
if let Some(registry) = backend.registry.as_mut() {
1532+
registry.enable_token_refresh = false;
1533+
}
1534+
}
1535+
15121536
Ok(BlobCacheEntryConfigV2 {
15131537
version: 2,
15141538
id: v.id.clone(),
1515-
backend: (&backend_config).try_into()?,
1539+
backend,
15161540
external_backends: v.external_backends.clone(),
15171541
cache: (&cache_config).try_into()?,
15181542
metadata_path: v.metadata_path.clone(),
@@ -1722,11 +1746,13 @@ mod tests {
17221746
"repo": "test/repo",
17231747
"auth": "base64_encoded_auth",
17241748
"registry_token": "bearer_token",
1725-
"blob_redirected_host": "blob_redirected_host"
1749+
"blob_redirected_host": "blob_redirected_host",
1750+
"enable_token_refresh": false
17261751
}"#;
17271752
let config: RegistryConfig = serde_json::from_str(content).unwrap();
17281753
assert_eq!(config.scheme, "http");
17291754
assert!(config.skip_verify);
1755+
assert!(!config.enable_token_refresh);
17301756
}
17311757

17321758
#[test]
@@ -1878,6 +1904,7 @@ mod tests {
18781904
connect_timeout = 10
18791905
retry_limit = 5
18801906
registry_token = "bear_token"
1907+
enable_token_refresh = false
18811908
blob_url_scheme = "https"
18821909
blob_redirected_host = "redirect.registry.com"
18831910
[backend.registry.proxy]
@@ -1906,6 +1933,7 @@ mod tests {
19061933
assert_eq!(registry.connect_timeout, 10);
19071934
assert_eq!(registry.retry_limit, 5);
19081935
assert_eq!(registry.registry_token.as_ref().unwrap(), "bear_token");
1936+
assert!(!registry.enable_token_refresh);
19091937
assert_eq!(registry.blob_url_scheme, "https");
19101938
assert_eq!(registry.blob_redirected_host, "redirect.registry.com");
19111939

@@ -2099,6 +2127,15 @@ mod tests {
20992127
"#;
21002128
let config = ConfigV2::from_str(content).unwrap();
21012129
assert_eq!(&config.id, "");
2130+
// token refresh should be disabled when prefetch is enabled
2131+
assert!(
2132+
!config
2133+
.get_backend_config()
2134+
.unwrap()
2135+
.get_registry_config()
2136+
.unwrap()
2137+
.enable_token_refresh
2138+
);
21022139
}
21032140

21042141
#[test]

storage/src/backend/registry.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -883,8 +883,12 @@ impl Registry {
883883
first: First::new(),
884884
};
885885

886-
registry.start_refresh_token_thread();
887-
info!("Refresh token thread started.");
886+
if config.enable_token_refresh {
887+
registry.start_refresh_token_thread();
888+
info!("Refresh token thread started.");
889+
} else {
890+
info!("Refresh token thread is disabled.");
891+
}
888892

889893
Ok(registry)
890894
}

0 commit comments

Comments
 (0)