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

Implement Source CRD instrumentation #2059

Open
wants to merge 12 commits into
base: feature/source-crd
Choose a base branch
from
3 changes: 2 additions & 1 deletion .github/workflows/build.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ on:
branches:
- main
- stable
- feature/source-crd

jobs:
build-autoscaler:
Expand Down Expand Up @@ -158,4 +159,4 @@ jobs:
- name: Test procdiscovery module
working-directory: ./procdiscovery
run: |
go test -v ./...
go test -v ./...
1 change: 1 addition & 0 deletions .github/workflows/e2e.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ on:
branches:
- main
- stable
- feature/source-crd

concurrency:
group: ${{ github.workflow }}-${{ github.head_ref || github.run_id }}
Expand Down
7 changes: 6 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,11 @@ cli-install:
@echo "Installing odigos from source. version: $(ODIGOS_CLI_VERSION)"
cd ./cli ; go run -tags=embed_manifests . install --version $(ODIGOS_CLI_VERSION)

.PHONY: cli-uninstall
cli-uninstall:
@echo "Installing odigos from source. version: $(ODIGOS_CLI_VERSION)"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Typo:

Suggested change
@echo "Installing odigos from source. version: $(ODIGOS_CLI_VERSION)"
@echo "Uninstalling odigos from source. version: $(ODIGOS_CLI_VERSION)"

cd ./cli ; go run -tags=embed_manifests . uninstall

.PHONY: cli-upgrade
cli-upgrade:
@echo "Upgrading odigos from source. version: $(ODIGOS_CLI_VERSION)"
Expand Down Expand Up @@ -255,4 +260,4 @@ dev-nop-destination:

.PHONY: dev-add-backpressue-destination
dev-backpressue-destination:
kubectl apply -f ./tests/backpressure-exporter.yaml
kubectl apply -f ./tests/backpressure-exporter.yaml
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) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit:

Suggested change
func GetSourceListForWorkload(ctx context.Context, kubeClient client.Client, obj client.Object) (*SourceList, error) {
func GetSourceListForWorkload(ctx context.Context, kubeClient client.Client, obj client.Object) (SourceList, error) {

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Another thought: maybe we could wrap this function in another once returning a single source or an 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{})
}
10 changes: 10 additions & 0 deletions cli/cmd/resources/instrumentor.go
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,16 @@ func NewInstrumentorClusterRole() *rbacv1.ClusterRole {
Resources: []string{"instrumentationconfigs"},
Verbs: []string{"create", "delete", "get", "list", "patch", "update", "watch"},
},
{
APIGroups: []string{"odigos.io"},
Resources: []string{"sources"},
Verbs: []string{"create", "delete", "get", "list", "patch", "update", "watch"},
},
{
APIGroups: []string{"odigos.io"},
Resources: []string{"sources/finalizers"},
Verbs: []string{"update"},
},
},
}
}
Expand Down
14 changes: 14 additions & 0 deletions cli/cmd/uninstall.go
Original file line number Diff line number Diff line change
Expand Up @@ -387,6 +387,20 @@ func uninstallCRDs(ctx context.Context, cmd *cobra.Command, client *kube.Client,
return err
}

// Clear finalizers from Source objects so they can be uninstalled
sources, err := client.OdigosClient.Sources("").List(ctx, metav1.ListOptions{})
for _, i := range sources.Items {
source, err := client.OdigosClient.Sources(i.Namespace).Get(ctx, i.Name, metav1.GetOptions{})
if err != nil {
return err
}
source.SetFinalizers([]string{})
_, err = client.OdigosClient.Sources(i.Namespace).Update(ctx, source, metav1.UpdateOptions{})
if err != nil {
return err
}
}

for _, i := range list.Items {
err = client.ApiExtensions.ApiextensionsV1().CustomResourceDefinitions().Delete(ctx, i.Name, metav1.DeleteOptions{})
if err != nil {
Expand Down
18 changes: 18 additions & 0 deletions helm/odigos/templates/instrumentor/clusterrole.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -78,3 +78,21 @@ rules:
- patch
- update
- watch
- apiGroups:
- odigos.io
resources:
- sources
verbs:
- create
- delete
- get
- list
- patch
- update
- watch
- apiGroups:
- odigos.io
resources:
- sources/finalizers
verbs:
- update
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ 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"
Expand All @@ -21,7 +22,14 @@ func reconcileWorkloadObject(ctx context.Context, kubeClient client.Client, work
}

if instEffectiveEnabled {
return nil
// Check if a Source object exists for this workload
sourceList, err := v1alpha1.GetSourceListForWorkload(ctx, kubeClient, workloadObject)
if err != nil {
return err
}
if len(sourceList.Items) == 0 {
return nil
}
}

if err := deleteWorkloadInstrumentedApplication(ctx, kubeClient, workloadObject); err != nil {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,10 @@ 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"

odigosv1 "github.com/odigos-io/odigos/api/odigos/v1alpha1"
appsv1 "k8s.io/api/apps/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
Expand Down Expand Up @@ -90,9 +91,16 @@ func (r *InstrumentedApplicationReconciler) Reconcile(ctx context.Context, req c
}

if !instEffectiveEnabled {
logger.Info("Deleting instrumented application for non-enabled workload")
err := r.Client.Delete(ctx, &instrumentedApplication)
return ctrl.Result{}, client.IgnoreNotFound(err)
// Check if a Source object exists for this workload
sourceList, err := v1alpha1.GetSourceListForWorkload(ctx, r.Client, workloadObject)
if err != nil {
return ctrl.Result{}, err
}
if len(sourceList.Items) == 0 {
logger.Info("Deleting instrumented application for non-enabled workload")
err := r.Client.Delete(ctx, &instrumentedApplication)
return ctrl.Result{}, client.IgnoreNotFound(err)
}
}

return ctrl.Result{}, nil
Expand Down
13 changes: 13 additions & 0 deletions instrumentor/controllers/deleteinstrumentedapplication/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,19 @@ func SetupWithManager(mgr ctrl.Manager) error {
return err
}

err = builder.
ControllerManagedBy(mgr).
Named("deleteinstrumentedapplication-source").
WithEventFilter(&SourceDeletedPredicate{}).
For(&odigosv1.Source{}).
Complete(&SourceReconciler{
Client: mgr.GetClient(),
Scheme: mgr.GetScheme(),
})
if err != nil {
return err
}

return nil

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
/*
Copyright 2022.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package deleteinstrumentedapplication

import (
"context"

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

"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/controller/controllerutil"
"sigs.k8s.io/controller-runtime/pkg/event"
"sigs.k8s.io/controller-runtime/pkg/log"
)

// Added by startlangdetection controller when Source is created
var instrumentedApplicationFinalizer = "odigos.io/source-instrumentedapplication-finalizer"

type SourceDeletedPredicate struct{}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We have a utils package for predicates.
This one exists in k8sutils/pkg.predicate/deletion.go


func (i *SourceDeletedPredicate) Create(_ event.CreateEvent) bool {
return false
}

func (i *SourceDeletedPredicate) Update(_ event.UpdateEvent) bool {
// We are actually looking for Update events that add a DeletionTimestamp
// This is so we can still get the workload from the Source object and remove the finalizer
// Then actual deletion of the Source will proceed
return true
}

func (i *SourceDeletedPredicate) Delete(_ event.DeleteEvent) bool {
return true
}

func (i *SourceDeletedPredicate) Generic(_ event.GenericEvent) bool {
return false
}

type SourceReconciler struct {
client.Client
Scheme *runtime.Scheme
}

func (r *SourceReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) {
logger := log.FromContext(ctx)
logger.Info("Reconciling Deleted Source object", "name", req.Name, "namespace", req.Namespace)

source := &v1alpha1.Source{}
err := r.Get(ctx, req.NamespacedName, source)
if err != nil {
return ctrl.Result{}, client.IgnoreNotFound(err)
}

if !source.DeletionTimestamp.IsZero() {
logger.Info("Reconciling workload for deleted Source object", "name", req.Name, "namespace", req.Namespace)
obj := workload.ClientObjectFromWorkloadKind(source.Spec.Workload.Kind)
err = r.Client.Get(ctx, types.NamespacedName{Name: source.Spec.Workload.Name, Namespace: source.Spec.Workload.Namespace}, obj)
if err != nil {
// TODO: Deleted objects should be filtered in the event filter
return ctrl.Result{}, err
}

if controllerutil.ContainsFinalizer(source, instrumentedApplicationFinalizer) {
controllerutil.RemoveFinalizer(source, instrumentedApplicationFinalizer)
if err := r.Update(ctx, source); err != nil {
return ctrl.Result{}, err
}
}

err = reconcileWorkloadObject(ctx, r.Client, obj)
if err != nil {
return ctrl.Result{}, err
}
}
return ctrl.Result{}, nil
}
16 changes: 15 additions & 1 deletion instrumentor/controllers/startlangdetection/manager.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
package startlangdetection

import (
odigospredicate "github.com/odigos-io/odigos/k8sutils/pkg/predicate"
appsv1 "k8s.io/api/apps/v1"
corev1 "k8s.io/api/core/v1"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/builder"
"sigs.k8s.io/controller-runtime/pkg/predicate"

"github.com/odigos-io/odigos/api/odigos/v1alpha1"
odigospredicate "github.com/odigos-io/odigos/k8sutils/pkg/predicate"
)

func SetupWithManager(mgr ctrl.Manager) error {
Expand Down Expand Up @@ -74,5 +76,17 @@ func SetupWithManager(mgr ctrl.Manager) error {
return err
}

err = builder.
ControllerManagedBy(mgr).
Named("startlangdetection-source").
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we add an event filter here to pass only Create events?

For(&v1alpha1.Source{}).
Complete(&SourceReconciler{
Client: mgr.GetClient(),
Scheme: mgr.GetScheme(),
})
if err != nil {
return err
}

return nil
}
Loading
Loading