forked from odigos-io/odigos
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
1d8eba9
commit 46e278c
Showing
4 changed files
with
143 additions
and
135 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
42 changes: 42 additions & 0 deletions
42
instrumentor/controllers/instrumentationconfig/workload_predicate.go
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,42 @@ | ||
package instrumentationconfig | ||
|
||
import ( | ||
"github.com/odigos-io/odigos/common/consts" | ||
"sigs.k8s.io/controller-runtime/pkg/event" | ||
"sigs.k8s.io/controller-runtime/pkg/predicate" | ||
) | ||
|
||
// workloadReportedNameAnnotationChanged is a custom predicate that detects changes | ||
// to the `odigos.io/reported-name` annotation on workload resources such as | ||
// Deployment, StatefulSet, and DaemonSet. This ensures that the controller | ||
// reacts only when the specific annotation is updated. | ||
type workloadReportedNameAnnotationChanged struct { | ||
predicate.Funcs | ||
} | ||
|
||
// the instrumentation config is create by the instrumented application controller | ||
func (w workloadReportedNameAnnotationChanged) Create(e event.CreateEvent) bool { | ||
return false | ||
} | ||
|
||
func (w workloadReportedNameAnnotationChanged) Update(e event.UpdateEvent) bool { | ||
if e.ObjectOld == nil || e.ObjectNew == nil { | ||
return false | ||
} | ||
|
||
oldAnnotations := e.ObjectOld.GetAnnotations() | ||
newAnnotations := e.ObjectNew.GetAnnotations() | ||
|
||
oldName := oldAnnotations[consts.OdigosReportedNameAnnotation] | ||
newName := newAnnotations[consts.OdigosReportedNameAnnotation] | ||
|
||
return oldName != newName | ||
} | ||
|
||
func (w workloadReportedNameAnnotationChanged) Delete(e event.DeleteEvent) bool { | ||
return false | ||
} | ||
|
||
func (w workloadReportedNameAnnotationChanged) Generic(e event.GenericEvent) bool { | ||
return false | ||
} |
86 changes: 86 additions & 0 deletions
86
instrumentor/controllers/instrumentationconfig/workloads_controllers.go
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,86 @@ | ||
package instrumentationconfig | ||
|
||
import ( | ||
"context" | ||
|
||
odigosv1alpha1 "github.com/odigos-io/odigos/api/odigos/v1alpha1" | ||
"github.com/odigos-io/odigos/common/consts" | ||
"github.com/odigos-io/odigos/k8sutils/pkg/utils" | ||
"github.com/odigos-io/odigos/k8sutils/pkg/workload" | ||
"k8s.io/apimachinery/pkg/api/errors" | ||
"k8s.io/apimachinery/pkg/runtime" | ||
"k8s.io/apimachinery/pkg/types" | ||
ctrl "sigs.k8s.io/controller-runtime" | ||
"sigs.k8s.io/controller-runtime/pkg/client" | ||
"sigs.k8s.io/controller-runtime/pkg/log" | ||
"sigs.k8s.io/controller-runtime/pkg/reconcile" | ||
) | ||
|
||
type DeploymentReconciler struct { | ||
client.Client | ||
Scheme *runtime.Scheme | ||
} | ||
|
||
func (r *DeploymentReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) { | ||
return reconcileWorkload(ctx, r.Client, workload.WorkloadKindDeployment, req) | ||
} | ||
|
||
type DaemonSetReconciler struct { | ||
client.Client | ||
Scheme *runtime.Scheme | ||
} | ||
|
||
func (r *DaemonSetReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) { | ||
return reconcileWorkload(ctx, r.Client, workload.WorkloadKindDaemonSet, req) | ||
} | ||
|
||
type StatefulSetReconciler struct { | ||
client.Client | ||
Scheme *runtime.Scheme | ||
} | ||
|
||
func (r *StatefulSetReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) { | ||
return reconcileWorkload(ctx, r.Client, workload.WorkloadKindStatefulSet, req) | ||
} | ||
|
||
func reconcileWorkload(ctx context.Context, k8sClient client.Client, objKind workload.WorkloadKind, req ctrl.Request) (ctrl.Result, error) { | ||
obj := workload.ClientObjectFromWorkloadKind(objKind) | ||
instConfigName := workload.CalculateWorkloadRuntimeObjectName(req.Name, objKind) | ||
|
||
if err := getWorkloadObject(ctx, k8sClient, req, obj); err != nil { | ||
if errors.IsNotFound(err) { | ||
return ctrl.Result{}, nil | ||
} | ||
return ctrl.Result{}, err | ||
} | ||
|
||
annotations := obj.GetAnnotations() | ||
reportedName := annotations[consts.OdigosReportedNameAnnotation] | ||
|
||
return createOrUpdateInstrumentationConfig(ctx, k8sClient, instConfigName, req.Namespace, reportedName) | ||
|
||
} | ||
|
||
func getWorkloadObject(ctx context.Context, k8sClient client.Client, req ctrl.Request, obj client.Object) error { | ||
return k8sClient.Get(ctx, types.NamespacedName{Name: req.Name, Namespace: req.Namespace}, obj) | ||
} | ||
|
||
func createOrUpdateInstrumentationConfig(ctx context.Context, k8sClient client.Client, instConfigName, namespace string, serviceName string) (reconcile.Result, error) { | ||
logger := log.FromContext(ctx) | ||
|
||
instConfig := &odigosv1alpha1.InstrumentationConfig{} | ||
err := k8sClient.Get(ctx, types.NamespacedName{Name: instConfigName, Namespace: namespace}, instConfig) | ||
if err != nil { | ||
return reconcile.Result{}, client.IgnoreNotFound(err) | ||
} | ||
|
||
if instConfig.Spec.ServiceName != serviceName { | ||
instConfig.Spec.ServiceName = serviceName | ||
|
||
logger.Info("Updating InstrumentationConfig", "name", instConfigName, "namespace", namespace) | ||
err = k8sClient.Update(ctx, instConfig) | ||
return utils.K8SUpdateErrorHandler(err) | ||
} | ||
|
||
return reconcile.Result{}, nil | ||
} |
78 changes: 0 additions & 78 deletions
78
instrumentor/controllers/instrumentationconfig/workloads_reconciler.go
This file was deleted.
Oops, something went wrong.