Skip to content

Commit 35d8891

Browse files
authored
Fix Cronjob name (#2717)
* Fix Cronjob name Signed-off-by: Janario Oliveira <[email protected]> * Mapped Job permissions Signed-off-by: Janario Oliveira <[email protected]> * Add changelog Signed-off-by: Janario Oliveira <[email protected]> * Add Job rbac permissions Signed-off-by: Janario Oliveira <[email protected]> * Add e2e test case Signed-off-by: Janario Oliveira <[email protected]> * Parent resources unit test Signed-off-by: Janario Oliveira <[email protected]> * ChooseServiceName unit test Signed-off-by: Janario Oliveira <[email protected]> * Generated manifest Signed-off-by: Janario Oliveira <[email protected]> * Rename e2e Signed-off-by: Janario Oliveira <[email protected]> * Fix e2e tests Signed-off-by: Janario Oliveira <[email protected]> * Fix lint Signed-off-by: Janario Oliveira <[email protected]> --------- Signed-off-by: Janario Oliveira <[email protected]>
1 parent 57024fc commit 35d8891

15 files changed

+654
-3
lines changed

.chloggen/cronjob-service-name.yaml

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix'
2+
change_type: enhancement
3+
4+
# The name of the component, or a single word describing the area of concern, (e.g. collector, target allocator, auto-instrumentation, opamp, github action)
5+
component: auto-instrumentation
6+
7+
# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
8+
note: CronJob name propagated to the OTEL Service Name
9+
10+
# One or more tracking issues related to the change
11+
issues: [2716]
12+
13+
# (Optional) One or more lines of additional information to render under the primary note.
14+
# These lines will be padded with 2 spaces and then inserted directly into the document.
15+
# Use pipe (|) for multiline entries.
16+
subtext: |
17+
When instrumenting a `CronJob` it would generate a `Job` and then a `Pod`.
18+
Previously it would use the `Job` name as the Service name, generating too many different services on each cron execution.
19+
This release fixed to collect the `Job` parent's name, if it has one.

bundle/manifests/opentelemetry-operator.clusterserviceversion.yaml

+9-1
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ metadata:
6565
categories: Logging & Tracing,Monitoring
6666
certified: "false"
6767
containerImage: ghcr.io/open-telemetry/opentelemetry-operator/opentelemetry-operator
68-
createdAt: "2024-03-25T08:38:05Z"
68+
createdAt: "2024-04-02T20:37:56Z"
6969
description: Provides the OpenTelemetry components, including the Collector
7070
operators.operatorframework.io/builder: operator-sdk-v1.29.0
7171
operators.operatorframework.io/project_layout: go.kubebuilder.io/v3
@@ -236,6 +236,14 @@ spec:
236236
- patch
237237
- update
238238
- watch
239+
- apiGroups:
240+
- batch
241+
resources:
242+
- jobs
243+
verbs:
244+
- get
245+
- list
246+
- watch
239247
- apiGroups:
240248
- config.openshift.io
241249
resources:

config/rbac/role.yaml

+8
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,14 @@ rules:
6969
- patch
7070
- update
7171
- watch
72+
- apiGroups:
73+
- batch
74+
resources:
75+
- jobs
76+
verbs:
77+
- get
78+
- list
79+
- watch
7280
- apiGroups:
7381
- config.openshift.io
7482
resources:

internal/webhook/podmutation/webhookhandler.go

+1
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ import (
3434
// +kubebuilder:rbac:groups=opentelemetry.io,resources=opentelemetrycollectors,verbs=get;list;watch
3535
// +kubebuilder:rbac:groups=opentelemetry.io,resources=instrumentations,verbs=get;list;watch
3636
// +kubebuilder:rbac:groups="apps",resources=replicasets,verbs=get;list;watch
37+
// +kubebuilder:rbac:groups="batch",resources=jobs,verbs=get;list;watch
3738

3839
var _ WebhookHandler = (*podMutationWebhook)(nil)
3940

pkg/instrumentation/sdk.go

+26-2
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ import (
3131
"go.opentelemetry.io/otel/attribute"
3232
semconv "go.opentelemetry.io/otel/semconv/v1.7.0"
3333
appsv1 "k8s.io/api/apps/v1"
34+
batchv1 "k8s.io/api/batch/v1"
3435
corev1 "k8s.io/api/core/v1"
3536
apierrors "k8s.io/apimachinery/pkg/api/errors"
3637
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
@@ -398,16 +399,19 @@ func chooseServiceName(pod corev1.Pod, resources map[string]string, index int) s
398399
if name := resources[string(semconv.K8SDeploymentNameKey)]; name != "" {
399400
return name
400401
}
402+
if name := resources[string(semconv.K8SReplicaSetNameKey)]; name != "" {
403+
return name
404+
}
401405
if name := resources[string(semconv.K8SStatefulSetNameKey)]; name != "" {
402406
return name
403407
}
404408
if name := resources[string(semconv.K8SDaemonSetNameKey)]; name != "" {
405409
return name
406410
}
407-
if name := resources[string(semconv.K8SJobNameKey)]; name != "" {
411+
if name := resources[string(semconv.K8SCronJobNameKey)]; name != "" {
408412
return name
409413
}
410-
if name := resources[string(semconv.K8SCronJobNameKey)]; name != "" {
414+
if name := resources[string(semconv.K8SJobNameKey)]; name != "" {
411415
return name
412416
}
413417
if name := resources[string(semconv.K8SPodNameKey)]; name != "" {
@@ -526,6 +530,26 @@ func (i *sdkInjector) addParentResourceLabels(ctx context.Context, uid bool, ns
526530
if uid {
527531
resources[semconv.K8SJobUIDKey] = string(owner.UID)
528532
}
533+
534+
// parent of Job can be CronJob which we are interested to know
535+
j := batchv1.Job{}
536+
nsn := types.NamespacedName{Namespace: ns.Name, Name: owner.Name}
537+
backOff := wait.Backoff{Duration: 10 * time.Millisecond, Factor: 1.5, Jitter: 0.1, Steps: 20, Cap: 2 * time.Second}
538+
539+
checkError := func(err error) bool {
540+
return apierrors.IsNotFound(err)
541+
}
542+
543+
getJob := func() error {
544+
return i.client.Get(ctx, nsn, &j)
545+
}
546+
547+
// use a retry loop to get the Job. A single call to client.get fails occasionally
548+
err := retry.OnError(backOff, checkError, getJob)
549+
if err != nil {
550+
i.logger.Error(err, "failed to get job", "job", nsn.Name, "namespace", nsn.Namespace)
551+
}
552+
i.addParentResourceLabels(ctx, uid, ns, j.ObjectMeta, resources)
529553
case "cronjob":
530554
resources[semconv.K8SCronJobNameKey] = owner.Name
531555
if uid {

0 commit comments

Comments
 (0)