Skip to content

Commit

Permalink
Use the mutex pool provided by k8s keymutex (#975)
Browse files Browse the repository at this point in the history
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
  • Loading branch information
benashz authored Dec 10, 2024
1 parent 6ab056c commit 3b75144
Show file tree
Hide file tree
Showing 21 changed files with 1,013 additions and 845 deletions.
11 changes: 11 additions & 0 deletions chart/templates/_helpers.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -334,3 +334,14 @@ vaultAuthGlobalRef generates the default VaultAuth spec.vaultAuthGlobalRef.
{{- $ret | toYaml | nindent 4 -}}
{{- end -}}
{{- end -}}

{{/*
clientCache numLocks
*/}}
{{- define "vso.clientCacheNumLocks" -}}
{{- with .Values.controller.manager.clientCache -}}
{{- if or .numLocks (eq .numLocks 0) -}}
--client-cache-num-locks={{ .numLocks }}
{{- end -}}
{{- end -}}
{{- end -}}
3 changes: 3 additions & 0 deletions chart/templates/deployment.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,9 @@ spec:
{{- if .Values.controller.manager.clientCache.cacheSize }}
- --client-cache-size={{ .Values.controller.manager.clientCache.cacheSize }}
{{- end }}
{{- with include "vso.clientCacheNumLocks" . }}
- {{ . }}
{{- end }}
{{- if .Values.controller.manager.maxConcurrentReconciles }}
- --max-concurrent-reconciles={{ .Values.controller.manager.maxConcurrentReconciles }}
{{- end }}
Expand Down
12 changes: 12 additions & 0 deletions chart/values.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,18 @@ controller:
# @type: integer
cacheSize:

# Defines the number of locks to use for the Vault client cache controller.
# May also be set via the `VSO_CLIENT_CACHE_NUM_LOCKS` environment variable.
#
# Setting this value less than 1 will cause the manager to set the number of locks equal
# to the number of logical CPUs of the run host.
#
# See the VSO help output for more information.
#
# default: 100
# @type: integer
numLocks:

# StorageEncryption provides the necessary configuration to encrypt the client storage
# cache within Kubernetes objects using (required) Vault Transit Engine.
# This should only be configured when client cache persistence with encryption is enabled and
Expand Down
2 changes: 1 addition & 1 deletion demo/infra/app/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ terraform {
required_providers {
helm = {
source = "hashicorp/helm"
version = "2.13.1"
version = "2.16.1"
}
kubernetes = {
source = "hashicorp/kubernetes"
Expand Down
4 changes: 3 additions & 1 deletion demo/infra/app/postgres.tf
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@ resource "helm_release" "postgres" {
wait = true
wait_for_jobs = true

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

set {
name = "auth.audit.logConnections"
Expand Down
3 changes: 3 additions & 0 deletions internal/options/env.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@ type VSOEnvOptions struct {

// GlobalVaultAuthOptions is VSO_GLOBAL_VAULT_AUTH_OPTIONS environment variable option
GlobalVaultAuthOptions []string `split_words:"true"`

// ClientCacheNumLocks is VSO_CLIENT_CACHE_NUM_LOCKS environment variable option
ClientCacheNumLocks *int `split_words:"true"`
}

// Parse environment variable options, prefixed with "VSO_"
Expand Down
12 changes: 5 additions & 7 deletions internal/options/env_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"k8s.io/utils/ptr"
)

func TestParse(t *testing.T) {
Expand All @@ -33,19 +34,21 @@ func TestParse(t *testing.T) {
"VSO_BACKOFF_MULTIPLIER": "2.5",
"VSO_GLOBAL_TRANSFORMATION_OPTIONS": "gOpt1,gOpt2",
"VSO_GLOBAL_VAULT_AUTH_OPTIONS": "vOpt1,vOpt2",
"VSO_CLIENT_CACHE_NUM_LOCKS": "10",
},
wantOptions: VSOEnvOptions{
OutputFormat: "json",
ClientCacheSize: makeInt(t, 100),
ClientCacheSize: ptr.To(100),
ClientCachePersistenceModel: "memory",
MaxConcurrentReconciles: makeInt(t, 10),
MaxConcurrentReconciles: ptr.To(10),
BackoffInitialInterval: time.Second * 1,
BackoffMaxInterval: time.Second * 60,
BackoffMaxElapsedTime: time.Hour * 24,
BackoffRandomizationFactor: 0.5,
BackoffMultiplier: 2.5,
GlobalTransformationOptions: []string{"gOpt1", "gOpt2"},
GlobalVaultAuthOptions: []string{"vOpt1", "vOpt2"},
ClientCacheNumLocks: ptr.To(10),
},
},
}
Expand All @@ -61,8 +64,3 @@ func TestParse(t *testing.T) {
})
}
}

func makeInt(t *testing.T, i int) *int {
t.Helper()
return &i
}
9 changes: 9 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,12 @@ func main() {
flag.IntVar(&cfc.ClientCacheSize, "client-cache-size", cfc.ClientCacheSize,
"Size of the in-memory LRU client cache. "+
"Also set from environment variable VSO_CLIENT_CACHE_SIZE.")
// update chart/values.yaml if changing the default value
flag.IntVar(&cfc.ClientCacheNumLocks, "client-cache-num-locks", 100,
"Number of locks to use for the client cache. "+
"Increasing this value may improve performance during Vault client creation, but requires more memory. "+
"When the value is <= 0 the number of locks will be set to the number of logical CPUs of the run host. "+
"Also set from environment variable VSO_CLIENT_CACHE_NUM_LOCKS.")
flag.StringVar(&clientCachePersistenceModel, "client-cache-persistence-model", defaultPersistenceModel,
fmt.Sprintf(
"The type of client cache persistence model that should be employed. "+
Expand Down Expand Up @@ -229,6 +235,9 @@ func main() {
if vsoEnvOptions.ClientCacheSize != nil {
cfc.ClientCacheSize = *vsoEnvOptions.ClientCacheSize
}
if vsoEnvOptions.ClientCacheNumLocks != nil {
cfc.ClientCacheNumLocks = *vsoEnvOptions.ClientCacheNumLocks
}
if vsoEnvOptions.ClientCachePersistenceModel != "" {
clientCachePersistenceModel = vsoEnvOptions.ClientCachePersistenceModel
}
Expand Down
2 changes: 1 addition & 1 deletion test/integration/hcpvaultsecretsapp/terraform/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ terraform {

helm = {
source = "hashicorp/helm"
version = "2.13.1"
version = "2.16.1"
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion test/integration/infra/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ terraform {
required_providers {
helm = {
source = "hashicorp/helm"
version = "2.13.1"
version = "2.16.1"
}
kubernetes = {
source = "hashicorp/kubernetes"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ terraform {
}
helm = {
source = "hashicorp/helm"
version = "2.13.1"
version = "2.16.1"
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion test/integration/operator/terraform/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ terraform {
required_providers {
helm = {
source = "hashicorp/helm"
version = "2.13.1"
version = "2.16.1"
}
kubernetes = {
source = "hashicorp/kubernetes"
Expand Down
2 changes: 1 addition & 1 deletion test/integration/revocation/terraform/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ terraform {
}
helm = {
source = "hashicorp/helm"
version = "2.13.1"
version = "2.16.1"
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion test/integration/vaultauthmethods/terraform/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ terraform {
}
helm = {
source = "hashicorp/helm"
version = "2.13.1"
version = "2.16.1"
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion test/integration/vaultdynamicsecret/terraform/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ terraform {
required_providers {
helm = {
source = "hashicorp/helm"
version = "2.13.1"
version = "2.16.1"
}
kubernetes = {
source = "hashicorp/kubernetes"
Expand Down
4 changes: 3 additions & 1 deletion test/integration/vaultdynamicsecret/terraform/postgres.tf
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@ resource "helm_release" "postgres" {
wait = true
wait_for_jobs = true

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

resource "kubernetes_namespace" "postgres" {
Expand Down
2 changes: 1 addition & 1 deletion test/integration/vaultpkisecret/terraform/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ terraform {
}
helm = {
source = "hashicorp/helm"
version = "2.13.1"
version = "2.16.1"
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion test/integration/vaultstaticsecret/terraform/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ terraform {
}
helm = {
source = "hashicorp/helm"
version = "2.13.1"
version = "2.16.1"
}
}
}
Expand Down
Loading

0 comments on commit 3b75144

Please sign in to comment.