Skip to content

Commit

Permalink
Merge pull request #3898 from nicolatimeus/backport-3895-to-release-5…
Browse files Browse the repository at this point in the history
….1.0

[Backport release-5.1.0] initial commit of all logger driver code
  • Loading branch information
nicolatimeus authored Mar 15, 2022
2 parents 270fbd5 + 274a2b6 commit c3ba2cd
Show file tree
Hide file tree
Showing 6 changed files with 173 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ public class ContainerConfiguration {
private Map<String, String> containerVolumes;
private Boolean containerPrivileged;
private Boolean isFrameworkManaged = true;
private Map<String, String> containerLoggerParameters;
private String containerLoggingType;

private ContainerConfiguration() {
}
Expand Down Expand Up @@ -139,6 +141,24 @@ public boolean getContainerPrivileged() {
return this.containerPrivileged;
}

/**
* Returns a Map that identifies configured logger parameters.
*
* @return
*/
public Map<String, String> getLoggerParameters() {
return this.containerLoggerParameters;
}

/**
* returns a string identifying which logger driver to use.
*
* @return
*/
public String getContainerLoggingType() {
return this.containerLoggingType;
}

/**
* Creates a builder for creating a new {@link ContainerConfiguration} instance.
*
Expand All @@ -151,7 +171,8 @@ public static ContainerConfigurationBuilder builder() {
@Override
public int hashCode() {
return Objects.hash(this.containerDevices, this.containerEnvVars, this.containerImage, this.containerImageTag,
this.containerName, this.containerPortsExternal, this.containerPortsInternal, this.containerPrivileged,
this.containerLoggerParameters, this.containerLoggingType, this.containerName,
this.containerPortsExternal, this.containerPortsInternal, this.containerPrivileged,
this.containerVolumes, this.isFrameworkManaged);
}

Expand All @@ -168,6 +189,8 @@ public boolean equals(Object obj) {
&& Objects.equals(this.containerEnvVars, other.containerEnvVars)
&& Objects.equals(this.containerImage, other.containerImage)
&& Objects.equals(this.containerImageTag, other.containerImageTag)
&& Objects.equals(this.containerLoggerParameters, other.containerLoggerParameters)
&& Objects.equals(this.containerLoggingType, other.containerLoggingType)
&& Objects.equals(this.containerName, other.containerName)
&& Objects.equals(this.containerPortsExternal, other.containerPortsExternal)
&& Objects.equals(this.containerPortsInternal, other.containerPortsInternal)
Expand All @@ -188,6 +211,8 @@ public static final class ContainerConfigurationBuilder {
private Map<String, String> containerVolumes = new HashMap<>();
private Boolean containerPrivilaged = false;
private Boolean isFrameworkManaged = false;
private Map<String, String> containerLoggerParameters;
private String containerLoggingType;

public ContainerConfigurationBuilder setContainerName(String serviceName) {
this.containerName = serviceName;
Expand Down Expand Up @@ -234,6 +259,16 @@ public ContainerConfigurationBuilder setVolumes(Map<String, String> volumeMap) {
return this;
}

public ContainerConfigurationBuilder setLoggerParameters(Map<String, String> paramMap) {
this.containerLoggerParameters = new HashMap<>(paramMap);
return this;
}

public ContainerConfigurationBuilder setLoggingType(String containerLoggingType) {
this.containerLoggingType = containerLoggingType;
return this;
}

public ContainerConfigurationBuilder setPrivilegedMode(Boolean containerPrivilaged) {
this.containerPrivilaged = containerPrivilaged;
return this;
Expand All @@ -252,6 +287,8 @@ public ContainerConfiguration build() {
result.containerVolumes = this.containerVolumes;
result.containerPrivileged = this.containerPrivilaged;
result.isFrameworkManaged = this.isFrameworkManaged;
result.containerLoggerParameters = this.containerLoggerParameters;
result.containerLoggingType = this.containerLoggingType;

return result;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@
import com.github.dockerjava.api.model.ExposedPort;
import com.github.dockerjava.api.model.HostConfig;
import com.github.dockerjava.api.model.Image;
import com.github.dockerjava.api.model.LogConfig;
import com.github.dockerjava.api.model.LogConfig.LoggingType;
import com.github.dockerjava.api.model.Ports;
import com.github.dockerjava.api.model.Ports.Binding;
import com.github.dockerjava.api.model.PullResponseItem;
Expand Down Expand Up @@ -408,6 +410,8 @@ private String createContainer(ContainerConfiguration containerDescription) thro
configuration = containerDevicesHandler(containerDescription, configuration);

configuration = containerPortManagementHandler(containerDescription, configuration);

configuration = containerLogConfigurationHandler(containerDescription, configuration);

if (containerDescription.getContainerPrivileged()) {
configuration = configuration.withPrivileged(containerDescription.getContainerPrivileged());
Expand All @@ -425,6 +429,62 @@ private String createContainer(ContainerConfiguration containerDescription) thro
}
}

private HostConfig containerLogConfigurationHandler(ContainerConfiguration containerDescription,
HostConfig configuration) {
LoggingType lt;
switch (containerDescription.getContainerLoggingType().toUpperCase().trim()) {
case "NONE":
lt = LoggingType.NONE;
break;
case "LOCAL":
lt = LoggingType.LOCAL;
break;
case "ETWLOGS":
lt = LoggingType.ETWLOGS;
break;
case "JSON_FILE":
lt = LoggingType.JSON_FILE;
break;
case "SYSLOG":
lt = LoggingType.SYSLOG;
break;
case "JOURNALD":
lt = LoggingType.JOURNALD;
break;
case "GELF":
lt = LoggingType.GELF;
break;
case "FLUENTD":
lt = LoggingType.FLUENTD;
break;
case "AWSLOGS":
lt = LoggingType.AWSLOGS;
break;
case "DB":
lt = LoggingType.DB;
break;
case "SPLUNK":
lt = LoggingType.SPLUNK;
break;
case "GCPLOGS":
lt = LoggingType.GCPLOGS;
break;
case "LOKI":
lt = LoggingType.LOKI;
break;
default:
lt = LoggingType.DEFAULT;
break;
}

LogConfig lc = new LogConfig(lt,
containerDescription.getLoggerParameters());

configuration.withLogConfig(lc);

return configuration;
}

private HostConfig containerPortManagementHandler(ContainerConfiguration containerDescription,
HostConfig commandBuilder) {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,30 @@
type="String" cardinality="1"
required="false" default="" />

<AD id="container.loggingType"
name="Logger Type"
description="Used to specify what logging driver the container will use. By default, containers will log to a JSON-FILE on the gateway." type="String" cardinality="0"
required="true"
default="DEFAULT">
<Option label="NONE" value="NONE" />
<Option label="DEFAULT" value="DEFAULT" />
<Option label="LOCAL" value="LOCAL" />
<Option label="ETWLOGS" value="ETWLOGS" />
<Option label="JSON_FILE" value="JSON_FILE" />
<Option label="SYSLOG" value="SYSLOG" />
<Option label="JOURNALD" value="JOURNALD" />
<Option label="GELF" value="GELF" />
<Option label="FLUENTD" value="FLUENTD" />
<Option label="AWSLOGS" value="AWSLOGS" />
<Option label="DB" value="DB" />
<Option label="SPLUNK" value="SPLUNK" />
<Option label="GCPLOGS" value="GCPLOGS" />
<Option label="LOKI" value="LOKI" />
</AD>

<AD id="container.loggerParameters" name="Logger Parameters"
description="Used to pass logger parameters to a container's logging driver. Example: max-buffer-size=4m, labels=location." type="String" cardinality="1"
required="false" default="" />

</OCD>
<Designate pid="org.eclipse.kura.container.provider.ContainerInstance"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ public class ContainerInstanceOptions {
"container.image.download.retries", 5);
private static final Property<Integer> CONTAINER_IMAGE_DOWNLOAD_RETRY_INTERVAL = new Property<>(
"container.image.download.interval", 30000);
private static final Property<String> CONTAINER_LOGGER_PARAMETERS = new Property<>("container.loggerParameters",
"");
private static final Property<String> CONTAINER_LOGGING_TYPE = new Property<>("container.loggingType", "default");

private final boolean enabled;
private final String image;
Expand All @@ -55,6 +58,8 @@ public class ContainerInstanceOptions {
private final Map<String, String> containerVolumes;
private final int maxDownloadRetries;
private final int retryInterval;
private final Map<String, String> containerLoggingParameters;
private final String containerLoggerType;

public ContainerInstanceOptions(final Map<String, Object> properties) {
if (isNull(properties)) {
Expand All @@ -74,6 +79,8 @@ public ContainerInstanceOptions(final Map<String, Object> properties) {
this.privilegedMode = CONTAINER_PRIVILEGED.get(properties);
this.maxDownloadRetries = CONTAINER_IMAGE_DOWNLOAD_RETRIES.get(properties);
this.retryInterval = CONTAINER_IMAGE_DOWNLOAD_RETRY_INTERVAL.get(properties);
this.containerLoggerType = CONTAINER_LOGGING_TYPE.get(properties);
this.containerLoggingParameters = parseLoggingParams(CONTAINER_LOGGER_PARAMETERS.get(properties));
}

private Map<String, String> parseVolume(String volumeString) {
Expand All @@ -93,6 +100,23 @@ private Map<String, String> parseVolume(String volumeString) {
return map;
}

private Map<String, String> parseLoggingParams(String loggingString) {
Map<String, String> map = new HashMap<>();

if (loggingString.isEmpty()) {
return map;
}

for (String entry : loggingString.trim().split(",")) {
String[] tempEntry = entry.split("=");
if (tempEntry.length == 2) {
map.put(tempEntry[0].trim(), tempEntry[1].trim());
}
}

return map;
}

private List<String> parseEnvVars(String containerVolumeString) {
List<String> envList = new LinkedList<>();

Expand Down Expand Up @@ -174,13 +198,22 @@ public int getRetryInterval() {
return this.retryInterval;
}

public String getLoggingType() {
return this.containerLoggerType;
}

public Map<String, String> getLoggerParameters() {
return this.containerLoggingParameters;
}

public ContainerConfiguration getContainerConfiguration() {
return ContainerConfiguration.builder().setContainerName(getContainerName())
.setContainerImage(getContainerImage()).setContainerImageTag(getContainerImageTag())
.setExternalPorts(getContainerPortsExternal()).setInternalPorts(getContainerPortsInternal())
.setEnvVars(getContainerEnvList()).setVolumes(getContainerVolumeList())
.setPrivilegedMode(this.privilegedMode).setDeviceList(getContainerDeviceList())
.setFrameworkManaged(true).build();
.setFrameworkManaged(true).setLoggingType(getLoggingType()).setLoggerParameters(getLoggerParameters())
.build();
}

private List<Integer> parsePortString(String ports) {
Expand All @@ -198,8 +231,9 @@ private List<Integer> parsePortString(String ports) {

@Override
public int hashCode() {
return Objects.hash(this.containerDevice, this.containerEnv, this.containerName, this.containerVolumeString,
this.containerVolumes, this.enabled, this.externalPorts, this.image, this.imageTag, this.internalPorts,
return Objects.hash(this.containerDevice, this.containerEnv, this.containerLoggerType,
this.containerLoggingParameters, this.containerName, this.containerVolumeString, this.containerVolumes,
this.enabled, this.externalPorts, this.image, this.imageTag, this.internalPorts,
this.maxDownloadRetries, this.privilegedMode, this.retryInterval);
}

Expand All @@ -214,6 +248,8 @@ public boolean equals(Object obj) {
ContainerInstanceOptions other = (ContainerInstanceOptions) obj;
return Objects.equals(this.containerDevice, other.containerDevice)
&& Objects.equals(this.containerEnv, other.containerEnv)
&& Objects.equals(this.containerLoggerType, other.containerLoggerType)
&& Objects.equals(this.containerLoggingParameters, other.containerLoggingParameters)
&& Objects.equals(this.containerName, other.containerName)
&& Objects.equals(this.containerVolumeString, other.containerVolumeString)
&& Objects.equals(this.containerVolumes, other.containerVolumes) && this.enabled == other.enabled
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ public class ContainerInstanceOptionsTest {
private static final boolean DEFAULT_CCONTAINER_PRIVILEGED = false;
private static final int DEFAULT_CONTAINER_IMAGE_DOWNLOAD_RETRIES = 5;
private static final int DEFAULT_CONTAINER_IMAGE_DOWNLOAD_RETRY_INTERVAL = 30000;
private static final String DEFAULT_CONTAINER_LOGGER_PARAMETERS = "";
private static final String DEFAULT_CONTAINER_LOGGING_TYPE = "default";

private static final String CONTAINER_ENV = "container.env";
private static final String CONTAINER_PORTS_INTERNAL = "container.ports.internal";
Expand All @@ -55,6 +57,8 @@ public class ContainerInstanceOptionsTest {
private static final String CONTAINER_PRIVILEGED = "container.privileged";
private static final String CONTAINER_IMAGE_DOWNLOAD_RETRIES = "container.image.download.retries";
private static final String CONTAINER_IMAGE_DOWNLOAD_RETRY_INTERVAL = "container.image.download.interval";
private static final String CONTAINER_LOGGER_PARAMETERS = "container.loggerParameters";
private static final String CONTAINER_LOGGING_TYPE = "container.loggingType";

private Map<String, Object> properties;

Expand Down Expand Up @@ -518,6 +522,8 @@ private void givenDefaultProperties() {
this.properties.put(CONTAINER_VOLUME,
DEFAULT_CONTAINER_PATH_FILE_PATH + ":" + DEFAULT_CONTAINER_PATH_DESTINATION);
this.properties.put(CONTAINER_DEVICE, DEFAULT_CONTAINER_DEVICE);
this.properties.put(CONTAINER_LOGGING_TYPE, DEFAULT_CONTAINER_LOGGING_TYPE);
this.properties.put(CONTAINER_LOGGER_PARAMETERS, DEFAULT_CONTAINER_LOGGER_PARAMETERS);
}

private void givenDifferentProperties() {
Expand All @@ -531,6 +537,8 @@ private void givenDifferentProperties() {
this.newProperties.put(CONTAINER_ENV, "");
this.newProperties.put(CONTAINER_VOLUME, "diffrent:diffrent");
this.newProperties.put(CONTAINER_DEVICE, "");
this.properties.put(CONTAINER_LOGGING_TYPE, "JOURNALD");
this.properties.put(CONTAINER_LOGGER_PARAMETERS, "label=true");
}

private void givenConfigurableGenericDockerServiceOptions() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ public class ContainerInstanceTest {
private static final String CONTAINER_IMAGE = "container.image";
private static final String CONTAINER_ENABLED = "container.enabled";
private static final String CONTAINER_DEVICE = "container.Device";
private static final String CONTAINER_LOGGER_PARAMETERS = "container.loggerParameters";
private static final String CONTAINER_LOGGING_TYPE = "container.loggingType";

private ContainerOrchestrationService dockerService;
private Map<String, Object> properties;
Expand Down Expand Up @@ -181,6 +183,8 @@ private void givenFullProperties(boolean enabled) {
this.properties.put(CONTAINER_PATH_DESTINATION, "");
this.properties.put(CONTAINER_PATH_FILE_PATH, "");
this.properties.put(CONTAINER_DEVICE, "");
this.properties.put(CONTAINER_LOGGER_PARAMETERS, "");
this.properties.put(CONTAINER_LOGGING_TYPE, "default");
}

private void givenConfigurableGenericDockerService() {
Expand Down

0 comments on commit c3ba2cd

Please sign in to comment.