Skip to content

Commit

Permalink
Fix Cronjob name
Browse files Browse the repository at this point in the history
Signed-off-by: Janario Oliveira <[email protected]>
  • Loading branch information
janario committed Mar 23, 2024
1 parent 78e7c2f commit d53ff48
Showing 1 changed file with 23 additions and 2 deletions.
25 changes: 23 additions & 2 deletions pkg/instrumentation/sdk.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import (
"go.opentelemetry.io/otel/attribute"
semconv "go.opentelemetry.io/otel/semconv/v1.7.0"
appsv1 "k8s.io/api/apps/v1"
batchv1 "k8s.io/api/batch/v1"
corev1 "k8s.io/api/core/v1"
apierrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
Expand Down Expand Up @@ -380,10 +381,10 @@ func chooseServiceName(pod corev1.Pod, resources map[string]string, index int) s
if name := resources[string(semconv.K8SDaemonSetNameKey)]; name != "" {
return name
}
if name := resources[string(semconv.K8SJobNameKey)]; name != "" {
if name := resources[string(semconv.K8SCronJobNameKey)]; name != "" {
return name
}
if name := resources[string(semconv.K8SCronJobNameKey)]; name != "" {
if name := resources[string(semconv.K8SJobNameKey)]; name != "" {
return name
}
if name := resources[string(semconv.K8SPodNameKey)]; name != "" {
Expand Down Expand Up @@ -502,6 +503,26 @@ func (i *sdkInjector) addParentResourceLabels(ctx context.Context, uid bool, ns
if uid {
resources[semconv.K8SJobUIDKey] = string(owner.UID)
}

// parent of Job can be CronJob which we are interested to know
j := batchv1.Job{}
nsn := types.NamespacedName{Namespace: ns.Name, Name: owner.Name}
backOff := wait.Backoff{Duration: 10 * time.Millisecond, Factor: 1.5, Jitter: 0.1, Steps: 20, Cap: 2 * time.Second}

checkError := func(err error) bool {
return apierrors.IsNotFound(err)
}

getJob := func() error {
return i.client.Get(ctx, nsn, &j)
}

// use a retry loop to get the Job. A single call to client.get fails occasionally
err := retry.OnError(backOff, checkError, getJob)
if err != nil {
i.logger.Error(err, "failed to get job", "job", nsn.Name, "namespace", nsn.Namespace)
}
i.addParentResourceLabels(ctx, uid, ns, j.ObjectMeta, resources)
case "cronjob":
resources[semconv.K8SCronJobNameKey] = owner.Name
if uid {
Expand Down

0 comments on commit d53ff48

Please sign in to comment.