Skip to content

Commit b007a75

Browse files
csiaddonsnode: delete the object after max connection retries
This patch adds the functionality to retry for a maximum of `CSIAddonsNodeConnectionMaxRetries` to connect to the sidecar. If the connection attempt is not successful, the object is considered obsolete and is deleted. An in-memory retry counter is used to keep things simple and to avoid updating the object's status/annotations. Signed-off-by: Niraj Yadav <niryadav@redhat.com>
1 parent 40a8a6c commit b007a75

File tree

1 file changed

+38
-12
lines changed

1 file changed

+38
-12
lines changed

internal/controller/csiaddons/csiaddonsnode_controller.go

Lines changed: 38 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import (
2424
"net/url"
2525
"slices"
2626
"strings"
27+
"time"
2728

2829
csiaddonsv1alpha1 "github.com/csi-addons/kubernetes-csi-addons/api/csiaddons/v1alpha1"
2930
"github.com/csi-addons/kubernetes-csi-addons/internal/connection"
@@ -42,6 +43,11 @@ import (
4243
"sigs.k8s.io/controller-runtime/pkg/predicate"
4344
)
4445

46+
const (
47+
CSIAddonsNodeConnectionMaxRetries = 3
48+
CSIAddonsNodeConnectionSleepInterval = 2 * time.Second
49+
)
50+
4551
var (
4652
csiAddonsNodeFinalizer = csiaddonsv1alpha1.GroupVersion.Group + "/csiaddonsnode"
4753
)
@@ -126,22 +132,42 @@ func (r *CSIAddonsNodeReconciler) Reconcile(ctx context.Context, req ctrl.Reques
126132
return ctrl.Result{}, err
127133
}
128134

129-
logger.Info("Connecting to sidecar")
130-
newConn, err := connection.NewConnection(ctx, endPoint, nodeID, driverName, csiAddonsNode.Namespace, csiAddonsNode.Name, r.EnableAuth)
131-
if err != nil {
132-
logger.Error(err, "Failed to establish connection with sidecar")
135+
// The reconciler is stateless and if we delete an object that is still valid
136+
// it will be recreated (by the sidecar). Use an in-memory retry loop to keep
137+
// things simple. If we wanted to preserve state we would have had to rely
138+
// on the CRD's status/annotations.
139+
var newConn *connection.Connection
140+
var connErr error
141+
for i := range CSIAddonsNodeConnectionMaxRetries {
142+
logger.Info("Connecting to sidecar", "attempt", i)
143+
newConn, connErr = connection.NewConnection(ctx, endPoint, nodeID, driverName, csiAddonsNode.Namespace, csiAddonsNode.Name, r.EnableAuth)
144+
145+
// Success, exit early. Logged later after getting fence client status
146+
if connErr == nil {
147+
break
148+
}
133149

134-
errMessage := util.GetErrorMessage(err)
135-
csiAddonsNode.Status.State = csiaddonsv1alpha1.CSIAddonsNodeStateFailed
136-
csiAddonsNode.Status.Message = fmt.Sprintf("Failed to establish connection with sidecar: %v", errMessage)
137-
statusErr := r.Status().Update(ctx, csiAddonsNode)
138-
if statusErr != nil {
139-
logger.Error(statusErr, "Failed to update status")
150+
// Do not spam the socket
151+
if i < CSIAddonsNodeConnectionMaxRetries-1 {
152+
time.Sleep(CSIAddonsNodeConnectionSleepInterval)
153+
}
154+
}
140155

141-
return ctrl.Result{}, statusErr
156+
// If we were still unable to connect after max retries
157+
if connErr != nil {
158+
logger.Error(connErr, fmt.Sprintf("Failed to establish connection with sidecar after %d attempts, deleting the object", CSIAddonsNodeConnectionMaxRetries))
159+
160+
// We do not update the status anymore as we consider deletion
161+
// as the resolution after max attempts.
162+
if delErr := r.Delete(ctx, csiAddonsNode); client.IgnoreNotFound(delErr) != nil {
163+
logger.Error(delErr, "failed to delete CSIAddonsNode object after max retries")
164+
165+
return ctrl.Result{}, delErr
142166
}
143167

144-
return ctrl.Result{}, err
168+
// Object is deleted, stop the reconcile phase
169+
logger.Info("successfully deleted CSIAddonsNode object due to reaching max reconnection attempts")
170+
return ctrl.Result{}, nil
145171
}
146172

147173
nfsc, err := r.getNetworkFenceClientStatus(ctx, &logger, newConn, csiAddonsNode)

0 commit comments

Comments
 (0)