k8s.io/apiserver@v0.31.1/pkg/server/options/feature.go (about)

     1  /*
     2  Copyright 2017 The Kubernetes 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  
    22  	"github.com/spf13/pflag"
    23  
    24  	"k8s.io/apimachinery/pkg/runtime/serializer"
    25  	"k8s.io/apiserver/pkg/server"
    26  	utilflowcontrol "k8s.io/apiserver/pkg/util/flowcontrol"
    27  	"k8s.io/client-go/informers"
    28  	"k8s.io/client-go/kubernetes"
    29  )
    30  
    31  type FeatureOptions struct {
    32  	EnableProfiling           bool
    33  	DebugSocketPath           string
    34  	EnableContentionProfiling bool
    35  	EnablePriorityAndFairness bool
    36  }
    37  
    38  func NewFeatureOptions() *FeatureOptions {
    39  	defaults := server.NewConfig(serializer.CodecFactory{})
    40  
    41  	return &FeatureOptions{
    42  		EnableProfiling:           defaults.EnableProfiling,
    43  		DebugSocketPath:           defaults.DebugSocketPath,
    44  		EnableContentionProfiling: defaults.EnableContentionProfiling,
    45  		EnablePriorityAndFairness: true,
    46  	}
    47  }
    48  
    49  func (o *FeatureOptions) AddFlags(fs *pflag.FlagSet) {
    50  	if o == nil {
    51  		return
    52  	}
    53  
    54  	fs.BoolVar(&o.EnableProfiling, "profiling", o.EnableProfiling,
    55  		"Enable profiling via web interface host:port/debug/pprof/")
    56  	fs.BoolVar(&o.EnableContentionProfiling, "contention-profiling", o.EnableContentionProfiling,
    57  		"Enable block profiling, if profiling is enabled")
    58  	fs.StringVar(&o.DebugSocketPath, "debug-socket-path", o.DebugSocketPath,
    59  		"Use an unprotected (no authn/authz) unix-domain socket for profiling with the given path")
    60  	fs.BoolVar(&o.EnablePriorityAndFairness, "enable-priority-and-fairness", o.EnablePriorityAndFairness, ""+
    61  		"If true, replace the max-in-flight handler with an enhanced one that queues and dispatches with priority and fairness")
    62  }
    63  
    64  func (o *FeatureOptions) ApplyTo(c *server.Config, clientset kubernetes.Interface, informers informers.SharedInformerFactory) error {
    65  	if o == nil {
    66  		return nil
    67  	}
    68  
    69  	c.EnableProfiling = o.EnableProfiling
    70  	c.DebugSocketPath = o.DebugSocketPath
    71  	c.EnableContentionProfiling = o.EnableContentionProfiling
    72  
    73  	if o.EnablePriorityAndFairness {
    74  		if clientset == nil {
    75  			return fmt.Errorf("invalid configuration: priority and fairness requires a core Kubernetes client")
    76  		}
    77  		if c.MaxRequestsInFlight+c.MaxMutatingRequestsInFlight <= 0 {
    78  			return fmt.Errorf("invalid configuration: MaxRequestsInFlight=%d and MaxMutatingRequestsInFlight=%d; they must add up to something positive", c.MaxRequestsInFlight, c.MaxMutatingRequestsInFlight)
    79  
    80  		}
    81  		c.FlowControl = utilflowcontrol.New(
    82  			informers,
    83  			clientset.FlowcontrolV1(),
    84  			c.MaxRequestsInFlight+c.MaxMutatingRequestsInFlight,
    85  		)
    86  	}
    87  
    88  	return nil
    89  }
    90  
    91  func (o *FeatureOptions) Validate() []error {
    92  	if o == nil {
    93  		return nil
    94  	}
    95  
    96  	errs := []error{}
    97  	return errs
    98  }