k8s.io/kubernetes@v1.31.0-alpha.0.0.20240520171757-56147500dadc/pkg/scheduler/framework/plugins/defaultbinder/default_binder.go (about) 1 /* 2 Copyright 2020 The Kubernetes Authors. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package defaultbinder 18 19 import ( 20 "context" 21 22 v1 "k8s.io/api/core/v1" 23 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" 24 "k8s.io/apimachinery/pkg/runtime" 25 "k8s.io/klog/v2" 26 "k8s.io/kubernetes/pkg/scheduler/framework" 27 "k8s.io/kubernetes/pkg/scheduler/framework/plugins/names" 28 ) 29 30 // Name of the plugin used in the plugin registry and configurations. 31 const Name = names.DefaultBinder 32 33 // DefaultBinder binds pods to nodes using a k8s client. 34 type DefaultBinder struct { 35 handle framework.Handle 36 } 37 38 var _ framework.BindPlugin = &DefaultBinder{} 39 40 // New creates a DefaultBinder. 41 func New(_ context.Context, _ runtime.Object, handle framework.Handle) (framework.Plugin, error) { 42 return &DefaultBinder{handle: handle}, nil 43 } 44 45 // Name returns the name of the plugin. 46 func (b DefaultBinder) Name() string { 47 return Name 48 } 49 50 // Bind binds pods to nodes using the k8s client. 51 func (b DefaultBinder) Bind(ctx context.Context, state *framework.CycleState, p *v1.Pod, nodeName string) *framework.Status { 52 logger := klog.FromContext(ctx) 53 logger.V(3).Info("Attempting to bind pod to node", "pod", klog.KObj(p), "node", klog.KRef("", nodeName)) 54 binding := &v1.Binding{ 55 ObjectMeta: metav1.ObjectMeta{Namespace: p.Namespace, Name: p.Name, UID: p.UID}, 56 Target: v1.ObjectReference{Kind: "Node", Name: nodeName}, 57 } 58 err := b.handle.ClientSet().CoreV1().Pods(binding.Namespace).Bind(ctx, binding, metav1.CreateOptions{}) 59 if err != nil { 60 return framework.AsStatus(err) 61 } 62 return nil 63 }