Skip to content

Commit

Permalink
Add GetSourceListForWorkload function
Browse files Browse the repository at this point in the history
  • Loading branch information
damemi committed Dec 23, 2024
1 parent 378d0bf commit adfc696
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 28 deletions.
21 changes: 21 additions & 0 deletions api/odigos/v1alpha1/source_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,11 @@ limitations under the License.
package v1alpha1

import (
"context"

metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/labels"
"sigs.k8s.io/controller-runtime/pkg/client"

"github.com/odigos-io/odigos/k8sutils/pkg/workload"
)
Expand Down Expand Up @@ -62,6 +66,23 @@ type SourceList struct {
Items []Source `json:"items"`
}

// GetSourceListForWorkload returns a SourceList of all Sources that have matching
// workload name, namespace, and kind labels for an object. In theory, this should only
// ever return a list with 0 or 1 items, but due diligence should handle unexpected cases.
func GetSourceListForWorkload(ctx context.Context, kubeClient client.Client, obj client.Object) (*SourceList, error) {
sourceList := &SourceList{}
selector := labels.SelectorFromSet(labels.Set{
"odigos.io/workload-name": obj.GetName(),
"odigos.io/workload-namespace": obj.GetNamespace(),
"odigos.io/workload-kind": obj.GetObjectKind().GroupVersionKind().Kind,
})
err := kubeClient.List(ctx, sourceList, &client.ListOptions{LabelSelector: selector})
if err != nil {
return nil, err
}
return sourceList, nil
}

func init() {
SchemeBuilder.Register(&Source{}, &SourceList{})
}
11 changes: 2 additions & 9 deletions instrumentor/controllers/deleteinstrumentedapplication/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ package deleteinstrumentedapplication
import (
"context"

"github.com/odigos-io/odigos/api/odigos/v1alpha1"
odigosv1 "github.com/odigos-io/odigos/api/odigos/v1alpha1"
"github.com/odigos-io/odigos/common/consts"
"github.com/odigos-io/odigos/k8sutils/pkg/workload"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/apimachinery/pkg/types"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/log"
Expand All @@ -23,14 +23,7 @@ func reconcileWorkloadObject(ctx context.Context, kubeClient client.Client, work

if instEffectiveEnabled {
// Check if a Source object exists for this workload
// TODO: Move this to IsWorkloadInstrumentationEffectiveEnabled (creates import loop right now)
sourceList := &odigosv1.SourceList{}
selector := labels.SelectorFromSet(labels.Set{
"odigos.io/workload-name": workloadObject.GetName(),
"odigos.io/workload-namespace": workloadObject.GetNamespace(),
"odigos.io/workload-kind": workloadObject.GetObjectKind().GroupVersionKind().Kind,
})
err := kubeClient.List(ctx, sourceList, &client.ListOptions{LabelSelector: selector})
sourceList, err := v1alpha1.GetSourceListForWorkload(ctx, kubeClient, workloadObject)
if err != nil {
return err
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,12 @@ import (
"context"
"fmt"

"github.com/odigos-io/odigos/api/odigos/v1alpha1"
odigosv1 "github.com/odigos-io/odigos/api/odigos/v1alpha1"
"github.com/odigos-io/odigos/k8sutils/pkg/workload"

appsv1 "k8s.io/api/apps/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/apimachinery/pkg/runtime"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
Expand Down Expand Up @@ -92,14 +92,7 @@ func (r *InstrumentedApplicationReconciler) Reconcile(ctx context.Context, req c

if !instEffectiveEnabled {
// Check if a Source object exists for this workload
// TODO: Move this to IsWorkloadInstrumentationEffectiveEnabled (creates import loop right now)
sourceList := &odigosv1.SourceList{}
selector := labels.SelectorFromSet(labels.Set{
"odigos.io/workload-name": workloadObject.GetName(),
"odigos.io/workload-namespace": workloadObject.GetNamespace(),
"odigos.io/workload-kind": workloadObject.GetObjectKind().GroupVersionKind().Kind,
})
err := r.Client.List(ctx, sourceList, &client.ListOptions{LabelSelector: selector})
sourceList, err := v1alpha1.GetSourceListForWorkload(ctx, r.Client, workloadObject)
if err != nil {
return ctrl.Result{}, err
}
Expand All @@ -108,7 +101,6 @@ func (r *InstrumentedApplicationReconciler) Reconcile(ctx context.Context, req c
err := r.Client.Delete(ctx, &instrumentedApplication)
return ctrl.Result{}, client.IgnoreNotFound(err)
}

}

return ctrl.Result{}, nil
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@ import (
"context"

metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/labels"
"sigs.k8s.io/yaml"

"k8s.io/apimachinery/pkg/runtime"

"sigs.k8s.io/controller-runtime/pkg/log"

"github.com/odigos-io/odigos/api/odigos/v1alpha1"
odigosv1 "github.com/odigos-io/odigos/api/odigos/v1alpha1"
"github.com/odigos-io/odigos/k8sutils/pkg/workload"
"k8s.io/apimachinery/pkg/types"
Expand Down Expand Up @@ -61,14 +61,7 @@ func reconcileWorkload(ctx context.Context, k8sClient client.Client, objKind wor

if !instrumented {
// Check if a Source object exists for this workload
// TODO: Move this to IsWorkloadInstrumentationEffectiveEnabled (creates import loop right now)
sourceList := &odigosv1.SourceList{}
selector := labels.SelectorFromSet(labels.Set{
"odigos.io/workload-name": obj.GetName(),
"odigos.io/workload-namespace": obj.GetNamespace(),
"odigos.io/workload-kind": obj.GetObjectKind().GroupVersionKind().Kind,
})
err := k8sClient.List(ctx, sourceList, &client.ListOptions{LabelSelector: selector})
sourceList, err := v1alpha1.GetSourceListForWorkload(ctx, k8sClient, obj)
if err != nil {
return ctrl.Result{}, err
}
Expand Down

0 comments on commit adfc696

Please sign in to comment.