Add support for caching to bind mounts when building images

When building an image using the Maven `spring-boot:build-image` goal or
the Gradle `bootBuildImage` task, the build and launch caches can be
configured to use a bind mount as an alternative to using a named
volume.

Closes gh-28387
This commit is contained in:
Scott Frederick
2023-08-21 15:03:39 -05:00
parent d46a58f0f6
commit c17ecf0f0b
25 changed files with 575 additions and 47 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2012-2021 the original author or authors.
* Copyright 2012-2023 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -32,8 +32,8 @@ public class CacheInfo {
public CacheInfo() {
}
CacheInfo(VolumeCacheInfo volumeCacheInfo) {
this.cache = Cache.volume(volumeCacheInfo.getName());
private CacheInfo(Cache cache) {
this.cache = cache;
}
public void setVolume(VolumeCacheInfo info) {
@@ -41,10 +41,23 @@ public class CacheInfo {
this.cache = Cache.volume(info.getName());
}
public void setBind(BindCacheInfo info) {
Assert.state(this.cache == null, "Each image building cache can be configured only once");
this.cache = Cache.bind(info.getSource());
}
Cache asCache() {
return this.cache;
}
static CacheInfo fromVolume(VolumeCacheInfo cacheInfo) {
return new CacheInfo(Cache.volume(cacheInfo.getName()));
}
static CacheInfo fromBind(BindCacheInfo cacheInfo) {
return new CacheInfo(Cache.bind(cacheInfo.getSource()));
}
/**
* Encapsulates configuration of an image building cache stored in a volume.
*/
@@ -69,4 +82,28 @@ public class CacheInfo {
}
/**
* Encapsulates configuration of an image building cache stored in a bind mount.
*/
public static class BindCacheInfo {
private String source;
public BindCacheInfo() {
}
BindCacheInfo(String name) {
this.source = name;
}
public String getSource() {
return this.source;
}
void setSource(String source) {
this.source = source;
}
}
}