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

Fix getPodWorkloadObject to work with more workloads names which contains hyphens #1314

Merged
merged 7 commits into from
Jul 2, 2024
Merged
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
9 changes: 8 additions & 1 deletion .github/workflows/build.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ jobs:
run: |
make test

build-odiglet:
build-and-test-odiglet:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
Expand All @@ -63,6 +63,13 @@ jobs:
context: .
push: false
tags: odiglet:pr-${{ github.event.number }}
- name: Install build dependencies
run: |
sudo apt-get update && sudo apt-get install -y clang llvm libbpf-dev
- name: run tests
working-directory: ./odiglet
run: |
make test
build-frontend:
runs-on: ubuntu-latest
steps:
Expand Down
10 changes: 9 additions & 1 deletion odiglet/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,12 @@ generate:
go mod download
GO_AUTO_PATH=$$(go list -m -f '{{.Dir}}' "go.opentelemetry.io/auto") && \
cd $$GO_AUTO_PATH && \
make generate
make generate

test:
go mod download
GO_AUTO_PATH=$$(go list -m -f '{{.Dir}}' "go.opentelemetry.io/auto") && \
sudo chmod -R +w $$GO_AUTO_PATH && \
cd $$GO_AUTO_PATH && \
make generate
go test -v ./...
2 changes: 1 addition & 1 deletion odiglet/pkg/kube/instrumentation_ebpf/pods.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ func (p *PodsReconciler) getPodWorkloadObject(ctx context.Context, pod *corev1.P
for _, owner := range pod.OwnerReferences {
if owner.Kind == "ReplicaSet" {
// ReplicaSet name is in the format <deployment-name>-<random-string>
hyphenIndex := strings.Index(owner.Name, "-")
hyphenIndex := strings.LastIndex(owner.Name, "-")
if hyphenIndex == -1 {
// It is possible for a user to define a bare ReplicaSet without a deployment, currently not supporting this
return nil, errors.New("replicaset name does not contain a hyphen")
Expand Down
114 changes: 114 additions & 0 deletions odiglet/pkg/kube/instrumentation_ebpf/pods_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
package instrumentation_ebpf

import (
"context"
"testing"

"github.com/odigos-io/odigos/common"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

func TestGetPodWorkloadObject(t *testing.T) {
pr := &PodsReconciler{}
cases := []struct{
name string
pod *corev1.Pod
expectedWorkload common.PodWorkload
}{
{
name: "pod in deployment",
pod: &corev1.Pod{
ObjectMeta: metav1.ObjectMeta {
OwnerReferences: []metav1.OwnerReference{
{
Kind: "ReplicaSet",
Name: "deployment-1234",
},
},
Namespace: "default",
},
},
expectedWorkload: common.PodWorkload{
Kind: "Deployment",
Name: "deployment",
Namespace: "default",
},
},
{
name: "pod with hyphen in name of deployment",
pod: &corev1.Pod{
ObjectMeta: metav1.ObjectMeta {
OwnerReferences: []metav1.OwnerReference{
{
Kind: "ReplicaSet",
Name: "deployment-foo-5678",
},
},
Namespace: "default",
},
},
expectedWorkload: common.PodWorkload{
Kind: "Deployment",
Name: "deployment-foo",
Namespace: "default",
},
},
{
name: "pod in DaemonSet",
pod: &corev1.Pod{
ObjectMeta: metav1.ObjectMeta {
OwnerReferences: []metav1.OwnerReference{
{
Kind: "DaemonSet",
Name: "someDaemonSet",
},
},
Namespace: "default",
},
},
expectedWorkload: common.PodWorkload{
Kind: "DaemonSet",
Name: "someDaemonSet",
Namespace: "default",
},
},
{
name: "pod in StatefulSet",
pod: &corev1.Pod{
ObjectMeta: metav1.ObjectMeta {
OwnerReferences: []metav1.OwnerReference{
{
Kind: "StatefulSet",
Name: "someStatefulSet",
},
},
Namespace: "default",
},
},
expectedWorkload: common.PodWorkload{
Kind: "StatefulSet",
Name: "someStatefulSet",
Namespace: "default",
},
},
}

for _, c := range cases {
t.Run(c.name, func(t *testing.T) {
workload, err := pr.getPodWorkloadObject(context.Background(), c.pod)
if err != nil {
t.Errorf("unexpected error: %v", err)
}
if workload.Kind != c.expectedWorkload.Kind {
t.Errorf("expected kind %s, got %s", c.expectedWorkload.Kind, workload.Kind)
}
if workload.Name != c.expectedWorkload.Name {
t.Errorf("expected name %s, got %s", c.expectedWorkload.Name, workload.Name)
}
if workload.Namespace != c.expectedWorkload.Namespace {
t.Errorf("expected namespace %s, got %s", c.expectedWorkload.Namespace, workload.Namespace)
}
})
}
}
17 changes: 17 additions & 0 deletions odiglet/pkg/process/process_other.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
//go:build !linux

package process

import procdiscovery "github.com/odigos-io/odigos/procdiscovery/pkg/process"

// These functions are stubs for non-linux platforms to allow running tests on them.

func isPodContainerPredicate(_ string, _ string) func(string) bool {
return func(procDirName string) bool {
return false
}
}

func FindAllInContainer(podUID string, containerName string) ([]procdiscovery.Details, error) {
return nil, nil
}
Loading