prebind.go 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. /*
  2. Copyright 2019 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 prebind
  14. import (
  15. "context"
  16. "fmt"
  17. v1 "k8s.io/api/core/v1"
  18. "k8s.io/apimachinery/pkg/runtime"
  19. framework "k8s.io/kubernetes/pkg/scheduler/framework/v1alpha1"
  20. )
  21. // StatelessPreBindExample is an example of a simple plugin that has no state
  22. // and implements only one hook for prebind.
  23. type StatelessPreBindExample struct{}
  24. var _ framework.PreBindPlugin = StatelessPreBindExample{}
  25. // Name is the name of the plugin used in Registry and configurations.
  26. const Name = "stateless-prebind-plugin-example"
  27. // Name returns name of the plugin. It is used in logs, etc.
  28. func (sr StatelessPreBindExample) Name() string {
  29. return Name
  30. }
  31. // PreBind is the functions invoked by the framework at "prebind" extension point.
  32. func (sr StatelessPreBindExample) PreBind(ctx context.Context, state *framework.CycleState, pod *v1.Pod, nodeName string) *framework.Status {
  33. if pod == nil {
  34. return framework.NewStatus(framework.Error, fmt.Sprintf("pod cannot be nil"))
  35. }
  36. if pod.Namespace != "foo" {
  37. return framework.NewStatus(framework.Unschedulable, "only pods from 'foo' namespace are allowed")
  38. }
  39. return nil
  40. }
  41. // New initializes a new plugin and returns it.
  42. func New(_ *runtime.Unknown, _ framework.FrameworkHandle) (framework.Plugin, error) {
  43. return &StatelessPreBindExample{}, nil
  44. }