From 95b6bd14ac606e6219b0d9deaa55b961fbc39dc0 Mon Sep 17 00:00:00 2001 From: Madhu Rajanna Date: Wed, 27 Nov 2024 11:37:29 +0100 Subject: [PATCH 1/2] use NormalizeLeaseName to store the key namespace + "/" + leader identity(pod name) is the key for the connection. this key is used by GetLeaderByDriver to get the connection. csi-lib-utils/leaderelection:sanitizeName() is used to sanitize the leader identity used for the leases csiaddonsnode need to store the key with same format so that it can be used to get the connection. Signed-off-by: Madhu Rajanna (cherry picked from commit 6c312483aae0bfe47f4a8ce7a19ee11ec6be912d) --- internal/controller/csiaddons/csiaddonsnode_controller.go | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/internal/controller/csiaddons/csiaddonsnode_controller.go b/internal/controller/csiaddons/csiaddonsnode_controller.go index 50d161b0d..266c19458 100644 --- a/internal/controller/csiaddons/csiaddonsnode_controller.go +++ b/internal/controller/csiaddons/csiaddonsnode_controller.go @@ -105,7 +105,9 @@ func (r *CSIAddonsNodeReconciler) Reconcile(ctx context.Context, req ctrl.Reques // namespace + "/" + leader identity(pod name) is the key for the connection. // this key is used by GetLeaderByDriver to get the connection - key := csiAddonsNode.Namespace + "/" + podName + // util.NormalizeLeaseName() is used to sanitize the leader identity used for the leases + // csiaddonsnode need to store the key with same format so that it can be used to get the connection. + key := csiAddonsNode.Namespace + "/" + util.NormalizeLeaseName(podName) logger = logger.WithValues("EndPoint", endPoint) From 06fad896aabd9dcf6bc241e1f0808e7caa08c44a Mon Sep 17 00:00:00 2001 From: Madhu Rajanna Date: Wed, 27 Nov 2024 11:39:13 +0100 Subject: [PATCH 2/2] set pod as the ownerRef for the csiaddons The pod might move to new nodes and this might create the stale CSIAddonsNode object. so, we need to set the pod as the owner for the deployment as we dont store any details that are required later on for any other operations like Fencing etc. Signed-off-by: Madhu Rajanna (cherry picked from commit f037599d69ee98b65d0ce45745ec4ba59ec8aced) --- .../internal/csiaddonsnode/csiaddonsnode.go | 29 ++++++++++++++++--- 1 file changed, 25 insertions(+), 4 deletions(-) diff --git a/sidecar/internal/csiaddonsnode/csiaddonsnode.go b/sidecar/internal/csiaddonsnode/csiaddonsnode.go index 402cf7886..a4af1e676 100644 --- a/sidecar/internal/csiaddonsnode/csiaddonsnode.go +++ b/sidecar/internal/csiaddonsnode/csiaddonsnode.go @@ -30,6 +30,7 @@ import ( v1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/runtime" + "k8s.io/apimachinery/pkg/types" "k8s.io/apimachinery/pkg/util/wait" "k8s.io/client-go/kubernetes" "k8s.io/client-go/rest" @@ -138,6 +139,8 @@ func (mgr *Manager) newCSIAddonsNode(node *csiaddonsv1alpha1.CSIAddonsNode) erro csiaddonNode.ResourceVersion = resourceVersion } node.Spec.DeepCopyInto(&csiaddonNode.Spec) + // set the ownerReferences + csiaddonNode.ObjectMeta.OwnerReferences = node.ObjectMeta.OwnerReferences return nil }) @@ -188,7 +191,10 @@ func (mgr *Manager) getCSIAddonsNode() (*csiaddonsv1alpha1.CSIAddonsNode, error) return nil, fmt.Errorf("%w: pod has no owner", errInvalidConfig) } - ownerReferences := []v1.OwnerReference{} + ownerKindForCSIAddonsName := "" + ownerNameForCSIAddonsName := "" + + ownerReferences := make([]v1.OwnerReference, 1) if pod.OwnerReferences[0].Kind == "ReplicaSet" { // If the pod is owned by a ReplicaSet, we need to get the owner of the ReplicaSet i.e. Deployment rs, err := mgr.KubeClient.AppsV1().ReplicaSets(mgr.PodNamespace).Get(context.TODO(), pod.OwnerReferences[0].Name, v1.GetOptions{}) @@ -198,14 +204,29 @@ func (mgr *Manager) getCSIAddonsNode() (*csiaddonsv1alpha1.CSIAddonsNode, error) if len(rs.OwnerReferences) == 0 { return nil, fmt.Errorf("%w: replicaset has no owner", errInvalidConfig) } - ownerReferences = append(ownerReferences, rs.OwnerReferences[0]) + ownerKindForCSIAddonsName = rs.OwnerReferences[0].Kind + ownerNameForCSIAddonsName = rs.OwnerReferences[0].Name + + // The pod (created using deployment) might move to new nodes and this might create the + // stale CSIAddonsNode object. + // So, we need to set the pod as the owner for the CSIAddonsNode as we dont store any details + // that are required later on for any other operations like Fencing etc. + ownerReferences[0] = v1.OwnerReference{ + APIVersion: "v1", + Kind: "Pod", + Name: pod.Name, + UID: types.UID(mgr.PodUID), + } } else { + ownerKindForCSIAddonsName = pod.OwnerReferences[0].Kind + ownerNameForCSIAddonsName = pod.OwnerReferences[0].Name // If the pod is owned by DeamonSet or StatefulSet get the owner of the pod. - ownerReferences = append(ownerReferences, pod.OwnerReferences[0]) + ownerReferences[0] = pod.OwnerReferences[0] } + // we need to have the constant name for the CSIAddonsNode object. // We will use the nodeID and the ownerName for the CSIAddonsNode object name. - name, err := generateName(mgr.Node, mgr.PodNamespace, ownerReferences[0].Kind, ownerReferences[0].Name) + name, err := generateName(mgr.Node, mgr.PodNamespace, ownerKindForCSIAddonsName, ownerNameForCSIAddonsName) if err != nil { return nil, fmt.Errorf("failed to generate name: %w", err) }