k8s.io/kubernetes@v1.31.0-alpha.0.0.20240520171757-56147500dadc/pkg/scheduler/framework/plugins/examples/multipoint/multipoint.go (about)

     1  /*
     2  Copyright 2019 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 multipoint
    18  
    19  import (
    20  	"context"
    21  
    22  	v1 "k8s.io/api/core/v1"
    23  	"k8s.io/apimachinery/pkg/runtime"
    24  	"k8s.io/kubernetes/pkg/scheduler/framework"
    25  )
    26  
    27  // CommunicatingPlugin is an example of a plugin that implements two
    28  // extension points. It communicates through state with another function.
    29  type CommunicatingPlugin struct{}
    30  
    31  var _ framework.ReservePlugin = CommunicatingPlugin{}
    32  var _ framework.PreBindPlugin = CommunicatingPlugin{}
    33  
    34  // Name is the name of the plugin used in Registry and configurations.
    35  const Name = "multipoint-communicating-plugin"
    36  
    37  // Name returns name of the plugin. It is used in logs, etc.
    38  func (mc CommunicatingPlugin) Name() string {
    39  	return Name
    40  }
    41  
    42  type stateData struct {
    43  	data string
    44  }
    45  
    46  func (s *stateData) Clone() framework.StateData {
    47  	copy := &stateData{
    48  		data: s.data,
    49  	}
    50  	return copy
    51  }
    52  
    53  // Reserve is the function invoked by the framework at "reserve" extension point.
    54  func (mc CommunicatingPlugin) Reserve(ctx context.Context, state *framework.CycleState, pod *v1.Pod, nodeName string) *framework.Status {
    55  	if pod == nil {
    56  		return framework.NewStatus(framework.Error, "pod cannot be nil")
    57  	}
    58  	if pod.Name == "my-test-pod" {
    59  		state.Write(framework.StateKey(pod.Name), &stateData{data: "never bind"})
    60  	}
    61  	return nil
    62  }
    63  
    64  // Unreserve is the function invoked by the framework when any error happens
    65  // during "reserve" extension point or later.
    66  func (mc CommunicatingPlugin) Unreserve(ctx context.Context, state *framework.CycleState, pod *v1.Pod, nodeName string) {
    67  	if pod.Name == "my-test-pod" {
    68  		// The pod is at the end of its lifecycle -- let's clean up the allocated
    69  		// resources. In this case, our clean up is simply deleting the key written
    70  		// in the Reserve operation.
    71  		state.Delete(framework.StateKey(pod.Name))
    72  	}
    73  }
    74  
    75  // PreBind is the function invoked by the framework at "prebind" extension point.
    76  func (mc CommunicatingPlugin) PreBind(ctx context.Context, state *framework.CycleState, pod *v1.Pod, nodeName string) *framework.Status {
    77  	if pod == nil {
    78  		return framework.NewStatus(framework.Error, "pod cannot be nil")
    79  	}
    80  	if v, e := state.Read(framework.StateKey(pod.Name)); e == nil {
    81  		if value, ok := v.(*stateData); ok && value.data == "never bind" {
    82  			return framework.NewStatus(framework.Unschedulable, "pod is not permitted")
    83  		}
    84  	}
    85  	return nil
    86  }
    87  
    88  // New initializes a new plugin and returns it.
    89  func New(_ context.Context, _ *runtime.Unknown, _ framework.Handle) (framework.Plugin, error) {
    90  	return &CommunicatingPlugin{}, nil
    91  }