dummyClient.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. package dummyclient
  2. import (
  3. "context"
  4. "fmt"
  5. "net/http"
  6. "github.com/containerd/log"
  7. "github.com/docker/docker/libnetwork/diagnostic"
  8. "github.com/docker/docker/libnetwork/networkdb"
  9. events "github.com/docker/go-events"
  10. )
  11. // DummyClientPaths2Func exported paths for the client
  12. var DummyClientPaths2Func = map[string]diagnostic.HTTPHandlerFunc{
  13. "/watchtable": watchTable,
  14. "/watchedtableentries": watchTableEntries,
  15. }
  16. const (
  17. missingParameter = "missing parameter"
  18. )
  19. type tableHandler struct {
  20. cancelWatch func()
  21. entries map[string]string
  22. }
  23. var clientWatchTable = map[string]tableHandler{}
  24. func watchTable(ctx interface{}, w http.ResponseWriter, r *http.Request) {
  25. r.ParseForm() //nolint:errcheck
  26. diagnostic.DebugHTTPForm(r)
  27. if len(r.Form["tname"]) < 1 {
  28. rsp := diagnostic.WrongCommand(missingParameter, fmt.Sprintf("%s?tname=table_name", r.URL.Path))
  29. diagnostic.HTTPReply(w, rsp, &diagnostic.JSONOutput{}) //nolint:errcheck
  30. return
  31. }
  32. tableName := r.Form["tname"][0]
  33. if _, ok := clientWatchTable[tableName]; ok {
  34. fmt.Fprintf(w, "OK\n")
  35. return
  36. }
  37. nDB, ok := ctx.(*networkdb.NetworkDB)
  38. if ok {
  39. ch, cancel := nDB.Watch(tableName, "")
  40. clientWatchTable[tableName] = tableHandler{cancelWatch: cancel, entries: make(map[string]string)}
  41. go handleTableEvents(tableName, ch)
  42. fmt.Fprintf(w, "OK\n")
  43. }
  44. }
  45. func watchTableEntries(ctx interface{}, w http.ResponseWriter, r *http.Request) {
  46. r.ParseForm() //nolint:errcheck
  47. diagnostic.DebugHTTPForm(r)
  48. if len(r.Form["tname"]) < 1 {
  49. rsp := diagnostic.WrongCommand(missingParameter, fmt.Sprintf("%s?tname=table_name", r.URL.Path))
  50. diagnostic.HTTPReply(w, rsp, &diagnostic.JSONOutput{}) //nolint:errcheck
  51. return
  52. }
  53. tableName := r.Form["tname"][0]
  54. table, ok := clientWatchTable[tableName]
  55. if !ok {
  56. fmt.Fprintf(w, "Table %s not watched\n", tableName)
  57. return
  58. }
  59. fmt.Fprintf(w, "total elements: %d\n", len(table.entries))
  60. i := 0
  61. for k, v := range table.entries {
  62. fmt.Fprintf(w, "%d) k:`%s` -> v:`%s`\n", i, k, v)
  63. i++
  64. }
  65. }
  66. func handleTableEvents(tableName string, ch *events.Channel) {
  67. var (
  68. // nid string
  69. eid string
  70. value []byte
  71. isAdd bool
  72. )
  73. log.G(context.TODO()).Infof("Started watching table:%s", tableName)
  74. for {
  75. select {
  76. case <-ch.Done():
  77. log.G(context.TODO()).Infof("End watching %s", tableName)
  78. return
  79. case evt := <-ch.C:
  80. log.G(context.TODO()).Infof("Recevied new event on:%s", tableName)
  81. switch event := evt.(type) {
  82. case networkdb.CreateEvent:
  83. // nid = event.NetworkID
  84. eid = event.Key
  85. value = event.Value
  86. isAdd = true
  87. case networkdb.DeleteEvent:
  88. // nid = event.NetworkID
  89. eid = event.Key
  90. value = event.Value
  91. isAdd = false
  92. default:
  93. log.G(context.TODO()).Fatalf("Unexpected table event = %#v", event)
  94. }
  95. if isAdd {
  96. // log.G(ctx).Infof("Add %s %s", tableName, eid)
  97. clientWatchTable[tableName].entries[eid] = string(value)
  98. } else {
  99. // log.G(ctx).Infof("Del %s %s", tableName, eid)
  100. delete(clientWatchTable[tableName].entries, eid)
  101. }
  102. }
  103. }
  104. }