Skip to content

Commit bd18dde

Browse files
committed
with object
Signed-off-by: Li Zhijian <[email protected]>
1 parent 796e47e commit bd18dde

File tree

4 files changed

+18
-13
lines changed

4 files changed

+18
-13
lines changed

pkg/controller/deployment/sync.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ func (dc *DeploymentController) sync(d *apps.Deployment, rsList []*apps.ReplicaS
7474
// that were paused for longer than progressDeadlineSeconds.
7575
func (dc *DeploymentController) checkPausedConditions(d *apps.Deployment) error {
7676
// Get span from annotations and set to ctx
77-
ctx := httptrace.SpanContextFromAnnotations(context.Background(), d.GetAnnotations())
77+
ctx := httptrace.SpanContextWithObject(context.Background(), d)
7878

7979
if !deploymentutil.HasProgressDeadline(d) {
8080
return nil
@@ -141,7 +141,7 @@ const (
141141
// Note that the pod-template-hash will be added to adopted RSes and pods.
142142
func (dc *DeploymentController) getNewReplicaSet(d *apps.Deployment, rsList, oldRSs []*apps.ReplicaSet, createIfNotExisted bool) (*apps.ReplicaSet, error) {
143143
// Get span from annotations and set to ctx
144-
ctx := httptrace.SpanContextFromAnnotations(context.Background(), d.GetAnnotations())
144+
ctx := httptrace.SpanContextWithObject(context.Background(), d)
145145

146146
existingNewRS := deploymentutil.FindNewReplicaSet(d, rsList)
147147

@@ -418,7 +418,7 @@ func (dc *DeploymentController) scaleReplicaSetAndRecordEvent(rs *apps.ReplicaSe
418418

419419
func (dc *DeploymentController) scaleReplicaSet(rs *apps.ReplicaSet, newScale int32, deployment *apps.Deployment, scalingOperation string) (bool, *apps.ReplicaSet, error) {
420420
// Get span from annotations and set to ctx
421-
ctx := httptrace.SpanContextFromAnnotations(context.Background(), rs.GetAnnotations())
421+
ctx := httptrace.SpanContextWithObject(context.Background(), rs)
422422

423423
sizeNeedsUpdate := *(rs.Spec.Replicas) != newScale
424424

@@ -444,7 +444,7 @@ func (dc *DeploymentController) scaleReplicaSet(rs *apps.ReplicaSet, newScale in
444444
// around by default 1) for historical reasons and 2) for the ability to rollback a deployment.
445445
func (dc *DeploymentController) cleanupDeployment(oldRSs []*apps.ReplicaSet, deployment *apps.Deployment) error {
446446
// Get span from annotations and set to ctx
447-
ctx := httptrace.SpanContextFromAnnotations(context.Background(), deployment.GetAnnotations())
447+
ctx := httptrace.SpanContextWithObject(context.Background(), deployment)
448448

449449
if !deploymentutil.HasRevisionHistoryLimit(deployment) {
450450
return nil
@@ -483,8 +483,8 @@ func (dc *DeploymentController) cleanupDeployment(oldRSs []*apps.ReplicaSet, dep
483483

484484
// syncDeploymentStatus checks if the status is up-to-date and sync it if necessary
485485
func (dc *DeploymentController) syncDeploymentStatus(allRSs []*apps.ReplicaSet, newRS *apps.ReplicaSet, d *apps.Deployment) error {
486-
// Get span from annotations and set to ctx
487-
ctx := httptrace.SpanContextFromAnnotations(context.Background(), d.GetAnnotations())
486+
// Get span and baggage from object and set to ctx
487+
ctx := httptrace.SpanContextWithObject(context.Background(), d)
488488

489489
newStatus := calculateStatus(allRSs, newRS, d)
490490

pkg/controller/replicaset/replica_set.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -541,7 +541,7 @@ func (rsc *ReplicaSetController) processNextWorkItem() bool {
541541
// It will requeue the replica set in case of an error while creating/deleting pods.
542542
func (rsc *ReplicaSetController) manageReplicas(filteredPods []*v1.Pod, rs *apps.ReplicaSet) error {
543543
// Get span from annotations and set to ctx
544-
ctx := httptrace.SpanContextFromAnnotations(context.Background(), rs.GetAnnotations())
544+
ctx := httptrace.SpanContextWithObject(context.Background(), rs)
545545

546546
diff := len(filteredPods) - int(*(rs.Spec.Replicas))
547547
rsKey, err := controller.KeyFunc(rs)
@@ -717,7 +717,7 @@ func (rsc *ReplicaSetController) syncReplicaSet(key string) error {
717717

718718
func (rsc *ReplicaSetController) claimPods(rs *apps.ReplicaSet, selector labels.Selector, filteredPods []*v1.Pod) ([]*v1.Pod, error) {
719719
// Get span from annotations and set to ctx
720-
ctx := httptrace.SpanContextFromAnnotations(context.Background(), rs.GetAnnotations())
720+
ctx := httptrace.SpanContextWithObject(context.Background(), rs)
721721

722722
// If any adoptions are attempted, we should first recheck for deletion with
723723
// an uncached quorum read sometime after listing Pods (see #42639).

pkg/controller/statefulset/stateful_pod_control.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ type realStatefulPodControl struct {
7575

7676
func (spc *realStatefulPodControl) CreateStatefulPod(set *apps.StatefulSet, pod *v1.Pod) error {
7777
// Get span from annotations and set to ctx
78-
ctx := httptrace.SpanContextFromAnnotations(context.Background(), set.GetAnnotations())
78+
ctx := httptrace.SpanContextWithObject(context.Background(), set)
7979

8080
// Create the Pod's PVCs prior to creating the Pod
8181
if err := spc.createPersistentVolumeClaims(set, pod); err != nil {
@@ -95,7 +95,7 @@ func (spc *realStatefulPodControl) CreateStatefulPod(set *apps.StatefulSet, pod
9595

9696
func (spc *realStatefulPodControl) UpdateStatefulPod(set *apps.StatefulSet, pod *v1.Pod) error {
9797
// Get span from annotations and set to ctx
98-
ctx := httptrace.SpanContextFromAnnotations(context.Background(), set.GetAnnotations())
98+
ctx := httptrace.SpanContextWithObject(context.Background(), set)
9999

100100
attemptedUpdate := false
101101
err := retry.RetryOnConflict(retry.DefaultBackoff, func() error {
@@ -145,7 +145,7 @@ func (spc *realStatefulPodControl) UpdateStatefulPod(set *apps.StatefulSet, pod
145145

146146
func (spc *realStatefulPodControl) DeleteStatefulPod(set *apps.StatefulSet, pod *v1.Pod) error {
147147
// Get span from annotations and set to ctx
148-
ctx := httptrace.SpanContextFromAnnotations(context.Background(), set.GetAnnotations())
148+
ctx := httptrace.SpanContextWithObject(context.Background(), set)
149149

150150
err := spc.client.CoreV1().Pods(set.Namespace).Delete(ctx, pod.Name, metav1.DeleteOptions{})
151151
spc.recordPodEvent("delete", set, pod, err)
@@ -191,7 +191,7 @@ func (spc *realStatefulPodControl) recordClaimEvent(verb string, set *apps.State
191191
// set's Spec.
192192
func (spc *realStatefulPodControl) createPersistentVolumeClaims(set *apps.StatefulSet, pod *v1.Pod) error {
193193
// Get span from annotations and set to ctx
194-
ctx := httptrace.SpanContextFromAnnotations(context.Background(), set.GetAnnotations())
194+
ctx := httptrace.SpanContextWithObject(context.Background(), set)
195195

196196
var errs []error
197197
for _, claim := range getPersistentVolumeClaims(set, pod) {

pkg/util/httptrace/httptrace.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ import (
2525
"go.opentelemetry.io/otel"
2626
apitrace "go.opentelemetry.io/otel/api/trace"
2727
"go.opentelemetry.io/otel/label"
28-
"go.opentelemetry.io/otel/propagators"
28+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2929
)
3030

3131
type contextKeyType int
@@ -38,6 +38,11 @@ const spanContextAnnotationKey string = "trace.kubernetes.io.span.context"
3838

3939
const initialTraceIDBaggageKey label.Key = "Initial-Trace-Id"
4040

41+
// SpanContextWithObject returns a context.Context with Spacn and Baggage from the passed object
42+
func SpanContextWithObject(ctx context.Context, meta metav1.Object) context.Context {
43+
return SpanContextFromAnnotations(ctx, meta.GetAnnotations())
44+
}
45+
4146
// SpanContextFromAnnotations get span context from annotations
4247
func SpanContextFromAnnotations(ctx context.Context, annotations map[string]string) context.Context {
4348
// get init trace id from annotations

0 commit comments

Comments
 (0)