Skip to content

Commit

Permalink
chore: init code to ia reconciler
Browse files Browse the repository at this point in the history
  • Loading branch information
alonbraymok committed Nov 21, 2024
1 parent f04a3c5 commit fe0f7d1
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 3 deletions.
48 changes: 47 additions & 1 deletion instrumentor/controllers/instrumentationconfig/common.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,21 @@
package instrumentationconfig

import (
"context"
"fmt"

odigosv1alpha1 "github.com/odigos-io/odigos/api/odigos/v1alpha1"
"github.com/odigos-io/odigos/api/odigos/v1alpha1/instrumentationrules"
"github.com/odigos-io/odigos/common"
"github.com/odigos-io/odigos/common/consts"
"github.com/odigos-io/odigos/instrumentor/controllers/utils"
"github.com/odigos-io/odigos/k8sutils/pkg/workload"
appsv1 "k8s.io/api/apps/v1"
"k8s.io/apimachinery/pkg/types"
"sigs.k8s.io/controller-runtime/pkg/client"
)

func updateInstrumentationConfigForWorkload(ic *odigosv1alpha1.InstrumentationConfig, ia *odigosv1alpha1.InstrumentedApplication, rules *odigosv1alpha1.InstrumentationRuleList) error {
func updateInstrumentationConfigForWorkload(ic *odigosv1alpha1.InstrumentationConfig, ia *odigosv1alpha1.InstrumentedApplication, rules *odigosv1alpha1.InstrumentationRuleList, k8sClient client.Client) error {

workloadName, workloadKind, err := workload.ExtractWorkloadInfoFromRuntimeObjectName(ia.Name)
if err != nil {
Expand All @@ -20,6 +27,35 @@ func updateInstrumentationConfigForWorkload(ic *odigosv1alpha1.InstrumentationCo
Kind: workloadKind,
}

var serviceName string
switch workloadKind {
case "Deployment":
var deployment appsv1.Deployment
err := k8sClient.Get(context.Background(), types.NamespacedName{Name: workloadName, Namespace: ia.Namespace}, &deployment)
if err != nil {
return fmt.Errorf("failed to get Deployment %s/%s: %w", ia.Namespace, workloadName, err)
}
serviceName = extractServiceNameFromAnnotations(deployment.Annotations, deployment.Name)
case "StatefulSet":
var statefulSet appsv1.StatefulSet
err := k8sClient.Get(context.Background(), types.NamespacedName{Name: workloadName, Namespace: ia.Namespace}, &statefulSet)
if err != nil {
return fmt.Errorf("failed to get StatefulSet %s/%s: %w", ia.Namespace, workloadName, err)
}
serviceName = extractServiceNameFromAnnotations(statefulSet.Annotations, statefulSet.Name)
case "DaemonSet":
var daemonSet appsv1.DaemonSet
err := k8sClient.Get(context.Background(), types.NamespacedName{Name: workloadName, Namespace: ia.Namespace}, &daemonSet)
if err != nil {
return fmt.Errorf("failed to get DaemonSet %s/%s: %w", ia.Namespace, workloadName, err)
}
serviceName = extractServiceNameFromAnnotations(daemonSet.Annotations, daemonSet.Name)
default:
return fmt.Errorf("unsupported workload kind: %s", workloadKind)
}

ic.Spec.ServiceName = serviceName

sdkConfigs := make([]odigosv1alpha1.SdkConfig, 0, len(ia.Spec.RuntimeDetails))

// create an empty sdk config for each detected programming language
Expand Down Expand Up @@ -239,3 +275,13 @@ func mergeMessagingPayloadCollectionRules(rule1 *instrumentationrules.MessagingP
func boolPtr(b bool) *bool {
return &b
}

func extractServiceNameFromAnnotations(annotations map[string]string, defaultName string) string {
if annotations == nil {
return defaultName
}
if reportedName, exists := annotations[consts.OdigosReportedNameAnnotation]; exists && reportedName != "" {
return reportedName
}
return defaultName
}
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ func (r *InstrumentationRuleReconciler) Reconcile(ctx context.Context, req ctrl.
}
}

err := updateInstrumentationConfigForWorkload(ic, &ia, instrumentationRules)
err := updateInstrumentationConfigForWorkload(ic, &ia, instrumentationRules, r.Client)
if err != nil {
logger.Error(err, "error updating instrumentation config", "workload", ia.Name)
continue
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ func (r *InstrumentedApplicationReconciler) Reconcile(ctx context.Context, req c
instrumentationRules := &odigosv1.InstrumentationRuleList{}
err = r.Client.List(ctx, instrumentationRules)

err = updateInstrumentationConfigForWorkload(&ic, &ia, instrumentationRules)
err = updateInstrumentationConfigForWorkload(&ic, &ia, instrumentationRules, r.Client)
if err != nil {
return ctrl.Result{}, err
}
Expand Down

0 comments on commit fe0f7d1

Please sign in to comment.