diff --git a/apis/logging/v1beta1/collector_config_types.go b/apis/logging/v1beta1/collector_config_types.go index b736e0f79c..8c79661708 100644 --- a/apis/logging/v1beta1/collector_config_types.go +++ b/apis/logging/v1beta1/collector_config_types.go @@ -50,8 +50,9 @@ type RKE2Spec struct { } type KubeAuditLogsSpec struct { - Enabled bool `json:"enabled,omitempty"` - LogPath string `json:"logPath,omitempty"` + Enabled bool `json:"enabled,omitempty"` + AuditFilename string `json:"auditFilename,omitempty"` + PathPrefix string `json:"pathPrefix,omitempty"` } // CollectorConfigSpec defines the desired state of CollectorConfig diff --git a/config/crd/bases/logging.opni.io_collectorconfigs.yaml b/config/crd/bases/logging.opni.io_collectorconfigs.yaml index 016252422a..0773be6590 100644 --- a/config/crd/bases/logging.opni.io_collectorconfigs.yaml +++ b/config/crd/bases/logging.opni.io_collectorconfigs.yaml @@ -41,9 +41,11 @@ spec: type: object kubeAuditLogs: properties: + auditFilename: + type: string enabled: type: boolean - logPath: + pathPrefix: type: string type: object provider: diff --git a/pkg/resources/collector/logging.go b/pkg/resources/collector/logging.go index 51f9356d4e..2ddcf11d4e 100644 --- a/pkg/resources/collector/logging.go +++ b/pkg/resources/collector/logging.go @@ -2,9 +2,7 @@ package collector import ( "bytes" - "fmt" "path/filepath" - "strings" opniloggingv1beta1 "github.com/rancher/opni/apis/logging/v1beta1" corev1 "k8s.io/api/core/v1" @@ -50,13 +48,16 @@ func (r *Reconciler) generateKubeAuditLogsReceiver(config *opniloggingv1beta1.Co if config.Spec.KubeAuditLogs != nil && config.Spec.KubeAuditLogs.Enabled { filelogDir := "/var/log/kube-audit" + if config.Spec.KubeAuditLogs.PathPrefix != "" { + filelogDir = config.Spec.KubeAuditLogs.PathPrefix + } - if config.Spec.KubeAuditLogs.LogPath != "" { - filelogDir = config.Spec.KubeAuditLogs.LogPath + auditLogFilename := "audit.log" + if config.Spec.KubeAuditLogs.AuditFilename != "" { + auditLogFilename = config.Spec.KubeAuditLogs.AuditFilename } - fileGlobPatterns := generateFileGlobPatterns(filelogDir, kubeAuditLogsFileTypes) - err := templateKubeAuditLogs.Execute(&receiver, fileGlobPatterns) + err := templateKubeAuditLogs.Execute(&receiver, filepath.Join(filelogDir, auditLogFilename)) if err != nil { return "", nil, err } @@ -110,8 +111,8 @@ func (r *Reconciler) hostLoggingVolumes() ( }) kubeAuditLogsDir := "/var/log/kube-audit" - if config.Spec.KubeAuditLogs != nil && config.Spec.KubeAuditLogs.LogPath != "" { - kubeAuditLogsDir = config.Spec.KubeAuditLogs.LogPath + if config.Spec.KubeAuditLogs != nil && config.Spec.KubeAuditLogs.PathPrefix != "" { + kubeAuditLogsDir = config.Spec.KubeAuditLogs.PathPrefix } retVolumeMounts = append(retVolumeMounts, corev1.VolumeMount{ @@ -222,24 +223,3 @@ func (r *Reconciler) hostLoggingVolumes() ( } return } - -// generateFileGlobPattern generates a file glob pattern based on the provided path and file type. -// If the path doesn't end with a slash, it appends one before constructing the pattern. -// -// path is the base path for the file glob pattern. fileType is the desired file types to match, -// e.g., [".log", ".json"]. -// -// It returns a single string of the format "[ /foo/*.log, /bar/*.json ]". -func generateFileGlobPatterns(path string, fileTypes []string) string { - if len(path) > 0 && path[len(path)-1] != '/' { - path += "/" - } - - var patterns []string - for _, fileType := range fileTypes { - pattern := filepath.Join(path, fmt.Sprintf("*%s", fileType)) - patterns = append(patterns, pattern) - } - - return fmt.Sprintf("[%s]", strings.Join(patterns, ",")) -} diff --git a/pkg/resources/collector/templates.go b/pkg/resources/collector/templates.go index 4c83451ef5..f24cfd4ee6 100644 --- a/pkg/resources/collector/templates.go +++ b/pkg/resources/collector/templates.go @@ -105,7 +105,7 @@ journald/k3s: templateKubeAuditLogs = template.Must(template.New("kubeauditlogsreceiver").Parse(` filelog/kubeauditlogs: - include: {{ . }} + include: [ {{ . }} ] start_at: beginning include_file_path: false include_file_name: false diff --git a/pkg/resources/collector/workloads.go b/pkg/resources/collector/workloads.go index 827df5cf55..5d9ea7c48a 100644 --- a/pkg/resources/collector/workloads.go +++ b/pkg/resources/collector/workloads.go @@ -38,11 +38,7 @@ const ( machineID = "/etc/machine-id" ) -var ( - directoryOrCreate = corev1.HostPathDirectoryOrCreate - - kubeAuditLogsFileTypes = []string{".log", ".json"} -) +var directoryOrCreate = corev1.HostPathDirectoryOrCreate func (r *Reconciler) agentConfigMapName() string { return fmt.Sprintf("%s-agent-config", r.collector.Name)