Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 19 additions & 6 deletions prometheus-to-sd/config/common_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,19 +30,21 @@ type PodConfig interface {
IsMetricLabel(labelName string) bool

// GetPodInfo returns the information required to identify the pod.
GetPodInfo(labels []*dto.LabelPair) (containerName, podId, namespaceId, tenantUID string)
GetPodInfo(labels []*dto.LabelPair) (containerName, podId, namespaceId, tenantUID, entityType, entityName string)
}

// NewPodConfig returns a PodConfig which uses for the provided pod, namespace and container label values,
// if found, and falls back to the podId and namespaceId.
func NewPodConfig(podId, namespaceId, podIdLabel, namespaceIdLabel, containerNameLabel, tenantUIDLabel string) PodConfig {
func NewPodConfig(podId, namespaceId, podIdLabel, namespaceIdLabel, containerNameLabel, tenantUIDLabel, entityTypeLabel, entityNameLabel string) PodConfig {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks a bit off. It just doesn't really look like it belongs to pod config.

return &podConfigImpl{
podId: podId,
namespaceId: namespaceId,
podIdLabel: podIdLabel,
namespaceIdLabel: namespaceIdLabel,
containerNameLabel: containerNameLabel,
tenantUIDLabel: tenantUIDLabel,
entityTypeLabel: entityTypeLabel,
entityNameLabel: entityNameLabel,
}
}

Expand All @@ -53,14 +55,21 @@ type podConfigImpl struct {
namespaceIdLabel string
containerNameLabel string
tenantUIDLabel string
entityTypeLabel string
entityNameLabel string
}

func (p *podConfigImpl) IsMetricLabel(labelName string) bool {
return labelName != p.podIdLabel && labelName != p.containerNameLabel && labelName != p.namespaceIdLabel && labelName != p.tenantUIDLabel
return labelName != p.podIdLabel &&
labelName != p.containerNameLabel &&
labelName != p.namespaceIdLabel &&
labelName != p.tenantUIDLabel &&
labelName != p.entityTypeLabel &&
labelName != p.entityNameLabel
}

func (p *podConfigImpl) GetPodInfo(labels []*dto.LabelPair) (containerName, podId, namespaceId, tenantUID string) {
containerName, podId, namespaceId, tenantUID = "", p.podId, p.namespaceId, ""
func (p *podConfigImpl) GetPodInfo(labels []*dto.LabelPair) (containerName, podId, namespaceId, tenantUID, entityType, entityName string) {
containerName, podId, namespaceId, tenantUID, entityType, entityName = "", p.podId, p.namespaceId, "", "", ""
for _, label := range labels {
if label.GetName() == p.containerNameLabel && label.GetValue() != "" {
containerName = label.GetValue()
Expand All @@ -70,9 +79,13 @@ func (p *podConfigImpl) GetPodInfo(labels []*dto.LabelPair) (containerName, podI
namespaceId = label.GetValue()
} else if label.GetName() == p.tenantUIDLabel && label.GetValue() != "" {
tenantUID = label.GetValue()
} else if label.GetName() == p.entityTypeLabel && label.GetValue() != "" {
entityType = label.GetValue()
} else if label.GetName() == p.entityNameLabel && label.GetValue() != "" {
entityName = label.GetValue()
}
}
return containerName, podId, namespaceId, tenantUID
return containerName, podId, namespaceId, tenantUID, entityType, entityName
}

// CommonConfig contains all required information about environment in which
Expand Down
4 changes: 3 additions & 1 deletion prometheus-to-sd/config/dynamic_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,14 +108,16 @@ func mapToSourceConfig(componentName string, url url.URL, ip string, podId, name
namespaceIdLabel := values.Get("namespaceIdLabel")
containerNamelabel := values.Get("containerNamelabel")
tenantUIDLabel := values.Get("tenantUIDLabel")
entityTypeLabel := values.Get("entityTypeLabel")
entityNameLabel := values.Get("entityNameLabel")
metricsPrefix := values.Get("metricsPrefix")
customResource := values.Get("customResourceType")
customLabels := getMap(values, "customLabels")
auth, err := parseAuthConfig(url)
if err != nil {
return nil, err
}
podConfig := NewPodConfig(podId, namespaceId, podIdLabel, namespaceIdLabel, containerNamelabel, tenantUIDLabel)
podConfig := NewPodConfig(podId, namespaceId, podIdLabel, namespaceIdLabel, containerNamelabel, tenantUIDLabel, entityTypeLabel, entityNameLabel)
whitelistedLabelsMap, err := parseWhitelistedLabels(url.Query().Get("whitelistedLabels"))
if err != nil {
return nil, err
Expand Down
27 changes: 22 additions & 5 deletions prometheus-to-sd/config/dynamic_source_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ func TestMapToSourceConfig(t *testing.T) {
Port: uint(8080),
Path: defaultMetricsPath,
AuthConfig: emptyAuthConfig,
PodConfig: NewPodConfig(podName, podNamespace, "", "", "", ""),
PodConfig: NewPodConfig(podName, podNamespace, "", "", "", "", "", ""),
WhitelistedLabelsMap: emptyWhitelistedLabelsMap,
CustomLabels: map[string]string{},
},
Expand All @@ -57,7 +57,7 @@ func TestMapToSourceConfig(t *testing.T) {
Path: defaultMetricsPath,
AuthConfig: tokenAuthConfig,
Whitelisted: []string{"metric1", "metric2"},
PodConfig: NewPodConfig(podName, podNamespace, "", "", "", ""),
PodConfig: NewPodConfig(podName, podNamespace, "", "", "", "", "", ""),
WhitelistedLabelsMap: emptyWhitelistedLabelsMap,
CustomLabels: map[string]string{},
},
Expand All @@ -76,7 +76,7 @@ func TestMapToSourceConfig(t *testing.T) {
Path: defaultMetricsPath,
AuthConfig: userAuthConfig,
Whitelisted: []string{"metric1", "metric2"},
PodConfig: NewPodConfig(podName, podNamespace, "pod-id", "namespace-id", "container-name", ""),
PodConfig: NewPodConfig(podName, podNamespace, "pod-id", "namespace-id", "container-name", "", "", ""),
WhitelistedLabelsMap: emptyWhitelistedLabelsMap,
CustomLabels: map[string]string{},
},
Expand All @@ -94,7 +94,7 @@ func TestMapToSourceConfig(t *testing.T) {
Port: uint(8080),
Path: defaultMetricsPath,
Whitelisted: []string{"metric1", "metric2"},
PodConfig: NewPodConfig(podName, podNamespace, "pod-id", "namespace-id", "id", ""),
PodConfig: NewPodConfig(podName, podNamespace, "pod-id", "namespace-id", "id", "", "", ""),
WhitelistedLabelsMap: map[string]map[string]bool{
"containerNameLabel": {"/system.slice/node-problem-detector.service": true},
},
Expand All @@ -113,7 +113,24 @@ func TestMapToSourceConfig(t *testing.T) {
Host: "very_important_ip",
Port: uint(8080),
Path: defaultMetricsPath,
PodConfig: NewPodConfig(podName, podNamespace, "pod-id", "namespace-id", "id", "tenant-uid"),
PodConfig: NewPodConfig(podName, podNamespace, "pod-id", "namespace-id", "id", "tenant-uid", "", ""),
WhitelistedLabelsMap: emptyWhitelistedLabelsMap,
CustomLabels: map[string]string{},
},
},
{
componentName: "custom-entity",
url: url.URL{Host: ":8080", RawQuery: "podIdLabel=pod-id&entityTypeLabel=custom-type-label&entityNameLabel=custom-name-label"},
ip: "very_important_ip",
podName: podName,
podNamespace: podNamespace,
want: SourceConfig{
Component: "custom-entity",
Protocol: "http",
Host: "very_important_ip",
Port: uint(8080),
Path: defaultMetricsPath,
PodConfig: NewPodConfig(podName, podNamespace, "pod-id", "", "", "", "custom-type-label", "custom-name-label"),
WhitelistedLabelsMap: emptyWhitelistedLabelsMap,
CustomLabels: map[string]string{},
},
Expand Down
111 changes: 109 additions & 2 deletions prometheus-to-sd/config/pod_config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,31 @@ func TestIsMetricLabel(t *testing.T) {
label: "def",
want: false,
},
{
desc: "entityTypeLabel matches",
config: &podConfigImpl{
containerNameLabel: "foo",
podIdLabel: "bar",
namespaceIdLabel: "abc",
tenantUIDLabel: "def",
entityTypeLabel: "ghi",
},
label: "ghi",
want: false,
},
{
desc: "entityNameLabel matches",
config: &podConfigImpl{
containerNameLabel: "foo",
podIdLabel: "bar",
namespaceIdLabel: "abc",
tenantUIDLabel: "def",
entityTypeLabel: "ghi",
entityNameLabel: "jkl",
},
label: "jkl",
want: false,
},
{
desc: "none match",
config: &podConfigImpl{
Expand All @@ -102,6 +127,8 @@ func TestGetPodInfo(t *testing.T) {
pod, podLabel := "pod", "pLabel"
namespace, namespaceLabel := "namespace", "nLabel"
tenantUID, tenantUIDLabel := "tenantUID", "tLabel"
entityType, entityTypeLabel := "entityType", "eTLabel"
entityName, entityNameLabel := "entityName", "eNLabel"
other, otherLabel := "other", "olabel"
labels := []*dto.LabelPair{
{
Expand All @@ -124,6 +151,14 @@ func TestGetPodInfo(t *testing.T) {
Name: &tenantUIDLabel,
Value: &tenantUID,
},
{
Name: &entityTypeLabel,
Value: &entityType,
},
{
Name: &entityNameLabel,
Value: &entityName,
},
}
for _, tc := range []struct {
desc string
Expand All @@ -132,13 +167,18 @@ func TestGetPodInfo(t *testing.T) {
wantPodId string
wantNamespaceId string
wantTenantUID string
wantEntityType string
wantEntityName string
}{
{
desc: "empty",
config: &podConfigImpl{},
wantContainerName: "",
wantPodId: "",
wantNamespaceId: "",
wantTenantUID: "",
wantEntityType: "",
wantEntityName: "",
},
{
desc: "not matching labels",
Expand All @@ -150,6 +190,9 @@ func TestGetPodInfo(t *testing.T) {
wantContainerName: "",
wantPodId: "",
wantNamespaceId: "",
wantTenantUID: "",
wantEntityType: "",
wantEntityName: "",
},
{
desc: "matching labels",
Expand All @@ -161,6 +204,9 @@ func TestGetPodInfo(t *testing.T) {
wantContainerName: container,
wantPodId: pod,
wantNamespaceId: namespace,
wantTenantUID: "",
wantEntityType: "",
wantEntityName: "",
},
{
desc: "matching labels w/ podId and namespaceId specified",
Expand All @@ -174,6 +220,9 @@ func TestGetPodInfo(t *testing.T) {
wantContainerName: container,
wantPodId: pod,
wantNamespaceId: namespace,
wantTenantUID: "",
wantEntityType: "",
wantEntityName: "",
},
{
desc: "not matching labels w/ podId and namespaceId specified",
Expand All @@ -187,6 +236,9 @@ func TestGetPodInfo(t *testing.T) {
wantContainerName: "",
wantPodId: "podid",
wantNamespaceId: "namespaceid",
wantTenantUID: "",
wantEntityType: "",
wantEntityName: "",
},
{
desc: "some matching labels w/ podId and namespaceId specified",
Expand All @@ -200,6 +252,9 @@ func TestGetPodInfo(t *testing.T) {
wantContainerName: container,
wantPodId: "podid",
wantNamespaceId: "namespaceid",
wantTenantUID: "",
wantEntityType: "",
wantEntityName: "",
},
{
desc: "podId and namespaceId specified",
Expand All @@ -210,17 +265,63 @@ func TestGetPodInfo(t *testing.T) {
wantContainerName: "",
wantPodId: "podid",
wantNamespaceId: "namespaceid",
wantTenantUID: "",
wantEntityType: "",
wantEntityName: "",
},
{
desc: "tenant UID specified",
config: &podConfigImpl{
tenantUIDLabel: tenantUIDLabel,
},
wantTenantUID: tenantUID,
wantContainerName: "",
wantPodId: "",
wantNamespaceId: "",
wantTenantUID: tenantUID,
wantEntityType: "",
wantEntityName: "",
},
{
desc: "entity type specified",
config: &podConfigImpl{
entityTypeLabel: entityTypeLabel,
},
wantContainerName: "",
wantPodId: "",
wantNamespaceId: "",
wantTenantUID: "",
wantEntityType: entityType,
wantEntityName: "",
},
{
desc: "entity name specified",
config: &podConfigImpl{
entityNameLabel: entityNameLabel,
},
wantContainerName: "",
wantPodId: "",
wantNamespaceId: "",
wantTenantUID: "",
wantEntityType: "",
wantEntityName: entityName,
},
{
desc: "all custom labels specified",
config: &podConfigImpl{
tenantUIDLabel: tenantUIDLabel,
entityTypeLabel: entityTypeLabel,
entityNameLabel: entityNameLabel,
},
wantContainerName: "",
wantPodId: "",
wantNamespaceId: "",
wantTenantUID: tenantUID,
wantEntityType: entityType,
wantEntityName: entityName,
},
} {
t.Run(tc.desc, func(t *testing.T) {
container, pod, namespace, tenantUID := tc.config.GetPodInfo(labels)
container, pod, namespace, tenantUID, entityType, entityName := tc.config.GetPodInfo(labels)
if container != tc.wantContainerName {
t.Errorf("Unexpected containerName; got %q, want %q", container, tc.wantContainerName)
}
Expand All @@ -233,6 +334,12 @@ func TestGetPodInfo(t *testing.T) {
if tenantUID != tc.wantTenantUID {
t.Errorf("Unexpected tenantUID; got %q, want %q", tenantUID, tc.wantTenantUID)
}
if entityType != tc.wantEntityType {
t.Errorf("Unexpected entityType; got %q, want %q", entityType, tc.wantEntityType)
}
if entityName != tc.wantEntityName {
t.Errorf("Unexpected entityName; got %q, want %q", entityName, tc.wantEntityName)
}
})
}
}
4 changes: 3 additions & 1 deletion prometheus-to-sd/config/source_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,14 +107,16 @@ func parseSourceConfig(uri flags.Uri, podId, namespaceId string) (*SourceConfig,
namespaceIdLabel := values.Get("namespaceIdLabel")
containerNameLabel := values.Get("containerNameLabel")
tenantUIDLabel := values.Get("tenantUIDLabel")
entityTypeLabel := values.Get("entityTypeLabel")
entityNameLabel := values.Get("entityNameLabel")
metricsPrefix := values.Get("metricsPrefix")
customResource := values.Get("customResourceType")
customLabels := getMap(values, "customLabels")
auth, err := parseAuthConfig(uri.Val)
if err != nil {
return nil, err
}
podConfig := NewPodConfig(podId, namespaceId, podIdLabel, namespaceIdLabel, containerNameLabel, tenantUIDLabel)
podConfig := NewPodConfig(podId, namespaceId, podIdLabel, namespaceIdLabel, containerNameLabel, tenantUIDLabel, entityTypeLabel, entityNameLabel)

whitelistedLabelsMap, err := parseWhitelistedLabels(values.Get("whitelistedLabels"))
if err != nil {
Expand Down
Loading