Skip to content

Commit 6bd5eaf

Browse files
Fix rotation enable check
1 parent 4df9a6d commit 6bd5eaf

File tree

5 files changed

+29
-38
lines changed

5 files changed

+29
-38
lines changed

controllers/secretproviderclasspodstatus_controller.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -419,13 +419,12 @@ func (r *SecretProviderClassPodStatusReconciler) createOrUpdateK8sSecret(ctx con
419419
return err
420420
}
421421

422-
klog.InfoS("Kubernetes secret is already created", "secret", klog.ObjectRef{Namespace: namespace, Name: name})
422+
klog.V(5).InfoS("Kubernetes secret is already created", "secret", klog.ObjectRef{Namespace: namespace, Name: name})
423423
err = r.writer.Update(ctx, secret)
424424
if err != nil {
425-
klog.ErrorS(err, "Unable to update kubernetes secret", "secret", klog.ObjectRef{Namespace: namespace, Name: name})
426425
return err
427426
}
428-
klog.InfoS("successfully updated Kubernetes secret", "secret", klog.ObjectRef{Namespace: namespace, Name: name})
427+
klog.V(5).InfoS("successfully updated Kubernetes secret", "secret", klog.ObjectRef{Namespace: namespace, Name: name})
429428
return nil
430429
}
431430

manifest_staging/charts/secrets-store-csi-driver/values.yaml

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,7 @@ syncSecret:
219219
enabled: false
220220

221221
## Enable secret rotation feature [alpha]
222+
## If this is set to true, make sure that `requiresRepublish` is true.
222223
enableSecretRotation: false
223224

224225
## Secret rotation poll interval duration
@@ -232,16 +233,11 @@ providerHealthCheckInterval: 2m
232233

233234
imagePullSecrets: []
234235

235-
## This allows CSI drivers to impersonate the pods that they mount the volumes for.
236-
# refer to https://kubernetes-csi.github.io/docs/token-requests.html for more details.
237-
# Supported only for Kubernetes v1.20+
238236
tokenRequests: []
239237
# - audience: aud1
240238
# - audience: aud2
241239

242-
# To set the requiresRepublish which can be used to refresh the mounted secret periodically
243-
# refer to https://kubernetes-csi.github.io/docs/token-requests.html for more details.
244-
# Supported only for Kubernetes v1.20+
240+
# this will be set to true even if enableSecretRotation is true
245241
requiresRepublish: true
246242
# -- Labels to apply to all resources
247243
commonLabels: {}

pkg/secrets-store/nodeserver.go

Lines changed: 14 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,16 @@ func (ns *nodeServer) NodePublishVolume(ctx context.Context, req *csi.NodePublis
121121
podNamespace = attrib[CSIPodNamespace]
122122
podUID = attrib[CSIPodUID]
123123

124+
if rotationEnabled {
125+
lastModificationTime, err := ns.getLastUpdateTime(targetPath)
126+
if err != nil {
127+
klog.InfoS("could not find last modification time for targetpath", targetPath, "error", err)
128+
} else if startTime.Before(lastModificationTime.Add(ns.rotationConfig.rotationCacheDuration)) {
129+
// if next rotation is not yet due, then skip the mount operation
130+
return &csi.NodePublishVolumeResponse{}, nil
131+
}
132+
}
133+
124134
mounted, err = ns.ensureMountPoint(targetPath)
125135
if err != nil {
126136
// kubelet will not create the CSI NodePublishVolume target directory in 1.20+, in accordance with the CSI specification.
@@ -143,16 +153,6 @@ func (ns *nodeServer) NodePublishVolume(ctx context.Context, req *csi.NodePublis
143153
return &csi.NodePublishVolumeResponse{}, nil
144154
}
145155

146-
if rotationEnabled {
147-
lastModificationTime, err := ns.getLastUpdateTime(targetPath)
148-
if err != nil {
149-
klog.InfoS("could not find last modification time for targetpath", targetPath, "error", err)
150-
} else if startTime.Before(lastModificationTime.Add(ns.rotationConfig.rotationPollInterval)) {
151-
// if next rotation is not yet due, then skip the mount operation
152-
return &csi.NodePublishVolumeResponse{}, nil
153-
}
154-
}
155-
156156
klog.V(2).InfoS("node publish volume", "target", targetPath, "volumeId", volumeID, "mount flags", mountFlags)
157157

158158
if isMockProvider(providerName) {
@@ -192,16 +192,12 @@ func (ns *nodeServer) NodePublishVolume(ctx context.Context, req *csi.NodePublis
192192
for k, v := range attrib {
193193
parameters[k] = v
194194
}
195-
// csi.storage.k8s.io/serviceAccount.tokens is empty for Kubernetes version < 1.20.
196-
// For 1.20+, if tokenRequests is set in the CSI driver spec, kubelet will generate
197-
// a token for the pod and send it to the CSI driver.
198-
// This check is done for backward compatibility to support passing token from driver
199-
// to provider irrespective of the Kubernetes version. If the token doesn't exist in the
200-
// volume request context, the CSI driver will generate the token for the configured audience
201-
// and send it to the provider in the parameters.
195+
// If the token doesn't exist in the volume request context, the CSI driver
196+
// will generate the token for the configured audience and send it to the
197+
// provider in the parameters.
202198
if parameters[CSIPodServiceAccountTokens] == "" {
203199
// Inject pod service account token into volume attributes
204-
klog.Info(err, "csi.storage.k8s.io/serviceAccount.tokens is not populated")
200+
klog.Warning("csi.storage.k8s.io/serviceAccount.tokens is not populated")
205201
}
206202

207203
// ensure it's read-only

pkg/secrets-store/nodeserver_test.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -297,8 +297,8 @@ func TestNodePublishVolume(t *testing.T) {
297297
},
298298
},
299299
rotationConfig: &rotationConfig{
300-
enabled: false,
301-
rotationPollInterval: time.Minute,
300+
enabled: false,
301+
rotationCacheDuration: time.Minute,
302302
},
303303
},
304304
{
@@ -331,8 +331,8 @@ func TestNodePublishVolume(t *testing.T) {
331331
},
332332
},
333333
rotationConfig: &rotationConfig{
334-
enabled: true,
335-
rotationPollInterval: -1 * time.Minute, // Using negative interval to pass the rotation interval check in unit tests
334+
enabled: true,
335+
rotationCacheDuration: -1 * time.Minute, // Using negative interval to pass the rotation interval check in unit tests
336336
},
337337
},
338338
{
@@ -362,8 +362,8 @@ func TestNodePublishVolume(t *testing.T) {
362362
},
363363
},
364364
rotationConfig: &rotationConfig{
365-
enabled: true,
366-
rotationPollInterval: time.Minute,
365+
enabled: true,
366+
rotationCacheDuration: time.Minute,
367367
},
368368
},
369369
}
@@ -407,7 +407,7 @@ func TestNodePublishVolume(t *testing.T) {
407407
t.Fatalf("expected err to be nil, got: %v", err)
408408
}
409409
expectedMounts := 1
410-
if ns.rotationConfig.enabled && ns.rotationConfig.rotationPollInterval > 0 {
410+
if ns.rotationConfig.enabled && ns.rotationConfig.rotationCacheDuration > 0 {
411411
// If due to rotation interval, NodePublishVolume has skipped, there should not be any mount operation
412412
expectedMounts = 0
413413
}

pkg/secrets-store/secrets-store.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,8 @@ type SecretsStore struct {
4040

4141
// rotationConfig stores the information required to rotate the secrets.
4242
type rotationConfig struct {
43-
enabled bool
44-
rotationPollInterval time.Duration
43+
enabled bool
44+
rotationCacheDuration time.Duration // After this much duration, NodePublishVolume will be acted.
4545
}
4646

4747
func NewSecretsStoreDriver(driverName, nodeID, endpoint string,
@@ -91,8 +91,8 @@ func newNodeServer(nodeID string,
9191

9292
func newRotationConfig(enabled bool, interval time.Duration) *rotationConfig {
9393
return &rotationConfig{
94-
enabled: enabled,
95-
rotationPollInterval: interval,
94+
enabled: enabled,
95+
rotationCacheDuration: interval,
9696
}
9797
}
9898

0 commit comments

Comments
 (0)