github.com/kubewharf/katalyst-core@v0.5.3/cmd/katalyst-controller/app/options/spd.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  	"fmt"
    21  	"time"
    22  
    23  	cliflag "k8s.io/component-base/cli/flag"
    24  
    25  	"github.com/kubewharf/katalyst-core/pkg/config/controller"
    26  )
    27  
    28  // SPDOptions holds the configurations for service profile data.
    29  type SPDOptions struct {
    30  	ResyncPeriod           time.Duration
    31  	SPDWorkloadGVResources []string
    32  	SPDPodLabelIndexerKeys []string
    33  	EnableCNCCache         bool
    34  	IndicatorPlugins       []string
    35  	BaselinePercent        map[string]int64
    36  }
    37  
    38  // NewSPDOptions creates a new Options with a default config.
    39  func NewSPDOptions() *SPDOptions {
    40  	return &SPDOptions{
    41  		ResyncPeriod:    time.Second * 30,
    42  		EnableCNCCache:  true,
    43  		BaselinePercent: map[string]int64{},
    44  	}
    45  }
    46  
    47  // AddFlags adds flags  to the specified FlagSet.
    48  func (o *SPDOptions) AddFlags(fss *cliflag.NamedFlagSets) {
    49  	fs := fss.FlagSet("spd")
    50  
    51  	fs.DurationVar(&o.ResyncPeriod, "spd-resync-period", o.ResyncPeriod, fmt.Sprintf(""+
    52  		"Period for spd controller to resync"))
    53  	fs.StringSliceVar(&o.SPDWorkloadGVResources, "spd-workload-resources", o.SPDWorkloadGVResources, ""+
    54  		"A list of resources to be spd controller watched. "+
    55  		"SPDWorkloadGVResources should be in the format of `resource.version.group.com` like 'deployments.v1.apps'.")
    56  	fs.StringSliceVar(&o.SPDPodLabelIndexerKeys, "spd-pod-label-indexers", o.SPDPodLabelIndexerKeys, ""+
    57  		"A list of pod label keys to be used as indexers for pod informer")
    58  	fs.BoolVar(&o.EnableCNCCache, "spd-enable-cnc-cache", o.EnableCNCCache, ""+
    59  		"Whether enable cnc cache to reduce agent api-server remote request")
    60  	fs.StringSliceVar(&o.IndicatorPlugins, "spd-indicator-plugins", o.IndicatorPlugins,
    61  		"A list of indicator plugins to be used")
    62  	fs.StringToInt64Var(&o.BaselinePercent, "spd-qos-baseline-percent", o.BaselinePercent, ""+
    63  		"A map of qosLeve to default baseline percent[0,100]")
    64  }
    65  
    66  // ApplyTo fills up config with options
    67  func (o *SPDOptions) ApplyTo(c *controller.SPDConfig) error {
    68  	c.ReSyncPeriod = o.ResyncPeriod
    69  	c.SPDWorkloadGVResources = o.SPDWorkloadGVResources
    70  	c.SPDPodLabelIndexerKeys = o.SPDPodLabelIndexerKeys
    71  	c.EnableCNCCache = o.EnableCNCCache
    72  	c.IndicatorPlugins = o.IndicatorPlugins
    73  	c.BaselinePercent = o.BaselinePercent
    74  	return nil
    75  }
    76  
    77  func (o *SPDOptions) Config() (*controller.SPDConfig, error) {
    78  	c := &controller.SPDConfig{}
    79  	if err := o.ApplyTo(c); err != nil {
    80  		return nil, err
    81  	}
    82  	return c, nil
    83  }