preStop hook using image distroless/java17:nonroot

189 views Asked by At

Does anyone had success implementing preStop hook with distroless/java17:nonroot ? Here is my (default) deployment:

# [...]
          lifecycle:
            preStop:
              exec:
                command: ["sh", "-c", "sleep 10"]

          securityContext:
            readOnlyRootFilesystem: true
            allowPrivilegeEscalation: false
            privileged: false
            runAsUser: 65532
            capabilities:
              drop:
                - ALL

          volumeMounts:
            - name: tmp-volume
              mountPath: /tmp
1

There are 1 answers

0
Jonathan Chevalier On

Solved using actuator endpoint

@Slf4j
@Component
@ControllerEndpoint(id = "preStopHook")
class WebMvcPreStopHookEndpoint {

  @ResponseStatus(OK)
  @GetMapping("/{delayInMillis}")
  public ResponseEntity<Void> preStopHook(@PathVariable("delayInMillis") final long delayInMillis)
      throws InterruptedException {
    log.info("[preStopHook] received signal to sleep for {}ms", delayInMillis);
    Thread.sleep(delayInMillis);
    return null;
  }
}