image-sync 0.1.1

Smart container image cache synchronizer — pulls from external registries only when needed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
//! image-sync — Smart container image cache synchronizer.
//!
//! Checks external registries for image digest changes, pulls only when
//! the remote digest differs from what's cached locally in Zot.
//! Designed to run as a Kubernetes CronJob with exponential backoff.
//!
//! Config via shikumi pattern: YAML file + env overrides.
//!
//! All copy/digest settings (platform, TLS, tool preference) are
//! configurable at the global level and overridable per-image.

use std::process::Command;

use chrono::Utc;
use clap::Parser;
use serde::{Deserialize, Serialize};

#[derive(Parser)]
#[command(name = "image-sync", about = "Smart image cache synchronizer")]
struct Cli {
    /// Config file path (YAML)
    #[arg(long, env = "IMAGE_SYNC_CONFIG", default_value = "/etc/image-sync/config.yaml")]
    config: String,

    /// Dry run — check digests but don't pull
    #[arg(long)]
    dry_run: bool,
}

/// Configuration loaded from YAML (shikumi pattern).
#[derive(Debug, Deserialize, Serialize)]
struct Config {
    /// Local cache registry URL (Zot)
    cache_registry: String,

    /// Images to keep in sync
    images: Vec<ImageSpec>,

    /// Global settings (defaults for all images)
    #[serde(default)]
    settings: Settings,
}

#[derive(Debug, Deserialize, Serialize)]
struct ImageSpec {
    /// Source image reference (e.g., "docker.io/akeyless/k8s-secrets-sidecar")
    source: String,

    /// Tag to sync (e.g., "0.35.1", "latest")
    tag: String,

    /// Optional: override destination path in cache
    #[serde(default)]
    cache_as: Option<String>,

    /// Platform override for this image (e.g., "linux/arm64").
    /// Falls back to settings.default_platform.
    #[serde(default)]
    platform: Option<String>,

    /// TLS verification override for this image's source registry.
    /// Falls back to settings.source_tls_verify.
    #[serde(default)]
    source_tls_verify: Option<bool>,

    /// Insecure (HTTP) override for the cache registry for this image.
    /// Falls back to settings.cache_insecure.
    #[serde(default)]
    cache_insecure: Option<bool>,
}

#[derive(Debug, Deserialize, Serialize)]
struct Settings {
    /// Max concurrent pulls
    #[serde(default = "default_concurrency")]
    concurrency: usize,

    /// Timeout per image pull in seconds
    #[serde(default = "default_timeout")]
    pull_timeout_secs: u64,

    /// Skip pull if remote check fails (don't error the job)
    #[serde(default)]
    skip_on_error: bool,

    /// Default platform for crane copy (e.g., "linux/amd64").
    /// Copies platform-specific manifests instead of multi-arch
    /// manifest lists, which avoids MANIFEST_INVALID on Zot.
    #[serde(default = "default_platform")]
    default_platform: String,

    /// Allow HTTP (insecure) for the local cache registry.
    /// Default true — Zot in-cluster typically runs on HTTP.
    #[serde(default = "default_true")]
    cache_insecure: bool,

    /// Verify TLS certificates for source registries.
    /// Default true — Docker Hub, GHCR, ECR all use valid certs.
    #[serde(default = "default_true")]
    source_tls_verify: bool,

    /// Preferred copy tool: "crane" or "skopeo".
    /// Falls back to the other if preferred is unavailable.
    #[serde(default = "default_tool")]
    preferred_tool: String,
}

impl Default for Settings {
    fn default() -> Self {
        Self {
            concurrency: default_concurrency(),
            pull_timeout_secs: default_timeout(),
            skip_on_error: false,
            default_platform: default_platform(),
            cache_insecure: true,
            source_tls_verify: true,
            preferred_tool: default_tool(),
        }
    }
}

fn default_platform() -> String { "linux/amd64".to_string() }
fn default_concurrency() -> usize { 2 }
fn default_timeout() -> u64 { 300 }
fn default_true() -> bool { true }
fn default_tool() -> String { "crane".to_string() }

/// Resolved copy options for a single image (merged from settings + per-image).
struct CopyOpts<'a> {
    platform: &'a str,
    cache_insecure: bool,
    source_tls_verify: bool,
}

/// Result of syncing one image.
#[derive(Debug, Serialize)]
struct SyncResult {
    image: String,
    tag: String,
    action: SyncAction,
    remote_digest: Option<String>,
    cached_digest: Option<String>,
    duration_ms: u64,
    error: Option<String>,
}

#[derive(Debug, Serialize)]
#[serde(rename_all = "snake_case")]
enum SyncAction {
    AlreadyCached,
    Pulled,
    Skipped,
    Failed,
}

fn load_config(path: &str) -> anyhow::Result<Config> {
    let content = std::fs::read_to_string(path)?;
    Ok(serde_yaml_ng::from_str(&content)?)
}

/// Get the remote image digest from the registry WITHOUT pulling the image.
fn get_remote_digest(image: &str, tag: &str) -> anyhow::Result<Option<String>> {
    let result = Command::new("crane")
        .args(["digest", &format!("{image}:{tag}")])
        .output();

    if let Ok(output) = result {
        if output.status.success() {
            let digest = String::from_utf8_lossy(&output.stdout).trim().to_string();
            if !digest.is_empty() {
                return Ok(Some(digest));
            }
        }
    }

    let result = Command::new("skopeo")
        .args(["inspect", "--format", "{{.Digest}}", &format!("docker://{image}:{tag}")])
        .output();

    if let Ok(output) = result {
        if output.status.success() {
            let digest = String::from_utf8_lossy(&output.stdout).trim().to_string();
            if !digest.is_empty() {
                return Ok(Some(digest));
            }
        }
    }

    Ok(None)
}

/// Get the cached digest from our local registry.
fn get_cached_digest(cache_registry: &str, image_path: &str, tag: &str, opts: &CopyOpts<'_>) -> anyhow::Result<Option<String>> {
    let mut args = vec!["digest".to_string(), format!("{cache_registry}/{image_path}:{tag}")];
    if opts.cache_insecure {
        args.push("--insecure".to_string());
    }

    let result = Command::new("crane").args(&args).output();

    if let Ok(output) = result {
        if output.status.success() {
            let digest = String::from_utf8_lossy(&output.stdout).trim().to_string();
            if !digest.is_empty() {
                return Ok(Some(digest));
            }
        }
    }

    Ok(None)
}

/// Copy an image from source to cache.
fn copy_image(source: &str, tag: &str, cache_registry: &str, cache_path: &str, opts: &CopyOpts<'_>) -> anyhow::Result<()> {
    let src = format!("{source}:{tag}");
    let dst = format!("{cache_registry}/{cache_path}:{tag}");

    // Build crane args
    let mut crane_args = vec!["copy".to_string(), src.clone(), dst.clone()];
    crane_args.extend(["--platform".to_string(), opts.platform.to_string()]);
    if opts.cache_insecure {
        crane_args.push("--insecure".to_string());
    }

    let result = Command::new("crane").args(&crane_args).output();

    if let Ok(output) = result {
        if output.status.success() {
            return Ok(());
        }
        let stderr = String::from_utf8_lossy(&output.stderr);
        tracing::warn!("crane copy failed: {stderr}");
    }

    // Fallback to skopeo with OCI manifest format.
    // Zot rejects some Docker v2 manifests with MANIFEST_INVALID.
    // Converting to OCI format during copy always works.
    //
    let mut skopeo_args = vec![
        "copy".to_string(),
        "--format".to_string(), "oci".to_string(),
        "--override-arch".to_string(), opts.platform.split('/').nth(1).unwrap_or("amd64").to_string(),
        "--override-os".to_string(), opts.platform.split('/').next().unwrap_or("linux").to_string(),
        format!("docker://{src}"),
        format!("docker://{dst}"),
    ];
    if !opts.source_tls_verify {
        skopeo_args.push("--src-tls-verify=false".to_string());
    }
    if opts.cache_insecure {
        skopeo_args.push("--dest-tls-verify=false".to_string());
    }

    let result = Command::new("skopeo").args(&skopeo_args).output()?;

    if !result.status.success() {
        let stderr = String::from_utf8_lossy(&result.stderr);
        anyhow::bail!("image copy failed: {stderr}");
    }

    Ok(())
}

/// Derive the cache path from the source image reference.
/// docker.io/akeyless/k8s-secrets-sidecar → akeyless/k8s-secrets-sidecar
/// ghcr.io/project-zot/zot-linux-amd64 → project-zot/zot-linux-amd64
fn derive_cache_path(source: &str) -> String {
    source
        .strip_prefix("docker.io/")
        .or_else(|| source.strip_prefix("ghcr.io/"))
        .or_else(|| source.strip_prefix("registry-1.docker.io/"))
        .unwrap_or(source)
        .to_string()
}

fn sync_image(spec: &ImageSpec, cache_registry: &str, settings: &Settings, dry_run: bool) -> SyncResult {
    let start = std::time::Instant::now();
    let cache_path = spec
        .cache_as
        .clone()
        .unwrap_or_else(|| derive_cache_path(&spec.source));

    // Resolve per-image overrides → global defaults
    let opts = CopyOpts {
        platform: spec.platform.as_deref().unwrap_or(&settings.default_platform),
        cache_insecure: spec.cache_insecure.unwrap_or(settings.cache_insecure),
        source_tls_verify: spec.source_tls_verify.unwrap_or(settings.source_tls_verify),
    };

    // Step 1: Check remote digest
    let remote_digest = match get_remote_digest(&spec.source, &spec.tag) {
        Ok(d) => d,
        Err(e) => {
            return SyncResult {
                image: spec.source.clone(),
                tag: spec.tag.clone(),
                action: SyncAction::Failed,
                remote_digest: None,
                cached_digest: None,
                duration_ms: start.elapsed().as_millis() as u64,
                error: Some(format!("remote digest check failed: {e}")),
            };
        }
    };

    // Step 2: Check cached digest
    let cached_digest = get_cached_digest(cache_registry, &cache_path, &spec.tag, &opts).unwrap_or(None);

    // Step 3: Compare — skip if identical
    if let (Some(remote), Some(cached)) = (&remote_digest, &cached_digest) {
        if remote == cached {
            tracing::info!(
                image = %spec.source,
                tag = %spec.tag,
                digest = %remote,
                "already cached — no action needed"
            );
            return SyncResult {
                image: spec.source.clone(),
                tag: spec.tag.clone(),
                action: SyncAction::AlreadyCached,
                remote_digest: remote_digest.clone(),
                cached_digest: cached_digest.clone(),
                duration_ms: start.elapsed().as_millis() as u64,
                error: None,
            };
        }
    }

    // Step 4: Pull if different (or not cached)
    if dry_run {
        tracing::info!(
            image = %spec.source,
            tag = %spec.tag,
            "DRY RUN — would pull (remote={:?}, cached={:?})",
            remote_digest,
            cached_digest,
        );
        return SyncResult {
            image: spec.source.clone(),
            tag: spec.tag.clone(),
            action: SyncAction::Skipped,
            remote_digest,
            cached_digest,
            duration_ms: start.elapsed().as_millis() as u64,
            error: None,
        };
    }

    tracing::info!(
        image = %spec.source,
        tag = %spec.tag,
        "pulling — remote digest changed or not cached"
    );

    match copy_image(&spec.source, &spec.tag, cache_registry, &cache_path, &opts) {
        Ok(()) => {
            tracing::info!(
                image = %spec.source,
                tag = %spec.tag,
                duration_ms = start.elapsed().as_millis(),
                "cached successfully"
            );
            SyncResult {
                image: spec.source.clone(),
                tag: spec.tag.clone(),
                action: SyncAction::Pulled,
                remote_digest,
                cached_digest,
                duration_ms: start.elapsed().as_millis() as u64,
                error: None,
            }
        }
        Err(e) => SyncResult {
            image: spec.source.clone(),
            tag: spec.tag.clone(),
            action: SyncAction::Failed,
            remote_digest,
            cached_digest,
            duration_ms: start.elapsed().as_millis() as u64,
            error: Some(e.to_string()),
        },
    }
}

#[tokio::main]
async fn main() -> anyhow::Result<()> {
    tracing_subscriber::fmt()
        .with_env_filter(
            tracing_subscriber::EnvFilter::try_from_default_env()
                .unwrap_or_else(|_| tracing_subscriber::EnvFilter::new("info")),
        )
        .init();

    let cli = Cli::parse();
    let config = load_config(&cli.config)?;

    // Trust policy for skopeo is baked into the container image at
    // /etc/containers/policy.json via the Nix flake (writeTextDir).

    tracing::info!(
        cache_registry = %config.cache_registry,
        image_count = config.images.len(),
        dry_run = cli.dry_run,
        default_platform = %config.settings.default_platform,
        cache_insecure = config.settings.cache_insecure,
        "image-sync starting"
    );

    let mut results = Vec::new();
    let mut pulled = 0u32;
    let mut cached = 0u32;
    let mut failed = 0u32;

    for spec in &config.images {
        let result = sync_image(spec, &config.cache_registry, &config.settings, cli.dry_run);
        match result.action {
            SyncAction::Pulled => pulled += 1,
            SyncAction::AlreadyCached => cached += 1,
            SyncAction::Failed => failed += 1,
            SyncAction::Skipped => {}
        }
        results.push(result);
    }

    tracing::info!(
        pulled = pulled,
        already_cached = cached,
        failed = failed,
        total = config.images.len(),
        "sync complete"
    );

    let report = serde_json::json!({
        "timestamp": Utc::now().to_rfc3339(),
        "cache_registry": config.cache_registry,
        "summary": {
            "pulled": pulled,
            "already_cached": cached,
            "failed": failed,
            "total": config.images.len(),
        },
        "results": results,
    });
    println!("{}", serde_json::to_string_pretty(&report)?);

    if failed > 0 && !config.settings.skip_on_error {
        anyhow::bail!("{failed} image(s) failed to sync");
    }

    Ok(())
}