From 0d6710f1093d82996119748d629cfe21238f6f6e Mon Sep 17 00:00:00 2001 From: Bence Csati Date: Wed, 11 Dec 2024 13:27:56 +0100 Subject: [PATCH] fix: selecting from all ns Signed-off-by: Bence Csati --- api/telemetry/v1alpha1/tenant_types.go | 13 ++++++++----- .../crds/telemetry.kube-logging.dev_tenants.yaml | 8 ++++++-- .../bases/telemetry.kube-logging.dev_tenants.yaml | 8 ++++++-- .../telemetry/otel_conf_gen/otel_conf_gen.go | 11 +++++++---- .../components/receiver/filelog_receiver.go | 6 +++--- 5 files changed, 30 insertions(+), 16 deletions(-) diff --git a/api/telemetry/v1alpha1/tenant_types.go b/api/telemetry/v1alpha1/tenant_types.go index b95f617..66430ab 100644 --- a/api/telemetry/v1alpha1/tenant_types.go +++ b/api/telemetry/v1alpha1/tenant_types.go @@ -88,12 +88,15 @@ type TenantSpec struct { SubscriptionNamespaceSelectors []metav1.LabelSelector `json:"subscriptionNamespaceSelectors,omitempty"` // Determines the namespaces from which logs are collected by this tenant. - // If initialized with an empty list, logs from all namespaces are collected. - // If uninitialized, no logs are collected. + // Cannot be used together with SelectFromAllNamespaces. LogSourceNamespaceSelectors []metav1.LabelSelector `json:"logSourceNamespaceSelectors,omitempty"` - Transform `json:"transform,omitempty"` - RouteConfig `json:"routeConfig,omitempty"` - PersistenceConfig `json:"persistenceConfig,omitempty"` + + // Determines the namespaces from which metrics are collected by this tenant. + // Cannot be used together with LogSourceNamespaceSelectors. + SelectFromAllNamespaces bool `json:"selectFromAllNamespaces,omitempty"` + Transform `json:"transform,omitempty"` + RouteConfig `json:"routeConfig,omitempty"` + PersistenceConfig `json:"persistenceConfig,omitempty"` } // TenantStatus defines the observed state of Tenant diff --git a/charts/telemetry-controller/crds/telemetry.kube-logging.dev_tenants.yaml b/charts/telemetry-controller/crds/telemetry.kube-logging.dev_tenants.yaml index d32c3da..204b805 100644 --- a/charts/telemetry-controller/crds/telemetry.kube-logging.dev_tenants.yaml +++ b/charts/telemetry-controller/crds/telemetry.kube-logging.dev_tenants.yaml @@ -57,8 +57,7 @@ spec: logSourceNamespaceSelectors: description: |- Determines the namespaces from which logs are collected by this tenant. - If initialized with an empty list, logs from all namespaces are collected. - If uninitialized, no logs are collected. + Cannot be used together with SelectFromAllNamespaces. items: description: |- A label selector is a label query over a set of resources. The result of matchLabels and @@ -154,6 +153,11 @@ spec: May only be false when used with resource context. type: boolean type: object + selectFromAllNamespaces: + description: |- + Determines the namespaces from which metrics are collected by this tenant. + Cannot be used together with LogSourceNamespaceSelectors. + type: boolean subscriptionNamespaceSelectors: description: Determines the namespaces from which subscriptions are collected by this tenant. diff --git a/config/crd/bases/telemetry.kube-logging.dev_tenants.yaml b/config/crd/bases/telemetry.kube-logging.dev_tenants.yaml index d32c3da..204b805 100644 --- a/config/crd/bases/telemetry.kube-logging.dev_tenants.yaml +++ b/config/crd/bases/telemetry.kube-logging.dev_tenants.yaml @@ -57,8 +57,7 @@ spec: logSourceNamespaceSelectors: description: |- Determines the namespaces from which logs are collected by this tenant. - If initialized with an empty list, logs from all namespaces are collected. - If uninitialized, no logs are collected. + Cannot be used together with SelectFromAllNamespaces. items: description: |- A label selector is a label query over a set of resources. The result of matchLabels and @@ -154,6 +153,11 @@ spec: May only be false when used with resource context. type: boolean type: object + selectFromAllNamespaces: + description: |- + Determines the namespaces from which metrics are collected by this tenant. + Cannot be used together with LogSourceNamespaceSelectors. + type: boolean subscriptionNamespaceSelectors: description: Determines the namespaces from which subscriptions are collected by this tenant. diff --git a/internal/controller/telemetry/otel_conf_gen/otel_conf_gen.go b/internal/controller/telemetry/otel_conf_gen/otel_conf_gen.go index 033fe4e..de70e91 100644 --- a/internal/controller/telemetry/otel_conf_gen/otel_conf_gen.go +++ b/internal/controller/telemetry/otel_conf_gen/otel_conf_gen.go @@ -149,10 +149,7 @@ func (cfgInput *OtelColConfigInput) generateReceivers() map[string]any { return tenantName == t.Name }); tenantIdx != -1 { namespaces := cfgInput.Tenants[tenantIdx].Status.LogSourceNamespaces - - // Generate filelog receiver for the tenant if it has any logsource namespaces. - // Or Handle "all namespaces" case: selectors are initialized but empty - if len(namespaces) > 0 || (cfgInput.Tenants[tenantIdx].Spec.LogSourceNamespaceSelectors != nil && len(namespaces) == 0) { + if len(namespaces) > 0 || cfgInput.Tenants[tenantIdx].Spec.SelectFromAllNamespaces { receivers[fmt.Sprintf("filelog/%s", tenantName)] = receiver.GenerateDefaultKubernetesReceiver(namespaces, cfgInput.Tenants[tenantIdx]) } } @@ -358,6 +355,12 @@ func validateTenants(tenants *[]v1alpha1.Tenant) error { return errors.New("no tenants provided, at least one tenant must be provided") } + for _, tenant := range *tenants { + if tenant.Spec.LogSourceNamespaceSelectors != nil && tenant.Spec.SelectFromAllNamespaces { + result = multierror.Append(result, fmt.Errorf("tenant %s has both log source namespace selectors and select from all namespaces enabled", tenant.Name)) + } + } + return result.ErrorOrNil() } diff --git a/internal/controller/telemetry/pipeline/components/receiver/filelog_receiver.go b/internal/controller/telemetry/pipeline/components/receiver/filelog_receiver.go index 49b82ba..55353b3 100644 --- a/internal/controller/telemetry/pipeline/components/receiver/filelog_receiver.go +++ b/internal/controller/telemetry/pipeline/components/receiver/filelog_receiver.go @@ -103,7 +103,7 @@ func GenerateDefaultKubernetesReceiver(namespaces []string, tenant v1alpha1.Tena } k8sReceiver := map[string]any{ - "include": createIncludeList(namespaces), + "include": createIncludeList(namespaces, tenant), "exclude": []string{"/var/log/pods/*/otc-container/*.log"}, "start_at": "end", "include_file_path": true, @@ -121,9 +121,9 @@ func GenerateDefaultKubernetesReceiver(namespaces []string, tenant v1alpha1.Tena return k8sReceiver } -func createIncludeList(namespaces []string) []string { +func createIncludeList(namespaces []string, tenant v1alpha1.Tenant) []string { includeList := make([]string, 0, len(namespaces)) - if len(namespaces) == 0 { + if tenant.Spec.SelectFromAllNamespaces { return []string{"/var/log/pods/*/*/*.log"} }