...

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

Documentation: edge-infra.dev/pkg/edge/edge-issuer/controllers

     1  package controllers
     2  
     3  import (
     4  	"context"
     5  	"database/sql"
     6  	"flag"
     7  	"fmt"
     8  	"time"
     9  
    10  	"github.com/peterbourgon/ff/v3"
    11  
    12  	"edge-infra.dev/pkg/lib/gcp/cloudsql"
    13  )
    14  
    15  type Config struct {
    16  	TopLevelProjectID string
    17  	TopLevelCNRMSA    string
    18  	TotpSecret        string
    19  	BannerID          string
    20  
    21  	// DB is used to create a dbinfrastatus.EdgeDB wrapper. Infra status recording is disabled when DB is nil.
    22  	DB *sql.DB
    23  
    24  	// DatabaseName is passed into the cluster-infra shipment.
    25  	DatabaseName           string
    26  	DatabaseUser           string
    27  	DatabaseConnectionName string
    28  }
    29  
    30  func NewConfig(args []string) (*Config, error) {
    31  	fs := flag.NewFlagSet("edge-issuer", flag.ExitOnError)
    32  	cfg := &Config{}
    33  	cfg.bind(fs)
    34  	if err := ff.Parse(fs, args, ff.WithEnvVarNoPrefix()); err != nil {
    35  		return nil, fmt.Errorf("failed to parse configuration: %w", err)
    36  	}
    37  	return cfg, nil
    38  }
    39  
    40  func (c *Config) bind(fs *flag.FlagSet) {
    41  	fs.StringVar(&c.BannerID, "banner-id", "", "The Banner ID")
    42  	fs.StringVar(&c.DatabaseConnectionName, "sql-connection-name", "", "The Database Connection Name")
    43  	fs.StringVar(&c.DatabaseName, "sql-db-name", "", "The Database Name")
    44  	fs.StringVar(&c.DatabaseUser, "sql-user", "", "The Database User")
    45  	fs.StringVar(&c.TopLevelProjectID, "top-level-project-id", "", "The Foreman Project ID")
    46  }
    47  
    48  func (c *Config) AfterParse() error {
    49  	db, err := cloudsql.GCPPostgresConnection(c.DatabaseConnectionName).
    50  		DBName(c.DatabaseName).
    51  		Username(c.DatabaseUser).
    52  		NewConnection()
    53  	if err != nil {
    54  		return fmt.Errorf("failed to create sql connection: %w", err)
    55  	}
    56  	pingCtx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
    57  	defer cancel()
    58  	if err := db.PingContext(pingCtx); err != nil {
    59  		return fmt.Errorf("failed to ping sql connection: %w", err)
    60  	}
    61  	c.DB = db
    62  	return nil
    63  }
    64  

View as plain text