Skip to content

Commit

Permalink
fix: file backend is not a list anymore
Browse files Browse the repository at this point in the history
  • Loading branch information
adityathebe committed Mar 24, 2023
1 parent 3dceb9d commit f7af6b8
Show file tree
Hide file tree
Showing 10 changed files with 73 additions and 74 deletions.
6 changes: 3 additions & 3 deletions api/logs/logs.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ type SearchBackendConfig struct {
ElasticSearch *ElasticSearchBackendConfig `json:"elasticsearch,omitempty"`
OpenSearch *OpenSearchBackendConfig `json:"opensearch,omitempty"`
Kubernetes *KubernetesSearchBackendConfig `json:"kubernetes,omitempty"`
Files []FileSearchBackendConfig `json:"file,omitempty" yaml:"file,omitempty"`
Files *FileSearchBackendConfig `json:"file,omitempty" yaml:"file,omitempty"`
}

type SearchBackend struct {
Expand All @@ -35,7 +35,7 @@ type SearchBackend struct {
ElasticSearch *ElasticSearchBackendConfig `json:"elasticsearch,omitempty"`
OpenSearch *OpenSearchBackendConfig `json:"opensearch,omitempty"`
Kubernetes *KubernetesSearchBackendConfig `json:"kubernetes,omitempty"`
Files []FileSearchBackendConfig `json:"file,omitempty" yaml:"file,omitempty"`
File *FileSearchBackendConfig `json:"file,omitempty" yaml:"file,omitempty"`
}

type Routes []SearchRoute
Expand All @@ -62,7 +62,7 @@ type CommonBackend struct {
func (b SearchBackendConfig) ToSearchBackend() SearchBackend {
return SearchBackend{
Kubernetes: b.Kubernetes,
Files: b.Files,
File: b.Files,
ElasticSearch: b.ElasticSearch,
OpenSearch: b.OpenSearch,
}
Expand Down
6 changes: 2 additions & 4 deletions api/logs/zz_generated.deepcopy.go

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

56 changes: 27 additions & 29 deletions chart/crds/apm-hub.flanksource.com_loggingbackends.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -214,36 +214,34 @@ spec:
type: object
type: object
file:
items:
properties:
labels:
additionalProperties:
type: string
description: Labels are custom labels specified in the
configuration file for a backend that will be attached
to each log line returned by that backend.
type: object
path:
items:
type: string
type: array
routes:
items:
properties:
id_prefix:
type: string
is_additive:
type: boolean
labels:
additionalProperties:
type: string
type: object
type:
properties:
labels:
additionalProperties:
type: string
description: Labels are custom labels specified in the configuration
file for a backend that will be attached to each log line
returned by that backend.
type: object
path:
items:
type: string
type: array
routes:
items:
properties:
id_prefix:
type: string
is_additive:
type: boolean
labels:
additionalProperties:
type: string
type: object
type: array
type: object
type: array
type: object
type:
type: string
type: object
type: array
type: object
kubernetes:
properties:
kubeconfig:
Expand Down
16 changes: 6 additions & 10 deletions pkg/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,21 +76,17 @@ func AttachSearchAPIToBackend(kommonsClient *kommons.Client, backend *logs.Searc
backend.API = k8s.NewKubernetesSearchBackend(k8sclient, backend.Kubernetes)
}

if len(backend.Files) > 0 {
if backend.File != nil {
// If the paths are not absolute,
// They should be parsed with respect to the current path
for i, f := range backend.Files {
for j, p := range f.Paths {
if !filepath.IsAbs(p) {
currentPath, _ := os.Getwd()
backend.Files[i].Paths[j] = filepath.Join(currentPath, p)
}
for j, p := range backend.File.Paths {
if !filepath.IsAbs(p) {
currentPath, _ := os.Getwd()
backend.File.Paths[j] = filepath.Join(currentPath, p)
}
}

backend.API = &files.FileSearch{
FilesBackendConfig: backend.Files,
}
backend.API = files.NewFileSearchBackend(backend.File)
}

if backend.ElasticSearch != nil {
Expand Down
26 changes: 11 additions & 15 deletions pkg/files/search.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,32 +12,28 @@ import (
"github.com/flanksource/commons/logger"
)

func NewFileSearchBackend(config *logs.FileSearchBackendConfig) *FileSearch {
return &FileSearch{
config: config,
}
}

type FileSearch struct {
FilesBackendConfig []logs.FileSearchBackendConfig
config *logs.FileSearchBackendConfig
}

func (t *FileSearch) Search(q *logs.SearchParams) (r logs.SearchResults, err error) {
var res logs.SearchResults

for _, b := range t.FilesBackendConfig {
files := readFilesLines(b.Paths, collections.MergeMap(b.Labels, q.Labels))
for _, content := range files {
res.Results = append(res.Results, content...)
}
lines := readFilesLines(t.config.Paths, collections.MergeMap(t.config.Labels, q.Labels))
for _, content := range lines {
res.Results = append(res.Results, content...)
}

return res, nil
}

func (t *FileSearch) MatchRoute(q *logs.SearchParams) (match bool, isAdditive bool) {
for _, k := range t.FilesBackendConfig {
match, isAdditive := k.Routes.MatchRoute(q)
if match {
return match, isAdditive
}
}

return false, false
return t.config.CommonBackend.Routes.MatchRoute(q)
}

type logsPerFile map[string][]logs.Result
Expand Down
29 changes: 19 additions & 10 deletions samples/config-file.yaml
Original file line number Diff line number Diff line change
@@ -1,12 +1,21 @@
backends:
- file:
- labels:
name: acmehost
type: Nginx
path:
- nginx-access.log
- labels:
name: all
type: Nginx
path:
- "*.log"
routes:
- idPrefix: "nginx-"
labels:
type: "access"
labels:
name: acmehost
type: Nginx
path:
- samples/data/nginx-access.log
- file:
routes:
- idPrefix: "nginx-"
labels:
type: "error"
labels:
name: acmehost
type: Nginx
path:
- samples/data/nginx-error.log
5 changes: 5 additions & 0 deletions samples/config-kubernetes.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
backends:
- kubernetes:
routes:
- idPrefix: "cluster-main"
kubeconfig:
3 changes: 0 additions & 3 deletions samples/config.yaml

This file was deleted.

File renamed without changes.
File renamed without changes.

0 comments on commit f7af6b8

Please sign in to comment.