Skip to content

Commit

Permalink
feature: add registry watcherStatus endpoint (#913) (#915)
Browse files Browse the repository at this point in the history
Co-authored-by: Kent Dong <[email protected]>
  • Loading branch information
hanxiantao and CH3CHO authored Apr 24, 2024
1 parent e68b5c8 commit a787088
Show file tree
Hide file tree
Showing 6 changed files with 78 additions and 4 deletions.
38 changes: 38 additions & 0 deletions pkg/bootstrap/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -398,6 +398,7 @@ func (s *Server) initHttpServer() error {
}
s.xdsServer.AddDebugHandlers(s.httpMux, nil, true, nil)
s.httpMux.HandleFunc("/ready", s.readyHandler)
s.httpMux.HandleFunc("/registry/watcherStatus", s.registryWatcherStatusHandler)
return nil
}

Expand All @@ -413,6 +414,43 @@ func (s *Server) readyHandler(w http.ResponseWriter, _ *http.Request) {
w.WriteHeader(http.StatusOK)
}

func (s *Server) registryWatcherStatusHandler(w http.ResponseWriter, _ *http.Request) {
ingressTranslation, ok := s.environment.IngressStore.(*translation.IngressTranslation)
if !ok {
http.Error(w, "IngressStore not found", http.StatusNotFound)
return
}

ingressConfig := ingressTranslation.GetIngressConfig()
if ingressConfig == nil {
http.Error(w, "IngressConfig not found", http.StatusNotFound)
return
}

registryReconciler := ingressConfig.RegistryReconciler
if registryReconciler == nil {
http.Error(w, "RegistryReconciler not found", http.StatusNotFound)
return
}

watcherStatusList := registryReconciler.GetRegistryWatcherStatusList()
writeJSON(w, watcherStatusList)
}

func writeJSON(w http.ResponseWriter, obj interface{}) {
w.Header().Set("Content-Type", "application/json")
b, err := config.ToJSON(obj)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
_, _ = w.Write([]byte(err.Error()))
return
}
_, err = w.Write(b)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
}
}

// cachesSynced checks whether caches have been synced.
func (s *Server) cachesSynced() bool {
return s.configController.HasSynced()
Expand Down
4 changes: 4 additions & 0 deletions pkg/ingress/translation/translation.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,10 @@ func (m *IngressTranslation) InitializeCluster(ingressController common.IngressC
return nil
}

func (m *IngressTranslation) GetIngressConfig() *ingressconfig.IngressConfig {
return m.ingressConfig
}

func (m *IngressTranslation) RegisterEventHandler(kind config.GroupVersionKind, f model.EventHandler) {
m.ingressConfig.RegisterEventHandler(kind, f)
if m.kingressConfig != nil {
Expand Down
4 changes: 4 additions & 0 deletions registry/direct/watcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,3 +162,7 @@ func (w *watcher) generateServiceEntry(host string) *v1alpha3.ServiceEntry {
}
return se
}

func (w *watcher) GetRegistryType() string {
return w.RegistryConfig.Type
}
22 changes: 22 additions & 0 deletions registry/reconcile/reconcile.go
Original file line number Diff line number Diff line change
Expand Up @@ -278,3 +278,25 @@ func (r *Reconciler) getAuthOption(registry *apiv1.RegistryConfig) (AuthOption,

return authOption, nil
}

type RegistryWatcherStatus struct {
Name string `json:"name"`
Type string `json:"type"`
Healthy bool `json:"healthy"`
Ready bool `json:"ready"`
}

func (r *Reconciler) GetRegistryWatcherStatusList() []RegistryWatcherStatus {
var registryStatusList []RegistryWatcherStatus
for key, watcher := range r.watchers {
_, name := path.Split(key)
registryStatus := RegistryWatcherStatus{
Name: name,
Type: watcher.GetRegistryType(),
Healthy: watcher.IsHealthy(),
Ready: watcher.IsReady(),
}
registryStatusList = append(registryStatusList, registryStatus)
}
return registryStatusList
}
10 changes: 8 additions & 2 deletions registry/watcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ type Watcher interface {
Run()
Stop()
IsHealthy() bool
IsReady() bool
GetRegistryType() string
AppendServiceUpdateHandler(f func())
ReadyHandler(f func(bool))
Expand All @@ -57,17 +58,22 @@ type Watcher interface {
type BaseWatcher struct {
UpdateService ServiceUpdateHandler
Ready ReadyHandler
ReadyStatus bool
}

func (w *BaseWatcher) Run() {}
func (w *BaseWatcher) Stop() {}
func (w *BaseWatcher) IsHealthy() bool { return true }
func (w *BaseWatcher) IsReady() bool { return w.ReadyStatus }
func (w *BaseWatcher) GetRegistryType() string { return "" }
func (w *BaseWatcher) AppendServiceUpdateHandler(f func()) {
w.UpdateService = f
}
func (w *BaseWatcher) ReadyHandler(f func(bool)) {
w.Ready = f
func (w *BaseWatcher) ReadyHandler(f func(isReady bool)) {
w.Ready = func(isReady bool) {
w.ReadyStatus = isReady
f(isReady)
}
}

type ServiceUpdateHandler func()
Expand Down
4 changes: 2 additions & 2 deletions registry/zookeeper/watcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -678,7 +678,7 @@ func (w *watcher) Run() {
select {
case <-ticker.C:
var needNewFetch bool
if w.IsReady() {
if w.watcherReady() {
w.Ready(true)
needNewFetch = true
}
Expand Down Expand Up @@ -727,7 +727,7 @@ func (w *watcher) GetRegistryType() string {
return w.RegistryType.String()
}

func (w *watcher) IsReady() bool {
func (w *watcher) watcherReady() bool {
if w.serviceRemaind == nil {
return true
}
Expand Down

0 comments on commit a787088

Please sign in to comment.