zelda/backend/watcher/zservice.go

49 lines
1.8 KiB
Go
Raw Normal View History

2024-11-19 16:57:27 +08:00
package watchers
import (
"context"
"time"
"k8s.io/apimachinery/pkg/types"
"k8s.io/client-go/util/workqueue"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/event"
"sigs.k8s.io/controller-runtime/pkg/reconcile"
)
type ZServiceWatcher struct {
Client client.Client
}
//监听create事件
func (zserviceWatcher *ZServiceWatcher) Create(ctx context.Context, e event.CreateEvent, limitingInterface workqueue.RateLimitingInterface) {
labels := e.Object.GetLabels()
if serviceName, ok := labels["zservices.zelda.io"]; ok {
//等待50ms
limitingInterface.AddAfter(&reconcile.Request{NamespacedName: types.NamespacedName{Namespace: e.Object.GetNamespace(), Name: serviceName}}, time.Millisecond*50)
}
}
//监听update事件
func (zserviceWatcher *ZServiceWatcher) Update(ctx context.Context, e event.UpdateEvent, limitingInterface workqueue.RateLimitingInterface) {
labels := e.ObjectNew.GetLabels()
if serviceName, ok := labels["zservices.zelda.io"]; ok {
//等待50ms
limitingInterface.AddAfter(&reconcile.Request{NamespacedName: types.NamespacedName{Namespace: e.ObjectNew.GetNamespace(), Name: serviceName}}, time.Millisecond*50)
}
}
//监听delete事件
func (zserviceWatcher *ZServiceWatcher) Delete(ctx context.Context, e event.DeleteEvent, limitingInterface workqueue.RateLimitingInterface) {
labels := e.Object.GetLabels()
if serviceName, ok := labels["zservices.zelda.io"]; ok {
//等待50ms
limitingInterface.AddAfter(&reconcile.Request{NamespacedName: types.NamespacedName{Namespace: e.Object.GetNamespace(), Name: serviceName}}, time.Millisecond*50)
}
}
//监听generic事件
func (zserviceWatcher *ZServiceWatcher) Generic(ctx context.Context, e event.GenericEvent, limitingInterface workqueue.RateLimitingInterface) {
}