From 80d752a7da838d153d4a7ee7ce0e0251eaf479ab Mon Sep 17 00:00:00 2001 From: Sambhav Kothari Date: Mon, 15 Jul 2024 19:48:48 +0100 Subject: [PATCH] Add scaling factor flag to scale client-side requests Signed-off-by: Sambhav Kothari --- cmd/controller/main.go | 14 ++++++++------ pkg/config/config.go | 1 + pkg/flaghelpers/credential_flags.go | 13 +++++++++++++ pkg/slsa/attest_test.go | 1 + 4 files changed, 23 insertions(+), 6 deletions(-) diff --git a/cmd/controller/main.go b/cmd/controller/main.go index 90a73f24c..cf29131a5 100644 --- a/cmd/controller/main.go +++ b/cmd/controller/main.go @@ -63,8 +63,8 @@ import ( ) const ( - routinesPerController = 2 - component = "controller" + defaultRoutinesPerController = 2 + component = "controller" ) var ( @@ -91,6 +91,7 @@ func main() { flag.BoolVar(&cfg.EnablePriorityClasses, "enable-priority-classes", flaghelpers.GetEnvBool("ENABLE_PRIORITY_CLASSES", false), "if set to true, enables different pod priority classes for normal builds and automated builds") flag.StringVar(&cfg.MaximumPlatformApiVersion, "maximum-platform-api-version", os.Getenv("MAXIMUM_PLATFORM_API_VERSION"), "The maximum allowed platform api version a build can utilize") flag.BoolVar(&cfg.SshTrustUnknownHosts, "insecure-ssh-trust-unknown-hosts", flaghelpers.GetEnvBool("INSECURE_SSH_TRUST_UNKNOWN_HOSTS", true), "if set to true, automatically trust unknown hosts when using git ssh source") + flag.IntVar(&cfg.ScalingFactor, "scaling-factor", flaghelpers.GetEnvInt("SCALING_FACTOR", 1), "The scaling factor to scale client-side rate limits by") flag.BoolVar(&featureFlags.InjectedSidecarSupport, "injected-sidecar-support", flaghelpers.GetEnvBool("INJECTED_SIDECAR_SUPPORT", false), "if set to true, all builds will execute in standard containers instead of init containers to support injected sidecars") flag.BoolVar(&featureFlags.GenerateSlsaAttestation, "experimental-generate-slsa-attestation", flaghelpers.GetEnvBool("EXPERIMENTAL_GENERATE_SLSA_ATTESTATION", false), "if set to true, SLSA attestations will be generated for each build") @@ -260,6 +261,7 @@ func main() { clusterStackInformer.Informer(), ) + routinesPerController := defaultRoutinesPerController * cfg.ScalingFactor err = runGroup( ctx, run(clusterStackController, routinesPerController), @@ -311,13 +313,13 @@ func runGroup(ctx context.Context, fns ...func(ctx context.Context) error) error const controllerCount = 7 // lifted from knative.dev/pkg/injection/sharedmain -func genericControllerSetup(ctx context.Context, cfg *rest.Config) (*zap.SugaredLogger, *informer.InformedWatcher, *http.Server) { +func genericControllerSetup(ctx context.Context, restCfg *rest.Config) (*zap.SugaredLogger, *informer.InformedWatcher, *http.Server) { metrics.MemStatsOrDie(ctx) // Adjust our client's rate limits based on the number of controllers we are running. - cfg.QPS = float32(controllerCount) * rest.DefaultQPS - cfg.Burst = controllerCount * rest.DefaultBurst - ctx, _ = injection.Default.SetupInformers(ctx, cfg) + restCfg.QPS = float32(controllerCount) * rest.DefaultQPS * float32(cfg.ScalingFactor) + restCfg.Burst = controllerCount * rest.DefaultBurst * cfg.ScalingFactor + ctx, _ = injection.Default.SetupInformers(ctx, restCfg) logger, atomicLevel := sharedmain.SetupLoggerOrDie(ctx, component) ctx = logging.WithLogger(ctx, logger) diff --git a/pkg/config/config.go b/pkg/config/config.go index f5aca33f6..358a5542f 100644 --- a/pkg/config/config.go +++ b/pkg/config/config.go @@ -9,6 +9,7 @@ type Config struct { EnablePriorityClasses bool `json:"enablePriorityClasses"` MaximumPlatformApiVersion string `json:"maximumPlatformApiVersion"` SshTrustUnknownHosts bool `json:"sshTrustUnknownHosts"` + ScalingFactor int `json:"scalingFactor"` } type FeatureFlags struct { diff --git a/pkg/flaghelpers/credential_flags.go b/pkg/flaghelpers/credential_flags.go index 8f645c12a..353e37797 100644 --- a/pkg/flaghelpers/credential_flags.go +++ b/pkg/flaghelpers/credential_flags.go @@ -23,9 +23,22 @@ func (i *CredentialsFlags) Set(value string) error { func GetEnvBool(key string, defaultValue bool) bool { s := os.Getenv(key) + if s == "" { + return defaultValue + } v, err := strconv.ParseBool(s) if err != nil { return defaultValue } return v } + + +func GetEnvInt(key string, defaultValue int) int { + s := os.Getenv(key) + v, err := strconv.Atoi(s) + if err != nil { + return defaultValue + } + return v +} diff --git a/pkg/slsa/attest_test.go b/pkg/slsa/attest_test.go index f4da89c76..3982e438a 100644 --- a/pkg/slsa/attest_test.go +++ b/pkg/slsa/attest_test.go @@ -188,6 +188,7 @@ func testAttester(t *testing.T, when spec.G, it spec.S) { "enablePriorityClasses": false, "maximumPlatformApiVersion": "", "sshTrustUnknownHosts": true, + "scalingFactor": 0, "buildInitImage": "build-init-image", "buildInitWindowsImage": "build-init-windows-image", "buildWaiterImage": "build-waiter-image",