Skip to content

Commit

Permalink
Feat: support render ${_k8s.clusterlogconfig} in fields (#569)
Browse files Browse the repository at this point in the history
  • Loading branch information
ethfoo authored Jun 16, 2023
1 parent 5521209 commit 8f07f0f
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 18 deletions.
22 changes: 11 additions & 11 deletions pkg/discovery/kubernetes/controller/selectpodhandler.go
Original file line number Diff line number Diff line change
Expand Up @@ -302,7 +302,7 @@ func (c *Controller) getConfigFromContainerAndLogConfig(lgc *logconfigv1beta1.Lo
return nil, errors.WithMessagef(err, "unpack logConfig %s sources failed", lgc.Namespace)
}

filesources, err := c.updateSources(sourceConfList, pod, logConf.Name)
filesources, err := c.updateSources(sourceConfList, pod, logConf)
if err != nil {
return nil, err
}
Expand All @@ -313,10 +313,10 @@ func (c *Controller) getConfigFromContainerAndLogConfig(lgc *logconfigv1beta1.Lo
return pipecfg, nil
}

func (c *Controller) updateSources(sourceConfList []*source.Config, pod *corev1.Pod, logConfigName string) ([]*source.Config, error) {
func (c *Controller) updateSources(sourceConfList []*source.Config, pod *corev1.Pod, lgc *logconfigv1beta1.LogConfig) ([]*source.Config, error) {
filesources := make([]*source.Config, 0)
for _, sourceConf := range sourceConfList {
filesrc, err := c.makeConfigPerSource(sourceConf, pod, logConfigName)
filesrc, err := c.makeConfigPerSource(sourceConf, pod, lgc)
if err != nil {
return nil, err
}
Expand All @@ -325,7 +325,7 @@ func (c *Controller) updateSources(sourceConfList []*source.Config, pod *corev1.
return filesources, nil
}

func (c *Controller) makeConfigPerSource(s *source.Config, pod *corev1.Pod, logconfigName string) ([]*source.Config, error) {
func (c *Controller) makeConfigPerSource(s *source.Config, pod *corev1.Pod, lgc *logconfigv1beta1.LogConfig) ([]*source.Config, error) {
extra, err := GetKubeExtraFromFileSource(s)
if err != nil {
return nil, err
Expand Down Expand Up @@ -367,7 +367,7 @@ func (c *Controller) makeConfigPerSource(s *source.Config, pod *corev1.Pod, logc
filesrc.Name = helper.GenTypePodSourceName(pod.Name, status.Name, filesrc.Name)

// inject default pod metadata
if err := c.injectTypePodFields(c.config.DynamicContainerLog, filesrc, extra, pod, logconfigName, status.Name); err != nil {
if err := c.injectTypePodFields(c.config.DynamicContainerLog, filesrc, extra, pod, lgc, status.Name); err != nil {
return nil, err
}

Expand Down Expand Up @@ -446,7 +446,7 @@ func (c *Controller) getPathsInNode(containerPaths []string, pod *corev1.Pod, co
return newPaths, err
}

func (c *Controller) injectTypePodFields(dynamicContainerLogs bool, src *source.Config, extra *KubeFileSourceExtra, pod *corev1.Pod, lgcName string, containerName string) error {
func (c *Controller) injectTypePodFields(dynamicContainerLogs bool, src *source.Config, extra *KubeFileSourceExtra, pod *corev1.Pod, lgc *logconfigv1beta1.LogConfig, containerName string) error {
if src.Fields == nil {
src.Fields = make(map[string]interface{})
}
Expand Down Expand Up @@ -474,11 +474,11 @@ func (c *Controller) injectTypePodFields(dynamicContainerLogs bool, src *source.
k8sFields[m.ContainerName] = containerName
}
if m.LogConfig != "" {
k8sFields[m.LogConfig] = lgcName
k8sFields[m.LogConfig] = lgc.Name
}

if len(c.extraTypePodFieldsPattern) > 0 {
for k, v := range renderTypePodFieldsPattern(c.extraTypePodFieldsPattern, pod, containerName, lgcName) {
for k, v := range renderTypePodFieldsPattern(c.extraTypePodFieldsPattern, pod, containerName, lgc) {
k8sFields[k] = v
}
}
Expand All @@ -489,7 +489,7 @@ func (c *Controller) injectTypePodFields(dynamicContainerLogs bool, src *source.
return err
}
podPattern := podFields.initPattern()
for k, v := range renderTypePodFieldsPattern(podPattern, pod, containerName, lgcName) {
for k, v := range renderTypePodFieldsPattern(podPattern, pod, containerName, lgc) {
k8sFields[k] = v
}
}
Expand Down Expand Up @@ -707,10 +707,10 @@ func toPipelineInterceptorWithPodInject(dynamicContainerLog bool, interceptorRaw
return icpConfList, nil
}

func renderTypePodFieldsPattern(pm map[string]*pattern.Pattern, pod *corev1.Pod, containerName string, logConfig string) map[string]interface{} {
func renderTypePodFieldsPattern(pm map[string]*pattern.Pattern, pod *corev1.Pod, containerName string, lgc *logconfigv1beta1.LogConfig) map[string]interface{} {
fields := make(map[string]interface{}, len(pm))
for k, p := range pm {
res, err := p.WithK8sPod(pattern.NewTypePodFieldsData(pod, containerName, logConfig)).Render()
res, err := p.WithK8sPod(pattern.NewTypePodFieldsData(pod, containerName, lgc)).Render()
if err != nil {
log.Warn("add extra k8s fields %s failed: %v", k, err)
continue
Expand Down
29 changes: 22 additions & 7 deletions pkg/util/pattern/k8meta.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package pattern

import (
"github.com/loggie-io/loggie/pkg/discovery/kubernetes/apis/loggie/v1beta1"
logconfigv1beta1 "github.com/loggie-io/loggie/pkg/discovery/kubernetes/apis/loggie/v1beta1"
"github.com/loggie-io/loggie/pkg/discovery/kubernetes/helper"
corev1 "k8s.io/api/core/v1"
"strings"
Expand All @@ -30,16 +31,27 @@ const (
)

type TypePodFieldsData struct {
Pod *corev1.Pod
ContainerName string
LogConfig string
Pod *corev1.Pod
ContainerName string
LogConfig string
ClusterLogConfig string
}

func NewTypePodFieldsData(pod *corev1.Pod, containerName string, logConfig string) *TypePodFieldsData {
func NewTypePodFieldsData(pod *corev1.Pod, containerName string, lgc *logconfigv1beta1.LogConfig) *TypePodFieldsData {
var logconfig, clusterlogconfig string
if lgc != nil {
if lgc.Namespace != "" {
logconfig = lgc.Name
} else {
clusterlogconfig = lgc.Name
}
}

return &TypePodFieldsData{
Pod: pod,
ContainerName: containerName,
LogConfig: logConfig,
Pod: pod,
ContainerName: containerName,
LogConfig: logconfig,
ClusterLogConfig: clusterlogconfig,
}
}

Expand Down Expand Up @@ -132,6 +144,9 @@ func renderTypePod(data *TypePodFieldsData, field string) string {
case "logconfig":
return data.LogConfig

case "clusterlogconfig":
return data.ClusterLogConfig

case "workload.kind":
return helper.GetWorkload(data.Pod).Kind

Expand Down

0 comments on commit 8f07f0f

Please sign in to comment.