diff --git a/scheduler/controllers/clustercollectorsgroup/common.go b/scheduler/controllers/clustercollectorsgroup/common.go new file mode 100644 index 000000000..c4f180661 --- /dev/null +++ b/scheduler/controllers/clustercollectorsgroup/common.go @@ -0,0 +1,51 @@ +package clustercollectorsgroup + +import ( + "context" + + odigosv1 "github.com/odigos-io/odigos/api/odigos/v1alpha1" + "github.com/odigos-io/odigos/k8sutils/pkg/consts" + "github.com/odigos-io/odigos/k8sutils/pkg/env" + "github.com/odigos-io/odigos/k8sutils/pkg/utils" + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" + "sigs.k8s.io/controller-runtime/pkg/client" +) + +func newClusterCollectorGroup(namespace string) *odigosv1.CollectorsGroup { + return &odigosv1.CollectorsGroup{ + TypeMeta: metav1.TypeMeta{ + Kind: "CollectorsGroup", + APIVersion: "odigos.io/v1alpha1", + }, + ObjectMeta: metav1.ObjectMeta{ + Name: consts.OdigosClusterCollectorCollectorGroupName, + Namespace: namespace, + }, + Spec: odigosv1.CollectorsGroupSpec{ + Role: odigosv1.CollectorsGroupRoleClusterGateway, + CollectorOwnMetricsPort: consts.OdigosClusterCollectorOwnTelemetryPortDefault, + }, + } +} + +func sync(ctx context.Context, c client.Client) error { + + namespace := env.GetCurrentNamespace() + + var dests odigosv1.DestinationList + err := c.List(ctx, &dests, client.InNamespace(namespace)) + if err != nil { + return err + } + + if len(dests.Items) > 0 { + err := utils.ApplyCollectorGroup(ctx, c, newClusterCollectorGroup(namespace)) + if err != nil { + return err + } + } + // once the gateway is created, it is not deleted, even if there are no destinations. + // we might want to re-consider this behavior. + + return nil +} diff --git a/scheduler/controllers/clustercollectorsgroup/destinations_controller.go b/scheduler/controllers/clustercollectorsgroup/destinations_controller.go new file mode 100644 index 000000000..7ee964a75 --- /dev/null +++ b/scheduler/controllers/clustercollectorsgroup/destinations_controller.go @@ -0,0 +1,19 @@ +package clustercollectorsgroup + +import ( + "context" + + "k8s.io/apimachinery/pkg/runtime" + ctrl "sigs.k8s.io/controller-runtime" + "sigs.k8s.io/controller-runtime/pkg/client" +) + +type clusterCollectorsGroupController struct { + client.Client + Scheme *runtime.Scheme +} + +func (r *clusterCollectorsGroupController) Reconcile(ctx context.Context, _ ctrl.Request) (ctrl.Result, error) { + err := sync(ctx, r.Client) + return ctrl.Result{}, err +} diff --git a/scheduler/controllers/clustercollectorsgroup/manager.go b/scheduler/controllers/clustercollectorsgroup/manager.go new file mode 100644 index 000000000..09411b7d4 --- /dev/null +++ b/scheduler/controllers/clustercollectorsgroup/manager.go @@ -0,0 +1,24 @@ +package clustercollectorsgroup + +import ( + odigosv1 "github.com/odigos-io/odigos/api/odigos/v1alpha1" + odigospredicates "github.com/odigos-io/odigos/k8sutils/pkg/predicate" + ctrl "sigs.k8s.io/controller-runtime" +) + +func SetupWithManager(mgr ctrl.Manager) error { + + err := ctrl.NewControllerManagedBy(mgr). + For(&odigosv1.Destination{}). + Named("clustercollectorgroup-destinations"). + WithEventFilter(&odigospredicates.ExistencePredicate{}). + Complete(&clusterCollectorsGroupController{ + Client: mgr.GetClient(), + Scheme: mgr.GetScheme(), + }) + if err != nil { + return err + } + + return nil +} diff --git a/scheduler/controllers/collectorgroups/cluster.go b/scheduler/controllers/collectorgroups/cluster.go deleted file mode 100644 index f9e9dfe63..000000000 --- a/scheduler/controllers/collectorgroups/cluster.go +++ /dev/null @@ -1,24 +0,0 @@ -package collectorgroups - -import ( - odigosv1 "github.com/odigos-io/odigos/api/odigos/v1alpha1" - "github.com/odigos-io/odigos/k8sutils/pkg/consts" - metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" -) - -func NewClusterCollectorGroup(namespace string) *odigosv1.CollectorsGroup { - return &odigosv1.CollectorsGroup{ - TypeMeta: metav1.TypeMeta{ - Kind: "CollectorsGroup", - APIVersion: "odigos.io/v1alpha1", - }, - ObjectMeta: metav1.ObjectMeta{ - Name: consts.OdigosClusterCollectorCollectorGroupName, - Namespace: namespace, - }, - Spec: odigosv1.CollectorsGroupSpec{ - Role: odigosv1.CollectorsGroupRoleClusterGateway, - CollectorOwnMetricsPort: consts.OdigosClusterCollectorOwnTelemetryPortDefault, - }, - } -} diff --git a/scheduler/controllers/destination_controller.go b/scheduler/controllers/destination_controller.go deleted file mode 100644 index 12fe20829..000000000 --- a/scheduler/controllers/destination_controller.go +++ /dev/null @@ -1,68 +0,0 @@ -/* -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 controllers - -import ( - "context" - - "github.com/odigos-io/odigos/k8sutils/pkg/utils" - - odigosv1 "github.com/odigos-io/odigos/api/odigos/v1alpha1" - odigospredicates "github.com/odigos-io/odigos/k8sutils/pkg/predicate" - "github.com/odigos-io/odigos/scheduler/controllers/collectorgroups" - - "k8s.io/apimachinery/pkg/runtime" - ctrl "sigs.k8s.io/controller-runtime" - "sigs.k8s.io/controller-runtime/pkg/client" - "sigs.k8s.io/controller-runtime/pkg/log" -) - -type DestinationReconciler struct { - client.Client - Scheme *runtime.Scheme -} - -func (r *DestinationReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) { - logger := log.FromContext(ctx) - - var dests odigosv1.DestinationList - err := r.List(ctx, &dests, client.InNamespace(req.Namespace)) - if err != nil { - logger.Error(err, "failed to list destinations") - return ctrl.Result{}, err - } - - if len(dests.Items) > 0 { - logger.V(0).Info("destinations found, syncing cluster collector group") - err := utils.ApplyCollectorGroup(ctx, r.Client, collectorgroups.NewClusterCollectorGroup(req.Namespace)) - if err != nil { - logger.Error(err, "failed to sync cluster collector group") - return ctrl.Result{}, err - } - } - // once the gateway is created, it is not deleted, even if there are no destinations. - // we might want to re-consider this behavior. - - return ctrl.Result{}, nil -} - -func (r *DestinationReconciler) SetupWithManager(mgr ctrl.Manager) error { - return ctrl.NewControllerManagedBy(mgr). - For(&odigosv1.Destination{}). - WithEventFilter(&odigospredicates.ExistencePredicate{}). // only care when destinations are created or deleted - Complete(r) -} diff --git a/scheduler/main.go b/scheduler/main.go index b1f39cf7b..3224ab642 100644 --- a/scheduler/main.go +++ b/scheduler/main.go @@ -42,7 +42,7 @@ import ( "sigs.k8s.io/controller-runtime/pkg/healthz" ctrlzap "sigs.k8s.io/controller-runtime/pkg/log/zap" - "github.com/odigos-io/odigos/scheduler/controllers" + "github.com/odigos-io/odigos/scheduler/controllers/clustercollectorsgroup" "github.com/odigos-io/odigos/scheduler/controllers/nodecollectorsgroup" //+kubebuilder:scaffold:imports ) @@ -105,11 +105,9 @@ func main() { os.Exit(1) } - if err = (&controllers.DestinationReconciler{ - Client: mgr.GetClient(), - Scheme: mgr.GetScheme(), - }).SetupWithManager(mgr); err != nil { - setupLog.Error(err, "unable to create controller", "controller", "Destination") + err = clustercollectorsgroup.SetupWithManager(mgr) + if err != nil { + setupLog.Error(err, "unable to create controllers for cluster collectors group") os.Exit(1) } err = nodecollectorsgroup.SetupWithManager(mgr)