Skip to content

feature: use watch instead of client get in Reconciler #452

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

Merged
merged 1 commit into from
Jul 6, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 42 additions & 11 deletions pkg/controller/inference/service_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"context"
"fmt"
"reflect"
"sync"

corev1 "k8s.io/api/core/v1"
apimeta "k8s.io/apimachinery/pkg/api/meta"
Expand All @@ -39,6 +40,7 @@ import (
"sigs.k8s.io/controller-runtime/pkg/handler"
"sigs.k8s.io/controller-runtime/pkg/log"
"sigs.k8s.io/controller-runtime/pkg/predicate"
"sigs.k8s.io/controller-runtime/pkg/reconcile"
lws "sigs.k8s.io/lws/api/leaderworkerset/v1"
applyconfigurationv1 "sigs.k8s.io/lws/client-go/applyconfiguration/leaderworkerset/v1"

Expand All @@ -52,8 +54,10 @@ import (
// ServiceReconciler reconciles a Service object
type ServiceReconciler struct {
client.Client
Scheme *runtime.Scheme
Record record.EventRecorder
Scheme *runtime.Scheme
Record record.EventRecorder
GlobalConfigsMutex sync.RWMutex
GlobalConfigs *helper.GlobalConfigs
}

func NewServiceReconciler(client client.Client, scheme *runtime.Scheme, record record.EventRecorder) *ServiceReconciler {
Expand Down Expand Up @@ -86,15 +90,12 @@ func (r *ServiceReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ct

logger.V(10).Info("reconcile Service", "Service", klog.KObj(service))

cm := &corev1.ConfigMap{}
if err := r.Get(ctx, types.NamespacedName{Name: "llmaz-global-config", Namespace: "llmaz-system"}, cm); err != nil {
if client.IgnoreNotFound(err) != nil {
return ctrl.Result{}, fmt.Errorf("failed to get llmaz-global-config configmap: %w", err)
}
}
configs, err := helper.ParseGlobalConfigmap(cm)
if err != nil {
return ctrl.Result{}, fmt.Errorf("failed to parse global configurations: %w", err)
r.GlobalConfigsMutex.RLock()
configs := r.GlobalConfigs
r.GlobalConfigsMutex.RUnlock()

if configs == nil {
return ctrl.Result{}, fmt.Errorf("globel configs not init")
}

// Set the global configurations to the service.
Expand Down Expand Up @@ -160,9 +161,39 @@ func (r *ServiceReconciler) SetupWithManager(mgr ctrl.Manager) error {
return !reflect.DeepEqual(oldBar.Status, newBar.Status)
},
})).
Watches(&corev1.ConfigMap{}, handler.EnqueueRequestsFromMapFunc(r.updateGlobalConfig),
Copy link
Member Author

Choose a reason for hiding this comment

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

We use watch instead of getting configmap every loop, which reduces calls to the api-server.

builder.WithPredicates(predicate.Funcs{
UpdateFunc: func(e event.UpdateEvent) bool {
cm := e.ObjectOld.(*corev1.ConfigMap)
return cm.Name == helper.GlobalConfigMapName && cm.Namespace == helper.GlobalConfigMapNamespace
},
CreateFunc: func(e event.CreateEvent) bool {
cm := e.Object.(*corev1.ConfigMap)
return cm.Name == helper.GlobalConfigMapName && cm.Namespace == helper.GlobalConfigMapNamespace
},
})).
Complete(r)
}

func (r *ServiceReconciler) updateGlobalConfig(ctx context.Context, obj client.Object) []reconcile.Request {
logger := log.FromContext(ctx)
cm, ok := obj.(*corev1.ConfigMap)
if !ok {
return nil
}

newConfig, err := helper.ParseGlobalConfigmap(cm)
if err != nil {
logger.Error(err, "failed to parse global config")
return nil
}
r.GlobalConfigsMutex.Lock()
defer r.GlobalConfigsMutex.Unlock()
r.GlobalConfigs = newConfig
logger.Info("global config updated", "config", newConfig)
return nil
}

func buildWorkloadApplyConfiguration(service *inferenceapi.Service, models []*coreapi.OpenModel, configs *helper.GlobalConfigs) (*applyconfigurationv1.LeaderWorkerSetApplyConfiguration, error) {
workload := applyconfigurationv1.LeaderWorkerSet(service.Name, service.Namespace)

Expand Down
7 changes: 7 additions & 0 deletions pkg/controller_helper/configmap.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,13 @@ import (
corev1 "k8s.io/api/core/v1"
)

const (
GlobalConfigMapName = "llmaz-global-config"
GlobalConfigMapNamespace = "llmaz-system"
)

// GlobalConfigs defines the global configuration parameters used across services.
// These configurations are typically provided via a ConfigMap named "llmaz-global-config"
type GlobalConfigs struct {
SchedulerName string `yaml:"scheduler-name"`
InitContainerImage string `yaml:"init-container-image"`
Expand Down
Loading