Skip to content

Commit

Permalink
refactor(schedualr): split node collectors group controller to files (#…
Browse files Browse the repository at this point in the history
…1818)

This PR aligns how the scheduler controllers are implemented, making it
similar to the existing pattern used in other places in the repo.
  • Loading branch information
blumamir authored Nov 21, 2024
1 parent 215e16f commit f1e15db
Show file tree
Hide file tree
Showing 8 changed files with 183 additions and 148 deletions.
36 changes: 0 additions & 36 deletions scheduler/controllers/collectorgroups/datacollection.go

This file was deleted.

106 changes: 0 additions & 106 deletions scheduler/controllers/nodecollector_controller.go

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package nodecollectorsgroup

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
}
72 changes: 72 additions & 0 deletions scheduler/controllers/nodecollectorsgroup/common.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package nodecollectorsgroup

import (
"context"
"errors"

odigosv1 "github.com/odigos-io/odigos/api/odigos/v1alpha1"
"github.com/odigos-io/odigos/common"
"github.com/odigos-io/odigos/k8sutils/pkg/consts"
k8sutilsconsts "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 newNodeCollectorGroup(odigosConfig common.OdigosConfiguration) *odigosv1.CollectorsGroup {

ownMetricsPort := consts.OdigosNodeCollectorOwnTelemetryPortDefault
if odigosConfig.CollectorNode != nil && odigosConfig.CollectorNode.CollectorOwnMetricsPort != 0 {
ownMetricsPort = odigosConfig.CollectorNode.CollectorOwnMetricsPort
}

return &odigosv1.CollectorsGroup{
TypeMeta: metav1.TypeMeta{
Kind: "CollectorsGroup",
APIVersion: "odigos.io/v1alpha1",
},
ObjectMeta: metav1.ObjectMeta{
Name: consts.OdigosNodeCollectorDaemonSetName,
Namespace: env.GetCurrentNamespace(),
},
Spec: odigosv1.CollectorsGroupSpec{
Role: odigosv1.CollectorsGroupRoleNodeCollector,
CollectorOwnMetricsPort: ownMetricsPort,
},
}
}

func sync(ctx context.Context, c client.Client) error {

namespace := env.GetCurrentNamespace()

var instrumentedConfigs odigosv1.InstrumentationConfigList
err := c.List(ctx, &instrumentedConfigs)
if err != nil {
return errors.Join(errors.New("failed to list InstrumentationConfigs"), err)
}
numberOfInstrumentedApps := len(instrumentedConfigs.Items)

if numberOfInstrumentedApps == 0 {
// TODO: should we delete the collector group if cluster collector is not ready?
return utils.DeleteCollectorGroup(ctx, c, namespace, k8sutilsconsts.OdigosNodeCollectorCollectorGroupName)
}

clusterCollectorGroup, err := utils.GetCollectorGroup(ctx, c, namespace, k8sutilsconsts.OdigosClusterCollectorCollectorGroupName)
if err != nil {
return client.IgnoreNotFound(err)
}

odigosConfig, err := utils.GetCurrentOdigosConfig(ctx, c)
if err != nil {
return err
}

clusterCollectorReady := clusterCollectorGroup.Status.Ready
if clusterCollectorReady {
return utils.ApplyCollectorGroup(ctx, c, newNodeCollectorGroup(odigosConfig))
}

return nil
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package nodecollectorsgroup

import (
"context"

"k8s.io/apimachinery/pkg/runtime"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
)

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

func (r *instrumentationConfigController) Reconcile(ctx context.Context, _ ctrl.Request) (ctrl.Result, error) {
err := sync(ctx, r.Client)
return ctrl.Result{}, err
}
50 changes: 50 additions & 0 deletions scheduler/controllers/nodecollectorsgroup/manager.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package nodecollectorsgroup

import (
odigosv1 "github.com/odigos-io/odigos/api/odigos/v1alpha1"
odigospredicates "github.com/odigos-io/odigos/k8sutils/pkg/predicate"
corev1 "k8s.io/api/core/v1"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/predicate"
)

func SetupWithManager(mgr ctrl.Manager) error {

err := ctrl.NewControllerManagedBy(mgr).
For(&odigosv1.InstrumentationConfig{}).
Named("nodecollectorgroup-instrumentationconfig").
WithEventFilter(&odigospredicates.ExistencePredicate{}).
Complete(&instrumentationConfigController{
Client: mgr.GetClient(),
Scheme: mgr.GetScheme(),
})
if err != nil {
return err
}

err = ctrl.NewControllerManagedBy(mgr).
For(&corev1.ConfigMap{}).
Named("nodecollectorgroup-odigosconfig").
WithEventFilter(&odigospredicates.OdigosConfigMapPredicate).
Complete(&odigosConfigController{
Client: mgr.GetClient(),
Scheme: mgr.GetScheme(),
})
if err != nil {
return err
}

err = ctrl.NewControllerManagedBy(mgr).
For(&odigosv1.CollectorsGroup{}).
Named("nodecollectorgroup-clustercollectorsgroup").
WithEventFilter(predicate.And(&odigospredicates.OdigosCollectorsGroupClusterPredicate, &odigospredicates.CgBecomesReadyPredicate{})).
Complete(&clusterCollectorsGroupController{
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,19 @@
package nodecollectorsgroup

import (
"context"

"k8s.io/apimachinery/pkg/runtime"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
)

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

func (r *odigosConfigController) Reconcile(ctx context.Context, _ ctrl.Request) (ctrl.Result, error) {
err := sync(ctx, r.Client)
return ctrl.Result{}, err
}
10 changes: 4 additions & 6 deletions scheduler/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ import (
ctrlzap "sigs.k8s.io/controller-runtime/pkg/log/zap"

"github.com/odigos-io/odigos/scheduler/controllers"
"github.com/odigos-io/odigos/scheduler/controllers/nodecollectorsgroup"
//+kubebuilder:scaffold:imports
)

Expand Down Expand Up @@ -111,14 +112,11 @@ func main() {
setupLog.Error(err, "unable to create controller", "controller", "Destination")
os.Exit(1)
}
if err = (&controllers.NodeCollectorsGroupReconciler{
Client: mgr.GetClient(),
Scheme: mgr.GetScheme(),
}).SetupWithManager(mgr); err != nil {
setupLog.Error(err, "unable to create controller", "controller", "InstrumentationConfig")
err = nodecollectorsgroup.SetupWithManager(mgr)
if err != nil {
setupLog.Error(err, "unable to create controllers for node collectors group")
os.Exit(1)
}
//+kubebuilder:scaffold:builder

if err := mgr.AddHealthzCheck("healthz", healthz.Ping); err != nil {
setupLog.Error(err, "unable to set up health check")
Expand Down

0 comments on commit f1e15db

Please sign in to comment.