...

Source file src/edge-infra.dev/pkg/edge/webhooks/edge-injector/config.go

Documentation: edge-infra.dev/pkg/edge/webhooks/edge-injector

     1  package edgeinjector
     2  
     3  import (
     4  	"flag"
     5  	"fmt"
     6  	"os"
     7  	"time"
     8  
     9  	ff "github.com/peterbourgon/ff/v3"
    10  )
    11  
    12  type Config struct {
    13  	EnableWebhook        bool
    14  	CertDir              string
    15  	Port                 int
    16  	ReconcileConcurrency int
    17  	RequeueTime          time.Duration
    18  	PollingInterval      time.Duration
    19  }
    20  
    21  func NewConfig() (*Config, error) {
    22  	cfg := &Config{}
    23  
    24  	fs := flag.NewFlagSet("edge-injector", flag.ExitOnError)
    25  
    26  	fs.StringVar(
    27  		&cfg.CertDir,
    28  		"cert-dir",
    29  		"/var/cert",
    30  		"Certificate path",
    31  	)
    32  
    33  	fs.IntVar(
    34  		&cfg.Port,
    35  		"port",
    36  		8443,
    37  		"webhook port",
    38  	)
    39  
    40  	fs.BoolVar(&cfg.EnableWebhook,
    41  		"enable-webhook",
    42  		true,
    43  		"disable webhook, useful for testing. etc...",
    44  	)
    45  
    46  	fs.DurationVar(
    47  		&cfg.RequeueTime,
    48  		"requeue-time",
    49  		10*time.Second,
    50  		"requeue on error",
    51  	)
    52  
    53  	fs.DurationVar(
    54  		&cfg.PollingInterval,
    55  		"polling-interval",
    56  		2*time.Minute,
    57  		"polling interval to check is secret exists",
    58  	)
    59  
    60  	fs.IntVar(&cfg.ReconcileConcurrency,
    61  		"reconcile-concurrency", 10,
    62  		"the max reconcile concurrency for the controller")
    63  
    64  	if err := ff.Parse(fs, os.Args[1:], ff.WithEnvVarNoPrefix(), ff.WithIgnoreUndefined(true)); err != nil {
    65  		return cfg, err
    66  	}
    67  
    68  	if err := cfg.Validate(); err != nil {
    69  		return cfg, err
    70  	}
    71  
    72  	return cfg, nil
    73  }
    74  
    75  func (c *Config) Validate() error {
    76  	if c.EnableWebhook {
    77  		if c.CertDir == "" {
    78  			return fmt.Errorf("missing webhook CERT_DIR environment variable")
    79  		}
    80  		if c.Port == 0 {
    81  			return fmt.Errorf("missing webhook PORT environment variable")
    82  		}
    83  	}
    84  	if c.RequeueTime == 0 {
    85  		return fmt.Errorf("missing REQUEUE_TIME environment variable")
    86  	}
    87  	if c.PollingInterval == 0 {
    88  		return fmt.Errorf("missing POLLING_INTERVAL environment variable")
    89  	}
    90  	if c.ReconcileConcurrency == 0 {
    91  		return fmt.Errorf("missing RECONCILE_CONCURRENCY environment variable")
    92  	}
    93  	return nil
    94  }
    95  

View as plain text