...

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

Documentation: edge-infra.dev/pkg/edge/psqlinjector

     1  package server
     2  
     3  import (
     4  	"database/sql"
     5  	"flag"
     6  	"fmt"
     7  	"os"
     8  	"time"
     9  
    10  	"github.com/peterbourgon/ff/v3"
    11  	"google.golang.org/grpc"
    12  
    13  	"edge-infra.dev/pkg/lib/gcp/cloudsql"
    14  )
    15  
    16  type Config struct {
    17  	MetricsAddr string
    18  	HealthzAddr string
    19  
    20  	ForemanProjectID string
    21  	TopicID          string
    22  	SubscriptionID   string
    23  
    24  	DBConnection string
    25  	DBName       string
    26  	DBUsername   string
    27  	DBPassword   string // for testing locally, not used when in the cloud.
    28  
    29  	PollBannersPeriod     time.Duration
    30  	PollBannersMaxRetries int
    31  
    32  	PollSubscriptionExistsPeriod time.Duration
    33  
    34  	// DelayScrapeMessageProcessing prevents deleting outdated watched field objects from thrashing the database
    35  	// with unnecessary writes & deletes due to pubsub's out-of-order message delivery.
    36  	//
    37  	// Tests use a smaller value to prevent unnecessary delays.
    38  	DelayScrapeMessageProcessing time.Duration
    39  
    40  	BannerCheckPeriod time.Duration
    41  
    42  	GarbageCollectDeletedWatchedFieldObjects bool
    43  
    44  	DB             *sql.DB          // for testing, the flags for DB will be ignored if set.
    45  	TestPubSubConn *grpc.ClientConn // for testing, needed since using manager.
    46  }
    47  
    48  func (cfg *Config) Validate() error {
    49  	if cfg.MetricsAddr == "" {
    50  		return fmt.Errorf("missing MetricsAddr")
    51  	}
    52  	if cfg.HealthzAddr == "" {
    53  		return fmt.Errorf("missing HealthzAddr")
    54  	}
    55  
    56  	if cfg.DB == nil {
    57  		switch "" {
    58  		case cfg.DBConnection:
    59  			return fmt.Errorf("missing DBConnection")
    60  		case cfg.DBName:
    61  			return fmt.Errorf("missing DBName")
    62  		case cfg.DBUsername:
    63  			return fmt.Errorf("missing DBUsername")
    64  		case cfg.ForemanProjectID:
    65  			return fmt.Errorf("missing ForemanProjectID")
    66  		case cfg.TopicID:
    67  			return fmt.Errorf("missing TopicID")
    68  		case cfg.SubscriptionID:
    69  			return fmt.Errorf("missing SubscriptionID")
    70  		}
    71  	}
    72  
    73  	return nil
    74  }
    75  
    76  func (cfg *Config) ConnectToDB() (*sql.DB, error) {
    77  	if cfg.DB != nil {
    78  		return cfg.DB, nil
    79  	}
    80  	return cloudsql.GCPPostgresConnection(cfg.DBConnection).
    81  		DBName(cfg.DBName).
    82  		Username(cfg.DBUsername).
    83  		Password(cfg.DBPassword).
    84  		NewConnection()
    85  }
    86  
    87  func NewConfig() (*Config, error) {
    88  	var cfg Config
    89  	fs := flag.NewFlagSet("kinform-server", flag.ContinueOnError)
    90  
    91  	fs.StringVar(&cfg.MetricsAddr, "metrics-addr", ":8081", "Address to bind metrics endpoint (/metrics) to")
    92  	fs.StringVar(&cfg.HealthzAddr, "healthz-addr", ":8082", "Address to bind heath-check endpoints (/livez, /readyz) to")
    93  
    94  	fs.StringVar(&cfg.ForemanProjectID, "foreman-project-id", "", "Foreman Project ID is used to create the shared PubSub client")
    95  	fs.StringVar(&cfg.TopicID, "topic", "", "PubSub Topic ID where kinform publishes messages")
    96  	fs.StringVar(&cfg.SubscriptionID, "subscription", "", "PubSub Subscription ID where psqlinjector receives kinform messages")
    97  
    98  	fs.DurationVar(&cfg.DelayScrapeMessageProcessing, "delay-scrape-message-processing", 10*time.Second, "How long to wait before deleting outdated watched_field_objects after a cluster scrapes")
    99  	fs.DurationVar(&cfg.BannerCheckPeriod, "banner-check-period", 60*time.Second, "The frequency of psqlinjector to check if banners were created or deleted")
   100  	fs.BoolVar(&cfg.GarbageCollectDeletedWatchedFieldObjects, "gc-deleted-watched-field-objects", true, "Garbage collect deleted watched field objects at startup")
   101  
   102  	fs.StringVar(&cfg.DBConnection, "db-connection", "", "postgress connection name")
   103  	fs.StringVar(&cfg.DBName, "db-name", "", "name of db to connect to")
   104  	fs.StringVar(&cfg.DBUsername, "db-username", "", "username of the db connection")
   105  	fs.StringVar(&cfg.DBPassword, "db-password", "", "password of the db connection for testing locally")
   106  
   107  	fs.DurationVar(&cfg.PollBannersPeriod, "poll-banners-period", 60*time.Second, "Time Duration period to poll for banner changes")
   108  	fs.IntVar(&cfg.PollBannersMaxRetries, "poll-banners-max-retries", 5, "maximum amount of consecutive poll errors before psqlinjector quits")
   109  	fs.DurationVar(&cfg.PollSubscriptionExistsPeriod, "poll-subscription-exists-period", 97*time.Second, "Time Duration period to probe for subscription deletions")
   110  
   111  	if err := ff.Parse(fs, os.Args[1:], ff.WithEnvVarNoPrefix()); err != nil {
   112  		return nil, err
   113  	}
   114  
   115  	return &cfg, nil
   116  }
   117  

View as plain text