github.com/kubewharf/katalyst-core@v0.5.3/cmd/katalyst-agent/app/options/metaserver/metric.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  	cliflag "k8s.io/component-base/cli/flag"
    23  
    24  	"github.com/kubewharf/katalyst-core/pkg/config/agent/metaserver"
    25  )
    26  
    27  const defaultMetricInsurancePeriod = 0 * time.Second
    28  
    29  const defaultRodanServerPort = 9102
    30  
    31  type MetricFetcherOptions struct {
    32  	MetricInsurancePeriod time.Duration
    33  	MetricProvisions      []string
    34  
    35  	DefaultInterval         time.Duration
    36  	ProvisionerIntervalSecs map[string]int
    37  
    38  	*MalachiteOptions
    39  	*CgroupOptions
    40  	*KubeletOptions
    41  	*RodanOptions
    42  }
    43  
    44  type (
    45  	MalachiteOptions struct{}
    46  	CgroupOptions    struct{}
    47  	KubeletOptions   struct{}
    48  	RodanOptions     struct {
    49  		ServerPort int
    50  	}
    51  )
    52  
    53  func NewMetricFetcherOptions() *MetricFetcherOptions {
    54  	return &MetricFetcherOptions{
    55  		MetricInsurancePeriod: defaultMetricInsurancePeriod,
    56  		MetricProvisions:      []string{metaserver.MetricProvisionerMalachite, metaserver.MetricProvisionerKubelet},
    57  
    58  		DefaultInterval:         time.Second * 5,
    59  		ProvisionerIntervalSecs: make(map[string]int),
    60  
    61  		MalachiteOptions: &MalachiteOptions{},
    62  		CgroupOptions:    &CgroupOptions{},
    63  		KubeletOptions:   &KubeletOptions{},
    64  		RodanOptions: &RodanOptions{
    65  			ServerPort: defaultRodanServerPort,
    66  		},
    67  	}
    68  }
    69  
    70  // AddFlags adds flags to the specified FlagSet.
    71  func (o *MetricFetcherOptions) AddFlags(fss *cliflag.NamedFlagSets) {
    72  	fs := fss.FlagSet("metric-server")
    73  
    74  	fs.DurationVar(&o.MetricInsurancePeriod, "metric-insurance-period", o.MetricInsurancePeriod,
    75  		"The meta server return metric data and MetricDataExpired if the update time of metric data is earlier than this period.")
    76  	fs.StringSliceVar(&o.MetricProvisions, "metric-provisioners", o.MetricProvisions,
    77  		"The provisioners that should be enabled by default")
    78  
    79  	fs.DurationVar(&o.DefaultInterval, "metric-interval", o.DefaultInterval,
    80  		"The default metric provisioner collecting interval")
    81  	fs.StringToIntVar(&o.ProvisionerIntervalSecs, "metric-provisioner-intervals", o.ProvisionerIntervalSecs,
    82  		"The metric provisioner collecting intervals for each individual provisioner")
    83  
    84  	fs.IntVar(&o.RodanOptions.ServerPort, "rodan-server-port", o.RodanOptions.ServerPort,
    85  		"The rodan metric provisioner server port")
    86  }
    87  
    88  // ApplyTo fills up config with options
    89  func (o *MetricFetcherOptions) ApplyTo(c *metaserver.MetricConfiguration) error {
    90  	c.MetricInsurancePeriod = o.MetricInsurancePeriod
    91  	c.MetricProvisions = o.MetricProvisions
    92  
    93  	c.DefaultInterval = o.DefaultInterval
    94  	c.ProvisionerIntervals = make(map[string]time.Duration)
    95  	for name, secs := range o.ProvisionerIntervalSecs {
    96  		c.ProvisionerIntervals[name] = time.Second * time.Duration(secs)
    97  	}
    98  
    99  	c.RodanServerPort = o.RodanOptions.ServerPort
   100  
   101  	return nil
   102  }