github.com/kubewharf/katalyst-core@v0.5.3/cmd/katalyst-controller/app/options/lifecycle.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 options
    18  
    19  import (
    20  	"time"
    21  
    22  	"k8s.io/apimachinery/pkg/labels"
    23  	cliflag "k8s.io/component-base/cli/flag"
    24  
    25  	"github.com/kubewharf/katalyst-core/pkg/config/controller"
    26  )
    27  
    28  type HealthzOptions struct {
    29  	DryRun        bool
    30  	NodeSelector  string
    31  	AgentSelector map[string]string
    32  
    33  	CheckWindow        time.Duration
    34  	UnhealthyPeriods   time.Duration
    35  	AgentUnhealthySecs map[string]int
    36  
    37  	HandlePeriod  time.Duration
    38  	AgentHandlers map[string]string
    39  
    40  	TaintQPS                 float32
    41  	EvictQPS                 float32
    42  	DisruptionTaintThreshold float32
    43  	DisruptionEvictThreshold float32
    44  }
    45  
    46  // LifeCycleOptions holds the configurations for life cycle.
    47  type LifeCycleOptions struct {
    48  	EnableHealthz      bool
    49  	EnableCNCLifecycle bool
    50  
    51  	*HealthzOptions
    52  }
    53  
    54  // NewLifeCycleOptions creates a new Options with a default config.
    55  func NewLifeCycleOptions() *LifeCycleOptions {
    56  	return &LifeCycleOptions{
    57  		EnableHealthz:      false,
    58  		EnableCNCLifecycle: true,
    59  		HealthzOptions: &HealthzOptions{
    60  			DryRun:        false,
    61  			NodeSelector:  "",
    62  			AgentSelector: map[string]string{"katalyst-agent": "app=katalyst-agent"},
    63  
    64  			CheckWindow:      5 * time.Minute,
    65  			UnhealthyPeriods: 10 * time.Minute,
    66  
    67  			HandlePeriod:  5 * time.Minute,
    68  			AgentHandlers: map[string]string{},
    69  
    70  			TaintQPS:                 0.2,
    71  			EvictQPS:                 0.1,
    72  			DisruptionTaintThreshold: 0.2,
    73  			DisruptionEvictThreshold: 0.2,
    74  		},
    75  	}
    76  }
    77  
    78  // AddFlags adds flags  to the specified FlagSet.
    79  func (o *LifeCycleOptions) AddFlags(fss *cliflag.NamedFlagSets) {
    80  	fs := fss.FlagSet("lifecycle")
    81  
    82  	fs.BoolVar(&o.EnableHealthz, "healthz-enabled", o.EnableHealthz,
    83  		"whether to enable the healthz controller")
    84  	fs.BoolVar(&o.EnableCNCLifecycle, "cnc-lifecycle-enabled", o.EnableCNCLifecycle,
    85  		"whether to enable the cnc lifecycle controller")
    86  
    87  	fs.BoolVar(&o.DryRun, "healthz-dry-run", o.DryRun,
    88  		"a bool to enable and disable dry-run logic of healthz controller.")
    89  	fs.StringVar(&o.NodeSelector, "healthz-node-selector", o.NodeSelector,
    90  		"the selector to match up with to-be handled nodes")
    91  	fs.StringToStringVar(&o.AgentSelector, "healthz-agent-selector", o.AgentSelector,
    92  		"the selector to match up with to-be handled agents, each agent many have a different selector")
    93  
    94  	fs.DurationVar(&o.CheckWindow, "healthz-period-check", o.CheckWindow,
    95  		"the interval to check agent healthz states")
    96  	fs.DurationVar(&o.UnhealthyPeriods, "healthz-unhealthy-period", o.UnhealthyPeriods,
    97  		"the default last intervals to put agent as unhealthy")
    98  	fs.StringToIntVar(&o.AgentUnhealthySecs, "healthz-unhealthy-agent-periods", o.AgentUnhealthySecs,
    99  		"the last intervals to put agent as unhealthy, each agent many have a different period")
   100  
   101  	fs.DurationVar(&o.HandlePeriod, "healthz-handle-period", o.HandlePeriod,
   102  		"the interval to trigger performs")
   103  	fs.StringToStringVar(&o.AgentHandlers, "healthz-agent-handles", o.AgentHandlers,
   104  		"the handler-name to handle each agent, each agent many have a corresponding handler")
   105  
   106  	fs.Float32Var(&o.TaintQPS, "healthz-taint-qps", o.TaintQPS,
   107  		"the qps to perform tainting")
   108  	fs.Float32Var(&o.EvictQPS, "healthz-evict-qps", o.EvictQPS,
   109  		"the qps to perform evicting")
   110  	fs.Float32Var(&o.DisruptionTaintThreshold, "healthz-taint-threshold", o.DisruptionTaintThreshold,
   111  		"the threshold to judge whether nodes should be disrupted to perform tainting")
   112  	fs.Float32Var(&o.DisruptionEvictThreshold, "healthz-evict-threshold", o.DisruptionEvictThreshold,
   113  		"the threshold to judge whether nodes should be disrupted to perform evicting")
   114  }
   115  
   116  // ApplyTo fills up config with options
   117  func (o *LifeCycleOptions) ApplyTo(c *controller.LifeCycleConfig) error {
   118  	c.EnableHealthz = o.EnableHealthz
   119  	c.EnableCNCLifecycle = o.EnableCNCLifecycle
   120  
   121  	c.DryRun = o.DryRun
   122  
   123  	if selector, err := labels.Parse(o.NodeSelector); err != nil {
   124  		return err
   125  	} else {
   126  		c.NodeSelector = selector
   127  	}
   128  
   129  	c.AgentSelector = make(map[string]labels.Selector)
   130  	for agent, s := range o.AgentSelector {
   131  		if selector, err := labels.Parse(s); err != nil {
   132  			return err
   133  		} else {
   134  			c.AgentSelector[agent] = selector
   135  		}
   136  	}
   137  
   138  	c.CheckWindow = o.CheckWindow
   139  	c.UnhealthyPeriods = o.UnhealthyPeriods
   140  	c.AgentUnhealthyPeriods = make(map[string]time.Duration)
   141  	for agent, secs := range o.AgentUnhealthySecs {
   142  		c.AgentUnhealthyPeriods[agent] = time.Duration(secs) * time.Second
   143  	}
   144  
   145  	c.HandlePeriod = o.HandlePeriod
   146  	c.AgentHandlers = o.AgentHandlers
   147  
   148  	c.TaintQPS = o.TaintQPS
   149  	c.EvictQPS = o.EvictQPS
   150  	c.DisruptionTaintThreshold = o.DisruptionTaintThreshold
   151  	c.DisruptionEvictThreshold = o.DisruptionEvictThreshold
   152  
   153  	return nil
   154  }
   155  
   156  func (o *LifeCycleOptions) Config() (*controller.LifeCycleConfig, error) {
   157  	c := &controller.LifeCycleConfig{}
   158  	if err := o.ApplyTo(c); err != nil {
   159  		return nil, err
   160  	}
   161  	return c, nil
   162  }