Skip to content

Commit

Permalink
feat: make otel commonfields available to users
Browse files Browse the repository at this point in the history
Signed-off-by: Bence Csati <[email protected]>
  • Loading branch information
csatib02 committed Dec 6, 2024
1 parent 0afa5e5 commit 3a3e8c3
Show file tree
Hide file tree
Showing 8 changed files with 13,766 additions and 15,667 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ Dockerfile.cross

# Output of the go coverage tool, specifically when used with LiteIDE
*.out
coverage.html

# Kubernetes Generated files - skip generated files, except for vendored files

Expand Down
16 changes: 9 additions & 7 deletions api/telemetry/v1alpha1/collector_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ package v1alpha1
import (
"time"

"github.com/cisco-open/operator-tools/pkg/typeoverride"
otelv1beta1 "github.com/open-telemetry/opentelemetry-operator/apis/v1beta1"
"k8s.io/apimachinery/pkg/api/resource"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
Expand Down Expand Up @@ -55,8 +55,8 @@ type CollectorSpec struct {
// Enables debug logging for the collector
Debug bool `json:"debug,omitempty"`
// Setting memory limits for the Collector
MemoryLimiter *MemoryLimiter `json:"memoryLimiter,omitempty"`
DaemonSetOverrides *typeoverride.DaemonSet `json:"daemonSet,omitempty"`
MemoryLimiter *MemoryLimiter `json:"memoryLimiter,omitempty"`
OtelCommonFields *otelv1beta1.OpenTelemetryCommonFields `json:"otelCommonFields,omitempty"`
}

func (c *CollectorSpec) SetDefaults() {
Expand All @@ -67,14 +67,16 @@ func (c *CollectorSpec) SetDefaults() {
MemorySpikeLimitMiB: 25,
}
}
if c.OtelCommonFields == nil {
c.OtelCommonFields = &otelv1beta1.OpenTelemetryCommonFields{}
}
}

func (c CollectorSpec) GetMemoryLimit() *resource.Quantity {
if c.DaemonSetOverrides != nil && len(c.DaemonSetOverrides.Spec.Template.Spec.Containers) > 0 {
if memoryLimit := c.DaemonSetOverrides.Spec.Template.Spec.Containers[0].Resources.Limits.Memory(); !memoryLimit.IsZero() {
return memoryLimit
}
if c.OtelCommonFields.Resources.Requests != nil && c.OtelCommonFields.Resources.Limits != nil {
return c.OtelCommonFields.Resources.Limits.Memory()
}

return nil
}

Expand Down
8 changes: 4 additions & 4 deletions api/telemetry/v1alpha1/zz_generated.deepcopy.go

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

14,568 changes: 6,761 additions & 7,807 deletions charts/telemetry-controller/crds/telemetry.kube-logging.dev_collectors.yaml

Large diffs are not rendered by default.

14,568 changes: 6,761 additions & 7,807 deletions config/crd/bases/telemetry.kube-logging.dev_collectors.yaml

Large diffs are not rendered by default.

4 changes: 3 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ require (
github.com/cisco-open/operator-tools v0.37.0
github.com/google/go-cmp v0.6.0
github.com/hashicorp/go-multierror v1.1.1
github.com/imdario/mergo v0.3.16
github.com/mitchellh/mapstructure v1.5.0
github.com/onsi/ginkgo/v2 v2.22.0
github.com/onsi/gomega v1.36.0
Expand Down Expand Up @@ -67,7 +68,6 @@ require (
github.com/hashicorp/errwrap v1.1.0 // indirect
github.com/hashicorp/go-version v1.7.0 // indirect
github.com/iancoleman/orderedmap v0.3.0 // indirect
github.com/imdario/mergo v0.3.16 // indirect
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/josharian/intern v1.0.0 // indirect
github.com/json-iterator/go v1.1.12 // indirect
Expand Down Expand Up @@ -176,3 +176,5 @@ require (
sigs.k8s.io/json v0.0.0-20221116044647-bc3834ca7abd // indirect
sigs.k8s.io/structured-merge-diff/v4 v4.4.1 // indirect
)

replace github.com/imdario/mergo => github.com/imdario/mergo v0.3.16
108 changes: 67 additions & 41 deletions internal/controller/telemetry/collector_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (

"emperror.dev/errors"
"github.com/cisco-open/operator-tools/pkg/reconciler"
"github.com/imdario/mergo"
otelv1beta1 "github.com/open-telemetry/opentelemetry-operator/apis/v1beta1"
corev1 "k8s.io/api/core/v1"
rbacv1 "k8s.io/api/rbac/v1"
Expand Down Expand Up @@ -229,7 +230,10 @@ func (r *CollectorReconciler) Reconcile(ctx context.Context, req ctrl.Request) (
return ctrl.Result{}, fmt.Errorf("%+v", err)
}

otelCollector, state := r.otelCollector(collector, otelConfig, additionalArgs, saName.Name)
otelCollector, state, err := r.otelCollector(collector, otelConfig, additionalArgs, saName.Name)
if err != nil {
return ctrl.Result{}, err
}

if err := ctrl.SetControllerReference(collector, otelCollector, r.Scheme); err != nil {
return ctrl.Result{}, err
Expand Down Expand Up @@ -380,54 +384,23 @@ func (r *CollectorReconciler) SetupWithManager(mgr ctrl.Manager) error {
Complete(r)
}

func (r *CollectorReconciler) otelCollector(collector *v1alpha1.Collector, otelConfig otelv1beta1.Config, additionalArgs map[string]string, saName string) (*otelv1beta1.OpenTelemetryCollector, reconciler.DesiredState) {
func (r *CollectorReconciler) otelCollector(collector *v1alpha1.Collector, otelConfig otelv1beta1.Config, additionalArgs map[string]string, saName string) (*otelv1beta1.OpenTelemetryCollector, reconciler.DesiredState, error) {
otelCollector := otelv1beta1.OpenTelemetryCollector{
TypeMeta: metav1.TypeMeta{},
ObjectMeta: metav1.ObjectMeta{
Name: fmt.Sprintf("otelcollector-%s", collector.Name),
Namespace: collector.Spec.ControlNamespace,
},
Spec: otelv1beta1.OpenTelemetryCollectorSpec{
UpgradeStrategy: "none",
Config: otelConfig,
Mode: otelv1beta1.ModeDaemonSet,
OpenTelemetryCommonFields: otelv1beta1.OpenTelemetryCommonFields{
Image: axoflowOtelCollectorImageRef,
Args: additionalArgs,
ServiceAccount: saName,
VolumeMounts: []corev1.VolumeMount{
{
Name: "varlog",
ReadOnly: true,
MountPath: "/var/log",
},
{
Name: "varlibdockercontainers",
ReadOnly: true,
MountPath: "/var/lib/docker/containers",
},
},
Volumes: []corev1.Volume{
{
Name: "varlog",
VolumeSource: corev1.VolumeSource{
HostPath: &corev1.HostPathVolumeSource{
Path: "/var/log",
},
},
},
{
Name: "varlibdockercontainers",
VolumeSource: corev1.VolumeSource{
HostPath: &corev1.HostPathVolumeSource{
Path: "/var/lib/docker/containers",
},
},
},
},
},
UpgradeStrategy: "none",
Config: otelConfig,
Mode: otelv1beta1.ModeDaemonSet,
OpenTelemetryCommonFields: *collector.Spec.OtelCommonFields,
},
}
if err := setOtelCommonFieldsDefaults(&otelCollector.Spec.OpenTelemetryCommonFields, additionalArgs, saName); err != nil {
return &otelCollector, nil, err
}

if memoryLimit := collector.Spec.GetMemoryLimit(); memoryLimit != nil {
// Calculate 80% of the specified memory limit for GOMEMLIMIT
Expand All @@ -446,7 +419,7 @@ func (r *CollectorReconciler) otelCollector(collector *v1alpha1.Collector, otelC
return nil
})

return &otelCollector, beforeUpdateHook
return &otelCollector, beforeUpdateHook, nil
}

func (r *CollectorReconciler) reconcileRBAC(ctx context.Context, collector *v1alpha1.Collector) (v1alpha1.NamespacedName, error) {
Expand Down Expand Up @@ -585,3 +558,56 @@ func normalizeStringSlice(inputList []string) []string {

return uniqueList
}

func setOtelCommonFieldsDefaults(otelCommonFields *otelv1beta1.OpenTelemetryCommonFields, additionalArgs map[string]string, saName string) error {
if otelCommonFields == nil {
otelCommonFields = &otelv1beta1.OpenTelemetryCommonFields{}
}

otelCommonFields.Image = axoflowOtelCollectorImageRef
otelCommonFields.ServiceAccount = saName

if err := mergo.Merge(&otelCommonFields.Args, additionalArgs, mergo.WithOverride); err != nil {
return err
}

volumeMounts := []corev1.VolumeMount{
{
Name: "varlog",
ReadOnly: true,
MountPath: "/var/log",
},
{
Name: "varlibdockercontainers",
ReadOnly: true,
MountPath: "/var/lib/docker/containers",
},
}
if err := mergo.Merge(&otelCommonFields.VolumeMounts, volumeMounts, mergo.WithOverride); err != nil {
return err
}

volumes := []corev1.Volume{
{
Name: "varlog",
VolumeSource: corev1.VolumeSource{
HostPath: &corev1.HostPathVolumeSource{
Path: "/var/log",
},
},
},
{
Name: "varlibdockercontainers",
VolumeSource: corev1.VolumeSource{
HostPath: &corev1.HostPathVolumeSource{
Path: "/var/lib/docker/containers",
},
},
},
}
if err := mergo.Merge(&otelCommonFields.Volumes, volumes, mergo.WithOverride); err != nil {
return err
}

return nil
}
Loading

0 comments on commit 3a3e8c3

Please sign in to comment.