Skip to content

Commit 7d98ac5

Browse files
committed
Trace by opentelemetry
Signed-off-by: zouyu <[email protected]>
1 parent 901911d commit 7d98ac5

File tree

10 files changed

+202
-46
lines changed

10 files changed

+202
-46
lines changed

pkg/controller/controller_utils.go

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ import (
4848
_ "k8s.io/kubernetes/pkg/apis/core/install"
4949
"k8s.io/kubernetes/pkg/apis/core/validation"
5050
hashutil "k8s.io/kubernetes/pkg/util/hash"
51+
"k8s.io/kubernetes/pkg/util/httptrace"
5152
taintutils "k8s.io/kubernetes/pkg/util/taints"
5253
"k8s.io/utils/integer"
5354

@@ -448,12 +449,12 @@ func (r RealControllerRevisionControl) PatchControllerRevision(namespace, name s
448449
// created as an interface to allow testing.
449450
type PodControlInterface interface {
450451
// CreatePods creates new pods according to the spec.
451-
CreatePods(namespace string, template *v1.PodTemplateSpec, object runtime.Object) error
452+
CreatePods(ctx context.Context, namespace string, template *v1.PodTemplateSpec, object runtime.Object) error
452453
// CreatePodsOnNode creates a new pod according to the spec on the specified node,
453454
// and sets the ControllerRef.
454-
CreatePodsOnNode(nodeName, namespace string, template *v1.PodTemplateSpec, object runtime.Object, controllerRef *metav1.OwnerReference) error
455+
CreatePodsOnNode(ctx context.Context, nodeName, namespace string, template *v1.PodTemplateSpec, object runtime.Object, controllerRef *metav1.OwnerReference) error
455456
// CreatePodsWithControllerRef creates new pods according to the spec, and sets object as the pod's controller.
456-
CreatePodsWithControllerRef(namespace string, template *v1.PodTemplateSpec, object runtime.Object, controllerRef *metav1.OwnerReference) error
457+
CreatePodsWithControllerRef(ctx context.Context, namespace string, template *v1.PodTemplateSpec, object runtime.Object, controllerRef *metav1.OwnerReference) error
457458
// DeletePod deletes the pod identified by podID.
458459
DeletePod(namespace string, podID string, object runtime.Object) error
459460
// PatchPod patches the pod.
@@ -518,22 +519,22 @@ func validateControllerRef(controllerRef *metav1.OwnerReference) error {
518519
return nil
519520
}
520521

521-
func (r RealPodControl) CreatePods(namespace string, template *v1.PodTemplateSpec, object runtime.Object) error {
522-
return r.createPods("", namespace, template, object, nil)
522+
func (r RealPodControl) CreatePods(ctx context.Context, namespace string, template *v1.PodTemplateSpec, object runtime.Object) error {
523+
return r.createPods(ctx, "", namespace, template, object, nil)
523524
}
524525

525-
func (r RealPodControl) CreatePodsWithControllerRef(namespace string, template *v1.PodTemplateSpec, controllerObject runtime.Object, controllerRef *metav1.OwnerReference) error {
526+
func (r RealPodControl) CreatePodsWithControllerRef(ctx context.Context, namespace string, template *v1.PodTemplateSpec, controllerObject runtime.Object, controllerRef *metav1.OwnerReference) error {
526527
if err := validateControllerRef(controllerRef); err != nil {
527528
return err
528529
}
529-
return r.createPods("", namespace, template, controllerObject, controllerRef)
530+
return r.createPods(ctx, "", namespace, template, controllerObject, controllerRef)
530531
}
531532

532-
func (r RealPodControl) CreatePodsOnNode(nodeName, namespace string, template *v1.PodTemplateSpec, object runtime.Object, controllerRef *metav1.OwnerReference) error {
533+
func (r RealPodControl) CreatePodsOnNode(ctx context.Context, nodeName, namespace string, template *v1.PodTemplateSpec, object runtime.Object, controllerRef *metav1.OwnerReference) error {
533534
if err := validateControllerRef(controllerRef); err != nil {
534535
return err
535536
}
536-
return r.createPods(nodeName, namespace, template, object, controllerRef)
537+
return r.createPods(ctx, nodeName, namespace, template, object, controllerRef)
537538
}
538539

539540
func (r RealPodControl) PatchPod(namespace, name string, data []byte) error {
@@ -566,7 +567,7 @@ func GetPodFromTemplate(template *v1.PodTemplateSpec, parentObject runtime.Objec
566567
return pod, nil
567568
}
568569

569-
func (r RealPodControl) createPods(nodeName, namespace string, template *v1.PodTemplateSpec, object runtime.Object, controllerRef *metav1.OwnerReference) error {
570+
func (r RealPodControl) createPods(ctx context.Context, nodeName, namespace string, template *v1.PodTemplateSpec, object runtime.Object, controllerRef *metav1.OwnerReference) error {
570571
pod, err := GetPodFromTemplate(template, object, controllerRef)
571572
if err != nil {
572573
return err
@@ -577,7 +578,8 @@ func (r RealPodControl) createPods(nodeName, namespace string, template *v1.PodT
577578
if len(labels.Set(pod.Labels)) == 0 {
578579
return fmt.Errorf("unable to create pods, no labels")
579580
}
580-
newPod, err := r.KubeClient.CoreV1().Pods(namespace).Create(context.TODO(), pod, metav1.CreateOptions{})
581+
httptrace.SpanContextToAnnotations(ctx, &pod.Annotations)
582+
newPod, err := r.KubeClient.CoreV1().Pods(namespace).Create(ctx, pod, metav1.CreateOptions{})
581583
if err != nil {
582584
// only send an event if the namespace isn't terminating
583585
if !apierrors.HasStatusCause(err, v1.NamespaceTerminatingCause) {
@@ -638,7 +640,7 @@ func (f *FakePodControl) PatchPod(namespace, name string, data []byte) error {
638640
return nil
639641
}
640642

641-
func (f *FakePodControl) CreatePods(namespace string, spec *v1.PodTemplateSpec, object runtime.Object) error {
643+
func (f *FakePodControl) CreatePods(ctx context.Context, namespace string, spec *v1.PodTemplateSpec, object runtime.Object) error {
642644
f.Lock()
643645
defer f.Unlock()
644646
f.CreateCallCount++
@@ -652,7 +654,7 @@ func (f *FakePodControl) CreatePods(namespace string, spec *v1.PodTemplateSpec,
652654
return nil
653655
}
654656

655-
func (f *FakePodControl) CreatePodsWithControllerRef(namespace string, spec *v1.PodTemplateSpec, object runtime.Object, controllerRef *metav1.OwnerReference) error {
657+
func (f *FakePodControl) CreatePodsWithControllerRef(ctx context.Context, namespace string, spec *v1.PodTemplateSpec, object runtime.Object, controllerRef *metav1.OwnerReference) error {
656658
f.Lock()
657659
defer f.Unlock()
658660
f.CreateCallCount++
@@ -667,7 +669,7 @@ func (f *FakePodControl) CreatePodsWithControllerRef(namespace string, spec *v1.
667669
return nil
668670
}
669671

670-
func (f *FakePodControl) CreatePodsOnNode(nodeName, namespace string, template *v1.PodTemplateSpec, object runtime.Object, controllerRef *metav1.OwnerReference) error {
672+
func (f *FakePodControl) CreatePodsOnNode(ctx context.Context, nodeName, namespace string, template *v1.PodTemplateSpec, object runtime.Object, controllerRef *metav1.OwnerReference) error {
671673
f.Lock()
672674
defer f.Unlock()
673675
f.CreateCallCount++

pkg/controller/controller_utils_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -298,7 +298,7 @@ func TestCreatePods(t *testing.T) {
298298
controllerSpec := newReplicationController(1)
299299

300300
// Make sure createReplica sends a POST to the apiserver with a pod from the controllers pod template
301-
err := podControl.CreatePods(ns, controllerSpec.Spec.Template, controllerSpec)
301+
err := podControl.CreatePods(context.Background(), ns, controllerSpec.Spec.Template, controllerSpec)
302302
assert.NoError(t, err, "unexpected error: %v", err)
303303

304304
expectedPod := v1.Pod{

pkg/controller/daemon/daemon_controller.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -968,7 +968,7 @@ func (dsc *DaemonSetsController) syncNodes(ds *apps.DaemonSet, podsToDelete, nod
968968
podTemplate.Spec.Affinity = util.ReplaceDaemonSetPodNodeNameNodeAffinity(
969969
podTemplate.Spec.Affinity, nodesNeedingDaemonPods[ix])
970970

971-
err := dsc.podControl.CreatePodsWithControllerRef(ds.Namespace, podTemplate,
971+
err := dsc.podControl.CreatePodsWithControllerRef(context.Background(), ds.Namespace, podTemplate,
972972
ds, metav1.NewControllerRef(ds, controllerKind))
973973

974974
if err != nil {

pkg/controller/daemon/daemon_controller_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -236,10 +236,10 @@ func newFakePodControl() *fakePodControl {
236236
}
237237
}
238238

239-
func (f *fakePodControl) CreatePodsOnNode(nodeName, namespace string, template *v1.PodTemplateSpec, object runtime.Object, controllerRef *metav1.OwnerReference) error {
239+
func (f *fakePodControl) CreatePodsOnNode(ctx context.Context, nodeName, namespace string, template *v1.PodTemplateSpec, object runtime.Object, controllerRef *metav1.OwnerReference) error {
240240
f.Lock()
241241
defer f.Unlock()
242-
if err := f.FakePodControl.CreatePodsOnNode(nodeName, namespace, template, object, controllerRef); err != nil {
242+
if err := f.FakePodControl.CreatePodsOnNode(context.Background(), nodeName, namespace, template, object, controllerRef); err != nil {
243243
return fmt.Errorf("failed to create pod on node %q", nodeName)
244244
}
245245

@@ -267,10 +267,10 @@ func (f *fakePodControl) CreatePodsOnNode(nodeName, namespace string, template *
267267
return nil
268268
}
269269

270-
func (f *fakePodControl) CreatePodsWithControllerRef(namespace string, template *v1.PodTemplateSpec, object runtime.Object, controllerRef *metav1.OwnerReference) error {
270+
func (f *fakePodControl) CreatePodsWithControllerRef(ctx context.Context, namespace string, template *v1.PodTemplateSpec, object runtime.Object, controllerRef *metav1.OwnerReference) error {
271271
f.Lock()
272272
defer f.Unlock()
273-
if err := f.FakePodControl.CreatePodsWithControllerRef(namespace, template, object, controllerRef); err != nil {
273+
if err := f.FakePodControl.CreatePodsWithControllerRef(context.Background(), namespace, template, object, controllerRef); err != nil {
274274
return fmt.Errorf("failed to create pod for DaemonSet")
275275
}
276276

pkg/controller/deployment/sync.go

Lines changed: 27 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,13 @@ import (
2424
"strconv"
2525

2626
apps "k8s.io/api/apps/v1"
27-
"k8s.io/api/core/v1"
27+
v1 "k8s.io/api/core/v1"
2828
"k8s.io/apimachinery/pkg/api/errors"
2929
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
3030
"k8s.io/klog/v2"
3131
"k8s.io/kubernetes/pkg/controller"
3232
deploymentutil "k8s.io/kubernetes/pkg/controller/deployment/util"
33+
"k8s.io/kubernetes/pkg/util/httptrace"
3334
labelsutil "k8s.io/kubernetes/pkg/util/labels"
3435
)
3536

@@ -72,6 +73,9 @@ func (dc *DeploymentController) sync(d *apps.Deployment, rsList []*apps.ReplicaS
7273
// These conditions are needed so that we won't accidentally report lack of progress for resumed deployments
7374
// that were paused for longer than progressDeadlineSeconds.
7475
func (dc *DeploymentController) checkPausedConditions(d *apps.Deployment) error {
76+
// Get span from annotations and set to ctx
77+
ctx := httptrace.SpanContextFromAnnotations(context.Background(), d.GetAnnotations())
78+
7579
if !deploymentutil.HasProgressDeadline(d) {
7680
return nil
7781
}
@@ -98,7 +102,7 @@ func (dc *DeploymentController) checkPausedConditions(d *apps.Deployment) error
98102
}
99103

100104
var err error
101-
_, err = dc.client.AppsV1().Deployments(d.Namespace).UpdateStatus(context.TODO(), d, metav1.UpdateOptions{})
105+
_, err = dc.client.AppsV1().Deployments(d.Namespace).UpdateStatus(ctx, d, metav1.UpdateOptions{})
102106
return err
103107
}
104108

@@ -136,6 +140,9 @@ const (
136140
// 3. If there's no existing new RS and createIfNotExisted is true, create one with appropriate revision number (maxOldRevision + 1) and replicas.
137141
// Note that the pod-template-hash will be added to adopted RSes and pods.
138142
func (dc *DeploymentController) getNewReplicaSet(d *apps.Deployment, rsList, oldRSs []*apps.ReplicaSet, createIfNotExisted bool) (*apps.ReplicaSet, error) {
143+
// Get span from annotations and set to ctx
144+
ctx := httptrace.SpanContextFromAnnotations(context.Background(), d.GetAnnotations())
145+
139146
existingNewRS := deploymentutil.FindNewReplicaSet(d, rsList)
140147

141148
// Calculate the max revision number among all old RSes
@@ -155,7 +162,7 @@ func (dc *DeploymentController) getNewReplicaSet(d *apps.Deployment, rsList, old
155162
minReadySecondsNeedsUpdate := rsCopy.Spec.MinReadySeconds != d.Spec.MinReadySeconds
156163
if annotationsUpdated || minReadySecondsNeedsUpdate {
157164
rsCopy.Spec.MinReadySeconds = d.Spec.MinReadySeconds
158-
return dc.client.AppsV1().ReplicaSets(rsCopy.ObjectMeta.Namespace).Update(context.TODO(), rsCopy, metav1.UpdateOptions{})
165+
return dc.client.AppsV1().ReplicaSets(rsCopy.ObjectMeta.Namespace).Update(ctx, rsCopy, metav1.UpdateOptions{})
159166
}
160167

161168
// Should use the revision in existingNewRS's annotation, since it set by before
@@ -173,7 +180,7 @@ func (dc *DeploymentController) getNewReplicaSet(d *apps.Deployment, rsList, old
173180

174181
if needsUpdate {
175182
var err error
176-
if _, err = dc.client.AppsV1().Deployments(d.Namespace).UpdateStatus(context.TODO(), d, metav1.UpdateOptions{}); err != nil {
183+
if _, err = dc.client.AppsV1().Deployments(d.Namespace).UpdateStatus(ctx, d, metav1.UpdateOptions{}); err != nil {
177184
return nil, err
178185
}
179186
}
@@ -220,7 +227,8 @@ func (dc *DeploymentController) getNewReplicaSet(d *apps.Deployment, rsList, old
220227
// hash collisions. If there is any other error, we need to report it in the status of
221228
// the Deployment.
222229
alreadyExists := false
223-
createdRS, err := dc.client.AppsV1().ReplicaSets(d.Namespace).Create(context.TODO(), &newRS, metav1.CreateOptions{})
230+
231+
createdRS, err := dc.client.AppsV1().ReplicaSets(d.Namespace).Create(ctx, &newRS, metav1.CreateOptions{})
224232
switch {
225233
// We may end up hitting this due to a slow cache or a fast resync of the Deployment.
226234
case errors.IsAlreadyExists(err):
@@ -252,7 +260,7 @@ func (dc *DeploymentController) getNewReplicaSet(d *apps.Deployment, rsList, old
252260
*d.Status.CollisionCount++
253261
// Update the collisionCount for the Deployment and let it requeue by returning the original
254262
// error.
255-
_, dErr := dc.client.AppsV1().Deployments(d.Namespace).UpdateStatus(context.TODO(), d, metav1.UpdateOptions{})
263+
_, dErr := dc.client.AppsV1().Deployments(d.Namespace).UpdateStatus(ctx, d, metav1.UpdateOptions{})
256264
if dErr == nil {
257265
klog.V(2).Infof("Found a hash collision for deployment %q - bumping collisionCount (%d->%d) to resolve it", d.Name, preCollisionCount, *d.Status.CollisionCount)
258266
}
@@ -268,7 +276,7 @@ func (dc *DeploymentController) getNewReplicaSet(d *apps.Deployment, rsList, old
268276
// We don't really care about this error at this point, since we have a bigger issue to report.
269277
// TODO: Identify which errors are permanent and switch DeploymentIsFailed to take into account
270278
// these reasons as well. Related issue: https://github.com/kubernetes/kubernetes/issues/18568
271-
_, _ = dc.client.AppsV1().Deployments(d.Namespace).UpdateStatus(context.TODO(), d, metav1.UpdateOptions{})
279+
_, _ = dc.client.AppsV1().Deployments(d.Namespace).UpdateStatus(ctx, d, metav1.UpdateOptions{})
272280
}
273281
dc.eventRecorder.Eventf(d, v1.EventTypeWarning, deploymentutil.FailedRSCreateReason, msg)
274282
return nil, err
@@ -285,7 +293,7 @@ func (dc *DeploymentController) getNewReplicaSet(d *apps.Deployment, rsList, old
285293
needsUpdate = true
286294
}
287295
if needsUpdate {
288-
_, err = dc.client.AppsV1().Deployments(d.Namespace).UpdateStatus(context.TODO(), d, metav1.UpdateOptions{})
296+
_, err = dc.client.AppsV1().Deployments(d.Namespace).UpdateStatus(ctx, d, metav1.UpdateOptions{})
289297
}
290298
return createdRS, err
291299
}
@@ -409,6 +417,8 @@ func (dc *DeploymentController) scaleReplicaSetAndRecordEvent(rs *apps.ReplicaSe
409417
}
410418

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

413423
sizeNeedsUpdate := *(rs.Spec.Replicas) != newScale
414424

@@ -420,7 +430,7 @@ func (dc *DeploymentController) scaleReplicaSet(rs *apps.ReplicaSet, newScale in
420430
rsCopy := rs.DeepCopy()
421431
*(rsCopy.Spec.Replicas) = newScale
422432
deploymentutil.SetReplicasAnnotations(rsCopy, *(deployment.Spec.Replicas), *(deployment.Spec.Replicas)+deploymentutil.MaxSurge(*deployment))
423-
rs, err = dc.client.AppsV1().ReplicaSets(rsCopy.Namespace).Update(context.TODO(), rsCopy, metav1.UpdateOptions{})
433+
rs, err = dc.client.AppsV1().ReplicaSets(rsCopy.Namespace).Update(ctx, rsCopy, metav1.UpdateOptions{})
424434
if err == nil && sizeNeedsUpdate {
425435
scaled = true
426436
dc.eventRecorder.Eventf(deployment, v1.EventTypeNormal, "ScalingReplicaSet", "Scaled %s replica set %s to %d", scalingOperation, rs.Name, newScale)
@@ -433,6 +443,9 @@ func (dc *DeploymentController) scaleReplicaSet(rs *apps.ReplicaSet, newScale in
433443
// where N=d.Spec.RevisionHistoryLimit. Old replica sets are older versions of the podtemplate of a deployment kept
434444
// around by default 1) for historical reasons and 2) for the ability to rollback a deployment.
435445
func (dc *DeploymentController) cleanupDeployment(oldRSs []*apps.ReplicaSet, deployment *apps.Deployment) error {
446+
// Get span from annotations and set to ctx
447+
ctx := httptrace.SpanContextFromAnnotations(context.Background(), deployment.GetAnnotations())
448+
436449
if !deploymentutil.HasRevisionHistoryLimit(deployment) {
437450
return nil
438451
}
@@ -458,7 +471,7 @@ func (dc *DeploymentController) cleanupDeployment(oldRSs []*apps.ReplicaSet, dep
458471
continue
459472
}
460473
klog.V(4).Infof("Trying to cleanup replica set %q for deployment %q", rs.Name, deployment.Name)
461-
if err := dc.client.AppsV1().ReplicaSets(rs.Namespace).Delete(context.TODO(), rs.Name, metav1.DeleteOptions{}); err != nil && !errors.IsNotFound(err) {
474+
if err := dc.client.AppsV1().ReplicaSets(rs.Namespace).Delete(ctx, rs.Name, metav1.DeleteOptions{}); err != nil && !errors.IsNotFound(err) {
462475
// Return error instead of aggregating and continuing DELETEs on the theory
463476
// that we may be overloading the api server.
464477
return err
@@ -470,6 +483,9 @@ func (dc *DeploymentController) cleanupDeployment(oldRSs []*apps.ReplicaSet, dep
470483

471484
// syncDeploymentStatus checks if the status is up-to-date and sync it if necessary
472485
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())
488+
473489
newStatus := calculateStatus(allRSs, newRS, d)
474490

475491
if reflect.DeepEqual(d.Status, newStatus) {
@@ -478,7 +494,7 @@ func (dc *DeploymentController) syncDeploymentStatus(allRSs []*apps.ReplicaSet,
478494

479495
newDeployment := d
480496
newDeployment.Status = newStatus
481-
_, err := dc.client.AppsV1().Deployments(newDeployment.Namespace).UpdateStatus(context.TODO(), newDeployment, metav1.UpdateOptions{})
497+
_, err := dc.client.AppsV1().Deployments(newDeployment.Namespace).UpdateStatus(ctx, newDeployment, metav1.UpdateOptions{})
482498
return err
483499
}
484500

pkg/controller/job/job_controller.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ import (
2626
"time"
2727

2828
batch "k8s.io/api/batch/v1"
29-
"k8s.io/api/core/v1"
29+
v1 "k8s.io/api/core/v1"
3030
"k8s.io/apimachinery/pkg/api/errors"
3131
apierrors "k8s.io/apimachinery/pkg/api/errors"
3232
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
@@ -776,7 +776,7 @@ func (jm *Controller) manageJob(activePods []*v1.Pod, succeeded int32, job *batc
776776
for i := int32(0); i < batchSize; i++ {
777777
go func() {
778778
defer wait.Done()
779-
err := jm.podControl.CreatePodsWithControllerRef(job.Namespace, &job.Spec.Template, job, metav1.NewControllerRef(job, controllerKind))
779+
err := jm.podControl.CreatePodsWithControllerRef(context.Background(), job.Namespace, &job.Spec.Template, job, metav1.NewControllerRef(job, controllerKind))
780780
if err != nil {
781781
if errors.HasStatusCause(err, v1.NamespaceTerminatingCause) {
782782
// If the namespace is being torn down, we can safely ignore

0 commit comments

Comments
 (0)