volcano.sh/volcano@v1.9.0/cmd/controller-manager/app/options/options.go (about)

     1  /*
     2  Copyright 2017 The Volcano 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  	"os"
    22  
    23  	"github.com/spf13/pflag"
    24  
    25  	"volcano.sh/volcano/pkg/kube"
    26  )
    27  
    28  const (
    29  	defaultQPS                 = 50.0
    30  	defaultBurst               = 100
    31  	defaultWorkers             = 3
    32  	defaultMaxRequeueNum       = 15
    33  	defaultSchedulerName       = "volcano"
    34  	defaultHealthzAddress      = ":11251"
    35  	defaultLockObjectNamespace = "volcano-system"
    36  	defaultPodGroupWorkers     = 5
    37  )
    38  
    39  // ServerOption is the main context object for the controllers.
    40  type ServerOption struct {
    41  	KubeClientOptions    kube.ClientOptions
    42  	CertFile             string
    43  	KeyFile              string
    44  	CaCertFile           string
    45  	CertData             []byte
    46  	KeyData              []byte
    47  	CaCertData           []byte
    48  	EnableLeaderElection bool
    49  	LockObjectNamespace  string
    50  	PrintVersion         bool
    51  	// WorkerThreads is the number of threads syncing job operations
    52  	// concurrently. Larger number = faster job updating, but more CPU load.
    53  	WorkerThreads uint32
    54  	// MaxRequeueNum is the number of times a job, queue or command will be requeued before it is dropped out of the queue.
    55  	// With the current rate-limiter in use (5ms*2^(maxRetries-1)) the following numbers represent the times
    56  	// a job, queue or command is going to be requeued:
    57  	// 5ms, 10ms, 20ms, 40ms, 80ms, 160ms, 320ms, 640ms, 1.3s, 2.6s, 5.1s, 10.2s, 20.4s, 41s, 82s
    58  	MaxRequeueNum  int
    59  	SchedulerNames []string
    60  	// HealthzBindAddress is the IP address and port for the health check server to serve on,
    61  	// defaulting to 0.0.0.0:11251
    62  	HealthzBindAddress string
    63  	EnableHealthz      bool
    64  	// To determine whether inherit owner's annotations for pods when create podgroup
    65  	InheritOwnerAnnotations bool
    66  	// WorkerThreadsForPG is the number of threads syncing podgroup operations
    67  	// The larger the number, the faster the podgroup processing, but requires more CPU load.
    68  	WorkerThreadsForPG uint32
    69  }
    70  
    71  type DecryptFunc func(c *ServerOption) error
    72  
    73  // NewServerOption creates a new CMServer with a default config.
    74  func NewServerOption() *ServerOption {
    75  	return &ServerOption{}
    76  }
    77  
    78  // AddFlags adds flags for a specific CMServer to the specified FlagSet.
    79  func (s *ServerOption) AddFlags(fs *pflag.FlagSet) {
    80  	fs.StringVar(&s.KubeClientOptions.Master, "master", s.KubeClientOptions.Master, "The address of the Kubernetes API server (overrides any value in kubeconfig)")
    81  	fs.StringVar(&s.KubeClientOptions.KubeConfig, "kubeconfig", s.KubeClientOptions.KubeConfig, "Path to kubeconfig file with authorization and master location information.")
    82  	fs.StringVar(&s.CaCertFile, "ca-cert-file", s.CaCertFile, "File containing the x509 Certificate for HTTPS.")
    83  	fs.StringVar(&s.CertFile, "tls-cert-file", s.CertFile, ""+
    84  		"File containing the default x509 Certificate for HTTPS. (CA cert, if any, concatenated "+
    85  		"after server cert).")
    86  	fs.StringVar(&s.KeyFile, "tls-private-key-file", s.KeyFile, "File containing the default x509 private key matching --tls-cert-file.")
    87  	fs.BoolVar(&s.EnableLeaderElection, "leader-elect", true, "Start a leader election client and gain leadership before "+
    88  		"executing the main loop. Enable this when running replicated vc-controller-manager for high availability; it is enabled by default")
    89  	fs.StringVar(&s.LockObjectNamespace, "lock-object-namespace", defaultLockObjectNamespace, "Define the namespace of the lock object; it is volcano-system by default")
    90  	fs.Float32Var(&s.KubeClientOptions.QPS, "kube-api-qps", defaultQPS, "QPS to use while talking with kubernetes apiserver")
    91  	fs.IntVar(&s.KubeClientOptions.Burst, "kube-api-burst", defaultBurst, "Burst to use while talking with kubernetes apiserver")
    92  	fs.BoolVar(&s.PrintVersion, "version", false, "Show version and quit")
    93  	fs.Uint32Var(&s.WorkerThreads, "worker-threads", defaultWorkers, "The number of threads syncing job operations concurrently. "+
    94  		"Larger number = faster job updating, but more CPU load")
    95  	fs.StringArrayVar(&s.SchedulerNames, "scheduler-name", []string{defaultSchedulerName}, "Volcano will handle pods whose .spec.SchedulerName is same as scheduler-name")
    96  	fs.IntVar(&s.MaxRequeueNum, "max-requeue-num", defaultMaxRequeueNum, "The number of times a job, queue or command will be requeued before it is dropped out of the queue")
    97  	fs.StringVar(&s.HealthzBindAddress, "healthz-address", defaultHealthzAddress, "The address to listen on for the health check server.")
    98  	fs.BoolVar(&s.EnableHealthz, "enable-healthz", false, "Enable the health check; it is false by default")
    99  	fs.BoolVar(&s.InheritOwnerAnnotations, "inherit-owner-annotations", true, "Enable inherit owner annotations for pods when create podgroup; it is enabled by default")
   100  	fs.Uint32Var(&s.WorkerThreadsForPG, "worker-threads-for-podgroup", defaultPodGroupWorkers, "The number of threads syncing podgroup operations. The larger the number, the faster the podgroup processing, but requires more CPU load.")
   101  }
   102  
   103  // CheckOptionOrDie checks the LockObjectNamespace.
   104  func (s *ServerOption) CheckOptionOrDie() error {
   105  	if s.EnableLeaderElection && s.LockObjectNamespace == "" {
   106  		return fmt.Errorf("lock-object-namespace must not be nil when LeaderElection is enabled")
   107  	}
   108  	return nil
   109  }
   110  
   111  // readCAFiles read data from ca file path
   112  func (s *ServerOption) readCAFiles() error {
   113  	var err error
   114  
   115  	s.CaCertData, err = os.ReadFile(s.CaCertFile)
   116  	if err != nil {
   117  		return fmt.Errorf("failed to read cacert file (%s): %v", s.CaCertFile, err)
   118  	}
   119  
   120  	s.CertData, err = os.ReadFile(s.CertFile)
   121  	if err != nil {
   122  		return fmt.Errorf("failed to read cert file (%s): %v", s.CertFile, err)
   123  	}
   124  
   125  	s.KeyData, err = os.ReadFile(s.KeyFile)
   126  	if err != nil {
   127  		return fmt.Errorf("failed to read key file (%s): %v", s.KeyFile, err)
   128  	}
   129  
   130  	return nil
   131  }
   132  
   133  // ParseCAFiles parse ca file by decryptFunc
   134  func (s *ServerOption) ParseCAFiles(decryptFunc DecryptFunc) error {
   135  	if err := s.readCAFiles(); err != nil {
   136  		return err
   137  	}
   138  
   139  	// users can add one function to decrypt tha data by their own way if CA data is encrypted
   140  	if decryptFunc != nil {
   141  		return decryptFunc(s)
   142  	}
   143  
   144  	return nil
   145  }