diff --git a/api/v1alpha1/opentelemetrycollector_types.go b/api/v1alpha1/opentelemetrycollector_types.go index c2d728b096..604a140570 100644 --- a/api/v1alpha1/opentelemetrycollector_types.go +++ b/api/v1alpha1/opentelemetrycollector_types.go @@ -76,6 +76,11 @@ type OpenTelemetryCollectorSpec struct { // +optional // +operator-sdk:gen-csv:customresourcedefinitions.specDescriptors=true Env []v1.EnvVar `json:"env,omitempty"` + + // Resources to set on the OpenTelemetry Collector pods. + // +optional + // +operator-sdk:gen-csv:customresourcedefinitions.specDescriptors=true + Resources v1.ResourceRequirements `json:"resources,omitempty"` } // OpenTelemetryCollectorStatus defines the observed state of OpenTelemetryCollector. diff --git a/api/v1alpha1/zz_generated.deepcopy.go b/api/v1alpha1/zz_generated.deepcopy.go index a0131fcbd3..0afdc20e3d 100644 --- a/api/v1alpha1/zz_generated.deepcopy.go +++ b/api/v1alpha1/zz_generated.deepcopy.go @@ -125,6 +125,7 @@ func (in *OpenTelemetryCollectorSpec) DeepCopyInto(out *OpenTelemetryCollectorSp (*in)[i].DeepCopyInto(&(*out)[i]) } } + in.Resources.DeepCopyInto(&out.Resources) } // DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new OpenTelemetryCollectorSpec. diff --git a/config/crd/bases/opentelemetry.io_opentelemetrycollectors.yaml b/config/crd/bases/opentelemetry.io_opentelemetrycollectors.yaml index 017548c16c..5c220111cb 100644 --- a/config/crd/bases/opentelemetry.io_opentelemetrycollectors.yaml +++ b/config/crd/bases/opentelemetry.io_opentelemetrycollectors.yaml @@ -248,6 +248,32 @@ spec: OpenTelemetry Collector format: int32 type: integer + resources: + description: Resources to set on the OpenTelemetry Collector pods. + properties: + limits: + additionalProperties: + anyOf: + - type: integer + - type: string + pattern: ^(\+|-)?(([0-9]+(\.[0-9]*)?)|(\.[0-9]+))(([KMGTPE]i)|[numkMGTPE]|([eE](\+|-)?(([0-9]+(\.[0-9]*)?)|(\.[0-9]+))))?$ + x-kubernetes-int-or-string: true + description: 'Limits describes the maximum amount of compute resources + allowed. More info: https://kubernetes.io/docs/concepts/configuration/manage-compute-resources-container/' + type: object + requests: + additionalProperties: + anyOf: + - type: integer + - type: string + pattern: ^(\+|-)?(([0-9]+(\.[0-9]*)?)|(\.[0-9]+))(([KMGTPE]i)|[numkMGTPE]|([eE](\+|-)?(([0-9]+(\.[0-9]*)?)|(\.[0-9]+))))?$ + x-kubernetes-int-or-string: true + description: 'Requests describes the minimum amount of compute + resources required. If Requests is omitted for a container, + it defaults to Limits if that is explicitly specified, otherwise + to an implementation-defined value. More info: https://kubernetes.io/docs/concepts/configuration/manage-compute-resources-container/' + type: object + type: object serviceAccount: description: ServiceAccount indicates the name of an existing service account to use with this instance. diff --git a/pkg/collector/container.go b/pkg/collector/container.go index e86d01902a..8646e33430 100644 --- a/pkg/collector/container.go +++ b/pkg/collector/container.go @@ -69,5 +69,6 @@ func Container(cfg config.Config, logger logr.Logger, otelcol v1alpha1.OpenTelem VolumeMounts: volumeMounts, Args: args, Env: envVars, + Resources: otelcol.Spec.Resources, } } diff --git a/pkg/collector/container_test.go b/pkg/collector/container_test.go index cfddf06424..7315ffa29c 100644 --- a/pkg/collector/container_test.go +++ b/pkg/collector/container_test.go @@ -18,6 +18,7 @@ import ( "testing" corev1 "k8s.io/api/core/v1" + "k8s.io/apimachinery/pkg/api/resource" logf "sigs.k8s.io/controller-runtime/pkg/log" "github.com/stretchr/testify/assert" @@ -133,3 +134,45 @@ func TestContainerEmptyEnvVarsByDefault(t *testing.T) { // verify assert.Empty(t, c.Env) } + +func TestContainerResourceRequirements(t *testing.T) { + otelcol := v1alpha1.OpenTelemetryCollector{ + Spec: v1alpha1.OpenTelemetryCollectorSpec{ + Resources: corev1.ResourceRequirements{ + Limits: corev1.ResourceList{ + corev1.ResourceCPU: resource.MustParse("100m"), + corev1.ResourceMemory: resource.MustParse("128M"), + }, + Requests: corev1.ResourceList{ + corev1.ResourceCPU: resource.MustParse("200m"), + corev1.ResourceMemory: resource.MustParse("256M"), + }, + }, + }, + } + + cfg := config.New() + + // test + c := Container(cfg, logger, otelcol) + + // verify + assert.Equal(t, resource.MustParse("100m"), *c.Resources.Limits.Cpu()) + assert.Equal(t, resource.MustParse("128M"), *c.Resources.Limits.Memory()) + assert.Equal(t, resource.MustParse("200m"), *c.Resources.Requests.Cpu()) + assert.Equal(t, resource.MustParse("256M"), *c.Resources.Requests.Memory()) +} + +func TestContainerDefaultResourceRequirements(t *testing.T) { + otelcol := v1alpha1.OpenTelemetryCollector{ + Spec: v1alpha1.OpenTelemetryCollectorSpec{}, + } + + cfg := config.New() + + // test + c := Container(cfg, logger, otelcol) + + // verify + assert.Empty(t, c.Resources) +}