Skip to content

Commit f5192a6

Browse files
nixpanicmergify[bot]
authored andcommitted
connection: track Namespace of driver Pods to locate Leases
The CSIAddonsNode objects are located in the Namespace where CSI-drivers are running. Use this Namespace in the Connection struct, so that the Lease for a group of CSI-Addons ControllerServers can be found. Updates: #422 Signed-off-by: Niels de Vos <ndevos@ibm.com>
1 parent b15ae1a commit f5192a6

File tree

6 files changed

+54
-4
lines changed

6 files changed

+54
-4
lines changed

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ help: ## Display this help.
119119

120120
.PHONY: manifests
121121
manifests: controller-gen kustomize ## Generate WebhookConfiguration, ClusterRole and CustomResourceDefinition objects.
122-
$(CONTROLLER_GEN) rbac:roleName=manager-role crd webhook paths="{./apis/...,./cmd/...,./controllers/...,./sidecar/...}" output:crd:artifacts:config=config/crd/bases
122+
$(CONTROLLER_GEN) rbac:roleName=manager-role crd webhook paths="{./apis/...,./cmd/...,./controllers/...,./internal/...,./sidecar/...}" output:crd:artifacts:config=config/crd/bases
123123
cd config/manager && $(KUSTOMIZE) edit set image controller=${CONTROLLER_IMG} rbac-proxy=${RBAC_PROXY_IMG}
124124
$(KUSTOMIZE) build config/crd > deploy/controller/crds.yaml
125125
$(KUSTOMIZE) build config/rbac > deploy/controller/rbac.yaml

config/rbac/role.yaml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,14 @@ rules:
2121
- get
2222
- list
2323
- watch
24+
- apiGroups:
25+
- coordination.k8s.io
26+
resources:
27+
- leases
28+
verbs:
29+
- get
30+
- list
31+
- watch
2432
- apiGroups:
2533
- ""
2634
resources:

controllers/csiaddons/csiaddonsnode_controller.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ func (r *CSIAddonsNodeReconciler) Reconcile(ctx context.Context, req ctrl.Reques
118118
}
119119

120120
logger.Info("Connecting to sidecar")
121-
newConn, err := connection.NewConnection(ctx, endPoint, nodeID, driverName, csiAddonsNode.Name)
121+
newConn, err := connection.NewConnection(ctx, endPoint, nodeID, driverName, csiAddonsNode.Namespace, csiAddonsNode.Name)
122122
if err != nil {
123123
logger.Error(err, "Failed to establish connection with sidecar")
124124

deploy/controller/rbac.yaml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,14 @@ rules:
7171
- get
7272
- list
7373
- watch
74+
- apiGroups:
75+
- coordination.k8s.io
76+
resources:
77+
- leases
78+
verbs:
79+
- get
80+
- list
81+
- watch
7482
- apiGroups:
7583
- ""
7684
resources:

internal/connection/connection.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ import (
3030
type Connection struct {
3131
Client *grpc.ClientConn
3232
Capabilities []*identity.Capability
33+
Namespace string
3334
Name string
3435
NodeID string
3536
DriverName string
@@ -38,7 +39,7 @@ type Connection struct {
3839

3940
// NewConnection establishes connection with sidecar, fetches capability and returns Connection object
4041
// filled with required information.
41-
func NewConnection(ctx context.Context, endpoint, nodeID, driverName, podName string) (*Connection, error) {
42+
func NewConnection(ctx context.Context, endpoint, nodeID, driverName, namespace, podName string) (*Connection, error) {
4243
opts := []grpc.DialOption{
4344
grpc.WithTransportCredentials(insecure.NewCredentials()),
4445
grpc.WithIdleTimeout(time.Duration(0)),
@@ -50,6 +51,7 @@ func NewConnection(ctx context.Context, endpoint, nodeID, driverName, podName st
5051

5152
conn := &Connection{
5253
Client: cc,
54+
Namespace: namespace,
5355
Name: podName,
5456
NodeID: nodeID,
5557
DriverName: driverName,

internal/connection/connection_pool.go

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ import (
2727
"github.com/csi-addons/kubernetes-csi-addons/internal/util"
2828
)
2929

30+
//+kubebuilder:rbac:groups=coordination.k8s.io,resources=leases,verbs=get;list;watch
31+
3032
// ConnectionPool consists of map of Connection objects and
3133
// methods Put, Get & Delete which operates with required rw locks
3234
// to ensure consistency.
@@ -103,13 +105,43 @@ func (cp *ConnectionPool) GetByNodeID(driverName, nodeID string) map[string]*Con
103105
return result
104106
}
105107

108+
// getNamespaceByDriverName loops through the connections in the pool and
109+
// returns the Namespace of the first connection that matches the driverName.
110+
func (cp *ConnectionPool) getNamespaceByDriverName(driverName string) (string, error) {
111+
cp.rwlock.RLock()
112+
defer cp.rwlock.RUnlock()
113+
114+
for _, conn := range cp.pool {
115+
if conn.DriverName != driverName {
116+
continue
117+
}
118+
119+
return conn.Namespace, nil
120+
}
121+
122+
// should be impossible to get here, all Connections have a Namespace
123+
return "", fmt.Errorf("failed to find the namespace where driver %q is running", driverName)
124+
}
125+
106126
// GetLeaderByDriver finds the holder of the lease for the driver, and returns
107127
// the connection to that particular CSI-Addons sidecar.
108128
func (cp *ConnectionPool) GetLeaderByDriver(ctx context.Context, reconciler client.Client, driverName string) (*Connection, error) {
129+
// detect the Namespace where the driver is deployed
130+
ns, err := cp.getNamespaceByDriverName(driverName)
131+
if err != nil {
132+
return nil, err
133+
}
134+
109135
// get the Lease for the driver
110136
leaseName := util.NormalizeLeaseName(driverName) + "-csi-addons"
111137
var lease coordination.Lease
112-
err := reconciler.Get(ctx, client.ObjectKey{Name: leaseName}, &lease)
138+
err = reconciler.Get(
139+
ctx,
140+
client.ObjectKey{
141+
Namespace: ns,
142+
Name: leaseName,
143+
},
144+
&lease)
113145
if err != nil {
114146
return nil, fmt.Errorf("no leader found for driver %q: %w", driverName, err)
115147
} else if lease.Spec.HolderIdentity == nil || *lease.Spec.HolderIdentity == "" {

0 commit comments

Comments
 (0)