Skip to content

Add namespace k8s tagger #3384

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
34 changes: 21 additions & 13 deletions processor/k8sprocessor/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,14 @@ import (

// fakeClient is used as a replacement for WatchClient in test cases.
type fakeClient struct {
Pods map[kube.PodIdentifier]*kube.Pod
Rules kube.ExtractionRules
Filters kube.Filters
Associations []kube.Association
Informer cache.SharedInformer
StopCh chan struct{}
Pods map[kube.PodIdentifier]*kube.Pod
Rules kube.ExtractionRules
Filters kube.Filters
Associations []kube.Association
Informer cache.SharedInformer
NamespaceInformer cache.SharedInformer
Namespaces map[string]*kube.Namespace
StopCh chan struct{}
}

func selectors() (labels.Selector, fields.Selector) {
Expand All @@ -41,17 +43,18 @@ func selectors() (labels.Selector, fields.Selector) {
}

// newFakeClient instantiates a new FakeClient object and satisfies the ClientProvider type
func newFakeClient(_ *zap.Logger, apiCfg k8sconfig.APIConfig, rules kube.ExtractionRules, filters kube.Filters, associations []kube.Association, _ kube.APIClientsetProvider, _ kube.InformerProvider) (kube.Client, error) {
func newFakeClient(_ *zap.Logger, apiCfg k8sconfig.APIConfig, rules kube.ExtractionRules, filters kube.Filters, associations []kube.Association, _ kube.APIClientsetProvider, _ kube.InformerProvider, _ kube.InformerProviderNamespace) (kube.Client, error) {
cs := fake.NewSimpleClientset()

ls, fs := selectors()
return &fakeClient{
Pods: map[kube.PodIdentifier]*kube.Pod{},
Rules: rules,
Filters: filters,
Associations: associations,
Informer: kube.NewFakeInformer(cs, "", ls, fs),
StopCh: make(chan struct{}),
Pods: map[kube.PodIdentifier]*kube.Pod{},
Rules: rules,
Filters: filters,
Associations: associations,
Informer: kube.NewFakeInformer(cs, "", ls, fs),
NamespaceInformer: kube.NewFakeInformer(cs, "", ls, fs),
StopCh: make(chan struct{}),
}, nil
}

Expand All @@ -62,6 +65,11 @@ func (f *fakeClient) GetPod(identifier kube.PodIdentifier) (*kube.Pod, bool) {
return p, ok
}

func (f *fakeClient) GetNamespace(namespace string) (*kube.Namespace, bool) {
ns, ok := f.Namespaces[namespace]
return ns, ok
}

// Start is a noop for FakeClient.
func (f *fakeClient) Start() {
if f.Informer != nil {
Expand Down
10 changes: 7 additions & 3 deletions processor/k8sprocessor/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,13 @@ func (cfg *Config) Validate() error {
// ExtractConfig section allows specifying extraction rules to extract
// data from k8s pod specs.
type ExtractConfig struct {
// Metadata allows to extract pod metadata from a list of metadata fields.
// Metadata allows to extract pod/namespace metadata from a list of metadata fields.
// The field accepts a list of strings.
//
// Metadata fields supported right now are,
// k8s.namespace.name, k8s.pod.name, k8s.pod.uid, k8s.deployment.name, k8s.cluster.name,
// k8s.node.name and k8s.pod.start_time
// k8s.pod.name, k8s.pod.uid, k8s.deployment.name, k8s.cluster.name,
// k8s.node.name, k8s.namespace.name and k8s.pod.start_time
//
// Specifying anything other than these values will result in an error.
// By default all of the fields are extracted and added to spans and metrics.
Metadata []string `mapstructure:"metadata"`
Expand Down Expand Up @@ -113,6 +114,9 @@ type FieldExtractConfig struct {
TagName string `mapstructure:"tag_name"`
Key string `mapstructure:"key"`
Regex string `mapstructure:"regex"`
// From represents the source of the labels/annotations.
// Allowed values are "pod" and "namespace".
From string `mapstructure:"from"`
}

// FilterConfig section allows specifying filters to filter
Expand Down
8 changes: 4 additions & 4 deletions processor/k8sprocessor/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,12 +59,12 @@ func TestLoadConfig(t *testing.T) {
Extract: ExtractConfig{
Metadata: []string{"k8s.pod.name", "k8s.pod.uid", "k8s.deployment.name", "k8s.cluster.name", "k8s.namespace.name", "k8s.node.name", "k8s.pod.start_time"},
Annotations: []FieldExtractConfig{
{TagName: "a1", Key: "annotation-one"},
{TagName: "a2", Key: "annotation-two", Regex: "field=(?P<value>.+)"},
{TagName: "a1", Key: "annotation-one", From: "pod"},
{TagName: "a2", Key: "annotation-two", Regex: "field=(?P<value>.+)", From: "pod"},
},
Labels: []FieldExtractConfig{
{TagName: "l1", Key: "label1"},
{TagName: "l2", Key: "label2", Regex: "field=(?P<value>.+)"},
{TagName: "l1", Key: "label1", From: "pod"},
{TagName: "l2", Key: "label2", Regex: "field=(?P<value>.+)", From: "pod"},
},
},
Filter: FilterConfig{
Expand Down
Loading