Skip to content

Commit dfd6c26

Browse files
committed
Ref #25533: support SpEL for max lifetime setting
1 parent edcab0c commit dfd6c26

5 files changed

Lines changed: 57 additions & 14 deletions

File tree

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/**
2+
* ContainerProxy
3+
*
4+
* Copyright (C) 2016-2021 Open Analytics
5+
*
6+
* ===========================================================================
7+
*
8+
* This program is free software: you can redistribute it and/or modify
9+
* it under the terms of the Apache License as published by
10+
* The Apache Software Foundation, either version 2 of the License, or
11+
* (at your option) any later version.
12+
*
13+
* This program is distributed in the hope that it will be useful,
14+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
15+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16+
* Apache License for more details.
17+
*
18+
* You should have received a copy of the Apache License
19+
* along with this program. If not, see <http://www.apache.org/licenses/>
20+
*/
21+
package eu.openanalytics.containerproxy.model.runtime.runtimevalues;
22+
23+
public class MaxLifetimeKey extends RuntimeValueKey<Long> {
24+
25+
private MaxLifetimeKey() {
26+
super("openanalytics.eu/sp-max-lifetime",
27+
"SHINYPROXY_MAX_LIFETIME",
28+
false,
29+
true,
30+
false,
31+
true, Long.class);
32+
}
33+
34+
public static MaxLifetimeKey inst = new MaxLifetimeKey();
35+
36+
}

src/main/java/eu/openanalytics/containerproxy/model/runtime/runtimevalues/RuntimeValueKeyRegistry.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,8 @@ public static RuntimeValueKey<?> getRuntimeValue(String key) {
6060
addRuntimeValueKey(UserGroupsKey.inst);
6161
addRuntimeValueKey(UserIdKey.inst);
6262
addRuntimeValueKey(ParametersKey.inst);
63+
addRuntimeValueKey(HeartbeatTimeoutKey.inst);
64+
addRuntimeValueKey(MaxLifetimeKey.inst);
6365
}
6466

6567
}

src/main/java/eu/openanalytics/containerproxy/model/spec/ProxySpec.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ public class ProxySpec {
3939

4040
private Parameters parameters;
4141

42-
private Long maxLifeTime;
42+
private String maxLifeTime;
4343
private Boolean stopOnLogout;
4444
private String heartbeatTimeout;
4545

@@ -132,11 +132,11 @@ public List<String> getKubernetesAdditionalPersistentManifests() {
132132
return kubernetesAdditionalPersistentManifests;
133133
}
134134

135-
public Long getMaxLifeTime() {
135+
public String getMaxLifeTime() {
136136
return maxLifeTime;
137137
}
138138

139-
public void setMaxLifeTime(Long maxLifeTime) {
139+
public void setMaxLifeTime(String maxLifeTime) {
140140
this.maxLifeTime = maxLifeTime;
141141
}
142142

src/main/java/eu/openanalytics/containerproxy/service/ProxyMaxLifetimeService.java

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222

2323
import eu.openanalytics.containerproxy.model.runtime.Proxy;
2424
import eu.openanalytics.containerproxy.model.runtime.ProxyStatus;
25+
import eu.openanalytics.containerproxy.model.runtime.runtimevalues.MaxLifetimeKey;
2526
import org.apache.commons.lang.time.DurationFormatUtils;
2627
import org.apache.logging.log4j.LogManager;
2728
import org.apache.logging.log4j.Logger;
@@ -42,22 +43,15 @@
4243
public class ProxyMaxLifetimeService {
4344

4445
private static final Integer CLEANUP_INTERVAL = 5 * 60 * 1000;
45-
private static final String PROP_DEFAULT_PROXY_MAX_LIFETIME = "proxy.default-proxy-max-lifetime";
4646

4747
private final Logger log = LogManager.getLogger(ProxyMaxLifetimeService.class);
4848

4949
@Inject
5050
private ProxyService proxyService;
5151

52-
@Inject
53-
private Environment environment;
54-
55-
private Long defaultMaxLifetime;
5652

5753
@PostConstruct
5854
public void init() {
59-
defaultMaxLifetime = environment.getProperty(PROP_DEFAULT_PROXY_MAX_LIFETIME, Long.class, -1L);
60-
6155
new Timer().schedule(new TimerTask() {
6256
@Override
6357
public void run() {
@@ -84,10 +78,7 @@ private Boolean mustBeReleased(Proxy proxy) {
8478
return false;
8579
}
8680

87-
Long maxLifeTime = proxy.getSpec().getMaxLifeTime();
88-
if (maxLifeTime == null) {
89-
maxLifeTime = defaultMaxLifetime;
90-
}
81+
Long maxLifeTime = proxy.getRuntimeObject(MaxLifetimeKey.inst);
9182

9283
if (maxLifeTime > 0) {
9384
Instant notBeforeTime = Instant.now().minus(maxLifeTime, ChronoUnit.MINUTES);

src/main/java/eu/openanalytics/containerproxy/service/RuntimeValueService.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import eu.openanalytics.containerproxy.model.runtime.ProvidedParameters;
2424
import eu.openanalytics.containerproxy.model.runtime.Proxy;
2525
import eu.openanalytics.containerproxy.model.runtime.runtimevalues.HeartbeatTimeoutKey;
26+
import eu.openanalytics.containerproxy.model.runtime.runtimevalues.MaxLifetimeKey;
2627
import eu.openanalytics.containerproxy.model.runtime.runtimevalues.ParametersKey;
2728
import eu.openanalytics.containerproxy.model.runtime.runtimevalues.RuntimeValue;
2829
import eu.openanalytics.containerproxy.model.spec.ProxySpec;
@@ -41,9 +42,14 @@ public class RuntimeValueService {
4142

4243
private static final String PROP_TIMEOUT = "proxy.heartbeat-timeout";
4344

45+
private static final String PROP_DEFAULT_PROXY_MAX_LIFETIME = "proxy.default-proxy-max-lifetime";
46+
4447
private static final Long DEFAULT_TIMEOUT = 60000L;
4548

4649
private long defaultHeartbeatTimeout;
50+
51+
private long defaultMaxLifetime;
52+
4753
@Inject
4854
private ParametersService parametersService;
4955

@@ -58,6 +64,7 @@ public class RuntimeValueService {
5864

5965
public void init() {
6066
defaultHeartbeatTimeout = environment.getProperty(PROP_TIMEOUT, Long.class, DEFAULT_TIMEOUT);
67+
defaultMaxLifetime = environment.getProperty(PROP_DEFAULT_PROXY_MAX_LIFETIME, Long.class, -1L);
6168
}
6269

6370
public void createRuntimeValues(ProxySpec spec, ProvidedParameters parameters, Proxy proxy) throws InvalidParametersException {
@@ -76,6 +83,13 @@ public void createRuntimeValues(ProxySpec spec, ProvidedParameters parameters, P
7683
} else {
7784
proxy.addRuntimeValue(new RuntimeValue(HeartbeatTimeoutKey.inst, defaultHeartbeatTimeout));
7885
}
86+
87+
if (spec.getMaxLifeTime() != null) {
88+
Long maxLifetime = expressionResolver.evaluateToLong(spec.getMaxLifeTime(), context);
89+
proxy.addRuntimeValue(new RuntimeValue(MaxLifetimeKey.inst, maxLifetime));
90+
} else {
91+
proxy.addRuntimeValue(new RuntimeValue(MaxLifetimeKey.inst, defaultMaxLifetime));
92+
}
7993
}
8094

8195
}

0 commit comments

Comments
 (0)