-
Notifications
You must be signed in to change notification settings - Fork 204
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
base: feature/source-crd
Are you sure you want to change the base?
Changes from all commits
3dd7ed5
dae2bd9
9ba9b57
df8cc4f
6bfeb1d
312854c
9528b4b
378d0bf
adfc696
32c3afe
b44f442
ef8f196
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -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" | ||||||
) | ||||||
|
@@ -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) { | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit:
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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{}) | ||||||
} |
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{} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We have a utils package for predicates. |
||
|
||
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 | ||
} |
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 { | ||
|
@@ -74,5 +76,17 @@ func SetupWithManager(mgr ctrl.Manager) error { | |
return err | ||
} | ||
|
||
err = builder. | ||
ControllerManagedBy(mgr). | ||
Named("startlangdetection-source"). | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Typo: