Skip to content

Commit f037599

Browse files
Madhu-1mergify[bot]
authored andcommitted
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 <madhupr007@gmail.com>
1 parent 6c31248 commit f037599

File tree

1 file changed

+25
-4
lines changed

1 file changed

+25
-4
lines changed

sidecar/internal/csiaddonsnode/csiaddonsnode.go

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ import (
3030

3131
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
3232
"k8s.io/apimachinery/pkg/runtime"
33+
"k8s.io/apimachinery/pkg/types"
3334
"k8s.io/apimachinery/pkg/util/wait"
3435
"k8s.io/client-go/kubernetes"
3536
"k8s.io/client-go/rest"
@@ -138,6 +139,8 @@ func (mgr *Manager) newCSIAddonsNode(node *csiaddonsv1alpha1.CSIAddonsNode) erro
138139
csiaddonNode.ResourceVersion = resourceVersion
139140
}
140141
node.Spec.DeepCopyInto(&csiaddonNode.Spec)
142+
// set the ownerReferences
143+
csiaddonNode.ObjectMeta.OwnerReferences = node.ObjectMeta.OwnerReferences
141144
return nil
142145
})
143146

@@ -188,7 +191,10 @@ func (mgr *Manager) getCSIAddonsNode() (*csiaddonsv1alpha1.CSIAddonsNode, error)
188191
return nil, fmt.Errorf("%w: pod has no owner", errInvalidConfig)
189192
}
190193

191-
ownerReferences := []v1.OwnerReference{}
194+
ownerKindForCSIAddonsName := ""
195+
ownerNameForCSIAddonsName := ""
196+
197+
ownerReferences := make([]v1.OwnerReference, 1)
192198
if pod.OwnerReferences[0].Kind == "ReplicaSet" {
193199
// If the pod is owned by a ReplicaSet, we need to get the owner of the ReplicaSet i.e. Deployment
194200
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)
198204
if len(rs.OwnerReferences) == 0 {
199205
return nil, fmt.Errorf("%w: replicaset has no owner", errInvalidConfig)
200206
}
201-
ownerReferences = append(ownerReferences, rs.OwnerReferences[0])
207+
ownerKindForCSIAddonsName = rs.OwnerReferences[0].Kind
208+
ownerNameForCSIAddonsName = rs.OwnerReferences[0].Name
209+
210+
// The pod (created using deployment) might move to new nodes and this might create the
211+
// stale CSIAddonsNode object.
212+
// So, we need to set the pod as the owner for the CSIAddonsNode as we dont store any details
213+
// that are required later on for any other operations like Fencing etc.
214+
ownerReferences[0] = v1.OwnerReference{
215+
APIVersion: "v1",
216+
Kind: "Pod",
217+
Name: pod.Name,
218+
UID: types.UID(mgr.PodUID),
219+
}
202220
} else {
221+
ownerKindForCSIAddonsName = pod.OwnerReferences[0].Kind
222+
ownerNameForCSIAddonsName = pod.OwnerReferences[0].Name
203223
// If the pod is owned by DeamonSet or StatefulSet get the owner of the pod.
204-
ownerReferences = append(ownerReferences, pod.OwnerReferences[0])
224+
ownerReferences[0] = pod.OwnerReferences[0]
205225
}
226+
206227
// we need to have the constant name for the CSIAddonsNode object.
207228
// We will use the nodeID and the ownerName for the CSIAddonsNode object name.
208-
name, err := generateName(mgr.Node, mgr.PodNamespace, ownerReferences[0].Kind, ownerReferences[0].Name)
229+
name, err := generateName(mgr.Node, mgr.PodNamespace, ownerKindForCSIAddonsName, ownerNameForCSIAddonsName)
209230
if err != nil {
210231
return nil, fmt.Errorf("failed to generate name: %w", err)
211232
}

0 commit comments

Comments
 (0)