Skip to content

Commit

Permalink
Merge pull request #1713 from lilai23/feature_opentelemetry_injector
Browse files Browse the repository at this point in the history
support install external agent by Sermant-injector
  • Loading branch information
Sherlockhan authored Dec 30, 2024
2 parents c66c14a + 9d52aaf commit e07ef86
Show file tree
Hide file tree
Showing 6 changed files with 87 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,9 @@ data:
# Injector injection action
SERMANT_AGENT_INJECT_ACTION: {{ .Values.injector.action }}
{{- if not (and .Values.configMap.enabled .Values.configMap.namespaces) }}
# External agent image address
SERMANT_AGENT_EXTERNALAGENT_IMAGEADDR: {{ .Values.externalAgent.imageAddr }}
# External agent file name in image
SERMANT_AGENT_EXTERNALAGENT_FILENAME: {{ .Values.externalAgent.fileName }}
SERMANT_AGENT_CONFIGMAP: ""
{{- end }}
6 changes: 6 additions & 0 deletions sermant-injector/deployment/release/injector/values.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,12 @@ agent:
# Never(only local image is used, never pull)
pullPolicy: IfNotPresent

externalAgent:
# external agent image address (for example: io.sermant/opentelemetry-javaagent:2.10.0)
imageAddr:
# external agent file name (for example: opentelemetry-javaagent.jar)
fileName:

config:
# Configuration center type: ZOOKEEPER/KIE/NACOS
type: ZOOKEEPER
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
FROM alpine:latest

WORKDIR /home

# must copy agent file to /home, 'opentelemetry-javaagent.jar' is an example
COPY opentelemetry-javaagent.jar /home
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# image name of external-agent, 'opentelemetry-javaagent' is an example
imageName=io.sermant/opentelemetry-javaagent
# image version of external-agent
imageVersion=2.10.0

name=${imageName}:${imageVersion}
docker build -f dockerfile -t ${name} .
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ public class SermantInjectorController {

private static final String IMAGE_PULL_POLICY_KEY = "imagePullPolicy";

private static final String IMAGE_NAME = "sermant-agent";
private static final String SERMANT_AGENT_INIT_CONTAINER_NAME = "sermant-agent";

private static final String VOLUME_DIR = "emptyDir";

Expand Down Expand Up @@ -180,6 +180,20 @@ public class SermantInjectorController {

private static final String SERMANT_ENV_PREFIX = "env.sermant.io/";

private static final String EXTERNAL_AGENT_INJECTION_CONFIG_KEY = "agent_config_externalAgent_injection";

private static final String EXTERNAL_AGENT_NAME_CONFIG_KEY = "agent_config_externalAgent_name";

private static final String EXTERNAL_AGENT_FILE_CONFIG_KEY = "agent_config_externalAgent_file";

private static final String EXTERNAL_AGENT_INIT_CONTAINER_NAME = "external-agent";

/**
* if external agent injection is to be enabled, the annotations should be config as
* 'env.sermant.io/external.agent.injection:"OTEL"' in spec -> template ->metadata -> annotations
*/
private static final String EXTERNAL_AGENT_INJECTION_ENV_IN_ANNOTATION = "external.agent.injection";

@Autowired
private ObjectMapper om;

Expand Down Expand Up @@ -213,6 +227,12 @@ public class SermantInjectorController {
@Value("${sermant-agent.inject.action:before}")
private String action;

@Value("${sermant-agent.externalAgent.imageAddr:}")
private String externalAgentAddr;

@Value("${sermant-agent.externalAgent.fileName:}")
private String externalAgentFileName;

/**
* Admission controller
*
Expand Down Expand Up @@ -258,7 +278,9 @@ private Optional<String> inject(ObjectNode body) {
ArrayNode arrayNode = om.createArrayNode();

// Added initContainers node
injectInitContainer(arrayNode, specNode);
injectSermantInitContainer(arrayNode, specNode);

injectExternalAgentInitContainer(arrayNode, annotationEnv);

// Add volume nodes
injectVolumes(arrayNode, specNode);
Expand Down Expand Up @@ -308,6 +330,11 @@ private void setEnvByMap(JsonNode srcMap, Map<String, String> tgtEnv) {
String envKey = labelName.substring(prefixLength);
String envValue = srcMap.findValue(labelName).textValue();
tgtEnv.put(envKey, envValue);
if (envKey.equals(EXTERNAL_AGENT_INJECTION_ENV_IN_ANNOTATION)) {
tgtEnv.put(EXTERNAL_AGENT_INJECTION_CONFIG_KEY, Boolean.TRUE.toString());
tgtEnv.put(EXTERNAL_AGENT_NAME_CONFIG_KEY, envValue);
tgtEnv.put(EXTERNAL_AGENT_FILE_CONFIG_KEY, mountPath + PATH_SEPARATOR + externalAgentFileName);
}
}
}
}
Expand All @@ -327,31 +354,54 @@ private void setEnv(JsonNode containersPath, Map<Integer, Map<String, String>> c
}
}

private void injectInitContainer(ArrayNode arrayNode, JsonNode specPath) {
// Create an initContainer
ObjectNode initContainerNode = putOrAddObject(arrayNode, specPath, INIT_CONTAINERS_PATH,
private void injectSermantInitContainer(ArrayNode arrayNode, JsonNode specPath) {
ObjectNode sermantInitContainerNode = putOrAddObject(arrayNode, specPath, INIT_CONTAINERS_PATH,
INIT_CONTAINERS_INJECT_PATH);

constructInitContainerNode(sermantInitContainerNode, SERMANT_AGENT_INIT_CONTAINER_NAME, imageAddr,
INIT_COMMAND);
}

private void injectExternalAgentInitContainer(ArrayNode arrayNode, Map<String, String> annotationEnv) {
if (!StringUtils.hasText(annotationEnv.get(EXTERNAL_AGENT_INJECTION_ENV_IN_ANNOTATION))) {
return;
}

ObjectNode node = arrayNode.addObject();
node.put(JSON_OPERATION_KEY, JSON_OPERATION_ADD);
node.put(PATH_KEY, INIT_CONTAINERS_INJECT_PATH + END_PATH);

ObjectNode externalAgentInitContainerNode = node.putObject(VALUE_KEY);
List<JsonNode> initCommandForExternalAgent =
Arrays.asList(new TextNode("cp"), new TextNode("/home/" + externalAgentFileName),
new TextNode(mountPath));
constructInitContainerNode(externalAgentInitContainerNode, EXTERNAL_AGENT_INIT_CONTAINER_NAME,
externalAgentAddr, initCommandForExternalAgent);
}

private void constructInitContainerNode(ObjectNode initContainerNode, String containerName, String image,
List<JsonNode> command) {
// Image name
initContainerNode.put(NAME_KEY, IMAGE_NAME);
initContainerNode.put(NAME_KEY, containerName);

// Image address
initContainerNode.put(IMAGE_KEY, imageAddr);
initContainerNode.put(IMAGE_KEY, image);

// Image pulling policy
initContainerNode.put(IMAGE_PULL_POLICY_KEY, pullPolicy);

// Initialization command
initContainerNode.putArray(COMMAND_KEY).addAll(INIT_COMMAND);
initContainerNode.putArray(COMMAND_KEY).addAll(command);

// Create a volumeMount
ObjectNode initContainerVolumeMountNode = initContainerNode.putArray(VOLUME_MOUNTS_PATH).addObject();
ObjectNode externalAgentVolumeMountNode = initContainerNode.putArray(VOLUME_MOUNTS_PATH)
.addObject();

// Disk name
initContainerVolumeMountNode.put(NAME_KEY, VOLUME_NAME);
externalAgentVolumeMountNode.put(NAME_KEY, VOLUME_NAME);

// Disk path
initContainerVolumeMountNode.put(MOUNT_PATH, INIT_SERMANT_PATH);
externalAgentVolumeMountNode.put(MOUNT_PATH, INIT_SERMANT_PATH);
}

private void injectVolumes(ArrayNode arrayNode, JsonNode specPath) {
Expand Down
3 changes: 3 additions & 0 deletions sermant-injector/src/main/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@ sermant-agent:
# injection before the original variable (default), after indicates injection after the original variable, and
# ignore indicates that injection is not performed
action: before
externalAgent:
imageAddr: io.sermant/opentelemetry-javaagent:2.10.0
fileName: opentelemetry-javaagent.jar

management:
endpoint:
Expand Down

0 comments on commit e07ef86

Please sign in to comment.