@@ -33,10 +33,12 @@ import (
33
33
"k8s.io/apiserver/pkg/storage"
34
34
storeerr "k8s.io/apiserver/pkg/storage/errors"
35
35
"k8s.io/apiserver/pkg/util/dryrun"
36
+ utilfeature "k8s.io/apiserver/pkg/util/feature"
36
37
policyclient "k8s.io/client-go/kubernetes/typed/policy/v1"
37
38
podutil "k8s.io/kubernetes/pkg/api/pod"
38
39
api "k8s.io/kubernetes/pkg/apis/core"
39
40
"k8s.io/kubernetes/pkg/apis/core/validation"
41
+ kubefeatures "k8s.io/kubernetes/pkg/features"
40
42
"k8s.io/kubernetes/pkg/kubelet/client"
41
43
"k8s.io/kubernetes/pkg/printers"
42
44
printersinternal "k8s.io/kubernetes/pkg/printers/internalversion"
@@ -191,7 +193,7 @@ func (r *BindingREST) Create(ctx context.Context, name string, obj runtime.Objec
191
193
}
192
194
}
193
195
194
- err = r .assignPod (ctx , binding .UID , binding .ResourceVersion , binding .Name , binding .Target .Name , binding .Annotations , dryrun .IsDryRun (options .DryRun ))
196
+ err = r .assignPod (ctx , binding .UID , binding .ResourceVersion , binding .Name , binding .Target .Name , binding .Annotations , binding . Labels , dryrun .IsDryRun (options .DryRun ))
195
197
out = & metav1.Status {Status : metav1 .StatusSuccess }
196
198
return
197
199
}
@@ -203,10 +205,10 @@ func (r *BindingREST) PreserveRequestObjectMetaSystemFieldsOnSubresourceCreate()
203
205
return true
204
206
}
205
207
206
- // setPodHostAndAnnotations sets the given pod's host to 'machine' if and only if
207
- // the pod is unassigned and merges the provided annotations with those of the pod.
208
+ // setPodNodeAndMetadata sets the given pod's nodeName to 'machine' if and only if
209
+ // the pod is unassigned, and merges the provided annotations and labels with those of the pod.
208
210
// Returns the current state of the pod, or an error.
209
- func (r * BindingREST ) setPodHostAndAnnotations (ctx context.Context , podUID types.UID , podResourceVersion , podID , machine string , annotations map [string ]string , dryRun bool ) (finalPod * api.Pod , err error ) {
211
+ func (r * BindingREST ) setPodNodeAndMetadata (ctx context.Context , podUID types.UID , podResourceVersion , podID , machine string , annotations , labels map [string ]string , dryRun bool ) (finalPod * api.Pod , err error ) {
210
212
podKey , err := r .store .KeyFunc (ctx , podID )
211
213
if err != nil {
212
214
return nil , err
@@ -245,6 +247,11 @@ func (r *BindingREST) setPodHostAndAnnotations(ctx context.Context, podUID types
245
247
for k , v := range annotations {
246
248
pod .Annotations [k ] = v
247
249
}
250
+ // Copy all labels from the Binding over to the Pod object, overwriting
251
+ // any existing labels set on the Pod.
252
+ if utilfeature .DefaultFeatureGate .Enabled (kubefeatures .PodTopologyLabelsAdmission ) {
253
+ copyLabelsWithOverwriting (pod , labels )
254
+ }
248
255
podutil .UpdatePodCondition (& pod .Status , & api.PodCondition {
249
256
Type : api .PodScheduled ,
250
257
Status : api .ConditionTrue ,
@@ -255,9 +262,23 @@ func (r *BindingREST) setPodHostAndAnnotations(ctx context.Context, podUID types
255
262
return finalPod , err
256
263
}
257
264
265
+ func copyLabelsWithOverwriting (pod * api.Pod , labels map [string ]string ) {
266
+ if len (labels ) == 0 {
267
+ // nothing to do
268
+ return
269
+ }
270
+ if pod .Labels == nil {
271
+ pod .Labels = make (map [string ]string )
272
+ }
273
+ // Iterate over the binding's labels and copy them across to the Pod.
274
+ for k , v := range labels {
275
+ pod .Labels [k ] = v
276
+ }
277
+ }
278
+
258
279
// assignPod assigns the given pod to the given machine.
259
- func (r * BindingREST ) assignPod (ctx context.Context , podUID types.UID , podResourceVersion , podID string , machine string , annotations map [string ]string , dryRun bool ) (err error ) {
260
- if _ , err = r .setPodHostAndAnnotations (ctx , podUID , podResourceVersion , podID , machine , annotations , dryRun ); err != nil {
280
+ func (r * BindingREST ) assignPod (ctx context.Context , podUID types.UID , podResourceVersion , podID string , machine string , annotations , labels map [string ]string , dryRun bool ) (err error ) {
281
+ if _ , err = r .setPodNodeAndMetadata (ctx , podUID , podResourceVersion , podID , machine , annotations , labels , dryRun ); err != nil {
261
282
err = storeerr .InterpretGetError (err , api .Resource ("pods" ), podID )
262
283
err = storeerr .InterpretUpdateError (err , api .Resource ("pods" ), podID )
263
284
if _ , ok := err .(* errors.StatusError ); ! ok {
0 commit comments