-
Notifications
You must be signed in to change notification settings - Fork 200
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create a common predicate for contaniers ready, use it in Pod reconci…
…ler for instrumentation (#1754) Following #1666, this PR includes the following: 1. Change the predicate for ebpf instrumentation to only pass events where all the containers in a pod become ready. This will fix cases where the reconciliation was called too early and missed the relevant process. 2. Create an `AllContainersReadyPredicate` which is common to the runtime details reconciler (the predicate was added in #1666 ) and the ebpf instrumentation reconciler. 3. Create a common `DeletionPredicate` predicate - for the instrumentation reconcilation it is an OR between the containers ready predicate and the delete one.
- Loading branch information
Showing
7 changed files
with
110 additions
and
115 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
package predicate | ||
|
||
import ( | ||
corev1 "k8s.io/api/core/v1" | ||
"sigs.k8s.io/controller-runtime/pkg/event" | ||
k8scontainer "github.com/odigos-io/odigos/k8sutils/pkg/container" | ||
) | ||
|
||
// AllContainersReadyPredicate is a predicate that checks if all containers in a pod are ready or becoming ready. | ||
// | ||
// For Create events, it returns true if the pod is in Running phase and all containers are ready. | ||
// For Update events, it returns true if the new pod has all containers ready and started, and the old pod had at least one container not ready or not started. | ||
// For Delete events, it returns false. | ||
type AllContainersReadyPredicate struct{} | ||
|
||
func (p *AllContainersReadyPredicate) Create(e event.CreateEvent) bool { | ||
if e.Object == nil { | ||
return false | ||
} | ||
|
||
pod, ok := e.Object.(*corev1.Pod) | ||
if !ok { | ||
return false | ||
} | ||
|
||
allContainersReady := k8scontainer.AllContainersReady(pod) | ||
// If all containers are not ready, return false. | ||
// Otherwise, return true | ||
return allContainersReady | ||
} | ||
|
||
func (p *AllContainersReadyPredicate) Update(e event.UpdateEvent) bool { | ||
if e.ObjectOld == nil || e.ObjectNew == nil { | ||
return false | ||
} | ||
|
||
oldPod, oldOk := e.ObjectOld.(*corev1.Pod) | ||
newPod, newOk := e.ObjectNew.(*corev1.Pod) | ||
|
||
if !oldOk || !newOk { | ||
return false | ||
} | ||
|
||
// First check if all containers in newPod are ready and started | ||
allNewContainersReady := k8scontainer.AllContainersReady(newPod) | ||
|
||
// If new containers aren't all ready, return false | ||
if !allNewContainersReady { | ||
return false | ||
} | ||
|
||
// Now check if any container in oldPod was not ready or not started | ||
allOldContainersReady := k8scontainer.AllContainersReady(oldPod) | ||
|
||
// Return true only if old pods had at least one container not ready/not started | ||
// and new pod has all containers ready/started | ||
return !allOldContainersReady && allNewContainersReady | ||
} | ||
|
||
func (p *AllContainersReadyPredicate) Delete(e event.DeleteEvent) bool { | ||
return false | ||
} | ||
|
||
func (p *AllContainersReadyPredicate) Generic(e event.GenericEvent) bool { | ||
return false | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package predicate | ||
|
||
import ( | ||
"sigs.k8s.io/controller-runtime/pkg/event" | ||
"sigs.k8s.io/controller-runtime/pkg/predicate" | ||
) | ||
|
||
// DeletionPredicate only allows delete events. | ||
type DeletionPredicate struct{} | ||
|
||
func (o DeletionPredicate) Create(e event.CreateEvent) bool { | ||
return false | ||
} | ||
|
||
func (i DeletionPredicate) Update(e event.UpdateEvent) bool { | ||
return false | ||
} | ||
|
||
func (i DeletionPredicate) Delete(e event.DeleteEvent) bool { | ||
return true | ||
} | ||
|
||
func (i DeletionPredicate) Generic(e event.GenericEvent) bool { | ||
return false | ||
} | ||
|
||
var _ predicate.Predicate = &DeletionPredicate{} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters