forked from open-telemetry/opentelemetry-collector-contrib
-
Notifications
You must be signed in to change notification settings - Fork 21
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
Add podresourcesstore in awscontainerinsightreceiver #167
Merged
aditya-purang
merged 16 commits into
amazon-contributing:main
from
aditya-purang:pod-correlation-library
Mar 6, 2024
Merged
Changes from 15 commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
ad74d49
add podresourcesstore to awscontainerinsightsreceivers
aditya-purang 5c5cc34
adding refresh logic
aditya-purang a5dbcc0
refactor tests
aditya-purang 4dfc194
adding getters to get pod and resouce info
aditya-purang 69bb375
add shutdown methods to podresourcesclient and podresourcesstore, cov…
aditya-purang f8cfd94
fix typos and rename constants
aditya-purang 0ed0726
adding AddResourceName method to podresourcestore and refactoring tests
aditya-purang b0d1000
skip updating maps when no resource names are allowlisted
aditya-purang 81bb4f2
minor refactoring in the podresourcesclient
aditya-purang ccc26ca
make fields of the ContainerInfo and REsourceInfo structs publicly ac…
aditya-purang c07e9ee
Merge branch 'main' into pod-correlation-library
aditya-purang 423710e
adding changelog
aditya-purang 9dac565
fix spacing in go.mod
aditya-purang 687e596
crosslinking go mod with make crosslink
aditya-purang 0cb3bd0
tidy mod file using make gotidy
aditya-purang 0cc295c
rerunning make on project to resolve issues
aditya-purang File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
# Use this changelog template to create an entry for release notes. | ||
# If your change doesn't affect end users, such as a test fix or a tooling change, | ||
# you should instead start your pull request title with [chore] or use the "Skip Changelog" label. | ||
|
||
# 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: awscontainerinsightreceiver | ||
|
||
# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`). | ||
note: added a new podresourcestore which provides mapping from resource to container and vice-versa | ||
|
||
# Mandatory: One or more tracking issues related to the change. You can use the PR number here if no issue exists. | ||
issues: [167] | ||
|
||
# (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: this change provides a new store in awscontainerinsightreceiver, which when started provides mapping from resources to container and vice versa using kubelet podresourcesapi. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
87 changes: 87 additions & 0 deletions
87
receiver/awscontainerinsightreceiver/internal/stores/kubeletutil/podresourcesclient.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 kubeletutil // import "github.com/open-telemetry/opentelemetry-collector-contrib/receiver/awscontainerinsightreceiver/internal/stores/kubeletutil" | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"google.golang.org/grpc" | ||
"net" | ||
"os" | ||
"time" | ||
|
||
"google.golang.org/grpc/credentials/insecure" | ||
podresourcesapi "k8s.io/kubelet/pkg/apis/podresources/v1" | ||
) | ||
|
||
const ( | ||
socketPath = "/var/lib/kubelet/pod-resources/kubelet.sock" | ||
connectionTimeout = 10 * time.Second | ||
) | ||
|
||
type PodResourcesClient struct { | ||
delegateClient podresourcesapi.PodResourcesListerClient | ||
conn *grpc.ClientConn | ||
} | ||
|
||
func NewPodResourcesClient() (*PodResourcesClient, error) { | ||
podResourcesClient := &PodResourcesClient{} | ||
|
||
conn, err := podResourcesClient.connectToServer(socketPath) | ||
podResourcesClient.conn = conn | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to connect to server: %w", err) | ||
} | ||
|
||
podResourcesClient.delegateClient = podresourcesapi.NewPodResourcesListerClient(conn) | ||
|
||
return podResourcesClient, nil | ||
} | ||
|
||
func (p *PodResourcesClient) connectToServer(socket string) (*grpc.ClientConn, error) { | ||
_, err := os.Stat(socket) | ||
if os.IsNotExist(err) { | ||
return nil, fmt.Errorf("socket path does not exist: %s", socket) | ||
} else if err != nil { | ||
return nil, fmt.Errorf("failed to check socket path: %w", err) | ||
} | ||
|
||
ctx, cancel := context.WithTimeout(context.Background(), connectionTimeout) | ||
defer cancel() | ||
|
||
conn, err := grpc.DialContext(ctx, | ||
socket, | ||
grpc.WithTransportCredentials(insecure.NewCredentials()), | ||
grpc.WithBlock(), | ||
grpc.WithContextDialer(func(ctx context.Context, addr string) (net.Conn, error) { | ||
d := net.Dialer{} | ||
return d.DialContext(ctx, "unix", addr) | ||
}), | ||
) | ||
|
||
if err != nil { | ||
return nil, fmt.Errorf("failure connecting to '%s': %w", socket, err) | ||
} | ||
|
||
return conn, nil | ||
} | ||
|
||
func (p *PodResourcesClient) ListPods() (*podresourcesapi.ListPodResourcesResponse, error) { | ||
ctx, cancel := context.WithTimeout(context.Background(), connectionTimeout) | ||
defer cancel() | ||
|
||
resp, err := p.delegateClient.List(ctx, &podresourcesapi.ListPodResourcesRequest{}) | ||
if err != nil { | ||
return nil, fmt.Errorf("failure getting pod resources: %w", err) | ||
} | ||
|
||
return resp, nil | ||
} | ||
|
||
func (p *PodResourcesClient) Shutdown() { | ||
err := p.conn.Close() | ||
if err != nil { | ||
return | ||
} | ||
} |
166 changes: 166 additions & 0 deletions
166
receiver/awscontainerinsightreceiver/internal/stores/podresourcesstore.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,166 @@ | ||
// Copyright The OpenTelemetry Authors | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package stores // import "github.com/open-telemetry/opentelemetry-collector-contrib/receiver/awscontainerinsightreceiver/internal/stores" | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"sync" | ||
"time" | ||
|
||
"go.uber.org/zap" | ||
v1 "k8s.io/kubelet/pkg/apis/podresources/v1" | ||
|
||
"github.com/open-telemetry/opentelemetry-collector-contrib/receiver/awscontainerinsightreceiver/internal/stores/kubeletutil" | ||
) | ||
|
||
const ( | ||
taskTimeout = 10 * time.Second | ||
) | ||
|
||
var ( | ||
instance *PodResourcesStore | ||
once sync.Once | ||
) | ||
|
||
type ContainerInfo struct { | ||
PodName string | ||
ContainerName string | ||
Namespace string | ||
} | ||
|
||
type ResourceInfo struct { | ||
ResourceName string | ||
DeviceID string | ||
} | ||
|
||
type PodResourcesClientInterface interface { | ||
ListPods() (*v1.ListPodResourcesResponse, error) | ||
Shutdown() | ||
} | ||
|
||
type PodResourcesStore struct { | ||
containerInfoToResourcesMap map[ContainerInfo][]ResourceInfo | ||
resourceToPodContainerMap map[ResourceInfo]ContainerInfo | ||
resourceNameSet map[string]struct{} | ||
lastRefreshed time.Time | ||
ctx context.Context | ||
cancel context.CancelFunc | ||
straussb marked this conversation as resolved.
Show resolved
Hide resolved
|
||
logger *zap.Logger | ||
podResourcesClient PodResourcesClientInterface | ||
} | ||
|
||
func NewPodResourcesStore(logger *zap.Logger) *PodResourcesStore { | ||
once.Do(func() { | ||
podResourcesClient, _ := kubeletutil.NewPodResourcesClient() | ||
ctx, cancel := context.WithCancel(context.Background()) | ||
instance = &PodResourcesStore{ | ||
containerInfoToResourcesMap: make(map[ContainerInfo][]ResourceInfo), | ||
resourceToPodContainerMap: make(map[ResourceInfo]ContainerInfo), | ||
resourceNameSet: make(map[string]struct{}), | ||
lastRefreshed: time.Now(), | ||
ctx: ctx, | ||
cancel: cancel, | ||
logger: logger, | ||
podResourcesClient: podResourcesClient, | ||
} | ||
|
||
go func() { | ||
refreshTicker := time.NewTicker(time.Second) | ||
for { | ||
select { | ||
case <-refreshTicker.C: | ||
instance.refreshTick() | ||
case <-instance.ctx.Done(): | ||
refreshTicker.Stop() | ||
return | ||
} | ||
} | ||
}() | ||
}) | ||
return instance | ||
} | ||
|
||
func (p *PodResourcesStore) refreshTick() { | ||
now := time.Now() | ||
if now.Sub(p.lastRefreshed) >= taskTimeout { | ||
p.refresh() | ||
p.lastRefreshed = now | ||
} | ||
} | ||
|
||
func (p *PodResourcesStore) refresh() { | ||
doRefresh := func() { | ||
p.updateMaps() | ||
} | ||
|
||
refreshWithTimeout(p.ctx, doRefresh, taskTimeout) | ||
} | ||
|
||
func (p *PodResourcesStore) updateMaps() { | ||
p.containerInfoToResourcesMap = make(map[ContainerInfo][]ResourceInfo) | ||
p.resourceToPodContainerMap = make(map[ResourceInfo]ContainerInfo) | ||
|
||
if len(p.resourceNameSet) == 0 { | ||
p.logger.Warn("No resource names allowlisted thus skipping updating of maps.") | ||
return | ||
} | ||
|
||
devicePods, err := p.podResourcesClient.ListPods() | ||
|
||
if err != nil { | ||
p.logger.Error(fmt.Sprintf("Error getting pod resources: %v", err)) | ||
return | ||
} | ||
|
||
for _, pod := range devicePods.GetPodResources() { | ||
for _, container := range pod.GetContainers() { | ||
for _, device := range container.GetDevices() { | ||
|
||
containerInfo := ContainerInfo{ | ||
PodName: pod.GetName(), | ||
Namespace: pod.GetNamespace(), | ||
ContainerName: container.GetName(), | ||
} | ||
|
||
for _, deviceID := range device.GetDeviceIds() { | ||
resourceInfo := ResourceInfo{ | ||
ResourceName: device.GetResourceName(), | ||
DeviceID: deviceID, | ||
} | ||
_, found := p.resourceNameSet[resourceInfo.ResourceName] | ||
if found { | ||
p.containerInfoToResourcesMap[containerInfo] = append(p.containerInfoToResourcesMap[containerInfo], resourceInfo) | ||
p.resourceToPodContainerMap[resourceInfo] = containerInfo | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
func (p *PodResourcesStore) GetContainerInfo(deviceID string, resourceName string) *ContainerInfo { | ||
key := ResourceInfo{DeviceID: deviceID, ResourceName: resourceName} | ||
if containerInfo, ok := p.resourceToPodContainerMap[key]; ok { | ||
return &containerInfo | ||
} | ||
return nil | ||
} | ||
|
||
func (p *PodResourcesStore) GetResourcesInfo(podName string, containerName string, namespace string) *[]ResourceInfo { | ||
key := ContainerInfo{PodName: podName, ContainerName: containerName, Namespace: namespace} | ||
if resourceInfo, ok := p.containerInfoToResourcesMap[key]; ok { | ||
return &resourceInfo | ||
} | ||
return nil | ||
} | ||
|
||
func (p *PodResourcesStore) AddResourceName(resourceName string) { | ||
p.resourceNameSet[resourceName] = struct{}{} | ||
} | ||
|
||
func (p *PodResourcesStore) Shutdown() { | ||
p.cancel() | ||
p.podResourcesClient.Shutdown() | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this just to avoid the compiler giving you a warning about ignoring the error?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yup