github.com/kubewharf/katalyst-core@v0.5.3/cmd/katalyst-agent/app/options/eviction/eviction_base.go (about)

     1  /*
     2  Copyright 2022 The Katalyst 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 eviction
    18  
    19  import (
    20  	"fmt"
    21  	"time"
    22  
    23  	"k8s.io/apimachinery/pkg/util/errors"
    24  	cliflag "k8s.io/component-base/cli/flag"
    25  
    26  	evictionconfig "github.com/kubewharf/katalyst-core/pkg/config/agent/eviction"
    27  	"github.com/kubewharf/katalyst-core/pkg/consts"
    28  )
    29  
    30  // GenericEvictionOptions holds the configurations for eviction manager.
    31  type GenericEvictionOptions struct {
    32  	InnerPlugins []string
    33  
    34  	// ConditionTransitionPeriod is duration the eviction manager has to wait before transitioning out of a condition.
    35  	ConditionTransitionPeriod time.Duration
    36  
    37  	// EvictionManagerSyncPeriod is the interval duration that eviction manager fetches information from registered plugins
    38  	EvictionManagerSyncPeriod time.Duration
    39  
    40  	// those two variables are used to filter out eviction-free pods
    41  	EvictionSkippedAnnotationKeys []string
    42  	EvictionSkippedLabelKeys      []string
    43  
    44  	// EvictionBurst limit the burst eviction counts
    45  	EvictionBurst int
    46  
    47  	// PodKiller specify the pod killer implementation
    48  	PodKiller string
    49  
    50  	// StrictAuthentication means whether to authenticate plugins strictly
    51  	StrictAuthentication bool
    52  
    53  	// PodMetricLabels defines the pod labels to be added into metric selector list.
    54  	PodMetricLabels []string
    55  }
    56  
    57  // NewGenericEvictionOptions creates a new Options with a default config.
    58  func NewGenericEvictionOptions() *GenericEvictionOptions {
    59  	return &GenericEvictionOptions{
    60  		InnerPlugins:                  []string{},
    61  		ConditionTransitionPeriod:     5 * time.Minute,
    62  		EvictionManagerSyncPeriod:     5 * time.Second,
    63  		EvictionSkippedAnnotationKeys: []string{},
    64  		EvictionSkippedLabelKeys:      []string{},
    65  		EvictionBurst:                 3,
    66  		PodKiller:                     consts.KillerNameEvictionKiller,
    67  		StrictAuthentication:          false,
    68  	}
    69  }
    70  
    71  // AddFlags adds flags  to the specified FlagSet.
    72  func (o *GenericEvictionOptions) AddFlags(fss *cliflag.NamedFlagSets) {
    73  	fs := fss.FlagSet("eviction")
    74  
    75  	fs.StringSliceVar(&o.InnerPlugins, "eviction-plugins", o.InnerPlugins, fmt.Sprintf(""+
    76  		"A list of eviction plugins to enable. '*' enables all on-by-default eviction plugins, 'foo' enables the eviction plugin "+
    77  		"named 'foo', '-foo' disables the eviction plugin named 'foo'"))
    78  
    79  	fs.DurationVar(&o.ConditionTransitionPeriod, "eviction-condition-transition-period", o.ConditionTransitionPeriod,
    80  		"duration the eviction manager has to wait before transitioning out of a condition")
    81  
    82  	fs.DurationVar(&o.EvictionManagerSyncPeriod, "eviction-manager-sync-period", o.EvictionManagerSyncPeriod,
    83  		"interval duration that eviction manager fetches information from registered plugins")
    84  
    85  	fs.StringSliceVar(&o.EvictionSkippedAnnotationKeys, "eviction-skipped-annotation", o.EvictionSkippedAnnotationKeys,
    86  		"A list of annotations to identify a bunch of pods that should be filtered out during eviction")
    87  	fs.StringSliceVar(&o.EvictionSkippedLabelKeys, "eviction-skipped-labels", o.EvictionSkippedLabelKeys,
    88  		"A list of labels to identify a bunch of pods that should be filtered out during eviction")
    89  
    90  	fs.IntVar(&o.EvictionBurst, "eviction-burst", o.EvictionBurst,
    91  		"The burst amount of pods to be evicted by edition manager")
    92  
    93  	fs.StringVar(&o.PodKiller, "pod-killer", o.PodKiller,
    94  		"the pod killer used to evict pod")
    95  
    96  	fs.BoolVar(&o.StrictAuthentication, "strict-authentication", o.StrictAuthentication,
    97  		"whether to authenticate plugins strictly, the out-of-tree plugins must use valid and authorized token "+
    98  			"to register if it set to true")
    99  
   100  	fs.StringSliceVar(&o.PodMetricLabels, "eviction-pod-metric-labels", o.PodMetricLabels,
   101  		"The pod labels to be added into metric selector list")
   102  }
   103  
   104  // ApplyTo fills up config with options
   105  func (o *GenericEvictionOptions) ApplyTo(c *evictionconfig.GenericEvictionConfiguration) error {
   106  	c.InnerPlugins = o.InnerPlugins
   107  	c.ConditionTransitionPeriod = o.ConditionTransitionPeriod
   108  	c.EvictionManagerSyncPeriod = o.EvictionManagerSyncPeriod
   109  	c.EvictionSkippedAnnotationKeys.Insert(o.EvictionSkippedAnnotationKeys...)
   110  	c.EvictionSkippedLabelKeys.Insert(o.EvictionSkippedLabelKeys...)
   111  	c.EvictionBurst = o.EvictionBurst
   112  	c.PodKiller = o.PodKiller
   113  	c.StrictAuthentication = o.StrictAuthentication
   114  	c.PodMetricLabels.Insert(o.PodMetricLabels...)
   115  	return nil
   116  }
   117  
   118  func (o *GenericEvictionOptions) Config() (*evictionconfig.GenericEvictionConfiguration, error) {
   119  	c := evictionconfig.NewGenericEvictionConfiguration()
   120  	if err := o.ApplyTo(c); err != nil {
   121  		return nil, err
   122  	}
   123  	return c, nil
   124  }
   125  
   126  type EvictionOptions struct {
   127  	*ReclaimedResourcesEvictionOptions
   128  	*MemoryPressureEvictionOptions
   129  	*CPUPressureEvictionOptions
   130  }
   131  
   132  func NewEvictionOptions() *EvictionOptions {
   133  	return &EvictionOptions{
   134  		ReclaimedResourcesEvictionOptions: NewReclaimedResourcesEvictionOptions(),
   135  		MemoryPressureEvictionOptions:     NewMemoryPressureEvictionOptions(),
   136  		CPUPressureEvictionOptions:        NewCPUPressureEvictionOptions(),
   137  	}
   138  }
   139  
   140  func (o *EvictionOptions) AddFlags(fss *cliflag.NamedFlagSets) {
   141  	o.ReclaimedResourcesEvictionOptions.AddFlags(fss)
   142  	o.MemoryPressureEvictionOptions.AddFlags(fss)
   143  	o.CPUPressureEvictionOptions.AddFlags(fss)
   144  }
   145  
   146  // ApplyTo fills up config with options
   147  func (o *EvictionOptions) ApplyTo(c *evictionconfig.EvictionConfiguration) error {
   148  	var errList []error
   149  	errList = append(errList,
   150  		o.ReclaimedResourcesEvictionOptions.ApplyTo(c.ReclaimedResourcesEvictionConfiguration),
   151  		o.MemoryPressureEvictionOptions.ApplyTo(c.MemoryPressureEvictionConfiguration),
   152  		o.CPUPressureEvictionOptions.ApplyTo(c.CPUPressureEvictionConfiguration),
   153  	)
   154  	return errors.NewAggregate(errList)
   155  }
   156  
   157  func (o *EvictionOptions) Config() (*evictionconfig.EvictionConfiguration, error) {
   158  	c := evictionconfig.NewEvictionConfiguration()
   159  	if err := o.ApplyTo(c); err != nil {
   160  		return nil, err
   161  	}
   162  	return c, nil
   163  }