Skip to content
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

[processor/resourcedetection] introduce kubeadm detector #35450

Open
wants to merge 24 commits into
base: main
Choose a base branch
from
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
27 changes: 27 additions & 0 deletions .chloggen/resourcedetection-local-cluster.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# Use this changelog template to create an entry for release notes.

# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix'
change_type: enhancement

# The name of the component, or a single word describing the area of concern, (e.g. filelogreceiver)
component: resourcedetectionprocessor

# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
note: "Introduce kubeadm detector to retrieve local cluster name."

# Mandatory: One or more tracking issues related to the change. You can use the PR number here if no issue exists.
issues: [35116]

# (Optional) One or more lines of additional information to render under the primary note.
# These lines will be padded with 2 spaces and then inserted directly into the document.
# Use pipe (|) for multiline entries.
subtext:

# If your change doesn't affect end users or the exported elements of any package,
# you should instead start your pull request title with [chore] or use the "Skip Changelog" label.
# Optional: The change log or logs in which this entry should be included.
# e.g. '[user]' or '[user, api]'
# Include 'user' if the change is relevant to end users.
# Include 'api' if there is a change to a library API.
# Default: '[user]'
change_logs: []
1 change: 1 addition & 0 deletions .github/CODEOWNERS
Validating CODEOWNERS rules …
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,7 @@ processor/resourcedetectionprocessor/internal/docker/ @open-teleme
processor/resourcedetectionprocessor/internal/gcp/ @open-telemetry/collector-contrib-approvers
processor/resourcedetectionprocessor/internal/heroku/ @open-telemetry/collector-contrib-approvers @atoulme
processor/resourcedetectionprocessor/internal/k8snode/ @open-telemetry/collector-contrib-approvers
processor/resourcedetectionprocessor/internal/kubeadm/ @open-telemetry/collector-contrib-approvers
processor/resourcedetectionprocessor/internal/openshift/ @open-telemetry/collector-contrib-approvers @frzifus
processor/resourcedetectionprocessor/internal/system/ @open-telemetry/collector-contrib-approvers
processor/resourceprocessor/ @open-telemetry/collector-contrib-approvers @dmitryax
Expand Down
1 change: 1 addition & 0 deletions .github/ISSUE_TEMPLATE/bug_report.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,7 @@ body:
- processor/resourcedetection/internal/gcp
- processor/resourcedetection/internal/heroku
- processor/resourcedetection/internal/k8snode
- processor/resourcedetection/internal/kubeadm
- processor/resourcedetection/internal/openshift
- processor/resourcedetection/internal/system
- processor/routing
Expand Down
1 change: 1 addition & 0 deletions .github/ISSUE_TEMPLATE/feature_request.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,7 @@ body:
- processor/resourcedetection/internal/gcp
- processor/resourcedetection/internal/heroku
- processor/resourcedetection/internal/k8snode
- processor/resourcedetection/internal/kubeadm
- processor/resourcedetection/internal/openshift
- processor/resourcedetection/internal/system
- processor/routing
Expand Down
1 change: 1 addition & 0 deletions .github/ISSUE_TEMPLATE/other.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,7 @@ body:
- processor/resourcedetection/internal/gcp
- processor/resourcedetection/internal/heroku
- processor/resourcedetection/internal/k8snode
- processor/resourcedetection/internal/kubeadm
- processor/resourcedetection/internal/openshift
- processor/resourcedetection/internal/system
- processor/routing
Expand Down
1 change: 1 addition & 0 deletions .github/ISSUE_TEMPLATE/unmaintained.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,7 @@ body:
- processor/resourcedetection/internal/gcp
- processor/resourcedetection/internal/heroku
- processor/resourcedetection/internal/k8snode
- processor/resourcedetection/internal/kubeadm
- processor/resourcedetection/internal/openshift
- processor/resourcedetection/internal/system
- processor/routing
Expand Down
46 changes: 46 additions & 0 deletions internal/metadataproviders/kubeadm/metadata.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0

package kubeadm // import "github.com/open-telemetry/opentelemetry-collector-contrib/internal/metadataproviders/kubeadm"

import (
"context"
"fmt"

metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"

"github.com/open-telemetry/opentelemetry-collector-contrib/internal/k8sconfig"
)

type Provider interface {
// ClusterName returns the current K8S cluster name
ClusterName(ctx context.Context) (string, error)
}

type kubeadmProvider struct {
kubeadmClient kubernetes.Interface
configMapName string
configMapNamespace string
}

func NewProvider(configMapName string, configMapNamespace string, apiConf k8sconfig.APIConfig) (Provider, error) {
k8sAPIClient, err := k8sconfig.MakeClient(apiConf)
if err != nil {
return nil, fmt.Errorf("failed to create K8s API client: %w", err)
}
return &kubeadmProvider{
kubeadmClient: k8sAPIClient,
configMapName: configMapName,
configMapNamespace: configMapNamespace,
}, nil
}

func (k *kubeadmProvider) ClusterName(ctx context.Context) (string, error) {
configmap, err := k.kubeadmClient.CoreV1().ConfigMaps(k.configMapNamespace).Get(ctx, k.configMapName, metav1.GetOptions{})
if err != nil {
return "", fmt.Errorf("failed to fetch ConfigMap with name %s and namespace %s from K8s API: %w", k.configMapName, k.configMapNamespace, err)
}

return configmap.Data["clusterName"], nil
}
87 changes: 87 additions & 0 deletions internal/metadataproviders/kubeadm/metadata_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0

package kubeadm

import (
"context"
"testing"

"github.com/stretchr/testify/assert"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes/fake"

"github.com/open-telemetry/opentelemetry-collector-contrib/internal/k8sconfig"
)

func TestNewProvider(t *testing.T) {
// set k8s cluster env variables to make the API client happy
t.Setenv("KUBERNETES_SERVICE_HOST", "127.0.0.1")
t.Setenv("KUBERNETES_SERVICE_PORT", "6443")

_, err := NewProvider("name", "ns", k8sconfig.APIConfig{AuthType: k8sconfig.AuthTypeNone})
assert.NoError(t, err)
}

func TestClusterName(t *testing.T) {
client := fake.NewSimpleClientset()
err := setupConfigMap(client)
assert.NoError(t, err)

tests := []struct {
testName string
CMname string
CMnamespace string
clusterName string
errMsg string
}{
{
testName: "valid",
CMname: "cm",
CMnamespace: "ns",
clusterName: "myClusterName",
errMsg: "",
},
{
testName: "configmap not found",
CMname: "cm2",
CMnamespace: "ns",
errMsg: "failed to fetch ConfigMap with name cm2 and namespace ns from K8s API: configmaps \"cm2\" not found",
},
}

for _, tt := range tests {
t.Run(tt.testName, func(t *testing.T) {
kubeadmP := &kubeadmProvider{
kubeadmClient: client,
configMapName: tt.CMname,
configMapNamespace: tt.CMnamespace,
}
clusterName, err := kubeadmP.ClusterName(context.Background())
if tt.errMsg != "" {
assert.EqualError(t, err, tt.errMsg)
} else {
assert.NoError(t, err)
assert.Equal(t, clusterName, tt.clusterName)
}
})
}
}

func setupConfigMap(client *fake.Clientset) error {
cm := &corev1.ConfigMap{
ObjectMeta: metav1.ObjectMeta{
Name: "cm",
Namespace: "ns",
},
Data: map[string]string{
"clusterName": "myClusterName",
},
}
_, err := client.CoreV1().ConfigMaps("ns").Create(context.Background(), cm, metav1.CreateOptions{})
if err != nil {
return err
}
return nil
}
14 changes: 14 additions & 0 deletions internal/metadataproviders/kubeadm/package_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0

package kubeadm

import (
"testing"

"go.uber.org/goleak"
)

func TestMain(m *testing.M) {
goleak.VerifyTestMain(m)
}
22 changes: 22 additions & 0 deletions processor/resourcedetectionprocessor/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -509,11 +509,33 @@ processors:
override: false
```

### Kubeadm Metadata

Queries the K8S API server to retrieve the following resource attributes:

* k8s.cluster.name

The following permissions are required:
```yaml
kind: ClusterRole
metadata:
name: otel-collector
rules:
- apiGroups: [""]
resources: ["configmaps"]
verbs: ["get", "list"]
```

| Name | Type | Required | Default | Docs |
| ---- | ---- |----------|-----------------|----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| auth_type | string | No | `serviceAccount` | How to authenticate to the K8s API server. This can be one of `none` (for no auth), `serviceAccount` (to use the standard service account token provided to the agent pod), or `kubeConfig` to use credentials from `~/.kube/config`. |

### K8S Node Metadata

Queries the K8S api server to retrieve the following node resource attributes:

* k8s.node.uid
* k8s.node.name
crobert-1 marked this conversation as resolved.
Show resolved Hide resolved

The following permissions are required:
```yaml
Expand Down
7 changes: 7 additions & 0 deletions processor/resourcedetectionprocessor/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"github.com/open-telemetry/opentelemetry-collector-contrib/processor/resourcedetectionprocessor/internal/gcp"
"github.com/open-telemetry/opentelemetry-collector-contrib/processor/resourcedetectionprocessor/internal/heroku"
"github.com/open-telemetry/opentelemetry-collector-contrib/processor/resourcedetectionprocessor/internal/k8snode"
"github.com/open-telemetry/opentelemetry-collector-contrib/processor/resourcedetectionprocessor/internal/kubeadm"
"github.com/open-telemetry/opentelemetry-collector-contrib/processor/resourcedetectionprocessor/internal/openshift"
"github.com/open-telemetry/opentelemetry-collector-contrib/processor/resourcedetectionprocessor/internal/system"
)
Expand Down Expand Up @@ -86,6 +87,9 @@ type DetectorConfig struct {

// K8SNode contains user-specified configurations for the K8SNode detector
K8SNodeConfig k8snode.Config `mapstructure:"k8snode"`

// Kubeadm contains user-specified configurations for the Kubeadm detector
KubeadmConfig kubeadm.Config `mapstructure:"kubeadm"`
}

func detectorCreateDefaultConfig() DetectorConfig {
Expand All @@ -104,6 +108,7 @@ func detectorCreateDefaultConfig() DetectorConfig {
SystemConfig: system.CreateDefaultConfig(),
OpenShiftConfig: openshift.CreateDefaultConfig(),
K8SNodeConfig: k8snode.CreateDefaultConfig(),
KubeadmConfig: kubeadm.CreateDefaultConfig(),
}
}

Expand Down Expand Up @@ -137,6 +142,8 @@ func (d *DetectorConfig) GetConfigFromType(detectorType internal.DetectorType) i
return d.OpenShiftConfig
case k8snode.TypeStr:
return d.K8SNodeConfig
case kubeadm.TypeStr:
return d.KubeadmConfig
default:
return nil
}
Expand Down
1 change: 1 addition & 0 deletions processor/resourcedetectionprocessor/doc.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
//go:generate mdatagen internal/openshift/metadata.yaml
//go:generate mdatagen internal/system/metadata.yaml
//go:generate mdatagen internal/k8snode/metadata.yaml
//go:generate mdatagen internal/kubeadm/metadata.yaml

// package resourcedetectionprocessor implements a processor
// which can be used to detect resource information from the host,
Expand Down
2 changes: 2 additions & 0 deletions processor/resourcedetectionprocessor/factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import (
"github.com/open-telemetry/opentelemetry-collector-contrib/processor/resourcedetectionprocessor/internal/gcp"
"github.com/open-telemetry/opentelemetry-collector-contrib/processor/resourcedetectionprocessor/internal/heroku"
"github.com/open-telemetry/opentelemetry-collector-contrib/processor/resourcedetectionprocessor/internal/k8snode"
"github.com/open-telemetry/opentelemetry-collector-contrib/processor/resourcedetectionprocessor/internal/kubeadm"
"github.com/open-telemetry/opentelemetry-collector-contrib/processor/resourcedetectionprocessor/internal/metadata"
"github.com/open-telemetry/opentelemetry-collector-contrib/processor/resourcedetectionprocessor/internal/openshift"
"github.com/open-telemetry/opentelemetry-collector-contrib/processor/resourcedetectionprocessor/internal/system"
Expand Down Expand Up @@ -63,6 +64,7 @@ func NewFactory() processor.Factory {
system.TypeStr: system.NewDetector,
openshift.TypeStr: openshift.NewDetector,
k8snode.TypeStr: k8snode.NewDetector,
kubeadm.TypeStr: kubeadm.NewDetector,
})

f := &factory{
Expand Down
21 changes: 21 additions & 0 deletions processor/resourcedetectionprocessor/internal/kubeadm/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0

package kubeadm // import "github.com/open-telemetry/opentelemetry-collector-contrib/processor/resourcedetectionprocessor/internal/kubeadm"

import (
"github.com/open-telemetry/opentelemetry-collector-contrib/internal/k8sconfig"
"github.com/open-telemetry/opentelemetry-collector-contrib/processor/resourcedetectionprocessor/internal/kubeadm/internal/metadata"
)

type Config struct {
k8sconfig.APIConfig `mapstructure:",squash"`
ResourceAttributes metadata.ResourceAttributesConfig `mapstructure:"resource_attributes"`
}

func CreateDefaultConfig() Config {
return Config{
APIConfig: k8sconfig.APIConfig{AuthType: k8sconfig.AuthTypeServiceAccount},
ResourceAttributes: metadata.DefaultResourceAttributesConfig(),
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
[comment]: <> (Code generated by mdatagen. DO NOT EDIT.)

# resourcedetectionprocessor/kubeadm

**Parent Component:** resourcedetection

## Resource Attributes

| Name | Description | Values | Enabled |
| ---- | ----------- | ------ | ------- |
| k8s.cluster.name | The Kubernetes cluster name | Any Str | true |

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

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

Loading