k8s.io/kubernetes@v1.29.3/pkg/scheduler/profile/profile.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 profile holds the definition of a scheduling Profile.
    18  package profile
    19  
    20  import (
    21  	"context"
    22  	"errors"
    23  	"fmt"
    24  
    25  	"github.com/google/go-cmp/cmp"
    26  	"k8s.io/apimachinery/pkg/runtime"
    27  	"k8s.io/client-go/kubernetes/scheme"
    28  	"k8s.io/client-go/tools/events"
    29  	"k8s.io/kubernetes/pkg/scheduler/apis/config"
    30  	"k8s.io/kubernetes/pkg/scheduler/framework"
    31  	frameworkruntime "k8s.io/kubernetes/pkg/scheduler/framework/runtime"
    32  )
    33  
    34  // RecorderFactory builds an EventRecorder for a given scheduler name.
    35  type RecorderFactory func(string) events.EventRecorder
    36  
    37  // newProfile builds a Profile for the given configuration.
    38  func newProfile(ctx context.Context, cfg config.KubeSchedulerProfile, r frameworkruntime.Registry, recorderFact RecorderFactory,
    39  	opts ...frameworkruntime.Option) (framework.Framework, error) {
    40  	recorder := recorderFact(cfg.SchedulerName)
    41  	opts = append(opts, frameworkruntime.WithEventRecorder(recorder))
    42  	return frameworkruntime.NewFramework(ctx, r, &cfg, opts...)
    43  }
    44  
    45  // Map holds frameworks indexed by scheduler name.
    46  type Map map[string]framework.Framework
    47  
    48  // NewMap builds the frameworks given by the configuration, indexed by name.
    49  func NewMap(ctx context.Context, cfgs []config.KubeSchedulerProfile, r frameworkruntime.Registry, recorderFact RecorderFactory,
    50  	opts ...frameworkruntime.Option) (Map, error) {
    51  	m := make(Map)
    52  	v := cfgValidator{m: m}
    53  
    54  	for _, cfg := range cfgs {
    55  		p, err := newProfile(ctx, cfg, r, recorderFact, opts...)
    56  		if err != nil {
    57  			return nil, fmt.Errorf("creating profile for scheduler name %s: %v", cfg.SchedulerName, err)
    58  		}
    59  		if err := v.validate(cfg, p); err != nil {
    60  			return nil, err
    61  		}
    62  		m[cfg.SchedulerName] = p
    63  	}
    64  	return m, nil
    65  }
    66  
    67  // HandlesSchedulerName returns whether a profile handles the given scheduler name.
    68  func (m Map) HandlesSchedulerName(name string) bool {
    69  	_, ok := m[name]
    70  	return ok
    71  }
    72  
    73  // NewRecorderFactory returns a RecorderFactory for the broadcaster.
    74  func NewRecorderFactory(b events.EventBroadcaster) RecorderFactory {
    75  	return func(name string) events.EventRecorder {
    76  		return b.NewRecorder(scheme.Scheme, name)
    77  	}
    78  }
    79  
    80  type cfgValidator struct {
    81  	m             Map
    82  	queueSort     string
    83  	queueSortArgs runtime.Object
    84  }
    85  
    86  func (v *cfgValidator) validate(cfg config.KubeSchedulerProfile, f framework.Framework) error {
    87  	if len(f.ProfileName()) == 0 {
    88  		return errors.New("scheduler name is needed")
    89  	}
    90  	if cfg.Plugins == nil {
    91  		return fmt.Errorf("plugins required for profile with scheduler name %q", f.ProfileName())
    92  	}
    93  	if v.m[f.ProfileName()] != nil {
    94  		return fmt.Errorf("duplicate profile with scheduler name %q", f.ProfileName())
    95  	}
    96  
    97  	queueSort := f.ListPlugins().QueueSort.Enabled[0].Name
    98  	var queueSortArgs runtime.Object
    99  	for _, plCfg := range cfg.PluginConfig {
   100  		if plCfg.Name == queueSort {
   101  			queueSortArgs = plCfg.Args
   102  			break
   103  		}
   104  	}
   105  	if len(v.queueSort) == 0 {
   106  		v.queueSort = queueSort
   107  		v.queueSortArgs = queueSortArgs
   108  		return nil
   109  	}
   110  	if v.queueSort != queueSort {
   111  		return fmt.Errorf("different queue sort plugins for profile %q: %q, first: %q", cfg.SchedulerName, queueSort, v.queueSort)
   112  	}
   113  	if !cmp.Equal(v.queueSortArgs, queueSortArgs) {
   114  		return fmt.Errorf("different queue sort plugin args for profile %q", cfg.SchedulerName)
   115  	}
   116  	return nil
   117  }