Skip to content

Commit

Permalink
chore: fixed pr comments
Browse files Browse the repository at this point in the history
  • Loading branch information
alonbraymok committed Nov 25, 2024
1 parent 1d8eba9 commit 46e278c
Show file tree
Hide file tree
Showing 4 changed files with 143 additions and 135 deletions.
72 changes: 15 additions & 57 deletions instrumentor/controllers/instrumentationconfig/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,50 +2,11 @@ package instrumentationconfig

import (
odigosv1alpha1 "github.com/odigos-io/odigos/api/odigos/v1alpha1"
"github.com/odigos-io/odigos/common/consts"
appsv1 "k8s.io/api/apps/v1"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/builder"
"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
}

func (w workloadReportedNameAnnotationChanged) Create(e event.CreateEvent) bool {
return true
}

func (w workloadReportedNameAnnotationChanged) Update(e event.UpdateEvent) bool {
if e.ObjectOld == nil || e.ObjectNew == nil {
return false
}

// Compare annotations
oldAnnotations := e.ObjectOld.GetAnnotations()
newAnnotations := e.ObjectNew.GetAnnotations()

// Check if the `odigos.io/reported-name` annotation has changed
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
}

func SetupWithManager(mgr ctrl.Manager) error {
// Watch InstrumentationRule
err := builder.
Expand Down Expand Up @@ -73,45 +34,42 @@ func SetupWithManager(mgr ctrl.Manager) error {
return err
}

// Watch for Deployment changes
err = builder.
// Watch for Deployment changes using DeploymentReconciler
if err := builder.
ControllerManagedBy(mgr).
Named("instrumentor-instrumentationconfig-workloads-deployment").
Named("instrumentor-instrumentationconfig-deployment").
For(&appsv1.Deployment{}).
WithEventFilter(workloadReportedNameAnnotationChanged{}).
Complete(&WorkloadsReconciler{
Complete(&DeploymentReconciler{
Client: mgr.GetClient(),
Scheme: mgr.GetScheme(),
})
if err != nil {
}); err != nil {
return err
}

// Watch for StatefulSet changes
err = builder.
// Watch for StatefulSet changes using StatefulSetReconciler
if err := builder.
ControllerManagedBy(mgr).
Named("instrumentor-instrumentationconfig-workloads-statefulset").
Named("instrumentor-instrumentationconfig-statefulset").
For(&appsv1.StatefulSet{}).
WithEventFilter(workloadReportedNameAnnotationChanged{}).
Complete(&WorkloadsReconciler{
Complete(&StatefulSetReconciler{
Client: mgr.GetClient(),
Scheme: mgr.GetScheme(),
})
if err != nil {
}); err != nil {
return err
}

// Watch for DaemonSet changes
err = builder.
// Watch for DaemonSet changes using DaemonSetReconciler
if err := builder.
ControllerManagedBy(mgr).
Named("instrumentor-instrumentationconfig-workloads-daemonset").
Named("instrumentor-instrumentationconfig-daemonset").
For(&appsv1.DaemonSet{}).
WithEventFilter(workloadReportedNameAnnotationChanged{}).
Complete(&WorkloadsReconciler{
Complete(&DaemonSetReconciler{
Client: mgr.GetClient(),
Scheme: mgr.GetScheme(),
})
if err != nil {
}); err != nil {
return err
}

Expand Down
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
}
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
}

This file was deleted.

0 comments on commit 46e278c

Please sign in to comment.