Skip to content

Commit

Permalink
chore: refactor get service name
Browse files Browse the repository at this point in the history
  • Loading branch information
alonbraymok committed Dec 8, 2024
1 parent 09011e8 commit 85deec2
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 31 deletions.
2 changes: 1 addition & 1 deletion odiglet/debug.Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ RUN ARCH_SUFFIX=$(cat /tmp/arch_suffix) && \
unzip opentelemetry-dotnet-instrumentation-linux-glibc-${ARCH_SUFFIX}.zip && \
rm opentelemetry-dotnet-instrumentation-linux-glibc-${ARCH_SUFFIX}.zip

FROM --platform=$BUILDPLATFORM keyval/odiglet-base:v1.5 AS builder
FROM --platform=$BUILDPLATFORM keyval/odiglet-base:v1.7 AS builder
WORKDIR /go/src/github.com/odigos-io/odigos
# Copyy local modules required by the build
COPY api/ api/
Expand Down
48 changes: 18 additions & 30 deletions opampserver/pkg/deviceid/k8sattributes.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@ package deviceid

import (
"context"
"errors"

"github.com/go-logr/logr"
"github.com/odigos-io/odigos/common/consts"
odigosclientset "github.com/odigos-io/odigos/api/generated/odigos/clientset/versioned"
"github.com/odigos-io/odigos/k8sutils/pkg/workload"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/rest"
)

// the purpose of this class is to use the k8s api to resolve container details
Expand All @@ -26,42 +26,30 @@ func NewK8sPodInfoResolver(logger logr.Logger, kubeClient *kubernetes.Clientset)
}
}

func (k *K8sPodInfoResolver) getServiceNameFromAnnotation(ctx context.Context, name string, kind string, namespace string) (string, bool) {
obj, err := k.getWorkloadObject(ctx, name, kind, namespace)
func (k *K8sPodInfoResolver) getServiceNameFromInstrumentationConfig(ctx context.Context, name string, kind string, namespace string) (string, bool) {

instConfigName := workload.CalculateWorkloadRuntimeObjectName(name, workload.WorkloadKind(kind))
cfg, err := rest.InClusterConfig()
if err != nil {
k.logger.Error(err, "failed to get workload object to resolve reported service name annotation. will use fallback service name")
return "", false
k.logger.Error(err, "Failed to init Kubernetes API client")
}

annotations := obj.GetAnnotations()
if annotations == nil {
// no annotations, so service name is not specified by user. fallback to workload name
return "", false
odigosKubeClient, err := odigosclientset.NewForConfig(cfg)
if err != nil {
k.logger.Error(err, "Failed to init odigos client")
}

overwrittenName, exists := annotations[consts.OdigosReportedNameAnnotation]
if !exists {
// the is no annotation by user for specific reported service name for this workload
// fallback to workload name
instConfig, err := odigosKubeClient.OdigosV1alpha1().InstrumentationConfigs(namespace).Get(ctx, instConfigName, metav1.GetOptions{})
if err != nil {
k.logger.Error(err, "Failed to get InstrumentationConfig for workload", "name", name, "kind", kind, "namespace", namespace)
return "", false
}

return overwrittenName, true
}
if instConfig.Spec.ServiceName == "" {

func (k *K8sPodInfoResolver) getWorkloadObject(ctx context.Context, name string, kind string, namespace string) (metav1.Object, error) {
switch kind {
case "Deployment":
return k.kubeClient.AppsV1().Deployments(namespace).Get(ctx, name, metav1.GetOptions{})
case "StatefulSet":
return k.kubeClient.AppsV1().StatefulSets(namespace).Get(ctx, name, metav1.GetOptions{})
case "DaemonSet":
return k.kubeClient.AppsV1().DaemonSets(namespace).Get(ctx, name, metav1.GetOptions{})
case "Pod":
return k.kubeClient.CoreV1().Pods(namespace).Get(ctx, name, metav1.GetOptions{})
k.logger.Info("ServiceName is not specified in InstrumentationConfig, falling back to workload name", "name", name, "namespace", namespace)
return "", false
}

return nil, errors.New("failed to get workload object for kind: " + kind)
return instConfig.Spec.ServiceName, true
}

// Resolves the service name, with the following priority:
Expand All @@ -72,7 +60,7 @@ func (k *K8sPodInfoResolver) getWorkloadObject(ctx context.Context, name string,
func (k *K8sPodInfoResolver) ResolveServiceName(ctx context.Context, workloadName string, workloadKind string, containerDetails *ContainerDetails) string {

// we always fetch the fresh service name from the annotation to make sure the most up to date value is returned
serviceName, foundReportedName := k.getServiceNameFromAnnotation(ctx, workloadName, workloadKind, containerDetails.PodNamespace)
serviceName, foundReportedName := k.getServiceNameFromInstrumentationConfig(ctx, workloadName, workloadKind, containerDetails.PodNamespace)
if foundReportedName {
return serviceName
}
Expand Down

0 comments on commit 85deec2

Please sign in to comment.