Skip to content

Commit

Permalink
fix: event driven reconciliation for service export (#385)
Browse files Browse the repository at this point in the history
* fix(): event driven reconciliation for service export

Signed-off-by: Mridul Gain <[email protected]>

* update ENVTEST_K8S_VERSION

Signed-off-by: Mridul Gain <[email protected]>

---------

Signed-off-by: Mridul Gain <[email protected]>
  • Loading branch information
mridulgain authored May 24, 2024
1 parent b3c625e commit feec8a3
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 3 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ BUNDLE_IMG ?= $(IMAGE_TAG_BASE)-bundle:v$(VERSION)
# Image URL to use all building/pushing image targets
IMG ?= docker.io/aveshasystems/worker-operator:$(VERSION)
# ENVTEST_K8S_VERSION refers to the version of kubebuilder assets to be downloaded by envtest binary.
ENVTEST_K8S_VERSION = 1.22
ENVTEST_K8S_VERSION = 1.23

# Get the currently used golang install path (in GOPATH/bin, unless GOBIN is set)
ifeq (,$(shell go env GOBIN))
Expand Down
85 changes: 83 additions & 2 deletions controllers/serviceexport/reconciler.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,24 +21,34 @@ package serviceexport
import (
"context"
"fmt"
"reflect"
"time"

"github.com/go-logr/logr"
"github.com/prometheus/client_golang/prometheus"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/types"

ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/builder"
"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/handler"
"sigs.k8s.io/controller-runtime/pkg/predicate"
"sigs.k8s.io/controller-runtime/pkg/reconcile"

"github.com/kubeslice/kubeslice-monitoring/pkg/events"
"github.com/kubeslice/kubeslice-monitoring/pkg/metrics"

kubeslicev1beta1 "github.com/kubeslice/worker-operator/api/v1beta1"
"github.com/kubeslice/worker-operator/controllers"
ossEvents "github.com/kubeslice/worker-operator/events"
"github.com/kubeslice/worker-operator/pkg/logger"
"github.com/kubeslice/worker-operator/pkg/utils"
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
)

// Reconciler reconciles serviceexport resource
Expand Down Expand Up @@ -225,10 +235,81 @@ func (r *Reconciler) Setup(mgr ctrl.Manager, mf metrics.MetricsFactory) error {
return r.SetupWithManager(mgr)
}

func (r *Reconciler) mapPodsToServiceExport(ctx context.Context, obj client.Object) (recs []reconcile.Request) {
log := logger.FromContext(ctx)
debugLog := log.V(1)
debugLog.Info("triggered watcher for svc export", "obj", obj.GetName())
_, ok := obj.(*corev1.Pod)
if !ok {
debugLog.Info("Unexpected object type in ServiceExport reconciler watch predicate expected *corev1.Pod found ", reflect.TypeOf(obj))
return
}
// get all svc export in the app ns
svcexpList := &kubeslicev1beta1.ServiceExportList{}
listOpts := []client.ListOption{
client.InNamespace(obj.GetNamespace()),
}
err := r.List(ctx, svcexpList, listOpts...)
if err != nil {
log.Error(err, "Failed to list service export", "application namespace", obj.GetNamespace())
return
}
debugLog.Info("Service export found in app ns", "count", len(svcexpList.Items))
for _, svcexp := range svcexpList.Items {
selector, err := metav1.LabelSelectorAsSelector(svcexp.Spec.Selector)
if err != nil {
log.Error(err, "Failed to parse selector", "service export", svcexp.Name, "selector", svcexp.Spec.Selector)
continue
}
if selector.Matches(labels.Set(obj.GetLabels())) {
debugLog.Info("requeueing svc export", "obj", types.NamespacedName{
Name: svcexp.Name,
Namespace: svcexp.Namespace,
})
recs = append(recs, reconcile.Request{
NamespacedName: types.NamespacedName{
Name: svcexp.Name,
Namespace: svcexp.Namespace,
},
})
}
}
return recs
}

// SetupWithManager setus up reconciler with manager
func (r *Reconciler) SetupWithManager(mgr ctrl.Manager) error {
var labelSelector metav1.LabelSelector
// "kubeslice.io/pod-type": "app"
labelSelector.MatchLabels = map[string]string{controllers.PodTypeSelectorLabelKey: controllers.PodTypeSelectorValueApp}

return ctrl.NewControllerManagedBy(mgr).
For(&kubeslicev1beta1.ServiceExport{}).
Watches(
&corev1.Pod{},
handler.EnqueueRequestsFromMapFunc(r.mapPodsToServiceExport),
builder.WithPredicates(predicate.Funcs{
CreateFunc: func(e event.CreateEvent) bool {
return false
},
DeleteFunc: func(e event.DeleteEvent) bool {
selector, err := metav1.LabelSelectorAsSelector(&labelSelector)
if err != nil {
return false
}
if selector.Matches(labels.Set(e.Object.GetLabels())) {
return true
}
return false
},
UpdateFunc: func(e event.UpdateEvent) bool {
return false
},
GenericFunc: func(e event.GenericEvent) bool {
return false
},
}),
).
Complete(r)
}

Expand Down
1 change: 1 addition & 0 deletions controllers/vars.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,5 +45,6 @@ const (
SliceGatewayEdgeTypeLabelKey = "kubeslice.io/slice-gw-edge-type"
NodeTypeSelectorLabelKey = "kubeslice.io/node-type"
PodTypeSelectorLabelKey = "kubeslice.io/pod-type"
PodTypeSelectorValueApp = "app"
TopologyKeySelector = "topology.kubeslice.io/gateway"
)

0 comments on commit feec8a3

Please sign in to comment.