-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
143 lines (131 loc) · 3.64 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
package main
import (
"context"
"fmt"
"log"
"net/http"
"os"
"strconv"
"time"
"github.com/envoyproxy/go-control-plane/pkg/cache/v3"
"github.com/envoyproxy/go-control-plane/pkg/server/v3"
"github.com/envoyproxy/go-control-plane/pkg/test/v3"
"github.com/fvbock/endless"
"github.com/gin-gonic/gin"
)
func main() {
httpServer := NewHttpServer()
router := gin.Default()
router.GET("/api/:key", func(c *gin.Context) {
key := c.Param("key")
if ret, ok := httpServer.Get(key); ok {
c.JSON(http.StatusOK, ret)
} else {
c.JSON(http.StatusNotFound, gin.H{"error": "prometheus target not found"})
}
})
router.GET("/api", func(c *gin.Context) {
c.JSON(http.StatusOK, httpServer.List())
})
router.POST("/api", func(c *gin.Context) {
var target KeyedEdsTarget
if err := c.ShouldBindJSON(&target); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}
err := httpServer.Post(target.Key, target)
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{"result": fmt.Sprintf("Error: %v", err)})
return
}
c.JSON(http.StatusAccepted, gin.H{"result": "ok"})
})
router.DELETE("/api/:key", func(c *gin.Context) {
key := c.Param("key")
err := httpServer.Delete(key)
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{"result": "key does not exist"})
return
}
c.JSON(http.StatusAccepted, gin.H{"result": "ok"})
})
stringHttpPort := os.Getenv("HTTP_LISTEN_PORT")
intHttpPort, err := strconv.Atoi(stringHttpPort)
if err != nil {
log.Fatal("please provide a valid environment variable HTTP_LISTEN_PORT")
return
}
nodeID := os.Getenv("NODE_ID")
stringGrpcPort := os.Getenv("GRPC_LISTEN_PORT")
intGrpcPort, err := strconv.Atoi(stringGrpcPort)
if err != nil {
log.Fatal("please provide a valid environment variable GRPC_LISTEN_PORT")
return
}
stringEvictionTimeout := os.Getenv("EVICTION_TIMEOUT_IN_SEC")
var intEvictionTimeout int
if stringEvictionTimeout == "" {
intEvictionTimeout = 42
} else {
intEvictionTimeout, err = strconv.Atoi(stringEvictionTimeout)
if err != nil {
log.Fatal("please provide a valid numeric value of seconds for environment variable EVICTION_TIMEOUT_IN_SEC")
return
}
}
httpServer.EvictionTimeout = intEvictionTimeout
edsResource := EdsResource{
ClusterName: "cluster",
WebServer: httpServer,
NodeId: nodeID,
SnapshotVersion: 1,
}
stopChan := make(chan int)
completionChan := make(chan int, 2)
evictionTicker := time.NewTicker(time.Second)
var edsServer server.Server
customEdsServer := CustomEdsServer{}
go func() {
log.Printf("EDS Server is listening for incoming HTTP requests on port %d", intHttpPort)
endless.ListenAndServe(fmt.Sprintf("0.0.0.0:%d", intHttpPort), router)
customEdsServer.Shutdown()
stopChan <- 1
}()
go func() {
l := Logger{}
datacache := cache.NewSnapshotCache(false, cache.IDHash{}, l)
snapshot := edsResource.GenerateSnapshot()
httpServer.DataCache = &datacache
httpServer.Eds = &edsResource
ctx := context.Background()
if err := datacache.SetSnapshot(ctx, nodeID, snapshot); err != nil {
l.Errorf("snapshot error %q for %+v", err, snapshot)
os.Exit(1)
}
cb := &test.Callbacks{
Fetches: 0,
Requests: 0,
}
edsServer = server.NewServer(ctx, datacache, cb)
customEdsServer.Initialize()
customEdsServer.RunGrpcServer(ctx, edsServer, uint(intGrpcPort))
completionChan <- 1
}()
go func() {
exit := false
for !exit {
select {
case <-evictionTicker.C:
if intEvictionTimeout > 0 {
httpServer.EvictHeartbeatTimeout()
}
case <-stopChan:
exit = true
evictionTicker.Stop()
}
}
completionChan <- 1
}()
<-completionChan
<-completionChan
}