3838import eu .openanalytics .containerproxy .spec .IProxySpecProvider ;
3939import eu .openanalytics .containerproxy .util .EnvironmentUtils ;
4040import eu .openanalytics .containerproxy .util .Retrying ;
41+ import org .apache .commons .lang3 .StringUtils ;
4142import org .springframework .boot .autoconfigure .condition .ConditionalOnProperty ;
4243import org .springframework .data .util .Pair ;
4344import org .springframework .security .core .Authentication ;
@@ -91,6 +92,7 @@ public class EcsBackend extends AbstractContainerBackend {
9192 private static final String PROPERTY_REGION = "region" ;
9293 private static final String PROPERTY_SERVICE_WAIT_TIME = "service-wait-time" ;
9394 private static final Pattern TAG_VALUE_PATTERN = Pattern .compile ("^[a-zA-Z0-9 +-=._:/@]*$" );
95+ private static final Pattern LOG_GORUP_REPLACE_PATTERN = Pattern .compile ("[^a-zA-Z0-9_\\ -.#]" );
9496 private static final List <RuntimeValueKey <?>> IGNORED_RUNTIME_VALUES = Arrays .asList (PortMappingsKey .inst , UserGroupsKey .inst );
9597
9698 private EcsClient ecsClient ;
@@ -198,7 +200,7 @@ public Proxy startContainer(Authentication user, Container initialContainer, Con
198200 }
199201 }
200202
201- String taskDefinitionArn = getTaskDefinition (user , spec , specExtension , proxy , initialContainer , containerId , tags );
203+ String taskDefinitionArn = getTaskDefinition (user , spec , specExtension , proxy , initialContainer , tags );
202204
203205 // tell the status service we are starting the pod/container
204206 proxyStartupLogBuilder .startingContainer (initialContainer .getIndex ());
@@ -259,7 +261,7 @@ public Proxy startContainer(Authentication user, Container initialContainer, Con
259261 }
260262 }
261263
262- private String getTaskDefinition (Authentication user , ContainerSpec spec , EcsSpecExtension specExtension , Proxy proxy , Container initialContainer , String containerId , List <Tag > tags ) throws IOException {
264+ private String getTaskDefinition (Authentication user , ContainerSpec spec , EcsSpecExtension specExtension , Proxy proxy , Container initialContainer , List <Tag > tags ) throws IOException {
263265 if (spec .getImage ().getValue ().startsWith ("arn:aws:ecs:" )) {
264266 // external task definition
265267 return spec .getImage ().getValue ();
@@ -286,16 +288,20 @@ private String getTaskDefinition(Authentication user, ContainerSpec spec, EcsSpe
286288 .sizeInGiB (specExtension .ecsEphemeralStorageSize .getValueOrDefault (21 ))
287289 .build ();
288290
291+ // name of the container inside the task (it's not possible to choose a name for the task)
292+ // automatically used in the cloudwatch stream name
293+ String containerName = StringUtils .left (spec .getResourceName ().getValueOrDefault ("sp-container-" + proxy .getId () + "-" + initialContainer .getIndex ()), 255 );
294+
289295 RegisterTaskDefinitionResponse registerTaskDefinitionResponse = ecsClient .registerTaskDefinition (builder -> builder
290296 .family ("sp-task-definition-" + proxy .getId ()) // family is a name for the task definition
291297 .containerDefinitions (ContainerDefinition .builder ()
292- .name ("sp-container-" + containerId )
298+ .name (containerName )
293299 .image (spec .getImage ().getValue ())
294300 .command (spec .getCmd ().getValueOrNull ())
295301 .environment (env )
296302 .stopTimeout (2 )
297303 .dockerLabels (dockerLabels )
298- .logConfiguration (getLogConfiguration (proxy .getId ()))
304+ .logConfiguration (getLogConfiguration (proxy .getSpecId ()))
299305 .mountPoints (volumes .getSecond ())
300306 .build ())
301307 .networkMode (NetworkMode .AWSVPC ) // only option when using fargate
@@ -316,12 +322,13 @@ private String getTaskDefinition(Authentication user, ContainerSpec spec, EcsSpe
316322 return registerTaskDefinitionResponse .taskDefinition ().taskDefinitionArn ();
317323 }
318324
319- private LogConfiguration getLogConfiguration (String proxyId ) {
325+ private LogConfiguration getLogConfiguration (String specId ) {
320326 if (enableCloudWatch ) {
321327 LogConfiguration .Builder logConfiguration = LogConfiguration .builder ();
322328 logConfiguration .logDriver (LogDriver .AWSLOGS );
323329 HashMap <String , String > options = new HashMap <>();
324- options .put ("awslogs-group" , cloudWatchGroupPrefix + "sp-" + proxyId );
330+ String sanitizedSpecId = LOG_GORUP_REPLACE_PATTERN .matcher (specId ).replaceAll ("" );
331+ options .put ("awslogs-group" , cloudWatchGroupPrefix + "sp-" + sanitizedSpecId );
325332 options .put ("awslogs-region" , cloudWatchRegion );
326333 options .put ("awslogs-stream-prefix" , cloudWatchStreamPrefix );
327334 options .put ("awslogs-create-group" , "true" );
0 commit comments