github.com/kubewharf/katalyst-core@v0.5.3/cmd/katalyst-controller/app/options/vpa.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  const (
    29  	defaultVpaSyncWorkers                   = 1
    30  	defaultVpaRecSyncWorkers                = 1
    31  	defaultResourceRecommendResyncVPAPeriod = 30 * time.Second
    32  )
    33  
    34  // VPARecommendationOptions holds the configurations for vertical pod auto-scaler recommendation.
    35  type VPARecommendationOptions struct{}
    36  
    37  // ResourceRecommendOptions holds the configurations for resource recommend.
    38  type ResourceRecommendOptions struct {
    39  	// time interval of resync VPA
    40  	VPAResyncPeriod time.Duration
    41  }
    42  
    43  // VPAOptions holds the configurations for vertical pod auto-scaler.
    44  type VPAOptions struct {
    45  	// we use VPAWorkloadGVResources to define those VPA concerned GVRs
    46  	VPAWorkloadGVResources []string
    47  	VPAPodLabelIndexerKeys []string
    48  	// count of workers to sync VPA and VPARec
    49  	VPASyncWorkers    int
    50  	VPARecSyncWorkers int
    51  
    52  	VPARecommendationOptions
    53  	ResourceRecommendOptions
    54  }
    55  
    56  // NewVPAOptions creates a new Options with a default config.
    57  func NewVPAOptions() *VPAOptions {
    58  	return &VPAOptions{}
    59  }
    60  
    61  // AddFlags adds flags  to the specified FlagSet.
    62  func (o *VPAOptions) AddFlags(fss *cliflag.NamedFlagSets) {
    63  	fs := fss.FlagSet("vpa")
    64  
    65  	fs.StringSliceVar(&o.VPAWorkloadGVResources, "vpa-workload-resources", o.VPAWorkloadGVResources, fmt.Sprintf(""+
    66  		"A list of resources to be vpa controller watched. "+
    67  		"VPAWorkloadGVResources should be in the format of `resource.version.group.com` like 'deployments.v1.apps'."))
    68  	fs.StringSliceVar(&o.VPAPodLabelIndexerKeys, "vpa-pod-label-indexers", o.VPAPodLabelIndexerKeys, ""+
    69  		"A list of pod label keys to be used as indexers for pod informer")
    70  	fs.IntVar(&o.VPASyncWorkers, "vpa-sync-workers", defaultVpaSyncWorkers, "num of goroutines to sync vpas")
    71  	fs.IntVar(&o.VPARecSyncWorkers, "vparec-sync-workers", defaultVpaRecSyncWorkers, "num of goroutines to sync vparecs")
    72  	fs.DurationVar(&o.ResourceRecommendOptions.VPAResyncPeriod, "resource-recommend-resync-vpa-period",
    73  		defaultResourceRecommendResyncVPAPeriod, "Period for recommend controller to sync vpa")
    74  }
    75  
    76  // ApplyTo fills up config with options
    77  func (o *VPAOptions) ApplyTo(c *controller.VPAConfig) error {
    78  	c.VPAWorkloadGVResources = o.VPAWorkloadGVResources
    79  	c.VPAPodLabelIndexerKeys = o.VPAPodLabelIndexerKeys
    80  	c.VPASyncWorkers = o.VPASyncWorkers
    81  	c.VPARecSyncWorkers = o.VPARecSyncWorkers
    82  	c.ResourceRecommendConfig.VPAReSyncPeriod = o.ResourceRecommendOptions.VPAResyncPeriod
    83  	return nil
    84  }
    85  
    86  func (o *VPAOptions) Config() (*controller.VPAConfig, error) {
    87  	c := &controller.VPAConfig{}
    88  	if err := o.ApplyTo(c); err != nil {
    89  		return nil, err
    90  	}
    91  	return c, nil
    92  }