Skip to content

Commit

Permalink
feat(vol): add custom instr volume spec
Browse files Browse the repository at this point in the history
  • Loading branch information
jnarezo committed Sep 12, 2024
1 parent e203cbc commit bf3d1e8
Show file tree
Hide file tree
Showing 7 changed files with 81 additions and 49 deletions.
28 changes: 28 additions & 0 deletions apis/v1alpha1/instrumentation_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,10 @@ type Java struct {
// +optional
Image string `json:"image,omitempty"`

// Volume defines the volume used for auto-instrumentation.
// The default volume is an emptyDir with size limit VolumeSizeLimit
Volume corev1.Volume `json:"volume,omitempty"`

// VolumeSizeLimit defines size limit for volume used for auto-instrumentation.
// The default size is 200Mi.
VolumeSizeLimit *resource.Quantity `json:"volumeLimitSize,omitempty"`
Expand Down Expand Up @@ -154,6 +158,10 @@ type NodeJS struct {
// +optional
Image string `json:"image,omitempty"`

// Volume defines the volume used for auto-instrumentation.
// The default volume is an emptyDir with size limit VolumeSizeLimit
Volume corev1.Volume `json:"volume,omitempty"`

// VolumeSizeLimit defines size limit for volume used for auto-instrumentation.
// The default size is 200Mi.
VolumeSizeLimit *resource.Quantity `json:"volumeLimitSize,omitempty"`
Expand All @@ -175,6 +183,10 @@ type Python struct {
// +optional
Image string `json:"image,omitempty"`

// Volume defines the volume used for auto-instrumentation.
// The default volume is an emptyDir with size limit VolumeSizeLimit
Volume corev1.Volume `json:"volume,omitempty"`

// VolumeSizeLimit defines size limit for volume used for auto-instrumentation.
// The default size is 200Mi.
VolumeSizeLimit *resource.Quantity `json:"volumeLimitSize,omitempty"`
Expand All @@ -196,6 +208,10 @@ type DotNet struct {
// +optional
Image string `json:"image,omitempty"`

// Volume defines the volume used for auto-instrumentation.
// The default volume is an emptyDir with size limit VolumeSizeLimit
Volume corev1.Volume `json:"volume,omitempty"`

// VolumeSizeLimit defines size limit for volume used for auto-instrumentation.
// The default size is 200Mi.
VolumeSizeLimit *resource.Quantity `json:"volumeLimitSize,omitempty"`
Expand All @@ -215,6 +231,10 @@ type Go struct {
// +optional
Image string `json:"image,omitempty"`

// Volume defines the volume used for auto-instrumentation.
// The default volume is an emptyDir with size limit VolumeSizeLimit
Volume corev1.Volume `json:"volume,omitempty"`

// VolumeSizeLimit defines size limit for volume used for auto-instrumentation.
// The default size is 200Mi.
VolumeSizeLimit *resource.Quantity `json:"volumeLimitSize,omitempty"`
Expand All @@ -236,6 +256,10 @@ type ApacheHttpd struct {
// +optional
Image string `json:"image,omitempty"`

// Volume defines the volume used for auto-instrumentation.
// The default volume is an emptyDir with size limit VolumeSizeLimit
Volume corev1.Volume `json:"volume,omitempty"`

// VolumeSizeLimit defines size limit for volume used for auto-instrumentation.
// The default size is 200Mi.
VolumeSizeLimit *resource.Quantity `json:"volumeLimitSize,omitempty"`
Expand Down Expand Up @@ -272,6 +296,10 @@ type Nginx struct {
// +optional
Image string `json:"image,omitempty"`

// Volume defines the volume used for auto-instrumentation.
// The default volume is an emptyDir with size limit VolumeSizeLimit
Volume corev1.Volume `json:"volume,omitempty"`

// VolumeSizeLimit defines size limit for volume used for auto-instrumentation.
// The default size is 200Mi.
VolumeSizeLimit *resource.Quantity `json:"volumeLimitSize,omitempty"`
Expand Down
11 changes: 3 additions & 8 deletions pkg/instrumentation/apachehttpd.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@ func injectApacheHttpdagent(_ logr.Logger, apacheSpec v1alpha1.ApacheHttpd, pod
// caller checks if there is at least one container
container := &pod.Spec.Containers[index]

volume, _ := instrVolume(apacheSpec.Volume, apacheAgentVolume, apacheSpec.VolumeSizeLimit)

// inject env vars
for _, env := range apacheSpec.Env {
idx := getIndexOfEnv(container.Env, env.Name)
Expand Down Expand Up @@ -135,14 +137,7 @@ func injectApacheHttpdagent(_ logr.Logger, apacheSpec v1alpha1.ApacheHttpd, pod
// Copy OTEL module to a shared volume
if isApacheInitContainerMissing(pod, apacheAgentInitContainerName) {
// Inject volume for agent
pod.Spec.Volumes = append(pod.Spec.Volumes, corev1.Volume{
Name: apacheAgentVolume,
VolumeSource: corev1.VolumeSource{
EmptyDir: &corev1.EmptyDirVolumeSource{
SizeLimit: volumeSize(apacheSpec.VolumeSizeLimit),
},
}})

pod.Spec.Volumes = append(pod.Spec.Volumes, volume)
pod.Spec.InitContainers = append(pod.Spec.InitContainers, corev1.Container{
Name: apacheAgentInitContainerName,
Image: apacheSpec.Image,
Expand Down
18 changes: 8 additions & 10 deletions pkg/instrumentation/dotnet.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,11 @@ func injectDotNetSDK(dotNetSpec v1alpha1.DotNet, pod corev1.Pod, index int, runt
return pod, err
}

volume, err := instrVolume(dotNetSpec.Volume, dotnetVolumeName, dotNetSpec.VolumeSizeLimit)
if err != nil {
return pod, err
}

// check if OTEL_DOTNET_AUTO_HOME env var is already set in the container
// if it is already set, then we assume that .NET Auto-instrumentation is already configured for this container
if getIndexOfEnv(container.Env, envDotNetOTelAutoHome) > -1 {
Expand Down Expand Up @@ -110,27 +115,20 @@ func injectDotNetSDK(dotNetSpec v1alpha1.DotNet, pod corev1.Pod, index int, runt
setDotNetEnvVar(container, envDotNetSharedStore, dotNetSharedStorePath, concatEnvValues)

container.VolumeMounts = append(container.VolumeMounts, corev1.VolumeMount{
Name: dotnetVolumeName,
Name: volume.Name,
MountPath: dotnetInstrMountPath,
})

// We just inject Volumes and init containers for the first processed container.
if isInitContainerMissing(pod, dotnetInitContainerName) {
pod.Spec.Volumes = append(pod.Spec.Volumes, corev1.Volume{
Name: dotnetVolumeName,
VolumeSource: corev1.VolumeSource{
EmptyDir: &corev1.EmptyDirVolumeSource{
SizeLimit: volumeSize(dotNetSpec.VolumeSizeLimit),
},
}})

pod.Spec.Volumes = append(pod.Spec.Volumes, volume)
pod.Spec.InitContainers = append(pod.Spec.InitContainers, corev1.Container{
Name: dotnetInitContainerName,
Image: dotNetSpec.Image,
Command: []string{"cp", "-r", "/autoinstrumentation/.", dotnetInstrMountPath},
Resources: dotNetSpec.Resources,
VolumeMounts: []corev1.VolumeMount{{
Name: dotnetVolumeName,
Name: volume.Name,
MountPath: dotnetInstrMountPath,
}},
})
Expand Down
17 changes: 17 additions & 0 deletions pkg/instrumentation/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package instrumentation

import (
"fmt"
"reflect"
"regexp"
"sort"
"strings"
Expand Down Expand Up @@ -123,6 +124,22 @@ func isInstrWithoutContainers(inst instrumentationWithContainers) int {
return 0
}

func instrVolume(volume corev1.Volume, name string, volumeSizeLimit *resource.Quantity) (corev1.Volume, error) {
if reflect.ValueOf(volume).IsValid() && volumeSizeLimit != nil {
return volume, fmt.Errorf("both Volume and VolumeSizeLimit cannot be defined simultaneously")
} else if reflect.ValueOf(volume).IsValid() {
return volume, nil
}

return corev1.Volume{
Name: name,
VolumeSource: corev1.VolumeSource{
EmptyDir: &corev1.EmptyDirVolumeSource{
SizeLimit: volumeSize(volumeSizeLimit),
},
}}, nil
}

func volumeSize(quantity *resource.Quantity) *resource.Quantity {
if quantity == nil {
return &defaultSize
Expand Down
20 changes: 9 additions & 11 deletions pkg/instrumentation/javaagent.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,11 @@ func injectJavaagent(javaSpec v1alpha1.Java, pod corev1.Pod, index int) (corev1.
return pod, err
}

volume, err := instrVolume(javaSpec.Volume, javaVolumeName, javaSpec.VolumeSizeLimit)
if err != nil {
return pod, err
}

// inject Java instrumentation spec env vars.
for _, env := range javaSpec.Env {
idx := getIndexOfEnv(container.Env, env.Name)
Expand All @@ -63,27 +68,20 @@ func injectJavaagent(javaSpec v1alpha1.Java, pod corev1.Pod, index int) (corev1.
}

container.VolumeMounts = append(container.VolumeMounts, corev1.VolumeMount{
Name: javaVolumeName,
Name: volume.Name,
MountPath: javaInstrMountPath,
})

// We just inject Volumes and init containers for the first processed container.
if isInitContainerMissing(pod, javaInitContainerName) {
pod.Spec.Volumes = append(pod.Spec.Volumes, corev1.Volume{
Name: javaVolumeName,
VolumeSource: corev1.VolumeSource{
EmptyDir: &corev1.EmptyDirVolumeSource{
SizeLimit: volumeSize(javaSpec.VolumeSizeLimit),
},
}})

pod.Spec.Volumes = append(pod.Spec.Volumes, volume)
pod.Spec.InitContainers = append(pod.Spec.InitContainers, corev1.Container{
Name: javaInitContainerName,
Image: javaSpec.Image,
Command: []string{"cp", "/javaagent.jar", javaInstrMountPath + "/javaagent.jar"},
Resources: javaSpec.Resources,
VolumeMounts: []corev1.VolumeMount{{
Name: javaVolumeName,
Name: volume.Name,
MountPath: javaInstrMountPath,
}},
})
Expand All @@ -95,7 +93,7 @@ func injectJavaagent(javaSpec v1alpha1.Java, pod corev1.Pod, index int) (corev1.
Command: []string{"cp", "-r", extension.Dir + "/.", javaInstrMountPath + "/extensions"},
Resources: javaSpec.Resources,
VolumeMounts: []corev1.VolumeMount{{
Name: javaVolumeName,
Name: volume.Name,
MountPath: javaInstrMountPath,
}},
})
Expand Down
18 changes: 8 additions & 10 deletions pkg/instrumentation/nodejs.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,11 @@ func injectNodeJSSDK(nodeJSSpec v1alpha1.NodeJS, pod corev1.Pod, index int) (cor
return pod, err
}

volume, err := instrVolume(nodeJSSpec.Volume, nodejsVolumeName, nodeJSSpec.VolumeSizeLimit)
if err != nil {
return pod, err
}

// inject NodeJS instrumentation spec env vars.
for _, env := range nodeJSSpec.Env {
idx := getIndexOfEnv(container.Env, env.Name)
Expand All @@ -56,27 +61,20 @@ func injectNodeJSSDK(nodeJSSpec v1alpha1.NodeJS, pod corev1.Pod, index int) (cor
}

container.VolumeMounts = append(container.VolumeMounts, corev1.VolumeMount{
Name: nodejsVolumeName,
Name: volume.Name,
MountPath: nodejsInstrMountPath,
})

// We just inject Volumes and init containers for the first processed container
if isInitContainerMissing(pod, nodejsInitContainerName) {
pod.Spec.Volumes = append(pod.Spec.Volumes, corev1.Volume{
Name: nodejsVolumeName,
VolumeSource: corev1.VolumeSource{
EmptyDir: &corev1.EmptyDirVolumeSource{
SizeLimit: volumeSize(nodeJSSpec.VolumeSizeLimit),
},
}})

pod.Spec.Volumes = append(pod.Spec.Volumes, volume)
pod.Spec.InitContainers = append(pod.Spec.InitContainers, corev1.Container{
Name: nodejsInitContainerName,
Image: nodeJSSpec.Image,
Command: []string{"cp", "-r", "/autoinstrumentation/.", nodejsInstrMountPath},
Resources: nodeJSSpec.Resources,
VolumeMounts: []corev1.VolumeMount{{
Name: nodejsVolumeName,
Name: volume.Name,
MountPath: nodejsInstrMountPath,
}},
})
Expand Down
18 changes: 8 additions & 10 deletions pkg/instrumentation/python.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,11 @@ func injectPythonSDK(pythonSpec v1alpha1.Python, pod corev1.Pod, index int) (cor
return pod, err
}

volume, err := instrVolume(pythonSpec.Volume, pythonVolumeName, pythonSpec.VolumeSizeLimit)
if err != nil {
return pod, err
}

// inject Python instrumentation spec env vars.
for _, env := range pythonSpec.Env {
idx := getIndexOfEnv(container.Env, env.Name)
Expand Down Expand Up @@ -89,27 +94,20 @@ func injectPythonSDK(pythonSpec v1alpha1.Python, pod corev1.Pod, index int) (cor
}

container.VolumeMounts = append(container.VolumeMounts, corev1.VolumeMount{
Name: pythonVolumeName,
Name: volume.Name,
MountPath: pythonInstrMountPath,
})

// We just inject Volumes and init containers for the first processed container.
if isInitContainerMissing(pod, pythonInitContainerName) {
pod.Spec.Volumes = append(pod.Spec.Volumes, corev1.Volume{
Name: pythonVolumeName,
VolumeSource: corev1.VolumeSource{
EmptyDir: &corev1.EmptyDirVolumeSource{
SizeLimit: volumeSize(pythonSpec.VolumeSizeLimit),
},
}})

pod.Spec.Volumes = append(pod.Spec.Volumes, volume)
pod.Spec.InitContainers = append(pod.Spec.InitContainers, corev1.Container{
Name: pythonInitContainerName,
Image: pythonSpec.Image,
Command: []string{"cp", "-r", "/autoinstrumentation/.", pythonInstrMountPath},
Resources: pythonSpec.Resources,
VolumeMounts: []corev1.VolumeMount{{
Name: pythonVolumeName,
Name: volume.Name,
MountPath: pythonInstrMountPath,
}},
})
Expand Down

0 comments on commit bf3d1e8

Please sign in to comment.