Skip to content

Commit 3b75144

Browse files
authored
Use the mutex pool provided by k8s keymutex (#975)
The previous client cache locking scheme was not thread safe, and allocated more locks than are typically needed. This change replaces that approach by using the KeyMutex provided by the k8s utils package. Locks are now a pooled resource. Other fixes - update invalid Bitnami Helm chart repo for postgres. We should phasing out its use. - Bump TF Helm to latest version - demo: update the postgres chart URL - lint bats tests
1 parent 6ab056c commit 3b75144

File tree

21 files changed

+1013
-845
lines changed

21 files changed

+1013
-845
lines changed

chart/templates/_helpers.tpl

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -334,3 +334,14 @@ vaultAuthGlobalRef generates the default VaultAuth spec.vaultAuthGlobalRef.
334334
{{- $ret | toYaml | nindent 4 -}}
335335
{{- end -}}
336336
{{- end -}}
337+
338+
{{/*
339+
clientCache numLocks
340+
*/}}
341+
{{- define "vso.clientCacheNumLocks" -}}
342+
{{- with .Values.controller.manager.clientCache -}}
343+
{{- if or .numLocks (eq .numLocks 0) -}}
344+
--client-cache-num-locks={{ .numLocks }}
345+
{{- end -}}
346+
{{- end -}}
347+
{{- end -}}

chart/templates/deployment.yaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,9 @@ spec:
7575
{{- if .Values.controller.manager.clientCache.cacheSize }}
7676
- --client-cache-size={{ .Values.controller.manager.clientCache.cacheSize }}
7777
{{- end }}
78+
{{- with include "vso.clientCacheNumLocks" . }}
79+
- {{ . }}
80+
{{- end }}
7881
{{- if .Values.controller.manager.maxConcurrentReconciles }}
7982
- --max-concurrent-reconciles={{ .Values.controller.manager.maxConcurrentReconciles }}
8083
{{- end }}

chart/values.yaml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -267,6 +267,18 @@ controller:
267267
# @type: integer
268268
cacheSize:
269269

270+
# Defines the number of locks to use for the Vault client cache controller.
271+
# May also be set via the `VSO_CLIENT_CACHE_NUM_LOCKS` environment variable.
272+
#
273+
# Setting this value less than 1 will cause the manager to set the number of locks equal
274+
# to the number of logical CPUs of the run host.
275+
#
276+
# See the VSO help output for more information.
277+
#
278+
# default: 100
279+
# @type: integer
280+
numLocks:
281+
270282
# StorageEncryption provides the necessary configuration to encrypt the client storage
271283
# cache within Kubernetes objects using (required) Vault Transit Engine.
272284
# This should only be configured when client cache persistence with encryption is enabled and

demo/infra/app/main.tf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ terraform {
55
required_providers {
66
helm = {
77
source = "hashicorp/helm"
8-
version = "2.13.1"
8+
version = "2.16.1"
99
}
1010
kubernetes = {
1111
source = "hashicorp/kubernetes"

demo/infra/app/postgres.tf

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,10 @@ resource "helm_release" "postgres" {
88
wait = true
99
wait_for_jobs = true
1010

11-
repository = "https://charts.bitnami.com/bitnami"
11+
# ref: https://github.com/bitnami/charts/issues/30582#issuecomment-2494545610
12+
repository = "oci://registry-1.docker.io/bitnamicharts"
1213
chart = "postgresql"
14+
version = "16.2.2"
1315

1416
set {
1517
name = "auth.audit.logConnections"

internal/options/env.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,9 @@ type VSOEnvOptions struct {
4646

4747
// GlobalVaultAuthOptions is VSO_GLOBAL_VAULT_AUTH_OPTIONS environment variable option
4848
GlobalVaultAuthOptions []string `split_words:"true"`
49+
50+
// ClientCacheNumLocks is VSO_CLIENT_CACHE_NUM_LOCKS environment variable option
51+
ClientCacheNumLocks *int `split_words:"true"`
4952
}
5053

5154
// Parse environment variable options, prefixed with "VSO_"

internal/options/env_test.go

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99

1010
"github.com/stretchr/testify/assert"
1111
"github.com/stretchr/testify/require"
12+
"k8s.io/utils/ptr"
1213
)
1314

1415
func TestParse(t *testing.T) {
@@ -33,19 +34,21 @@ func TestParse(t *testing.T) {
3334
"VSO_BACKOFF_MULTIPLIER": "2.5",
3435
"VSO_GLOBAL_TRANSFORMATION_OPTIONS": "gOpt1,gOpt2",
3536
"VSO_GLOBAL_VAULT_AUTH_OPTIONS": "vOpt1,vOpt2",
37+
"VSO_CLIENT_CACHE_NUM_LOCKS": "10",
3638
},
3739
wantOptions: VSOEnvOptions{
3840
OutputFormat: "json",
39-
ClientCacheSize: makeInt(t, 100),
41+
ClientCacheSize: ptr.To(100),
4042
ClientCachePersistenceModel: "memory",
41-
MaxConcurrentReconciles: makeInt(t, 10),
43+
MaxConcurrentReconciles: ptr.To(10),
4244
BackoffInitialInterval: time.Second * 1,
4345
BackoffMaxInterval: time.Second * 60,
4446
BackoffMaxElapsedTime: time.Hour * 24,
4547
BackoffRandomizationFactor: 0.5,
4648
BackoffMultiplier: 2.5,
4749
GlobalTransformationOptions: []string{"gOpt1", "gOpt2"},
4850
GlobalVaultAuthOptions: []string{"vOpt1", "vOpt2"},
51+
ClientCacheNumLocks: ptr.To(10),
4952
},
5053
},
5154
}
@@ -61,8 +64,3 @@ func TestParse(t *testing.T) {
6164
})
6265
}
6366
}
64-
65-
func makeInt(t *testing.T, i int) *int {
66-
t.Helper()
67-
return &i
68-
}

main.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,12 @@ func main() {
158158
flag.IntVar(&cfc.ClientCacheSize, "client-cache-size", cfc.ClientCacheSize,
159159
"Size of the in-memory LRU client cache. "+
160160
"Also set from environment variable VSO_CLIENT_CACHE_SIZE.")
161+
// update chart/values.yaml if changing the default value
162+
flag.IntVar(&cfc.ClientCacheNumLocks, "client-cache-num-locks", 100,
163+
"Number of locks to use for the client cache. "+
164+
"Increasing this value may improve performance during Vault client creation, but requires more memory. "+
165+
"When the value is <= 0 the number of locks will be set to the number of logical CPUs of the run host. "+
166+
"Also set from environment variable VSO_CLIENT_CACHE_NUM_LOCKS.")
161167
flag.StringVar(&clientCachePersistenceModel, "client-cache-persistence-model", defaultPersistenceModel,
162168
fmt.Sprintf(
163169
"The type of client cache persistence model that should be employed. "+
@@ -229,6 +235,9 @@ func main() {
229235
if vsoEnvOptions.ClientCacheSize != nil {
230236
cfc.ClientCacheSize = *vsoEnvOptions.ClientCacheSize
231237
}
238+
if vsoEnvOptions.ClientCacheNumLocks != nil {
239+
cfc.ClientCacheNumLocks = *vsoEnvOptions.ClientCacheNumLocks
240+
}
232241
if vsoEnvOptions.ClientCachePersistenceModel != "" {
233242
clientCachePersistenceModel = vsoEnvOptions.ClientCachePersistenceModel
234243
}

test/integration/hcpvaultsecretsapp/terraform/main.tf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ terraform {
99

1010
helm = {
1111
source = "hashicorp/helm"
12-
version = "2.13.1"
12+
version = "2.16.1"
1313
}
1414
}
1515
}

test/integration/infra/main.tf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ terraform {
55
required_providers {
66
helm = {
77
source = "hashicorp/helm"
8-
version = "2.13.1"
8+
version = "2.16.1"
99
}
1010
kubernetes = {
1111
source = "hashicorp/kubernetes"

0 commit comments

Comments
 (0)