|
1 | 1 | package handlers
|
2 | 2 |
|
3 | 3 | import (
|
| 4 | + "context" |
| 5 | + "errors" |
| 6 | + "net/http" |
| 7 | + "time" |
| 8 | + |
4 | 9 | "github.com/gin-gonic/gin"
|
| 10 | + "go.uber.org/zap" |
| 11 | + |
| 12 | + "github.com/e2b-dev/infra/packages/proxy/internal/edge/orchestrators" |
| 13 | + orchestratorinfo "github.com/e2b-dev/infra/packages/shared/pkg/grpc/orchestrator-info" |
5 | 14 | )
|
6 | 15 |
|
7 | 16 | func (a *APIStore) V1ServiceDiscoveryNodeDrain(c *gin.Context, nodeId string) {
|
8 |
| - /* |
9 |
| - findCtx, findCtxCancel := context.WithTimeout(c, 5*time.Second) |
10 |
| - defer findCtxCancel() |
11 |
| -
|
12 |
| - logger := a.logger.With(zap.String("node_id", nodeId)) |
13 |
| -
|
14 |
| - node, err := a.serviceDiscovery.GetNodeById(findCtx, nodeId) |
15 |
| - if err != nil { |
16 |
| - logger.Error("failed to get node by id", zap.Error(err)) |
17 |
| -
|
18 |
| - if errors.Is(err, service_discovery.NodeNotFoundErr) { |
19 |
| - a.sendAPIStoreError(c, http.StatusNotFound, "node not found") |
20 |
| - } else { |
21 |
| - a.sendAPIStoreError(c, http.StatusInternalServerError, "failed to get node by id") |
22 |
| - } |
23 |
| -
|
24 |
| - return |
25 |
| - } |
| 17 | + err := a.sendNodeRequest(c, nodeId, orchestratorinfo.ServiceInfoStatus_OrchestratorDraining) |
| 18 | + if err != nil { |
| 19 | + a.sendAPIStoreError(c, http.StatusBadRequest, "Error when calling service discovery node") |
| 20 | + return |
| 21 | + } |
26 | 22 |
|
27 |
| - currentNodeId := a.serviceDiscovery.GetSelfNodeId() |
28 |
| - if nodeId != currentNodeId { |
29 |
| - logger.Info("sending draining node request to neighbor", zap.String("node_ip", node.NodeIp)) |
| 23 | + c.Status(http.StatusOK) |
| 24 | +} |
30 | 25 |
|
31 |
| - err := a.serviceDiscoveryCallNeighborNodes(c, nodeId, node.NodeIp, node.NodePort, "drain") |
32 |
| - if err != nil { |
33 |
| - logger.Error("failed to call node drain", zap.Error(err)) |
34 |
| - a.sendAPIStoreError(c, http.StatusInternalServerError, "failed to call neighbor node") |
35 |
| - return |
36 |
| - } |
| 26 | +func (a *APIStore) sendNodeRequest(ctx context.Context, nodeId string, status orchestratorinfo.ServiceInfoStatus) error { |
| 27 | + findCtx, findCtxCancel := context.WithTimeout(ctx, 5*time.Second) |
| 28 | + defer findCtxCancel() |
37 | 29 |
|
38 |
| - logger.Info("cluster node drain request delivered") |
39 |
| - c.Status(http.StatusOK) |
40 |
| - return |
41 |
| - } |
| 30 | + logger := a.logger.With(zap.String("node_id", nodeId)) |
42 | 31 |
|
43 |
| - // handle self drain process |
44 |
| - if a.selfDrainHandler == nil { |
45 |
| - logger.Error("self drain handler is not set") |
46 |
| - a.sendAPIStoreError(c, http.StatusInternalServerError, "self drain handler is not configured") |
47 |
| - return |
| 32 | + // try to find orchestrator node first |
| 33 | + o, err := a.orchestratorPool.GetOrchestrator(nodeId) |
| 34 | + if err != nil { |
| 35 | + if !errors.Is(err, orchestrators.ErrOrchestratorNotFound) { |
| 36 | + logger.Warn("Failed to get orchestrator", zap.Error(err)) |
| 37 | + return errors.New("Error when getting orchestrator node") |
48 | 38 | }
|
| 39 | + } |
49 | 40 |
|
50 |
| - logger.Info("starting self-drain process") |
| 41 | + if o != nil { |
| 42 | + logger.Info("found orchestrator node, calling drain request") |
| 43 | + _, err = o.Client.Info.ServiceStatusOverride( |
| 44 | + findCtx, &orchestratorinfo.ServiceStatusChangeRequest{ServiceStatus: status}, |
| 45 | + ) |
51 | 46 |
|
52 |
| - err = (*a.selfDrainHandler)() |
53 | 47 | if err != nil {
|
54 |
| - logger.Error("failed to start self drain process", zap.Error(err)) |
55 |
| - a.sendAPIStoreError(c, http.StatusInternalServerError, "failed to start self drain process") |
56 |
| - return |
| 48 | + logger.Error("failed to drain orchestrator node", zap.Error(err)) |
| 49 | + return errors.New("Failed to drain orchestrator node") |
57 | 50 | }
|
58 | 51 |
|
59 |
| - a.healthStatus = api.Draining |
60 |
| - c.Status(http.StatusOK) |
61 |
| -
|
62 |
| - */ |
63 |
| -} |
64 |
| - |
65 |
| -/* |
66 |
| -func (a *APIStore) serviceDiscoveryCallNeighborNodes(ctx context.Context, nodeId string, nodeIp string, nodePort int, callMethod string) error { |
67 |
| - // todo: add authorization when implemented |
68 |
| - // update / drain |
69 |
| - reqUrl := fmt.Sprintf("http://%s:%d/v1/service-discovery/nodes/%s/%s", nodeIp, nodePort, nodeId, callMethod) |
70 |
| - req, err := http.NewRequest(http.MethodPost, reqUrl, nil) |
71 |
| - if err != nil { |
72 |
| - return err |
73 |
| - } |
74 |
| -
|
75 |
| - reqCtx, reqCtxCancel := context.WithTimeout(ctx, 5*time.Second) |
76 |
| - defer reqCtxCancel() |
77 |
| -
|
78 |
| - req.WithContext(reqCtx) |
79 |
| - res, err := http.DefaultClient.Do(req) |
80 |
| - if err != nil { |
81 |
| - return err |
82 |
| - } |
83 |
| -
|
84 |
| - if res.StatusCode != http.StatusOK { |
85 |
| - return fmt.Errorf("failed to call neighbor node: %s", res.Status) |
| 52 | + return nil |
86 | 53 | }
|
87 | 54 |
|
88 |
| - return nil |
| 55 | + // todo: call edge api node |
| 56 | + return errors.New("Failed to find node, it must be edge api node") |
89 | 57 | }
|
90 |
| -*/ |
0 commit comments