From 9bf3223275fdc262b33ec0c1a47d652043ba260f Mon Sep 17 00:00:00 2001 From: Rafael da Fonseca Date: Fri, 5 Jul 2024 22:44:04 +0100 Subject: [PATCH] Fix syncing loops, fix logger issue with updated kubernetes controller (#8) --- cmd/provider/main.go | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/cmd/provider/main.go b/cmd/provider/main.go index 338fefb..be3c90f 100644 --- a/cmd/provider/main.go +++ b/cmd/provider/main.go @@ -6,6 +6,7 @@ package main import ( "context" + "io" "os" "path/filepath" "time" @@ -37,19 +38,21 @@ func main() { var ( app = kingpin.New(filepath.Base(os.Args[0]), "Terraform based Crossplane provider for Vault").DefaultEnvars() debug = app.Flag("debug", "Run with debug logging.").Short('d').Bool() - syncPeriod = app.Flag("sync", "Controller manager sync period such as 300ms, 1.5h, or 2h45m").Short('s').Default("1h").Duration() + syncInterval = app.Flag("sync", "Controller manager sync period such as 300ms, 1.5h, or 2h45m").Short('s').Default("12h").Duration() leaderElection = app.Flag("leader-election", "Use leader election for the controller manager.").Short('l').Default("false").OverrideDefaultFromEnvar("LEADER_ELECTION").Bool() terraformVersion = app.Flag("terraform-version", "Terraform version.").Required().Envar("TERRAFORM_VERSION").String() providerSource = app.Flag("terraform-provider-source", "Terraform provider source.").Required().Envar("TERRAFORM_PROVIDER_SOURCE").String() providerVersion = app.Flag("terraform-provider-version", "Terraform provider version.").Required().Envar("TERRAFORM_PROVIDER_VERSION").String() maxReconcileRate = app.Flag("max-reconcile-rate", "The global maximum rate per second at which resources may checked for drift from the desired state.").Default("10").Int() maxConcurrentReconciles = app.Flag("max-concurrent-reconciles", "The amount of items that will be processed concurrently.").Default("10").Int() + pollInterval = app.Flag("poll", "Poll interval controls how often an individual resource should be checked for drift.").Default("30m").Duration() namespace = app.Flag("namespace", "Namespace used to set as default scope in default secret store config.").Default("crossplane-system").Envar("POD_NAMESPACE").String() enableExternalSecretStores = app.Flag("enable-external-secret-stores", "Enable support for ExternalSecretStores.").Default("false").Envar("ENABLE_EXTERNAL_SECRET_STORES").Bool() ) kingpin.MustParse(app.Parse(os.Args[1:])) + ctrl.SetLogger(zap.New(zap.WriteTo(io.Discard))) zl := zap.New(zap.UseDevMode(*debug)) log := logging.NewLogrLogger(zl.WithName("upjet-provider-vault")) @@ -60,7 +63,10 @@ func main() { ctrl.SetLogger(zl) } - log.Debug("Starting", "sync-period", syncPeriod.String()) + // currently, we configure the jitter to be the 5% of the poll interval + pollJitter := time.Duration(float64(*pollInterval) * 0.05) + log.Debug("Starting", "sync-interval", syncInterval.String(), + "poll-interval", pollInterval.String(), "poll-jitter", pollJitter, "max-reconcile-rate", *maxReconcileRate) cfg, err := ctrl.GetConfig() kingpin.FatalIfError(err, "Cannot get API server rest config") @@ -69,7 +75,7 @@ func main() { LeaderElection: *leaderElection, LeaderElectionID: "crossplane-leader-election-upjet-provider-vault", Cache: cache.Options{ - SyncPeriod: syncPeriod, + SyncPeriod: syncInterval, }, LeaderElectionResourceLock: resourcelock.LeasesResourceLock, LeaseDuration: func() *time.Duration { d := 60 * time.Second; return &d }(), @@ -81,7 +87,7 @@ func main() { Options: xpcontroller.Options{ Logger: log, GlobalRateLimiter: ratelimiter.NewGlobal(*maxReconcileRate), - PollInterval: 1 * time.Minute, + PollInterval: *pollInterval, MaxConcurrentReconciles: *maxConcurrentReconciles, }, Provider: config.GetProvider(), @@ -89,6 +95,7 @@ func main() { // terraform.WithProviderRunner(terraform.NewSharedProvider(log, os.Getenv("TERRAFORM_NATIVE_PROVIDER_PATH"), terraform.WithNativeProviderArgs("-debuggable"))) WorkspaceStore: terraform.NewWorkspaceStore(log), SetupFn: clients.TerraformSetupBuilder(*terraformVersion, *providerSource, *providerVersion), + PollJitter: pollJitter, } if *enableExternalSecretStores {