Skip to content

Commit

Permalink
Allow resource configuration on collector spec (open-telemetry#248)
Browse files Browse the repository at this point in the history
  • Loading branch information
jnodorp-jaconi authored and hero committed Dec 12, 2021
1 parent a0ce584 commit a832b6d
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 0 deletions.
5 changes: 5 additions & 0 deletions api/v1alpha1/opentelemetrycollector_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
1 change: 1 addition & 0 deletions api/v1alpha1/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

26 changes: 26 additions & 0 deletions config/crd/bases/opentelemetry.io_opentelemetrycollectors.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
1 change: 1 addition & 0 deletions pkg/collector/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
}
43 changes: 43 additions & 0 deletions pkg/collector/container_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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)
}

0 comments on commit a832b6d

Please sign in to comment.