diff --git a/docs/content.en/docs/release-notes/_index.md b/docs/content.en/docs/release-notes/_index.md index 98820a6c..a5f29699 100644 --- a/docs/content.en/docs/release-notes/_index.md +++ b/docs/content.en/docs/release-notes/_index.md @@ -30,6 +30,7 @@ Information about release notes of INFINI Console is provided here. - Enhance LDAP authentication logging (#156) - Optimize UI for copying metric requests (#155) - Enhance deletion tips by adding cluster info for indices +- Retain a single instance when registering duplicate endpoints (#163) ## 1.28.2 (2025-02-15) diff --git a/docs/content.zh/docs/release-notes/_index.md b/docs/content.zh/docs/release-notes/_index.md index ab301c16..5cf1c342 100644 --- a/docs/content.zh/docs/release-notes/_index.md +++ b/docs/content.zh/docs/release-notes/_index.md @@ -30,6 +30,7 @@ title: "版本历史" - 增强 LDAP 身份验证的日志记录 (#156) - 优化监控报表里拷贝指标请求的 UI (#155) - 删除索引提示增加集群信息 (#162) +- 自动注册实例时相同 endpoint 的实例不再重复注册 (#163) ## 1.28.2 (2025-02-15) diff --git a/plugin/managed/server/instance.go b/plugin/managed/server/instance.go index df414fe6..83218d97 100644 --- a/plugin/managed/server/instance.go +++ b/plugin/managed/server/instance.go @@ -85,6 +85,11 @@ func (h APIHandler) registerInstance(w http.ResponseWriter, req *http.Request, p err := h.DecodeJSON(req, obj) if err != nil { h.WriteError(w, err.Error(), http.StatusInternalServerError) + return + } + if obj.Endpoint == "" { + h.WriteError(w, "empty endpoint", http.StatusInternalServerError) + return } oldInst := &model.Instance{} @@ -95,8 +100,28 @@ func (h APIHandler) registerInstance(w http.ResponseWriter, req *http.Request, p h.WriteError(w, errMsg, http.StatusInternalServerError) return } - - err = orm.Create(nil, obj) + err, result := orm.GetBy("endpoint", obj.Endpoint, oldInst) + if err != nil { + log.Error(err) + h.WriteError(w, err.Error(), http.StatusInternalServerError) + return + } + if len(result.Result) > 0 { + buf := util.MustToJSONBytes(result.Result[0]) + util.MustFromJSONBytes(buf, &oldInst) + if oldInst.ID != "" { + //keep old created time + obj.Created = oldInst.Created + log.Infof("remove old instance [%s] with the same endpoint %s", oldInst.ID, oldInst.Endpoint) + err = orm.Delete(nil, oldInst) + if err != nil { + log.Error(err) + h.WriteError(w, err.Error(), http.StatusInternalServerError) + return + } + } + } + err = orm.Save(nil, obj) if err != nil { h.WriteError(w, err.Error(), http.StatusInternalServerError) return