Skip to content

Commit 51c17df

Browse files
feat(options): adding a rate limited kube client to the web options (#112)
* adding a rate limited kube client to the web options * more descriptive errs * linter update * imports * updating linter cfg * Apply suggestion from @Copilot Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> * gci fmt --------- Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
1 parent 155a518 commit 51c17df

File tree

5 files changed

+52
-10
lines changed

5 files changed

+52
-10
lines changed

.golangci.yaml

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ linters:
2222
ignore-calls: true
2323
gocritic:
2424
enable-all: true
25+
disabled-checks:
26+
- unnamedResult
2527
iface:
2628
enable:
2729
- identical
@@ -81,8 +83,9 @@ linters:
8183
exclude:
8284
- ""
8385
sloglint:
84-
# attr-only: true
86+
attr-only: false
8587
static-msg: true
88+
no-raw-keys: false
8689
key-naming-case: snake
8790
forbidden-keys:
8891
- time
@@ -136,16 +139,28 @@ linters:
136139
- linters:
137140
- goconst
138141
path: (.+)_test\.go
142+
- linters:
143+
- unused
144+
path: magefiles/* # Exclude magefiles from unused checks as build targets are not imported
139145
paths:
140146
- third_party$
141147
- builtin$
142148
- examples$
143149
formatters:
144150
enable:
145151
- gofmt
152+
- gci
146153
settings:
154+
gci:
155+
sections:
156+
- standard
157+
- default
158+
- blank
159+
- dot
160+
- prefix(github.com/jacobbrewer1)
161+
custom-order: true
147162
gofmt:
148-
simplify: true
163+
simplify: false
149164
rewrite-rules:
150165
- pattern: interface{}
151166
replacement: any

app.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -521,7 +521,9 @@ func (a *App) RedisPool() goredis.Pool {
521521
func (a *App) WorkerPool(name string) pkgsync.WorkerPool {
522522
v, ok := a.workerPools.Load(name)
523523
if !ok {
524-
a.l.Error("worker pool has not been registered", "name", name)
524+
a.l.Error("worker pool has not been registered",
525+
"name", name,
526+
)
525527
panic(fmt.Sprintf("worker pool '%s' has not been registered", name))
526528
}
527529

database/connection.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,9 @@ import (
77
"strconv"
88
"time"
99

10-
_ "github.com/go-sql-driver/mysql"
1110
"github.com/jmoiron/sqlx"
11+
12+
_ "github.com/go-sql-driver/mysql"
1213
)
1314

1415
const (

options.go

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -193,18 +193,38 @@ func WithDatabaseFromVault() StartOption {
193193

194194
// WithInClusterKubeClient is a StartOption that sets up the in-cluster Kubernetes client.
195195
func WithInClusterKubeClient() StartOption {
196+
// Default QPS and Burst values are set to 5 and 10 respectively. This should prevent the client from being rate
197+
// limited by the Kubernetes API server under normal operation. These values can be adjusted based on the
198+
// application's requirements and the cluster's capacity.
199+
return WithRateLimitedInClusterKubernetesClient(5, 10)
200+
}
201+
202+
// WithRateLimitedInClusterKubernetesClient configures the app to set up an in-cluster Kubernetes client with rate limiting.
203+
//
204+
// Parameters:
205+
//
206+
// qps - The maximum number of queries per second (QPS) allowed for the Kubernetes client.
207+
// This controls the rate at which requests are sent to the Kubernetes API server.
208+
// Typical values range from 1 to 100, depending on the application's needs and the cluster's capacity.
209+
// burst - The maximum burst of requests allowed. This is the maximum number of requests that can be sent in a short period.
210+
// Burst should generally be set higher than QPS to allow for short spikes in request rate.
211+
// Typical values are 2x to 5x the QPS value.
212+
//
213+
// Choose values appropriate for your application's expected load and the API server's limits to avoid rate limiting.
214+
func WithRateLimitedInClusterKubernetesClient(qps float32, burst int) StartOption {
196215
return func(a *App) error {
197216
cfg, err := rest.InClusterConfig()
198217
if err != nil {
199-
return fmt.Errorf("failed to get in-cluster config: %w", err)
218+
return fmt.Errorf("error getting in-cluster config: %w", err)
200219
}
201220

202-
kubeClient, err := kubernetes.NewForConfig(cfg)
221+
cfg.QPS = qps
222+
cfg.Burst = burst
223+
224+
a.kubeClient, err = kubernetes.NewForConfig(cfg)
203225
if err != nil {
204-
return fmt.Errorf("failed to create kube client: %w", err)
226+
return fmt.Errorf("error creating in-cluster kube client: %w", err)
205227
}
206-
207-
a.kubeClient = kubeClient
208228
return nil
209229
}
210230
}

vault/auth.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ import (
88

99
hashivault "github.com/hashicorp/vault/api"
1010
kubeauth "github.com/hashicorp/vault/api/auth/kubernetes"
11+
12+
"github.com/jacobbrewer1/web/logging"
1113
)
1214

1315
// kubernetesLogin authenticates with Vault using the Kubernetes auth method.
@@ -47,6 +49,8 @@ func (c *client) renewAuthInfo() {
4749
return authInfo, nil
4850
})
4951
if err != nil {
50-
c.l.Error("unable to renew auth info", "error", err)
52+
c.l.Error("unable to renew auth info",
53+
logging.KeyError, err,
54+
)
5155
}
5256
}

0 commit comments

Comments
 (0)