1 package clusterctl
2
3 import (
4 "context"
5 "database/sql"
6 "flag"
7 "fmt"
8 "time"
9
10 "github.com/peterbourgon/ff/v3"
11
12 bsltypes "edge-infra.dev/pkg/edge/api/bsl/types"
13 "edge-infra.dev/pkg/edge/api/services"
14 "edge-infra.dev/pkg/edge/api/services/artifacts"
15 clustersvc "edge-infra.dev/pkg/edge/api/services/cluster"
16 "edge-infra.dev/pkg/edge/bsl"
17 "edge-infra.dev/pkg/edge/k8objectsutils"
18 ipranger "edge-infra.dev/pkg/f8n/ipranger/server"
19 "edge-infra.dev/pkg/lib/gcp/cloudsql"
20 )
21
22 type Config struct {
23 CreateClient ContainerClusterClientFunc
24 EdgeAPI string
25 TopLevelProjectID string
26 IPRangerClient *ipranger.Client
27 DefaultRequeue time.Duration
28 TopLevelCNRMSA string
29 TotpSecret string
30 Domain string
31 DatasyncDNSName string
32 DatasyncDNSZone string
33 LDKey string
34 GCPRegion string
35 GCPZone string
36 GCPForemanProjectNumber string
37
38
39 WaitForSetTimeout time.Duration
40 ClusterReconcilerConcurrency int
41 GKEClusterReconcilerConcurrency int
42
43
44 DB *sql.DB
45
46
47 DatabaseName string
48 DatabaseUser string
49 DatabaseConnectionName string
50 GCPService services.GCPService
51 BSLClient *bsl.Client
52 BSLAccessKey bsl.AccessKey
53 BSLConfig bsltypes.BSPConfig
54 ArtifactsService artifacts.Service
55
56
57 PluginConcurrency int
58
59
60 BackgroundWaitForSetConcurrency int
61
62
63 HelmCacheLimit int
64
65
66 EdgeSecMaxLeasePeriod string
67 EdgeSecMaxValidityPeriod string
68 }
69
70 func NewConfig(args []string) (*Config, error) {
71 fs := flag.NewFlagSet("clusterctl", flag.ExitOnError)
72 cfg := &Config{}
73 cfg.bind(fs)
74 if err := ff.Parse(fs, args, ff.WithEnvVarNoPrefix()); err != nil {
75 return nil, fmt.Errorf("failed to parse configuration: %w", err)
76 }
77 return cfg, nil
78 }
79
80 func (c *Config) bind(fs *flag.FlagSet) {
81 fs.StringVar(&c.EdgeAPI, "edge-api", "", "The URL for the Edge API")
82 fs.StringVar(&c.TopLevelProjectID, "top-level-project-id", "", "The Foreman Project ID")
83 fs.DurationVar(&c.DefaultRequeue, "default-requeue", 30*time.Second, "The default requeue interval")
84 fs.StringVar(&c.TopLevelCNRMSA, "top-level-cnrm-sa", "", "The top level cnrm service account")
85 fs.StringVar(&c.TotpSecret, "totp-secret-key", "", "The totp secret for authenication to the Edge API")
86 fs.StringVar(&c.Domain, "domain", "", "The Edge Domain")
87 fs.StringVar(&c.BSLAccessKey.SharedKey, "edge-bsl-shared-key", "", "The shared key for BSL access")
88 fs.StringVar(&c.BSLAccessKey.SecretKey, "edge-bsl-secret-key", "", "The secret key for BSL access")
89 fs.StringVar(&c.BSLConfig.Endpoint, "bsl-endpoint", "", "The BSL endpoint for a specific environment")
90 fs.StringVar(&c.BSLConfig.Root, "bsl-root-org", "", "The BSL root org")
91 fs.StringVar(&c.BSLConfig.OrganizationPrefix, "bsp-organization-prefix", "", "The BSL organization prefix for a specific environment")
92 fs.StringVar(&c.DatasyncDNSName, "datasync-dns-name", "", "The Datasync DNS Name")
93 fs.StringVar(&c.DatasyncDNSZone, "datasync-dns-zone", "", "The Datasync DNS Zone")
94 fs.DurationVar(&c.WaitForSetTimeout, "wait-for-set-timeout", 2*time.Minute, "The Reconciler wait for set timeout duration")
95 fs.IntVar(&c.ClusterReconcilerConcurrency, "cluster-concurrency", 24, "The concurrency value for the cluster reconciler")
96 fs.IntVar(&c.GKEClusterReconcilerConcurrency, "gkecluster-concurrency", 6, "The concurrency value for the gke cluster reconciler")
97 fs.StringVar(&c.DatabaseName, "sql-db-name", "", "The Database Name")
98 fs.StringVar(&c.DatabaseUser, "sql-user", "", "The Database User")
99 fs.StringVar(&c.DatabaseConnectionName, "sql-connection-name", "", "The Database Connection Name")
100 fs.StringVar(&c.LDKey, "ld-key", "", "Launch Darkly Key")
101 fs.StringVar(&c.GCPRegion, "gcp-region", "", "GCP Region - eg: us-east1")
102 fs.StringVar(&c.GCPZone, "gcp-zone", "", "GCP Zone - eg: a/b/c")
103 fs.IntVar(&c.PluginConcurrency, "plugin-concurrency", 24, "The concurrency value for plugins")
104 fs.IntVar(&c.BackgroundWaitForSetConcurrency, "wait-for-set-concurrency", 5, "The concurrency for background waitforset processing")
105 fs.IntVar(&c.HelmCacheLimit, "helm-cache-limit", 1200, "The maximum entries allowed in the helm cache")
106 fs.StringVar(&c.EdgeSecMaxLeasePeriod, "edge-sec-max-lease-period", "", "Maximum secret lease period")
107 fs.StringVar(&c.EdgeSecMaxValidityPeriod, "edge-sec-max-validity-period", "", "Maximum secret validity period")
108 fs.StringVar(&c.GCPForemanProjectNumber, "gcp-foreman-project-number", "", "The Foreman GCP Project Number")
109 }
110
111 func (c *Config) AfterParse() error {
112 db, err := cloudsql.GCPPostgresConnection(c.DatabaseConnectionName).
113 DBName(c.DatabaseName).
114 Username(c.DatabaseUser).
115 NewConnection()
116 if err != nil {
117 return fmt.Errorf("failed to create sql connection: %w", err)
118 }
119 pingCtx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
120 defer cancel()
121 if err := db.PingContext(pingCtx); err != nil {
122 return fmt.Errorf("failed to ping sql connection: %w", err)
123 }
124 c.DB = db
125 c.CreateClient = k8objectsutils.CreateClient
126 c.IPRangerClient = ipranger.NewClient(ipranger.ClusterLocalSvcHost)
127 gcpClientSvc := services.NewGcpClientService()
128 c.GCPService = services.NewGcpService(gcpClientSvc, c.TopLevelProjectID, nil)
129 c.BSLClient = bsl.NewBSLClient(c.BSLConfig).SetDefaultAccessKey(c.BSLAccessKey.SharedKey, c.BSLAccessKey.SecretKey)
130
131 clusterLabelSvc := clustersvc.NewLabelService(db)
132 c.ArtifactsService = artifacts.NewArtifactsService(db, clusterLabelSvc)
133
134 return nil
135 }
136
View as plain text