@@ -24,12 +24,13 @@ import (
24
24
"strconv"
25
25
26
26
apps "k8s.io/api/apps/v1"
27
- "k8s.io/api/core/v1"
27
+ v1 "k8s.io/api/core/v1"
28
28
"k8s.io/apimachinery/pkg/api/errors"
29
29
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
30
30
"k8s.io/klog/v2"
31
31
"k8s.io/kubernetes/pkg/controller"
32
32
deploymentutil "k8s.io/kubernetes/pkg/controller/deployment/util"
33
+ "k8s.io/kubernetes/pkg/util/httptrace"
33
34
labelsutil "k8s.io/kubernetes/pkg/util/labels"
34
35
)
35
36
@@ -72,6 +73,9 @@ func (dc *DeploymentController) sync(d *apps.Deployment, rsList []*apps.ReplicaS
72
73
// These conditions are needed so that we won't accidentally report lack of progress for resumed deployments
73
74
// that were paused for longer than progressDeadlineSeconds.
74
75
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
+
75
79
if ! deploymentutil .HasProgressDeadline (d ) {
76
80
return nil
77
81
}
@@ -98,7 +102,7 @@ func (dc *DeploymentController) checkPausedConditions(d *apps.Deployment) error
98
102
}
99
103
100
104
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 {})
102
106
return err
103
107
}
104
108
@@ -136,6 +140,9 @@ const (
136
140
// 3. If there's no existing new RS and createIfNotExisted is true, create one with appropriate revision number (maxOldRevision + 1) and replicas.
137
141
// Note that the pod-template-hash will be added to adopted RSes and pods.
138
142
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
+
139
146
existingNewRS := deploymentutil .FindNewReplicaSet (d , rsList )
140
147
141
148
// Calculate the max revision number among all old RSes
@@ -155,7 +162,7 @@ func (dc *DeploymentController) getNewReplicaSet(d *apps.Deployment, rsList, old
155
162
minReadySecondsNeedsUpdate := rsCopy .Spec .MinReadySeconds != d .Spec .MinReadySeconds
156
163
if annotationsUpdated || minReadySecondsNeedsUpdate {
157
164
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 {})
159
166
}
160
167
161
168
// 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
173
180
174
181
if needsUpdate {
175
182
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 {
177
184
return nil , err
178
185
}
179
186
}
@@ -220,7 +227,8 @@ func (dc *DeploymentController) getNewReplicaSet(d *apps.Deployment, rsList, old
220
227
// hash collisions. If there is any other error, we need to report it in the status of
221
228
// the Deployment.
222
229
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 {})
224
232
switch {
225
233
// We may end up hitting this due to a slow cache or a fast resync of the Deployment.
226
234
case errors .IsAlreadyExists (err ):
@@ -252,7 +260,7 @@ func (dc *DeploymentController) getNewReplicaSet(d *apps.Deployment, rsList, old
252
260
* d .Status .CollisionCount ++
253
261
// Update the collisionCount for the Deployment and let it requeue by returning the original
254
262
// 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 {})
256
264
if dErr == nil {
257
265
klog .V (2 ).Infof ("Found a hash collision for deployment %q - bumping collisionCount (%d->%d) to resolve it" , d .Name , preCollisionCount , * d .Status .CollisionCount )
258
266
}
@@ -268,7 +276,7 @@ func (dc *DeploymentController) getNewReplicaSet(d *apps.Deployment, rsList, old
268
276
// We don't really care about this error at this point, since we have a bigger issue to report.
269
277
// TODO: Identify which errors are permanent and switch DeploymentIsFailed to take into account
270
278
// 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 {})
272
280
}
273
281
dc .eventRecorder .Eventf (d , v1 .EventTypeWarning , deploymentutil .FailedRSCreateReason , msg )
274
282
return nil , err
@@ -285,7 +293,7 @@ func (dc *DeploymentController) getNewReplicaSet(d *apps.Deployment, rsList, old
285
293
needsUpdate = true
286
294
}
287
295
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 {})
289
297
}
290
298
return createdRS , err
291
299
}
@@ -409,6 +417,8 @@ func (dc *DeploymentController) scaleReplicaSetAndRecordEvent(rs *apps.ReplicaSe
409
417
}
410
418
411
419
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 ())
412
422
413
423
sizeNeedsUpdate := * (rs .Spec .Replicas ) != newScale
414
424
@@ -420,7 +430,7 @@ func (dc *DeploymentController) scaleReplicaSet(rs *apps.ReplicaSet, newScale in
420
430
rsCopy := rs .DeepCopy ()
421
431
* (rsCopy .Spec .Replicas ) = newScale
422
432
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 {})
424
434
if err == nil && sizeNeedsUpdate {
425
435
scaled = true
426
436
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
433
443
// where N=d.Spec.RevisionHistoryLimit. Old replica sets are older versions of the podtemplate of a deployment kept
434
444
// around by default 1) for historical reasons and 2) for the ability to rollback a deployment.
435
445
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
+
436
449
if ! deploymentutil .HasRevisionHistoryLimit (deployment ) {
437
450
return nil
438
451
}
@@ -458,7 +471,7 @@ func (dc *DeploymentController) cleanupDeployment(oldRSs []*apps.ReplicaSet, dep
458
471
continue
459
472
}
460
473
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 ) {
462
475
// Return error instead of aggregating and continuing DELETEs on the theory
463
476
// that we may be overloading the api server.
464
477
return err
@@ -470,6 +483,9 @@ func (dc *DeploymentController) cleanupDeployment(oldRSs []*apps.ReplicaSet, dep
470
483
471
484
// syncDeploymentStatus checks if the status is up-to-date and sync it if necessary
472
485
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
+
473
489
newStatus := calculateStatus (allRSs , newRS , d )
474
490
475
491
if reflect .DeepEqual (d .Status , newStatus ) {
@@ -478,7 +494,7 @@ func (dc *DeploymentController) syncDeploymentStatus(allRSs []*apps.ReplicaSet,
478
494
479
495
newDeployment := d
480
496
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 {})
482
498
return err
483
499
}
484
500
0 commit comments