Skip to content

Commit 6d52ae0

Browse files
committed
VC: update spec.timeout to be a string
Previously, spec.timeout was a *metav1.Duration. This caused some challenges when creating/updating VaultConnection instances via VSO's API, when the value was 60s or greater the marshalled value would no longer be in valid duration notation and the API request would fail during field validation.
1 parent cb1e7eb commit 6d52ae0

File tree

8 files changed

+20
-21
lines changed

8 files changed

+20
-21
lines changed

api/v1beta1/vaultconnection_types.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ type VaultConnectionSpec struct {
2424
// default timeout from the Vault API client config is used.
2525
// +kubebuilder:validation:Type=string
2626
// +kubebuilder:validation:Pattern="^([0-9]+(\\.[0-9]+)?(s|m|h))$"
27-
Timeout *metav1.Duration `json:"timeout,omitempty"`
27+
Timeout string `json:"timeout,omitempty"`
2828
}
2929

3030
// VaultConnectionStatus defines the observed state of VaultConnection

api/v1beta1/zz_generated.deepcopy.go

Lines changed: 0 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

docs/api/api-reference.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -883,7 +883,7 @@ _Appears in:_
883883
| `tlsServerName` _string_ | TLSServerName to use as the SNI host for TLS connections. | | |
884884
| `caCertSecretRef` _string_ | CACertSecretRef is the name of a Kubernetes secret containing the trusted PEM encoded CA certificate chain as `ca.crt`. | | |
885885
| `skipTLSVerify` _boolean_ | SkipTLSVerify for TLS connections. | false | |
886-
| `timeout` _[Duration](https://kubernetes.io/docs/reference/generated/kubernetes-api/v1.24/#duration-v1-meta)_ | Timeout applied to all Vault requests for this connection. If not set, the<br />default timeout from the Vault API client config is used. | | Pattern: `^([0-9]+(\.[0-9]+)?(s|m|h))$` <br />Type: string <br /> |
886+
| `timeout` _string_ | Timeout applied to all Vault requests for this connection. If not set, the<br />default timeout from the Vault API client config is used. | | Pattern: `^([0-9]+(\.[0-9]+)?(s|m|h))$` <br />Type: string <br /> |
887887

888888

889889

internal/vault/client.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -906,8 +906,12 @@ func NewClientConfigFromConnObj(connObj *secretsv1beta1.VaultConnection, vaultNS
906906
VaultNamespace: vaultNS,
907907
}
908908

909-
if connObj.Spec.Timeout != nil {
910-
cfg.Timeout = connObj.Spec.Timeout
909+
if connObj.Spec.Timeout != "" {
910+
d, err := time.ParseDuration(connObj.Spec.Timeout)
911+
if err != nil {
912+
return nil, fmt.Errorf("failed to parse timeout: %w", err)
913+
}
914+
cfg.Timeout = &d
911915
}
912916
return cfg, nil
913917
}

internal/vault/client_factory_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -346,12 +346,12 @@ func Test_cachingClientFactory_storageEncryptionClient(t *testing.T) {
346346
UID: connUID,
347347
},
348348
Spec: secretsv1beta1.VaultConnectionSpec{
349-
Timeout: &metav1.Duration{Duration: 10 * time.Second},
349+
Timeout: "10s",
350350
},
351351
}
352352

353353
vcObjTimeout5s := vcObj.DeepCopy()
354-
vcObjTimeout5s.Spec.Timeout = &metav1.Duration{Duration: 5 * time.Second}
354+
vcObjTimeout5s.Spec.Timeout = "5s"
355355

356356
saObj := &corev1.ServiceAccount{
357357
ObjectMeta: metav1.ObjectMeta{

internal/vault/client_test.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import (
1818
"golang.org/x/crypto/blake2b"
1919
corev1 "k8s.io/api/core/v1"
2020
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
21+
"k8s.io/utils/ptr"
2122
ctrlclient "sigs.k8s.io/controller-runtime/pkg/client"
2223
"sigs.k8s.io/controller-runtime/pkg/client/fake"
2324

@@ -1168,12 +1169,12 @@ func TestNewClientConfigFromConnObj(t *testing.T) {
11681169
TLSServerName: "baz.biff",
11691170
CACertSecretRef: "ca.crt",
11701171
SkipTLSVerify: true,
1171-
Timeout: &metav1.Duration{Duration: 10 * time.Second},
1172+
Timeout: "10s",
11721173
},
11731174
}
11741175

11751176
connObjNilTimeout := connObjBase.DeepCopy()
1176-
connObjNilTimeout.Spec.Timeout = nil
1177+
connObjNilTimeout.Spec.Timeout = ""
11771178

11781179
tests := []struct {
11791180
name string
@@ -1191,7 +1192,7 @@ func TestNewClientConfigFromConnObj(t *testing.T) {
11911192
TLSServerName: "baz.biff",
11921193
CACertSecretRef: "ca.crt",
11931194
SkipTLSVerify: true,
1194-
Timeout: &metav1.Duration{Duration: 10 * time.Second},
1195+
Timeout: ptr.To[time.Duration](10 * time.Second),
11951196
},
11961197
wantErr: assert.NoError,
11971198
},

internal/vault/config.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,11 @@ import (
77
"context"
88
"crypto/x509"
99
"fmt"
10+
"time"
1011

1112
"github.com/hashicorp/vault/api"
1213
vconsts "github.com/hashicorp/vault/sdk/helper/consts"
1314
v1 "k8s.io/api/core/v1"
14-
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
1515
ctrlclient "sigs.k8s.io/controller-runtime/pkg/client"
1616
"sigs.k8s.io/controller-runtime/pkg/log"
1717

@@ -41,7 +41,7 @@ type ClientConfig struct {
4141
Headers map[string]string
4242
// Timeout applied to all Vault requests. If not set, the default timeout from
4343
// the Vault API client config is used.
44-
Timeout *metav1.Duration `json:"timeout,omitempty"`
44+
Timeout *time.Duration
4545
}
4646

4747
// MakeVaultClient creates a Vault api.Client from a ClientConfig.
@@ -93,7 +93,7 @@ func MakeVaultClient(ctx context.Context, cfg *ClientConfig, client ctrlclient.C
9393
}
9494

9595
if cfg.Timeout != nil {
96-
config.Timeout = cfg.Timeout.Duration
96+
config.Timeout = *cfg.Timeout
9797
}
9898

9999
config.CloneToken = true

internal/vault/config_test.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import (
1717
"github.com/stretchr/testify/require"
1818
corev1 "k8s.io/api/core/v1"
1919
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
20+
"k8s.io/utils/ptr"
2021
"sigs.k8s.io/controller-runtime/pkg/client/fake"
2122

2223
"github.com/hashicorp/vault-secrets-operator/internal/consts"
@@ -81,9 +82,7 @@ func TestMakeVaultClient(t *testing.T) {
8182
"vault timeout not nil": {
8283
vaultConfig: &ClientConfig{
8384
VaultNamespace: "vault-test-namespace",
84-
Timeout: &v1.Duration{
85-
Duration: 10 * time.Second,
86-
},
85+
Timeout: ptr.To[time.Duration](10 * time.Second),
8786
},
8887
CACert: nil,
8988
expectedError: nil,
@@ -165,7 +164,7 @@ func TestMakeVaultClient(t *testing.T) {
165164

166165
var expectedTimeout time.Duration
167166
if tc.vaultConfig.Timeout != nil {
168-
expectedTimeout = tc.vaultConfig.Timeout.Duration
167+
expectedTimeout = *tc.vaultConfig.Timeout
169168
} else {
170169
expectedTimeout = api.DefaultConfig().Timeout
171170
}

0 commit comments

Comments
 (0)