Skip to content

Commit

Permalink
fix: selecting from all ns
Browse files Browse the repository at this point in the history
Signed-off-by: Bence Csati <[email protected]>
  • Loading branch information
csatib02 committed Dec 11, 2024
1 parent 0018b84 commit b20b81a
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 14 deletions.
13 changes: 8 additions & 5 deletions api/telemetry/v1alpha1/tenant_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 6 additions & 2 deletions config/crd/bases/telemetry.kube-logging.dev_tenants.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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.
Expand Down
11 changes: 7 additions & 4 deletions internal/controller/telemetry/otel_conf_gen/otel_conf_gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -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])
}
}
Expand Down Expand Up @@ -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()
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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"}
}

Expand Down

0 comments on commit b20b81a

Please sign in to comment.