Skip to content

armgit / floci

Public Imported from GitHub
0

Light, fluffy, and always free - The AWS Local Emulator alternative

fix(kinesisanalytics): make the savepoints volume writable by the Flink user (#4490)

Aymen JAMAAOUI1b176bda0d15
diff --git a/src/main/java/io/github/hectorvent/floci/core/common/docker/ContainerLifecycleManager.java b/src/main/java/io/github/hectorvent/floci/core/common/docker/ContainerLifecycleManager.java
index 4c8807e1de..ca8347f10a 100644
--- a/src/main/java/io/github/hectorvent/floci/core/common/docker/ContainerLifecycleManager.java
+++ b/src/main/java/io/github/hectorvent/floci/core/common/docker/ContainerLifecycleManager.java
@@ -468,13 +468,20 @@ public class ContainerLifecycleManager {
* {@code --filter label=floci_emulator=floci-aws} (this emulator only) work.
*/
public void ensureVolume(String volumeName) {
- if (!volumeExists(volumeName)) {
- dockerClient.createVolumeCmd()
- .withName(volumeName)
- .withLabels(ContainerStorageHelper.defaultLabels(config))
- .exec();
- LOG.debugv("Created volume {0}", volumeName);
+ createVolumeIfAbsent(volumeName);
+ }
+
+ /** Creates the named volume when it is missing; returns whether it was created. */
+ private boolean createVolumeIfAbsent(String volumeName) {
+ if (volumeExists(volumeName)) {
+ return false;
}
+ dockerClient.createVolumeCmd()
+ .withName(volumeName)
+ .withLabels(ContainerStorageHelper.defaultLabels(config))
+ .exec();
+ LOG.debugv("Created volume {0}", volumeName);
+ return true;
}
/**
@@ -511,7 +518,11 @@ public class ContainerLifecycleManager {
*/
public void ensureSharedVolume(String volumeName, OptionalInt ownerUid, OptionalInt ownerGid,
Optional<String> rootPermissions, String initImage) {
- ensureVolume(volumeName);
+ // A volume created now is root:root 0755 again, so a memo left by a volume of the same name
+ // that was removed since (e.g. DeleteApplication, docker volume rm) is stale.
+ if (createVolumeIfAbsent(volumeName)) {
+ initializedSharedVolumes.remove(volumeName);
+ }
if (rootPermissions.isEmpty() && ownerUid.isEmpty() && ownerGid.isEmpty()) {
return;
}
diff --git a/src/main/java/io/github/hectorvent/floci/services/kinesisanalytics/container/FlinkContainerManager.java b/src/main/java/io/github/hectorvent/floci/services/kinesisanalytics/container/FlinkContainerManager.java
index e725012ff6..e1d4fb0115 100644
--- a/src/main/java/io/github/hectorvent/floci/services/kinesisanalytics/container/FlinkContainerManager.java
+++ b/src/main/java/io/github/hectorvent/floci/services/kinesisanalytics/container/FlinkContainerManager.java
@@ -40,6 +40,7 @@ import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Optional;
+import java.util.OptionalInt;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.TimeUnit;
@@ -65,6 +66,8 @@ public class FlinkContainerManager {
private static final Logger LOG = Logger.getLogger(FlinkContainerManager.class);
private static final int JOBMANAGER_REST_PORT = 8081;
private static final String SAVEPOINTS_MOUNT = "/opt/flink/savepoints";
+ private static final int FLINK_UID = 9999;
+ private static final int FLINK_GID = 9999;
private final ContainerBuilder containerBuilder;
private final ContainerLifecycleManager lifecycleManager;
@@ -178,8 +181,12 @@ public class FlinkContainerManager {
// Stop/StartApplication cycle — stopCluster() removes the JobManager container, but this
// volume is only removed on DeleteApplication (removeSavepointsVolume), mirroring how other
// Docker-backed services keep persistent data outside the container lifecycle.
- ContainerStorageHelper.applyNamedVolume(jmSpec, lifecycleManager,
- resolveVolumeName(app), SAVEPOINTS_MOUNT);
+ // Docker creates the volume root:root, but the official apache/flink images run Flink as
+ // uid/gid 9999, so the volume root is chowned to that user before the JobManager starts.
+ String savepointsVolume = resolveVolumeName(app);
+ lifecycleManager.ensureSharedVolume(savepointsVolume, OptionalInt.of(FLINK_UID),
+ OptionalInt.of(FLINK_GID), Optional.empty(), config.storage().efs().initImage());
+ jmSpec.withNamedVolume(savepointsVolume, SAVEPOINTS_MOUNT);
if (!containerDetector.isRunningInContainer()) {
jmSpec.withDynamicPort(JOBMANAGER_REST_PORT);
} else {
diff --git a/src/test/java/io/github/hectorvent/floci/core/common/docker/ContainerLifecycleManagerVolumeTest.java b/src/test/java/io/github/hectorvent/floci/core/common/docker/ContainerLifecycleManagerVolumeTest.java
index 2f4944117f..2210157670 100644
--- a/src/test/java/io/github/hectorvent/floci/core/common/docker/ContainerLifecycleManagerVolumeTest.java
+++ b/src/test/java/io/github/hectorvent/floci/core/common/docker/ContainerLifecycleManagerVolumeTest.java
@@ -3,6 +3,7 @@ package io.github.hectorvent.floci.core.common.docker;
import com.github.dockerjava.api.DockerClient;
import com.github.dockerjava.api.command.CreateContainerCmd;
import com.github.dockerjava.api.command.CreateContainerResponse;
+import com.github.dockerjava.api.command.CreateVolumeCmd;
import com.github.dockerjava.api.command.InspectVolumeCmd;
import com.github.dockerjava.api.command.InspectVolumeResponse;
import com.github.dockerjava.api.command.ListVolumesCmd;
@@ -333,6 +334,47 @@ class ContainerLifecycleManagerVolumeTest {
verify(dockerClient, times(1)).removeContainerCmd("helper-id");
}
+ @Test
+ void ensureSharedVolume_volumeRecreated_reinitialisesOwnership() {
+ // The volume exists on the first launch, then is removed (e.g. DeleteApplication in memory
+ // mode) and recreated root:root on the next one: the run-once guard must not skip the chown.
+ InspectVolumeCmd ivc = mock(InspectVolumeCmd.class);
+ when(dockerClient.inspectVolumeCmd("shared")).thenReturn(ivc);
+ when(ivc.exec())
+ .thenReturn(mock(InspectVolumeResponse.class))
+ .thenThrow(new NotFoundException("No such volume"));
+
+ CreateVolumeCmd cvc = mock(CreateVolumeCmd.class, RETURNS_SELF);
+ when(dockerClient.createVolumeCmd()).thenReturn(cvc);
+
+ CreateContainerCmd ccc = mock(CreateContainerCmd.class, RETURNS_SELF);
+ when(dockerClient.createContainerCmd("busybox:stable")).thenReturn(ccc);
+ CreateContainerResponse resp = mock(CreateContainerResponse.class);
+ when(resp.getId()).thenReturn("helper-id");
+ when(ccc.exec()).thenReturn(resp);
+
+ StartContainerCmd scc = mock(StartContainerCmd.class);
+ when(dockerClient.startContainerCmd("helper-id")).thenReturn(scc);
+
+ WaitContainerCmd wcc = mock(WaitContainerCmd.class);
+ when(dockerClient.waitContainerCmd("helper-id")).thenReturn(wcc);
+ WaitContainerResultCallback wcb = mock(WaitContainerResultCallback.class);
+ when(wcc.exec(any(WaitContainerResultCallback.class))).thenReturn(wcb);
+ when(wcb.awaitStatusCode(anyLong(), any())).thenReturn(0);
+
+ RemoveContainerCmd rcc = mock(RemoveContainerCmd.class, RETURNS_SELF);
+ when(dockerClient.removeContainerCmd("helper-id")).thenReturn(rcc);
+
+ manager.ensureSharedVolume("shared", OptionalInt.of(9999), OptionalInt.of(9999),
+ Optional.empty(), "busybox:stable");
+ manager.ensureSharedVolume("shared", OptionalInt.of(9999), OptionalInt.of(9999),
+ Optional.empty(), "busybox:stable");
+
+ verify(dockerClient, times(1)).createVolumeCmd();
+ verify(dockerClient, times(2)).createContainerCmd("busybox:stable");
+ verify(ccc, times(2)).withCmd("sh", "-c", "chown 9999:9999 /floci-shared-volume && true");
+ }
+
@Test
void ensureSharedVolume_partialOwnershipConfig_isRejected() {
InspectVolumeCmd cmd = mock(InspectVolumeCmd.class);
diff --git a/src/test/java/io/github/hectorvent/floci/services/kinesisanalytics/container/FlinkContainerManagerTest.java b/src/test/java/io/github/hectorvent/floci/services/kinesisanalytics/container/FlinkContainerManagerTest.java
index f5ac284d4d..17a967c9e6 100644
--- a/src/test/java/io/github/hectorvent/floci/services/kinesisanalytics/container/FlinkContainerManagerTest.java
+++ b/src/test/java/io/github/hectorvent/floci/services/kinesisanalytics/container/FlinkContainerManagerTest.java
@@ -3,6 +3,7 @@ package io.github.hectorvent.floci.services.kinesisanalytics.container;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.dockerjava.api.DockerClient;
+import com.github.dockerjava.api.model.MountType;
import io.github.hectorvent.floci.config.EmulatorConfig;
import io.github.hectorvent.floci.core.common.RegionResolver;
import io.github.hectorvent.floci.core.common.docker.ContainerBuilder;
@@ -27,6 +28,7 @@ import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
+import java.util.OptionalInt;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
@@ -87,10 +89,13 @@ class FlinkContainerManagerTest {
EmulatorConfig.DnsConfig dnsConfig = mock(EmulatorConfig.DnsConfig.class);
when(dnsConfig.containerFallbackEnabled()).thenReturn(false);
+ EmulatorConfig.StorageConfig storageConfig = storageWithInitImage("busybox:stable");
+
EmulatorConfig config = mock(EmulatorConfig.class);
when(config.docker()).thenReturn(dockerConfig);
when(config.services()).thenReturn(servicesConfig);
when(config.dns()).thenReturn(dnsConfig);
+ when(config.storage()).thenReturn(storageConfig);
when(config.defaultRegion()).thenReturn("us-west-2");
DockerHostResolver dockerHostResolver = mock(DockerHostResolver.class);
@@ -125,6 +130,14 @@ class FlinkContainerManagerTest {
MAPPER);
}
+ private static EmulatorConfig.StorageConfig storageWithInitImage(String initImage) {
+ EmulatorConfig.EfsSharingConfig efs = mock(EmulatorConfig.EfsSharingConfig.class);
+ when(efs.initImage()).thenReturn(initImage);
+ EmulatorConfig.StorageConfig storage = mock(EmulatorConfig.StorageConfig.class);
+ when(storage.efs()).thenReturn(efs);
+ return storage;
+ }
+
private FlinkApplication application(String name) {
return new FlinkApplication(name,
"arn:aws:kinesisanalytics:us-west-2:000000000000:application/" + name,
@@ -145,7 +158,7 @@ class FlinkContainerManagerTest {
when(config.docker()).thenReturn(docker);
when(docker.logMaxSize()).thenReturn("10m");
when(docker.logMaxFile()).thenReturn("3");
- EmulatorConfig.StorageConfig storage = Mockito.mock(EmulatorConfig.StorageConfig.class);
+ EmulatorConfig.StorageConfig storage = storageWithInitImage("busybox:stable");
when(config.storage()).thenReturn(storage);
when(storage.hostPersistentPath()).thenReturn("floci-data");
@@ -280,6 +293,24 @@ class FlinkContainerManagerTest {
verify(awsEnv).sdkBaselineEnv("us-west-2", Optional.empty());
}
+ @Test
+ void startCluster_savepointsVolume_isChownedToFlinkUser() {
+ FlinkApplication app = application("snap");
+ when(lifecycleManager.create(any())).thenReturn("jm-id");
+ when(lifecycleManager.startCreated(any(), any())).thenReturn(new ContainerInfo(
+ "jm-id", Map.of(8081, new EndpointInfo("localhost", 49152))));
+
+ manager.startCluster(app);
+
+ String volumeName = app.getDockerVolumeName();
+ verify(lifecycleManager).ensureSharedVolume(volumeName, OptionalInt.of(9999), OptionalInt.of(9999),
+ Optional.empty(), "busybox:stable");
+ ContainerSpec jmSpec = captureCreatedSpecs().getFirst();
+ assertTrue(jmSpec.mounts().stream().anyMatch(mount -> mount.getType() == MountType.VOLUME
+ && volumeName.equals(mount.getSource())
+ && "/opt/flink/savepoints".equals(mount.getTarget())));
+ }
+
@Test
void codeClusterInjectsTheSameAwsSdkEnvironmentIntoBothFlinkProcesses() {
FlinkApplication app = application("with-code");