Skip to content

Commit

Permalink
feat: add ephemeral volume option
Browse files Browse the repository at this point in the history
  • Loading branch information
jnarezo committed Oct 8, 2024
1 parent 717834e commit 1b029b9
Show file tree
Hide file tree
Showing 9 changed files with 79 additions and 68 deletions.
42 changes: 21 additions & 21 deletions apis/v1alpha1/instrumentation_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,9 +120,9 @@ 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"`
// VolumeClaimTemplate defines a ephemeral volume used for auto-instrumentation.
// If omitted, an emptyDir is used with size limit VolumeSizeLimit
VolumeClaimTemplate corev1.PersistentVolumeClaimTemplate `json:"volumeClaimTemplate,omitempty"`

// VolumeSizeLimit defines size limit for volume used for auto-instrumentation.
// The default size is 200Mi.
Expand Down Expand Up @@ -158,9 +158,9 @@ 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"`
// VolumeClaimTemplate defines a ephemeral volume used for auto-instrumentation.
// If omitted, an emptyDir is used with size limit VolumeSizeLimit
VolumeClaimTemplate corev1.PersistentVolumeClaimTemplate `json:"volumeClaimTemplate,omitempty"`

// VolumeSizeLimit defines size limit for volume used for auto-instrumentation.
// The default size is 200Mi.
Expand All @@ -183,9 +183,9 @@ 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"`
// VolumeClaimTemplate defines a ephemeral volume used for auto-instrumentation.
// If omitted, an emptyDir is used with size limit VolumeSizeLimit
VolumeClaimTemplate corev1.PersistentVolumeClaimTemplate `json:"volumeClaimTemplate,omitempty"`

// VolumeSizeLimit defines size limit for volume used for auto-instrumentation.
// The default size is 200Mi.
Expand All @@ -208,9 +208,9 @@ 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"`
// VolumeClaimTemplate defines a ephemeral volume used for auto-instrumentation.
// If omitted, an emptyDir is used with size limit VolumeSizeLimit
VolumeClaimTemplate corev1.PersistentVolumeClaimTemplate `json:"volumeClaimTemplate,omitempty"`

// VolumeSizeLimit defines size limit for volume used for auto-instrumentation.
// The default size is 200Mi.
Expand All @@ -231,9 +231,9 @@ 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"`
// VolumeClaimTemplate defines a ephemeral volume used for auto-instrumentation.
// If omitted, an emptyDir is used with size limit VolumeSizeLimit
VolumeClaimTemplate corev1.PersistentVolumeClaimTemplate `json:"volumeClaimTemplate,omitempty"`

// VolumeSizeLimit defines size limit for volume used for auto-instrumentation.
// The default size is 200Mi.
Expand All @@ -256,9 +256,9 @@ 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"`
// VolumeClaimTemplate defines a ephemeral volume used for auto-instrumentation.
// If omitted, an emptyDir is used with size limit VolumeSizeLimit
VolumeClaimTemplate corev1.PersistentVolumeClaimTemplate `json:"volumeClaimTemplate,omitempty"`

// VolumeSizeLimit defines size limit for volume used for auto-instrumentation.
// The default size is 200Mi.
Expand Down Expand Up @@ -296,9 +296,9 @@ 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"`
// VolumeClaimTemplate defines a ephemeral volume used for auto-instrumentation.
// If omitted, an emptyDir is used with size limit VolumeSizeLimit
VolumeClaimTemplate corev1.PersistentVolumeClaimTemplate `json:"volumeClaimTemplate,omitempty"`

// VolumeSizeLimit defines size limit for volume used for auto-instrumentation.
// The default size is 200Mi.
Expand Down
32 changes: 16 additions & 16 deletions apis/v1alpha1/instrumentation_webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -239,33 +239,33 @@ func (w InstrumentationWebhook) validate(r *Instrumentation) (admission.Warnings
}

var err error
err = validateInstrVolume(r.Spec.ApacheHttpd.Volume, r.Spec.ApacheHttpd.VolumeSizeLimit)
err = validateInstrVolume(r.Spec.ApacheHttpd.VolumeClaimTemplate, r.Spec.ApacheHttpd.VolumeSizeLimit)
if err != nil {
return warnings, fmt.Errorf("spec.apachehttpd.volume and spec.apachehttpd.volumeSizeLimit cannot both be defined: %w", err)
return warnings, fmt.Errorf("spec.apachehttpd.volumeClaimTemplate and spec.apachehttpd.volumeSizeLimit cannot both be defined: %w", err)
}
err = validateInstrVolume(r.Spec.DotNet.Volume, r.Spec.DotNet.VolumeSizeLimit)
err = validateInstrVolume(r.Spec.DotNet.VolumeClaimTemplate, r.Spec.DotNet.VolumeSizeLimit)
if err != nil {
return warnings, fmt.Errorf("spec.dotnet.volume and spec.dotnet.volumeSizeLimit cannot both be defined: %w", err)
return warnings, fmt.Errorf("spec.dotnet.volumeClaimTemplate and spec.dotnet.volumeSizeLimit cannot both be defined: %w", err)
}
err = validateInstrVolume(r.Spec.Go.Volume, r.Spec.Go.VolumeSizeLimit)
err = validateInstrVolume(r.Spec.Go.VolumeClaimTemplate, r.Spec.Go.VolumeSizeLimit)
if err != nil {
return warnings, fmt.Errorf("spec.go.volume and spec.go.volumeSizeLimit cannot both be defined: %w", err)
return warnings, fmt.Errorf("spec.go.volumeClaimTemplate and spec.go.volumeSizeLimit cannot both be defined: %w", err)
}
err = validateInstrVolume(r.Spec.Java.Volume, r.Spec.Java.VolumeSizeLimit)
err = validateInstrVolume(r.Spec.Java.VolumeClaimTemplate, r.Spec.Java.VolumeSizeLimit)
if err != nil {
return warnings, fmt.Errorf("spec.java.volume and spec.java.volumeSizeLimit cannot both be defined: %w", err)
return warnings, fmt.Errorf("spec.java.volumeClaimTemplate and spec.java.volumeSizeLimit cannot both be defined: %w", err)
}
err = validateInstrVolume(r.Spec.Nginx.Volume, r.Spec.Nginx.VolumeSizeLimit)
err = validateInstrVolume(r.Spec.Nginx.VolumeClaimTemplate, r.Spec.Nginx.VolumeSizeLimit)
if err != nil {
return warnings, fmt.Errorf("spec.nginx.volume and spec.nginx.volumeSizeLimit cannot both be defined: %w", err)
return warnings, fmt.Errorf("spec.nginx.volumeClaimTemplate and spec.nginx.volumeSizeLimit cannot both be defined: %w", err)
}
err = validateInstrVolume(r.Spec.NodeJS.Volume, r.Spec.NodeJS.VolumeSizeLimit)
err = validateInstrVolume(r.Spec.NodeJS.VolumeClaimTemplate, r.Spec.NodeJS.VolumeSizeLimit)
if err != nil {
return warnings, fmt.Errorf("spec.nodejs.volume and spec.nodejs.volumeSizeLimit cannot both be defined: %w", err)
return warnings, fmt.Errorf("spec.nodejs.volumeClaimTemplate and spec.nodejs.volumeSizeLimit cannot both be defined: %w", err)
}
err = validateInstrVolume(r.Spec.Python.Volume, r.Spec.Python.VolumeSizeLimit)
err = validateInstrVolume(r.Spec.Python.VolumeClaimTemplate, r.Spec.Python.VolumeSizeLimit)
if err != nil {
return warnings, fmt.Errorf("spec.python.volume and spec.python.volumeSizeLimit cannot both be defined: %w", err)
return warnings, fmt.Errorf("spec.python.volumeClaimTemplate and spec.python.volumeSizeLimit cannot both be defined: %w", err)
}

return warnings, nil
Expand Down Expand Up @@ -302,8 +302,8 @@ func validateJaegerRemoteSamplerArgument(argument string) error {
return nil
}

func validateInstrVolume(volume corev1.Volume, volumeSizeLimit *resource.Quantity) error {
if !reflect.ValueOf(volume).IsZero() && volumeSizeLimit != nil {
func validateInstrVolume(volumeClaimTemplate corev1.PersistentVolumeClaimTemplate, volumeSizeLimit *resource.Quantity) error {
if !reflect.ValueOf(volumeClaimTemplate).IsZero() && volumeSizeLimit != nil {
return fmt.Errorf("unable to resolve volume size")
}
return nil
Expand Down
2 changes: 1 addition & 1 deletion pkg/instrumentation/apachehttpd.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ const (

func injectApacheHttpdagent(_ logr.Logger, apacheSpec v1alpha1.ApacheHttpd, pod corev1.Pod, index int, otlpEndpoint string, resourceMap map[string]string) corev1.Pod {

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

// caller checks if there is at least one container
container := &pod.Spec.Containers[index]
Expand Down
2 changes: 1 addition & 1 deletion pkg/instrumentation/dotnet.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ const (

func injectDotNetSDK(dotNetSpec v1alpha1.DotNet, pod corev1.Pod, index int, runtime string) (corev1.Pod, error) {

volume := instrVolume(dotNetSpec.Volume, dotnetVolumeName, dotNetSpec.VolumeSizeLimit)
volume := instrVolume(dotNetSpec.VolumeClaimTemplate, dotnetVolumeName, dotNetSpec.VolumeSizeLimit)

// caller checks if there is at least one container.
container := &pod.Spec.Containers[index]
Expand Down
13 changes: 10 additions & 3 deletions pkg/instrumentation/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,9 +125,16 @@ func isInstrWithoutContainers(inst instrumentationWithContainers) int {
}

// Return volume if defined, otherwise return emptyDir with given name and size limit.
func instrVolume(volume corev1.Volume, name string, quantity *resource.Quantity) corev1.Volume {
if !reflect.ValueOf(volume).IsZero() {
return volume
func instrVolume(volumeClaimTemplate corev1.PersistentVolumeClaimTemplate, name string, quantity *resource.Quantity) corev1.Volume {
if !reflect.ValueOf(volumeClaimTemplate).IsZero() {
return corev1.Volume{
Name: name,
VolumeSource: corev1.VolumeSource{
Ephemeral: &corev1.EphemeralVolumeSource{
VolumeClaimTemplate: &volumeClaimTemplate,
},
},
}
}

return corev1.Volume{
Expand Down
50 changes: 27 additions & 23 deletions pkg/instrumentation/helper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -192,33 +192,35 @@ func TestDuplicatedContainers(t *testing.T) {
func TestInstrVolume(t *testing.T) {
tests := []struct {
name string
volume corev1.Volume
volume corev1.PersistentVolumeClaimTemplate
volumeName string
quantity *resource.Quantity
expected corev1.Volume
}{
{
name: "With volume",
volume: corev1.Volume{
Name: "vol1",
VolumeSource: corev1.VolumeSource{
EmptyDir: &corev1.EmptyDirVolumeSource{
SizeLimit: &resource.Quantity{},
},
}},
volume: corev1.PersistentVolumeClaimTemplate{
Spec: corev1.PersistentVolumeClaimSpec{
AccessModes: []corev1.PersistentVolumeAccessMode{corev1.ReadWriteOnce},
},
},
volumeName: "default-vol",
quantity: nil,
expected: corev1.Volume{
Name: "vol1",
Name: "default-vol",
VolumeSource: corev1.VolumeSource{
EmptyDir: &corev1.EmptyDirVolumeSource{
SizeLimit: &resource.Quantity{},
Ephemeral: &corev1.EphemeralVolumeSource{
VolumeClaimTemplate: &corev1.PersistentVolumeClaimTemplate{
Spec: corev1.PersistentVolumeClaimSpec{
AccessModes: []corev1.PersistentVolumeAccessMode{corev1.ReadWriteOnce},
},
},
},
}},
},
{
name: "With volume size limit",
volume: corev1.Volume{},
volume: corev1.PersistentVolumeClaimTemplate{},
volumeName: "default-vol",
quantity: &defaultVolumeLimitSize,
expected: corev1.Volume{
Expand All @@ -231,7 +233,7 @@ func TestInstrVolume(t *testing.T) {
},
{
name: "No volume or size limit",
volume: corev1.Volume{},
volume: corev1.PersistentVolumeClaimTemplate{},
volumeName: "default-vol",
quantity: nil,
expected: corev1.Volume{
Expand All @@ -244,20 +246,22 @@ func TestInstrVolume(t *testing.T) {
},
{
name: "With volume and size limit",
volume: corev1.Volume{
Name: "vol1",
VolumeSource: corev1.VolumeSource{
EmptyDir: &corev1.EmptyDirVolumeSource{
SizeLimit: &resource.Quantity{},
},
}},
volume: corev1.PersistentVolumeClaimTemplate{
Spec: corev1.PersistentVolumeClaimSpec{
AccessModes: []corev1.PersistentVolumeAccessMode{corev1.ReadWriteOnce},
},
},
volumeName: "default-vol",
quantity: &defaultVolumeLimitSize,
expected: corev1.Volume{
Name: "vol1",
Name: "default-vol",
VolumeSource: corev1.VolumeSource{
EmptyDir: &corev1.EmptyDirVolumeSource{
SizeLimit: &resource.Quantity{},
Ephemeral: &corev1.EphemeralVolumeSource{
VolumeClaimTemplate: &corev1.PersistentVolumeClaimTemplate{
Spec: corev1.PersistentVolumeClaimSpec{
AccessModes: []corev1.PersistentVolumeAccessMode{corev1.ReadWriteOnce},
},
},
},
}},
},
Expand Down
2 changes: 1 addition & 1 deletion pkg/instrumentation/javaagent.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ const (
)

func injectJavaagent(javaSpec v1alpha1.Java, pod corev1.Pod, index int) (corev1.Pod, error) {
volume := instrVolume(javaSpec.Volume, javaVolumeName, javaSpec.VolumeSizeLimit)
volume := instrVolume(javaSpec.VolumeClaimTemplate, javaVolumeName, javaSpec.VolumeSizeLimit)

// caller checks if there is at least one container.
container := &pod.Spec.Containers[index]
Expand Down
2 changes: 1 addition & 1 deletion pkg/instrumentation/nodejs.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ const (
)

func injectNodeJSSDK(nodeJSSpec v1alpha1.NodeJS, pod corev1.Pod, index int) (corev1.Pod, error) {
volume := instrVolume(nodeJSSpec.Volume, nodejsVolumeName, nodeJSSpec.VolumeSizeLimit)
volume := instrVolume(nodeJSSpec.VolumeClaimTemplate, nodejsVolumeName, nodeJSSpec.VolumeSizeLimit)

// caller checks if there is at least one container.
container := &pod.Spec.Containers[index]
Expand Down
2 changes: 1 addition & 1 deletion pkg/instrumentation/python.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ const (
)

func injectPythonSDK(pythonSpec v1alpha1.Python, pod corev1.Pod, index int) (corev1.Pod, error) {
volume := instrVolume(pythonSpec.Volume, pythonVolumeName, pythonSpec.VolumeSizeLimit)
volume := instrVolume(pythonSpec.VolumeClaimTemplate, pythonVolumeName, pythonSpec.VolumeSizeLimit)

// caller checks if there is at least one container.
container := &pod.Spec.Containers[index]
Expand Down

0 comments on commit 1b029b9

Please sign in to comment.