github.com/kubewharf/katalyst-core@v0.5.3/cmd/katalyst-agent/app/options/metaserver/metaserver.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 metaserver
    18  
    19  import (
    20  	"time"
    21  
    22  	"golang.org/x/time/rate"
    23  	cliflag "k8s.io/component-base/cli/flag"
    24  
    25  	"github.com/kubewharf/katalyst-core/pkg/config/agent/metaserver"
    26  )
    27  
    28  const (
    29  	defaultCheckpointManagerDir = "/var/lib/katalyst/metaserver/checkpoints"
    30  	defaultEnableMetricsFetcher = true
    31  	defaultEnableCNCFetcher     = true
    32  )
    33  
    34  const (
    35  	defaultConfigCacheTTL                 = 15 * time.Second
    36  	defaultConfigDisableDynamic           = false
    37  	defaultConfigSkipFailedInitialization = true
    38  	defaultConfigCheckpointGraceTime      = 2 * time.Hour
    39  )
    40  
    41  const (
    42  	defaultServiceProfileSkipCorruptionError = true
    43  	defaultServiceProfileCacheTTL            = 1 * time.Minute
    44  )
    45  
    46  const (
    47  	defaultKubeletPodCacheSyncPeriod    = 30 * time.Second
    48  	defaultKubeletPodCacheSyncMaxRate   = 5
    49  	defaultKubeletPodCacheSyncBurstBulk = 1
    50  	defaultRuntimePodCacheSyncPeriod    = 30 * time.Second
    51  )
    52  
    53  const defaultCustomNodeResourceCacheTTL = 15 * time.Second
    54  
    55  const defaultCustomNodeConfigCacheTTL = 15 * time.Second
    56  
    57  // MetaServerOptions holds all the configurations for metaserver.
    58  // we will not try to separate this structure into several individual
    59  // structures since it will not be used directly by other components; instead,
    60  // we will only separate them with blanks in a single structure.
    61  // todo separate this option-structure into individual structures
    62  type MetaServerOptions struct {
    63  	// generic configurations for metaserver
    64  	CheckpointManagerDir string
    65  	EnableMetricsFetcher bool
    66  	EnableCNCFetcher     bool
    67  
    68  	// configurations for kcc
    69  	ConfigCacheTTL                 time.Duration
    70  	ConfigDisableDynamic           bool
    71  	ConfigSkipFailedInitialization bool
    72  	ConfigCheckpointGraceTime      time.Duration
    73  
    74  	// configurations for spd
    75  	ServiceProfileSkipCorruptionError bool
    76  	ServiceProfileCacheTTL            time.Duration
    77  
    78  	// configurations for pod-cache
    79  	KubeletPodCacheSyncPeriod    time.Duration
    80  	KubeletPodCacheSyncMaxRate   int
    81  	KubeletPodCacheSyncBurstBulk int
    82  	RuntimePodCacheSyncPeriod    time.Duration
    83  
    84  	// configurations for cnr
    85  	CNRCacheTTL time.Duration
    86  
    87  	// configurations for cnc
    88  	CustomNodeConfigCacheTTL time.Duration
    89  
    90  	// configurations for metric-fetcher
    91  	*MetricFetcherOptions
    92  }
    93  
    94  func NewMetaServerOptions() *MetaServerOptions {
    95  	return &MetaServerOptions{
    96  		CheckpointManagerDir: defaultCheckpointManagerDir,
    97  		EnableMetricsFetcher: defaultEnableMetricsFetcher,
    98  		EnableCNCFetcher:     defaultEnableCNCFetcher,
    99  
   100  		ConfigCacheTTL:                 defaultConfigCacheTTL,
   101  		ConfigDisableDynamic:           defaultConfigDisableDynamic,
   102  		ConfigSkipFailedInitialization: defaultConfigSkipFailedInitialization,
   103  		ConfigCheckpointGraceTime:      defaultConfigCheckpointGraceTime,
   104  
   105  		ServiceProfileSkipCorruptionError: defaultServiceProfileSkipCorruptionError,
   106  		ServiceProfileCacheTTL:            defaultServiceProfileCacheTTL,
   107  
   108  		KubeletPodCacheSyncPeriod:    defaultKubeletPodCacheSyncPeriod,
   109  		KubeletPodCacheSyncMaxRate:   defaultKubeletPodCacheSyncMaxRate,
   110  		KubeletPodCacheSyncBurstBulk: defaultKubeletPodCacheSyncBurstBulk,
   111  		RuntimePodCacheSyncPeriod:    defaultRuntimePodCacheSyncPeriod,
   112  
   113  		CNRCacheTTL: defaultCustomNodeResourceCacheTTL,
   114  
   115  		CustomNodeConfigCacheTTL: defaultCustomNodeConfigCacheTTL,
   116  
   117  		MetricFetcherOptions: NewMetricFetcherOptions(),
   118  	}
   119  }
   120  
   121  // AddFlags adds flags to the specified FlagSet.
   122  func (o *MetaServerOptions) AddFlags(fss *cliflag.NamedFlagSets) {
   123  	fs := fss.FlagSet("meta-server")
   124  
   125  	fs.StringVar(&o.CheckpointManagerDir, "checkpoint-manager-directory", o.CheckpointManagerDir,
   126  		"The checkpoint manager directory")
   127  	fs.BoolVar(&o.EnableMetricsFetcher, "enable-metrics-fetcher", o.EnableMetricsFetcher,
   128  		"Whether to enable metrics fetcher")
   129  	fs.BoolVar(&o.EnableCNCFetcher, "enable-cnc-fetcher", o.EnableCNCFetcher,
   130  		"Whether to enable cnc fetcher")
   131  
   132  	fs.DurationVar(&o.ConfigCacheTTL, "config-cache-ttl", o.ConfigCacheTTL,
   133  		"The ttl of katalyst custom config loader cache remote config")
   134  	fs.BoolVar(&o.ConfigDisableDynamic, "config-disable-dynamic", o.ConfigDisableDynamic,
   135  		"Whether disable dynamic configuration")
   136  	fs.BoolVar(&o.ConfigSkipFailedInitialization, "config-skip-failed-initialization", o.ConfigSkipFailedInitialization,
   137  		"Whether skip if updating dynamic configuration fails")
   138  	fs.DurationVar(&o.ConfigCheckpointGraceTime, "config-checkpoint-grace-time", o.ConfigCheckpointGraceTime,
   139  		"The grace time of meta server config checkpoint")
   140  
   141  	fs.BoolVar(&o.ServiceProfileSkipCorruptionError, "service-profile-skip-corruption-error", o.ServiceProfileSkipCorruptionError,
   142  		"Whether to skip corruption error when loading spd checkpoint")
   143  	fs.DurationVar(&o.ServiceProfileCacheTTL, "service-profile-cache-ttl", o.ServiceProfileCacheTTL,
   144  		"The ttl of service profile manager cache remote spd")
   145  
   146  	fs.DurationVar(&o.KubeletPodCacheSyncPeriod, "kubelet-pod-cache-sync-period", o.KubeletPodCacheSyncPeriod,
   147  		"The period of meta server to sync pod from kubelet 10255 port")
   148  	fs.IntVar(&o.KubeletPodCacheSyncMaxRate, "kubelet-pod-cache-sync-max-rate", o.KubeletPodCacheSyncMaxRate,
   149  		"The max rate for kubelet pod sync")
   150  	fs.IntVar(&o.KubeletPodCacheSyncBurstBulk, "kubelet-pod-cache-sync-burst-bulk", o.KubeletPodCacheSyncBurstBulk,
   151  		"The burst bulk for kubelet pod sync")
   152  	fs.DurationVar(&o.RuntimePodCacheSyncPeriod, "runtime-pod-cache-sync-period", o.RuntimePodCacheSyncPeriod,
   153  		"The period of meta server to sync pod from cri")
   154  
   155  	fs.DurationVar(&o.CNRCacheTTL, "cnr-cache-ttl", o.CNRCacheTTL,
   156  		"The sync period of cnr fetcher to sync remote to local")
   157  
   158  	fs.DurationVar(&o.CustomNodeConfigCacheTTL, "custom-node-config-cache-ttl", o.CustomNodeConfigCacheTTL,
   159  		"The ttl of custom node config fetcher cache remote cnc")
   160  
   161  	o.MetricFetcherOptions.AddFlags(fss)
   162  }
   163  
   164  // ApplyTo fills up config with options
   165  func (o *MetaServerOptions) ApplyTo(c *metaserver.MetaServerConfiguration) error {
   166  	c.CheckpointManagerDir = o.CheckpointManagerDir
   167  	c.EnableMetricsFetcher = o.EnableMetricsFetcher
   168  	c.EnableCNCFetcher = o.EnableCNCFetcher
   169  
   170  	c.ConfigCacheTTL = o.ConfigCacheTTL
   171  	c.ConfigDisableDynamic = o.ConfigDisableDynamic
   172  	c.ConfigSkipFailedInitialization = o.ConfigSkipFailedInitialization
   173  	c.ConfigCheckpointGraceTime = o.ConfigCheckpointGraceTime
   174  
   175  	c.ServiceProfileSkipCorruptionError = o.ServiceProfileSkipCorruptionError
   176  	c.ServiceProfileCacheTTL = o.ServiceProfileCacheTTL
   177  
   178  	c.KubeletPodCacheSyncPeriod = o.KubeletPodCacheSyncPeriod
   179  	c.KubeletPodCacheSyncMaxRate = rate.Limit(o.KubeletPodCacheSyncMaxRate)
   180  	c.KubeletPodCacheSyncBurstBulk = o.KubeletPodCacheSyncBurstBulk
   181  	c.RuntimePodCacheSyncPeriod = o.RuntimePodCacheSyncPeriod
   182  
   183  	c.CNRCacheTTL = o.CNRCacheTTL
   184  
   185  	c.CustomNodeConfigCacheTTL = o.CustomNodeConfigCacheTTL
   186  
   187  	if err := o.MetricFetcherOptions.ApplyTo(c.MetricConfiguration); err != nil {
   188  		return err
   189  	}
   190  
   191  	return nil
   192  }