-
Notifications
You must be signed in to change notification settings - Fork 29
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix race condition in strict mode #306
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -197,6 +197,33 @@ func (r *PolicyEndpointsReconciler) cleanUpPolicyEndpoint(ctx context.Context, r | |
return nil | ||
} | ||
|
||
func (r *PolicyEndpointsReconciler) isProgFdShared(ctx context.Context, targetPodName string, | ||
targetPodNamespace string) bool { | ||
targetpodNamespacedName := utils.GetPodNamespacedName(targetPodName, targetPodNamespace) | ||
var foundSharedIngress bool | ||
var foundSharedEgress bool | ||
if targetProgFD, ok := r.ebpfClient.GetIngressPodToProgMap().Load(targetpodNamespacedName); ok { | ||
if currentList, ok := r.ebpfClient.GetIngressProgToPodsMap().Load(targetProgFD); ok { | ||
podsList, ok := currentList.(map[string]struct{}) | ||
if ok && len(podsList) > 1 { | ||
foundSharedIngress = true | ||
r.log.Info("isProgFdShared", "Found shared ingress progFD for target: ", targetPodName, "progFD: ", targetProgFD) | ||
} | ||
} | ||
} | ||
|
||
if targetProgFD, ok := r.ebpfClient.GetEgressPodToProgMap().Load(targetpodNamespacedName); ok { | ||
if currentList, ok := r.ebpfClient.GetEgressProgToPodsMap().Load(targetProgFD); ok { | ||
podsList, ok := currentList.(map[string]struct{}) | ||
if ok && len(podsList) > 1 { | ||
foundSharedEgress = true | ||
r.log.Info("isProgFdShared", "Found shared egress progFD for target: ", targetPodName, "progFD: ", targetProgFD) | ||
} | ||
} | ||
} | ||
return foundSharedIngress || foundSharedEgress | ||
} | ||
|
||
func (r *PolicyEndpointsReconciler) updatePolicyEnforcementStatusForPods(ctx context.Context, policyEndpointName string, | ||
targetPods []types.NamespacedName, podIdentifiers map[string]bool, isDeleteFlow bool) error { | ||
var err error | ||
|
@@ -211,11 +238,10 @@ func (r *PolicyEndpointsReconciler) updatePolicyEnforcementStatusForPods(ctx con | |
deletePinPath := true | ||
podIdentifier := utils.GetPodIdentifier(targetPod.Name, targetPod.Namespace, r.log) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do we still need podIdentifier? Seems like only used in the below log? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah we can remove that one. I will add optimization and clean up of this line in next PR |
||
r.log.Info("Derived ", "Pod identifier to check if update is needed : ", podIdentifier) | ||
//Derive the podIdentifier and check if there is another pod in the same replicaset using the pinpath | ||
if found, ok := podIdentifiers[podIdentifier]; ok { | ||
//podIdentifiers will always have true in the value if found.. | ||
r.log.Info("PodIdentifier pinpath ", "shared: ", found) | ||
deletePinPath = !found | ||
// check if ebpf progs are being shared by any other pods | ||
if progFdShared := r.isProgFdShared(ctx, targetPod.Name, targetPod.Namespace); progFdShared { | ||
r.log.Info("ProgFD pinpath ", "shared: ", progFdShared) | ||
deletePinPath = !progFdShared | ||
} | ||
|
||
cleanupErr := r.cleanupeBPFProbes(ctx, targetPod, policyEndpointName, deletePinPath, isDeleteFlow) | ||
|
@@ -584,9 +610,17 @@ func (r *PolicyEndpointsReconciler) getPodListToBeCleanedUp(oldPodSet []types.Na | |
break | ||
} | ||
} | ||
// We want to clean up the pod and detach ebpf probes in two cases | ||
// 1. When pod is still running but pod is not an active pod against policy endpoint which implies policy endpoint is no longer applied to the podIdentifier | ||
// 2. When pod is deleted and is the last pod in the PodIdentifier | ||
if !activePod && !podIdentifiers[oldPodIdentifier] { | ||
r.log.Info("Pod to cleanup: ", "name: ", oldPod.Name, "namespace: ", oldPod.Namespace) | ||
podsToBeCleanedUp = append(podsToBeCleanedUp, oldPod) | ||
// When pod is deleted and this is not the last pod in podIdentifier, we are just updating the podName and progFD local caches | ||
} else if !activePod { | ||
r.log.Info("Pod not active. Deleting from progPod caches", "podName: ", oldPod.Name, "podNamespace: ", oldPod.Namespace) | ||
r.ebpfClient.DeletePodFromIngressProgPodCaches(oldPod.Name, oldPod.Namespace) | ||
r.ebpfClient.DeletePodFromEgressProgPodCaches(oldPod.Name, oldPod.Namespace) | ||
} | ||
} | ||
|
||
|
@@ -651,7 +685,6 @@ func (r *PolicyEndpointsReconciler) deletePolicyEndpointFromPodIdentifierMap(ctx | |
policyEndpoint string) { | ||
r.podIdentifierToPolicyEndpointMapMutex.Lock() | ||
defer r.podIdentifierToPolicyEndpointMapMutex.Unlock() | ||
|
||
var currentPEList []string | ||
if policyEndpointList, ok := r.podIdentifierToPolicyEndpointMap.Load(podIdentifier); ok { | ||
for _, policyEndpointName := range policyEndpointList.([]string) { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we can optimize this as a pod will always have both ingress and egress probes attached (i.e.,) there will never be a scenario where only one of the probes is attached for any particular pod. Probably can collapse the maps in to one unless there are plans to use them for something else.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@achevuru Yeah using one just one map to store reverse mapping from progFds to Pods list makes sense. I will just add the ingress one and we can remove the new egress map
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We will update this optimization in a separate PR as this is change is blocking cx