Skip to content

Commit

Permalink
Make kubeAuditLogsSpec follow rancher-logging pattern
Browse files Browse the repository at this point in the history
Signed-off-by: Joao Henri <joao.henri@suse.com>
  • Loading branch information
jaehnri committed Sep 18, 2023
1 parent 4665256 commit b96a3ae
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 38 deletions.
5 changes: 3 additions & 2 deletions apis/logging/v1beta1/collector_config_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 3 additions & 1 deletion config/crd/bases/logging.opni.io_collectorconfigs.yaml

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

38 changes: 9 additions & 29 deletions pkg/resources/collector/logging.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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{
Expand Down Expand Up @@ -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, ","))
}
2 changes: 1 addition & 1 deletion pkg/resources/collector/templates.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 1 addition & 5 deletions pkg/resources/collector/workloads.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit b96a3ae

Please sign in to comment.