Skip to content
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

refactor: env record trigger into odigos CRD #1193

Merged
merged 1 commit into from
May 13, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
74 changes: 44 additions & 30 deletions odiglet/pkg/kube/runtime_details/workload_enabled_predicated.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,38 +39,30 @@ func (i *WorkloadEnabledPredicate) Update(e event.UpdateEvent) bool {
newEnabled := isInstrumentationEnabled(e.ObjectNew)
becameEnabled := !oldEnabled && newEnabled

switch e.ObjectNew.GetObjectKind().GroupVersionKind().Kind {
case "Deployment":
oldDeployment, oldOk := e.ObjectOld.(*appsv1.Deployment)
newDeployment, newOk := e.ObjectNew.(*appsv1.Deployment)
if oldOk && newOk {
hadAvailableReplicas := isDeploymentAvailableReplicas(oldDeployment)
hasAvailableReplicas := isDeploymentAvailableReplicas(newDeployment)
replicasBecameAvailable := !hadAvailableReplicas && hasAvailableReplicas
return becameEnabled || replicasBecameAvailable
}
case "DaemonSet":
oldDaemonSet, oldOk := e.ObjectOld.(*appsv1.DaemonSet)
newDaemonSet, newOk := e.ObjectNew.(*appsv1.DaemonSet)
if oldOk && newOk {
hadAvailableReplicas := isDaemonsetAvailableReplicas(oldDaemonSet)
hasAvailableReplicas := isDaemonsetAvailableReplicas(newDaemonSet)
replicasBecameAvailable := !hadAvailableReplicas && hasAvailableReplicas
return becameEnabled || replicasBecameAvailable
}
case "StatefulSet":
oldStatefulSet, oldOk := e.ObjectOld.(*appsv1.StatefulSet)
newStatefulSet, newOk := e.ObjectNew.(*appsv1.StatefulSet)
if oldOk && newOk {
hadAvailableReplicas := isStatefulsetAvailableReplicas(oldStatefulSet)
hasAvailableReplicas := isStatefulsetAvailableReplicas(newStatefulSet)
replicasBecameAvailable := !hadAvailableReplicas && hasAvailableReplicas
return becameEnabled || replicasBecameAvailable
}
if becameEnabled {
return true
}

// for namespace events or if there was issue with type casting
return becameEnabled
replicasBecameAvailable := didReplicasBecomeAvailable(e.ObjectOld, e.ObjectNew)
if replicasBecameAvailable {
return true
}

// The language detection process currently does 2 things:
// 1. Detect the language of each container in the workload
// 2. Detect the actual value of relevant environment variables for each container.
//
// thus, we need to re-run language detection if something that might affect
// any of these 2 things has changed.
//
// currently, we only check if the enabled label has changed, or the pod become available,
// but other events that might affect the language detection are not checked.
// for example: if the container array changed, if an env var was added/removed, if the image was changed, etc.
// we might need to add these checks in the future.
// notice that the change alone is not enough - after the change, the workload running pods still
// run an old manifest. we should re-calculate the runtime details only with up-to-date running pods.

return false
}

func (i *WorkloadEnabledPredicate) Delete(e event.DeleteEvent) bool {
Expand Down Expand Up @@ -102,3 +94,25 @@ func isDaemonsetAvailableReplicas(dep *appsv1.DaemonSet) bool {
func isStatefulsetAvailableReplicas(dep *appsv1.StatefulSet) bool {
return dep.Status.ReadyReplicas > 0
}

// language detection relies on the fact that the workload has available replicas.
// if we did not have available replicas before, and now we do, we need to re-run language detection
func didReplicasBecomeAvailable(old client.Object, new client.Object) bool {

switch new.(type) {
case *appsv1.Deployment:
hadAvailableReplicas := isDeploymentAvailableReplicas(new.(*appsv1.Deployment))
hasAvailableReplicas := isDeploymentAvailableReplicas(old.(*appsv1.Deployment))
return !hadAvailableReplicas && hasAvailableReplicas
case *appsv1.DaemonSet:
hadAvailableReplicas := isDaemonsetAvailableReplicas(new.(*appsv1.DaemonSet))
hasAvailableReplicas := isDaemonsetAvailableReplicas(old.(*appsv1.DaemonSet))
return !hadAvailableReplicas && hasAvailableReplicas
case *appsv1.StatefulSet:
hadAvailableReplicas := isStatefulsetAvailableReplicas(new.(*appsv1.StatefulSet))
hasAvailableReplicas := isStatefulsetAvailableReplicas(old.(*appsv1.StatefulSet))
return !hadAvailableReplicas && hasAvailableReplicas
}

return false
}
Loading