Skip to content

Commit 6ca41bd

Browse files
authored
Merge pull request #35 from munnerz/simplify-backoff-handling
Simplify issuance backoff handling & improve error messages
2 parents c913514 + ccb95da commit 6ca41bd

File tree

6 files changed

+196
-57
lines changed

6 files changed

+196
-57
lines changed

deploy/cert-manager-csi-driver.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
apiVersion: storage.k8s.io/v1beta1
1+
apiVersion: storage.k8s.io/v1
22
kind: CSIDriver
33
metadata:
44
name: csi.cert-manager.io

deploy/example/example-app.yaml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,15 @@ kind: Namespace
33
metadata:
44
name: sandbox
55
---
6-
apiVersion: cert-manager.io/v1alpha2
6+
apiVersion: cert-manager.io/v1
77
kind: Issuer
88
metadata:
99
name: selfsigned-issuer
1010
namespace: sandbox
1111
spec:
1212
selfSigned: {}
1313
---
14-
apiVersion: cert-manager.io/v1alpha2
14+
apiVersion: cert-manager.io/v1
1515
kind: Certificate
1616
metadata:
1717
name: ca-issuer
@@ -25,7 +25,7 @@ spec:
2525
kind: Issuer
2626
group: cert-manager.io
2727
---
28-
apiVersion: cert-manager.io/v1alpha2
28+
apiVersion: cert-manager.io/v1
2929
kind: Issuer
3030
metadata:
3131
name: ca-issuer

driver/nodeserver.go

Lines changed: 34 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ import (
2626
"github.com/go-logr/logr"
2727
"google.golang.org/grpc/codes"
2828
"google.golang.org/grpc/status"
29-
"k8s.io/apimachinery/pkg/util/wait"
3029
"k8s.io/mount-utils"
3130

3231
"github.com/cert-manager/csi-lib/manager"
@@ -48,8 +47,8 @@ type nodeServer struct {
4847
func (ns *nodeServer) NodePublishVolume(ctx context.Context, req *csi.NodePublishVolumeRequest) (*csi.NodePublishVolumeResponse, error) {
4948
meta := metadata.FromNodePublishVolumeRequest(req)
5049
log := loggerForMetadata(ns.log, meta)
51-
ctx, _ = context.WithTimeout(ctx, time.Second*30)
52-
50+
ctx, cancel := context.WithTimeout(ctx, time.Second*60)
51+
defer cancel()
5352
// clean up after ourselves if provisioning fails.
5453
// this is required because if publishing never succeeds, unpublish is not
5554
// called which leaves files around (and we may continue to renew if so).
@@ -69,6 +68,14 @@ func (ns *nodeServer) NodePublishVolume(ctx context.Context, req *csi.NodePublis
6968
return nil, status.Error(codes.InvalidArgument, "pod.spec.volumes[].csi.readOnly must be set to 'true'")
7069
}
7170

71+
// If continueOnNotReady is enabled, set the NextIssuanceTime in the metadata file to epoch.
72+
// This allows the manager to start management for the volume again on restart if the first
73+
// issuance did not successfully complete.
74+
if meta.NextIssuanceTime == nil && ns.continueOnNotReady {
75+
epoch := time.Time{}
76+
meta.NextIssuanceTime = &epoch
77+
}
78+
7279
if registered, err := ns.store.RegisterMetadata(meta); err != nil {
7380
return nil, err
7481
} else {
@@ -79,32 +86,31 @@ func (ns *nodeServer) NodePublishVolume(ctx context.Context, req *csi.NodePublis
7986
}
8087
}
8188

82-
if err := ns.manager.ManageVolume(req.GetVolumeId()); err != nil {
83-
return nil, err
84-
}
85-
86-
log.Info("Volume registered for management")
87-
88-
// Only wait for the volume to be ready if it is in a state of 'ready to request'
89-
// already. This allows implementors to defer actually requesting certificates
90-
// until later in the pod lifecycle (e.g. after CNI has run & an IP address has been
91-
// allocated, if a user wants to embed pod IPs into their requests).
92-
isReadyToRequest, reason := ns.manager.IsVolumeReadyToRequest(req.GetVolumeId())
93-
if !isReadyToRequest {
94-
log.Info("Unable to request a certificate right now, will be retried", "reason", reason)
95-
}
96-
if isReadyToRequest || !ns.continueOnNotReady {
97-
log.Info("Waiting for certificate to be issued...")
98-
if err := wait.PollUntil(time.Second, func() (done bool, err error) {
99-
return ns.manager.IsVolumeReady(req.GetVolumeId()), nil
100-
}, ctx.Done()); err != nil {
101-
return nil, err
89+
if !ns.manager.IsVolumeReady(req.GetVolumeId()) {
90+
// Only wait for the volume to be ready if it is in a state of 'ready to request'
91+
// already. This allows implementors to defer actually requesting certificates
92+
// until later in the pod lifecycle (e.g. after CNI has run & an IP address has been
93+
// allocated, if a user wants to embed pod IPs into their requests).
94+
isReadyToRequest, reason := ns.manager.IsVolumeReadyToRequest(req.GetVolumeId())
95+
if isReadyToRequest {
96+
log.V(4).Info("Waiting for certificate to be issued...")
97+
if _, err := ns.manager.ManageVolumeImmediate(ctx, req.GetVolumeId()); err != nil {
98+
return nil, err
99+
}
100+
log.Info("Volume registered for management")
101+
} else {
102+
if ns.continueOnNotReady {
103+
log.V(4).Info("Skipping waiting for certificate to be issued")
104+
ns.manager.ManageVolume(req.GetVolumeId())
105+
log.V(4).Info("Volume registered for management")
106+
} else {
107+
log.Info("Unable to request a certificate right now, will be retried", "reason", reason)
108+
return nil, fmt.Errorf("volume is not yet ready to be setup, will be retried: %s", reason)
109+
}
102110
}
103-
} else {
104-
log.Info("Skipping waiting for certificate to be issued")
105111
}
106112

107-
log.Info("Volume ready for mounting")
113+
log.Info("Ensuring data directory for volume is mounted into pod...")
108114
notMnt, err := mount.IsNotMountPoint(ns.mounter, req.GetTargetPath())
109115
switch {
110116
case os.IsNotExist(err):
@@ -118,11 +124,12 @@ func (ns *nodeServer) NodePublishVolume(ctx context.Context, req *csi.NodePublis
118124

119125
if !notMnt {
120126
// Nothing more to do if the targetPath is already a bind mount
127+
log.Info("Volume already mounted to pod, nothing to do")
121128
success = true
122129
return &csi.NodePublishVolumeResponse{}, nil
123130
}
124131

125-
log.Info("Bind mounting data directory to the targetPath")
132+
log.Info("Bind mounting data directory to the pod's mount namespace")
126133
// bind mount the targetPath to the data directory
127134
if err := ns.mounter.Mount(ns.store.PathForVolume(req.GetVolumeId()), req.GetTargetPath(), "", []string{"bind", "ro"}); err != nil {
128135
return nil, err

0 commit comments

Comments
 (0)