mirror of
https://github.com/datarhei/core.git
synced 2025-10-05 16:07:07 +08:00
Refactor cluster node code
This commit is contained in:
@@ -7,8 +7,7 @@ import (
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
clientapi "github.com/datarhei/core-client-go/v16/api"
|
||||
"github.com/datarhei/core/v16/cluster/proxy"
|
||||
"github.com/datarhei/core/v16/cluster/node"
|
||||
"github.com/datarhei/core/v16/cluster/store"
|
||||
"github.com/datarhei/core/v16/encoding/json"
|
||||
"github.com/datarhei/core/v16/glob"
|
||||
@@ -20,7 +19,7 @@ import (
|
||||
"github.com/lithammer/shortuuid/v4"
|
||||
)
|
||||
|
||||
// GetAllProcesses returns the list of processes running on all nodes of the cluster
|
||||
// ProcessList returns the list of processes running on all nodes of the cluster
|
||||
// @Summary List of processes in the cluster
|
||||
// @Description List of processes in the cluster
|
||||
// @Tags v16.?.?
|
||||
@@ -37,7 +36,7 @@ import (
|
||||
// @Success 200 {array} api.Process
|
||||
// @Security ApiKeyAuth
|
||||
// @Router /api/v3/cluster/process [get]
|
||||
func (h *ClusterHandler) GetAllProcesses(c echo.Context) error {
|
||||
func (h *ClusterHandler) ProcessList(c echo.Context) error {
|
||||
ctxuser := util.DefaultContext(c, "user", "")
|
||||
filter := newFilter(util.DefaultQuery(c, "filter", ""))
|
||||
reference := util.DefaultQuery(c, "reference", "")
|
||||
@@ -50,7 +49,7 @@ func (h *ClusterHandler) GetAllProcesses(c echo.Context) error {
|
||||
ownerpattern := util.DefaultQuery(c, "ownerpattern", "")
|
||||
domainpattern := util.DefaultQuery(c, "domainpattern", "")
|
||||
|
||||
procs := h.proxy.ListProcesses(proxy.ProcessListOptions{
|
||||
procs := h.proxy.ProcessList(node.ProcessListOptions{
|
||||
ID: wantids,
|
||||
Filter: filter.Slice(),
|
||||
Domain: domain,
|
||||
@@ -61,7 +60,7 @@ func (h *ClusterHandler) GetAllProcesses(c echo.Context) error {
|
||||
DomainPattern: domainpattern,
|
||||
})
|
||||
|
||||
processes := []clientapi.Process{}
|
||||
processes := []api.Process{}
|
||||
pmap := map[app.ProcessID]struct{}{}
|
||||
|
||||
for _, p := range procs {
|
||||
@@ -77,7 +76,7 @@ func (h *ClusterHandler) GetAllProcesses(c echo.Context) error {
|
||||
|
||||
// Here we have to add those processes that are in the cluster DB and couldn't be deployed
|
||||
{
|
||||
processes := h.cluster.ListProcesses()
|
||||
processes := h.cluster.Store().ProcessList()
|
||||
filtered := h.getFilteredStoreProcesses(processes, wantids, domain, reference, idpattern, refpattern, ownerpattern, domainpattern)
|
||||
|
||||
for _, p := range filtered {
|
||||
@@ -139,7 +138,7 @@ func (h *ClusterHandler) GetAllProcesses(c echo.Context) error {
|
||||
return c.Stream(http.StatusOK, "application/json", buf)
|
||||
}
|
||||
|
||||
func (h *ClusterHandler) getFilteredStoreProcesses(processes []store.Process, wantids []string, domain, reference, idpattern, refpattern, ownerpattern, domainpattern string) []store.Process {
|
||||
func (h *ClusterHandler) getFilteredStoreProcesses(processes []store.Process, wantids []string, _, reference, idpattern, refpattern, ownerpattern, domainpattern string) []store.Process {
|
||||
filtered := []store.Process{}
|
||||
|
||||
count := 0
|
||||
@@ -293,7 +292,7 @@ func (h *ClusterHandler) convertStoreProcessToAPIProcess(p store.Process, filter
|
||||
return process
|
||||
}
|
||||
|
||||
// GetProcess returns the process with the given ID whereever it's running on the cluster
|
||||
// ProcessGet returns the process with the given ID whereever it's running on the cluster
|
||||
// @Summary List a process by its ID
|
||||
// @Description List a process by its ID. Use the filter parameter to specifiy the level of detail of the output.
|
||||
// @Tags v16.?.?
|
||||
@@ -307,7 +306,7 @@ func (h *ClusterHandler) convertStoreProcessToAPIProcess(p store.Process, filter
|
||||
// @Failure 404 {object} api.Error
|
||||
// @Security ApiKeyAuth
|
||||
// @Router /api/v3/cluster/process/{id} [get]
|
||||
func (h *ClusterHandler) GetProcess(c echo.Context) error {
|
||||
func (h *ClusterHandler) ProcessGet(c echo.Context) error {
|
||||
ctxuser := util.DefaultContext(c, "user", "")
|
||||
id := util.PathParam(c, "id")
|
||||
filter := newFilter(util.DefaultQuery(c, "filter", ""))
|
||||
@@ -317,7 +316,7 @@ func (h *ClusterHandler) GetProcess(c echo.Context) error {
|
||||
return api.Err(http.StatusForbidden, "")
|
||||
}
|
||||
|
||||
procs := h.proxy.ListProcesses(proxy.ProcessListOptions{
|
||||
procs := h.proxy.ProcessList(node.ProcessListOptions{
|
||||
ID: []string{id},
|
||||
Filter: filter.Slice(),
|
||||
Domain: domain,
|
||||
@@ -325,7 +324,7 @@ func (h *ClusterHandler) GetProcess(c echo.Context) error {
|
||||
|
||||
if len(procs) == 0 {
|
||||
// Check the store in the cluster for an undeployed process
|
||||
p, err := h.cluster.GetProcess(app.NewProcessID(id, domain))
|
||||
p, err := h.cluster.Store().ProcessGet(app.NewProcessID(id, domain))
|
||||
if err != nil {
|
||||
return api.Err(http.StatusNotFound, "", "Unknown process ID: %s", id)
|
||||
}
|
||||
@@ -355,7 +354,7 @@ func (h *ClusterHandler) GetProcess(c echo.Context) error {
|
||||
// @Failure 403 {object} api.Error
|
||||
// @Security ApiKeyAuth
|
||||
// @Router /api/v3/cluster/process [post]
|
||||
func (h *ClusterHandler) AddProcess(c echo.Context) error {
|
||||
func (h *ClusterHandler) ProcessAdd(c echo.Context) error {
|
||||
ctxuser := util.DefaultContext(c, "user", "")
|
||||
superuser := util.DefaultContext(c, "superuser", false)
|
||||
|
||||
@@ -390,12 +389,12 @@ func (h *ClusterHandler) AddProcess(c echo.Context) error {
|
||||
|
||||
config, metadata := process.Marshal()
|
||||
|
||||
if err := h.cluster.AddProcess("", config); err != nil {
|
||||
if err := h.cluster.ProcessAdd("", config); err != nil {
|
||||
return api.Err(http.StatusBadRequest, "", "adding process config: %s", err.Error())
|
||||
}
|
||||
|
||||
for key, value := range metadata {
|
||||
h.cluster.SetProcessMetadata("", config.ProcessID(), key, value)
|
||||
h.cluster.ProcessSetMetadata("", config.ProcessID(), key, value)
|
||||
}
|
||||
|
||||
return c.JSON(http.StatusOK, process)
|
||||
@@ -417,7 +416,7 @@ func (h *ClusterHandler) AddProcess(c echo.Context) error {
|
||||
// @Failure 404 {object} api.Error
|
||||
// @Security ApiKeyAuth
|
||||
// @Router /api/v3/cluster/process/{id} [put]
|
||||
func (h *ClusterHandler) UpdateProcess(c echo.Context) error {
|
||||
func (h *ClusterHandler) ProcessUpdate(c echo.Context) error {
|
||||
ctxuser := util.DefaultContext(c, "user", "")
|
||||
superuser := util.DefaultContext(c, "superuser", false)
|
||||
domain := util.DefaultQuery(c, "domain", "")
|
||||
@@ -437,7 +436,7 @@ func (h *ClusterHandler) UpdateProcess(c echo.Context) error {
|
||||
|
||||
pid := process.ProcessID()
|
||||
|
||||
current, err := h.cluster.GetProcess(pid)
|
||||
current, err := h.cluster.Store().ProcessGet(pid)
|
||||
if err != nil {
|
||||
return api.Err(http.StatusNotFound, "", "process not found: %s in domain '%s'", pid.ID, pid.Domain)
|
||||
}
|
||||
@@ -461,7 +460,7 @@ func (h *ClusterHandler) UpdateProcess(c echo.Context) error {
|
||||
|
||||
config, metadata := process.Marshal()
|
||||
|
||||
if err := h.cluster.UpdateProcess("", pid, config); err != nil {
|
||||
if err := h.cluster.ProcessUpdate("", pid, config); err != nil {
|
||||
if err == restream.ErrUnknownProcess {
|
||||
return api.Err(http.StatusNotFound, "", "process not found: %s in domain '%s'", pid.ID, pid.Domain)
|
||||
}
|
||||
@@ -472,7 +471,7 @@ func (h *ClusterHandler) UpdateProcess(c echo.Context) error {
|
||||
pid = process.ProcessID()
|
||||
|
||||
for key, value := range metadata {
|
||||
h.cluster.SetProcessMetadata("", pid, key, value)
|
||||
h.cluster.ProcessSetMetadata("", pid, key, value)
|
||||
}
|
||||
|
||||
return c.JSON(http.StatusOK, process)
|
||||
@@ -494,7 +493,7 @@ func (h *ClusterHandler) UpdateProcess(c echo.Context) error {
|
||||
// @Failure 404 {object} api.Error
|
||||
// @Security ApiKeyAuth
|
||||
// @Router /api/v3/cluster/process/{id}/command [put]
|
||||
func (h *ClusterHandler) SetProcessCommand(c echo.Context) error {
|
||||
func (h *ClusterHandler) ProcessSetCommand(c echo.Context) error {
|
||||
id := util.PathParam(c, "id")
|
||||
ctxuser := util.DefaultContext(c, "user", "")
|
||||
domain := util.DefaultQuery(c, "domain", "")
|
||||
@@ -523,14 +522,14 @@ func (h *ClusterHandler) SetProcessCommand(c echo.Context) error {
|
||||
return api.Err(http.StatusBadRequest, "", "unknown command provided. known commands are: start, stop, reload, restart")
|
||||
}
|
||||
|
||||
if err := h.cluster.SetProcessCommand("", pid, command.Command); err != nil {
|
||||
if err := h.cluster.ProcessSetCommand("", pid, command.Command); err != nil {
|
||||
return api.Err(http.StatusNotFound, "", "command failed: %s", err.Error())
|
||||
}
|
||||
|
||||
return c.JSON(http.StatusOK, "OK")
|
||||
}
|
||||
|
||||
// SetProcessMetadata stores metadata with a process
|
||||
// ProcessSetMetadata stores metadata with a process
|
||||
// @Summary Add JSON metadata with a process under the given key
|
||||
// @Description Add arbitrary JSON metadata under the given key. If the key exists, all already stored metadata with this key will be overwritten. If the key doesn't exist, it will be created.
|
||||
// @Tags v16.?.?
|
||||
@@ -546,7 +545,7 @@ func (h *ClusterHandler) SetProcessCommand(c echo.Context) error {
|
||||
// @Failure 404 {object} api.Error
|
||||
// @Security ApiKeyAuth
|
||||
// @Router /api/v3/cluster/process/{id}/metadata/{key} [put]
|
||||
func (h *ClusterHandler) SetProcessMetadata(c echo.Context) error {
|
||||
func (h *ClusterHandler) ProcessSetMetadata(c echo.Context) error {
|
||||
id := util.PathParam(c, "id")
|
||||
key := util.PathParam(c, "key")
|
||||
ctxuser := util.DefaultContext(c, "user", "")
|
||||
@@ -571,14 +570,14 @@ func (h *ClusterHandler) SetProcessMetadata(c echo.Context) error {
|
||||
Domain: domain,
|
||||
}
|
||||
|
||||
if err := h.cluster.SetProcessMetadata("", pid, key, data); err != nil {
|
||||
if err := h.cluster.ProcessSetMetadata("", pid, key, data); err != nil {
|
||||
return api.Err(http.StatusNotFound, "", "setting metadata failed: %s", err.Error())
|
||||
}
|
||||
|
||||
return c.JSON(http.StatusOK, data)
|
||||
}
|
||||
|
||||
// GetProcessMetadata returns the metadata stored with a process
|
||||
// ProcessGetMetadata returns the metadata stored with a process
|
||||
// @Summary Retrieve JSON metadata stored with a process under a key
|
||||
// @Description Retrieve the previously stored JSON metadata under the given key. If the key is empty, all metadata will be returned.
|
||||
// @Tags v16.?.?
|
||||
@@ -593,7 +592,7 @@ func (h *ClusterHandler) SetProcessMetadata(c echo.Context) error {
|
||||
// @Failure 404 {object} api.Error
|
||||
// @Security ApiKeyAuth
|
||||
// @Router /api/v3/cluster/process/{id}/metadata/{key} [get]
|
||||
func (h *ClusterHandler) GetProcessMetadata(c echo.Context) error {
|
||||
func (h *ClusterHandler) ProcessGetMetadata(c echo.Context) error {
|
||||
id := util.PathParam(c, "id")
|
||||
key := util.PathParam(c, "key")
|
||||
ctxuser := util.DefaultContext(c, "user", "")
|
||||
@@ -608,7 +607,7 @@ func (h *ClusterHandler) GetProcessMetadata(c echo.Context) error {
|
||||
Domain: domain,
|
||||
}
|
||||
|
||||
data, err := h.cluster.GetProcessMetadata("", pid, key)
|
||||
data, err := h.cluster.ProcessGetMetadata("", pid, key)
|
||||
if err != nil {
|
||||
return api.Err(http.StatusNotFound, "", "unknown process ID: %s", err.Error())
|
||||
}
|
||||
@@ -628,7 +627,7 @@ func (h *ClusterHandler) GetProcessMetadata(c echo.Context) error {
|
||||
// @Failure 403 {object} api.Error
|
||||
// @Security ApiKeyAuth
|
||||
// @Router /api/v3/cluster/process/{id}/probe [get]
|
||||
func (h *ClusterHandler) ProbeProcess(c echo.Context) error {
|
||||
func (h *ClusterHandler) ProcessProbe(c echo.Context) error {
|
||||
id := util.PathParam(c, "id")
|
||||
ctxuser := util.DefaultContext(c, "user", "")
|
||||
domain := util.DefaultQuery(c, "domain", "")
|
||||
@@ -642,14 +641,14 @@ func (h *ClusterHandler) ProbeProcess(c echo.Context) error {
|
||||
Domain: domain,
|
||||
}
|
||||
|
||||
nodeid, err := h.proxy.FindNodeFromProcess(pid)
|
||||
nodeid, err := h.proxy.ProcessFindNodeID(pid)
|
||||
if err != nil {
|
||||
return c.JSON(http.StatusOK, api.Probe{
|
||||
Log: []string{fmt.Sprintf("the process can't be found: %s", err.Error())},
|
||||
})
|
||||
}
|
||||
|
||||
probe, _ := h.proxy.ProbeProcess(nodeid, pid)
|
||||
probe, _ := h.proxy.ProcessProbe(nodeid, pid)
|
||||
|
||||
return c.JSON(http.StatusOK, probe)
|
||||
}
|
||||
@@ -669,7 +668,7 @@ func (h *ClusterHandler) ProbeProcess(c echo.Context) error {
|
||||
// @Failure 500 {object} api.Error
|
||||
// @Security ApiKeyAuth
|
||||
// @Router /api/v3/cluster/process/probe [post]
|
||||
func (h *ClusterHandler) ProbeProcessConfig(c echo.Context) error {
|
||||
func (h *ClusterHandler) ProcessProbeConfig(c echo.Context) error {
|
||||
ctxuser := util.DefaultContext(c, "user", "")
|
||||
coreid := util.DefaultQuery(c, "coreid", "")
|
||||
|
||||
@@ -702,12 +701,12 @@ func (h *ClusterHandler) ProbeProcessConfig(c echo.Context) error {
|
||||
|
||||
config, _ := process.Marshal()
|
||||
|
||||
coreid = h.proxy.FindNodeFromResources(coreid, config.LimitCPU, config.LimitMemory)
|
||||
coreid = h.proxy.FindNodeForResources(coreid, config.LimitCPU, config.LimitMemory)
|
||||
if len(coreid) == 0 {
|
||||
return api.Err(http.StatusInternalServerError, "", "Not enough available resources available to execute probe")
|
||||
return api.Err(http.StatusInternalServerError, "", "Not enough resources available to execute probe")
|
||||
}
|
||||
|
||||
probe, _ := h.proxy.ProbeProcessConfig(coreid, config)
|
||||
probe, _ := h.proxy.ProcessProbeConfig(coreid, config)
|
||||
|
||||
return c.JSON(http.StatusOK, probe)
|
||||
}
|
||||
@@ -724,7 +723,7 @@ func (h *ClusterHandler) ProbeProcessConfig(c echo.Context) error {
|
||||
// @Failure 403 {object} api.Error
|
||||
// @Security ApiKeyAuth
|
||||
// @Router /api/v3/cluster/process/{id} [delete]
|
||||
func (h *ClusterHandler) DeleteProcess(c echo.Context) error {
|
||||
func (h *ClusterHandler) ProcessDelete(c echo.Context) error {
|
||||
ctxuser := util.DefaultContext(c, "user", "")
|
||||
domain := util.DefaultQuery(c, "domain", "")
|
||||
id := util.PathParam(c, "id")
|
||||
@@ -738,7 +737,7 @@ func (h *ClusterHandler) DeleteProcess(c echo.Context) error {
|
||||
Domain: domain,
|
||||
}
|
||||
|
||||
if err := h.cluster.RemoveProcess("", pid); err != nil {
|
||||
if err := h.cluster.ProcessRemove("", pid); err != nil {
|
||||
return api.Err(http.StatusBadRequest, "", "%s", err.Error())
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user