default_binder.go 1.9 KB

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