// URL /api/container/:container_id/addvlan/ func addVlanForContainer(req *Request) (int, interface{}) { type IP struct { Nid int `json:"nid"` IP string `json:"ip"` } type Data struct { TaskID int `json:"task_id"` IPs []IP `json:"ips"` } conn := g.GetRedisConn() defer g.Rds.Release(conn) cid := req.URL.Query().Get(":container_id") data := &Data{} decoder := json.NewDecoder(req.Body) err := decoder.Decode(data) if err != nil { return http.StatusBadRequest, JSON{"message": "wrong JSON format"} } feedKey := fmt.Sprintf("eru:agent:%s:feedback", data.TaskID) for seq, ip := range data.IPs { vethName := fmt.Sprintf("%s%d.%d", common.VLAN_PREFIX, ip.Nid, seq) if network.AddVLan(vethName, ip.IP, cid) { gore.NewCommand("LPUSH", feedKey, fmt.Sprintf("1|%s|%s|%s", cid, vethName, ip.IP)).Run(conn) continue } else { gore.NewCommand("LPUSH", feedKey, "0|||").Run(conn) } } return http.StatusOK, JSON{"message": "ok"} }
func vlanWatcher() { conn := g.GetRedisConn() report := g.GetRedisConn() defer g.ReleaseRedisConn(conn) defer g.ReleaseRedisConn(report) subs := gore.NewSubscriptions(conn) defer subs.Close() subKey := fmt.Sprintf("eru:agent:%s:vlan", g.Config.HostName) logs.Debug("API vlan subscribe", subKey) subs.Subscribe(subKey) for message := range subs.Message() { if message == nil { logs.Info("API vLan watcher shutdown") break } command := string(message.Message) logs.Debug("API vlan watcher get", command) parser := strings.Split(command, "|") if len(parser) <= 2 { logs.Info("API vlan watcher command invaild", command) continue } taskID, cid := parser[0], parser[1] feedKey := fmt.Sprintf("eru:agent:%s:feedback", taskID) for seq, content := range parser[2:] { p := strings.Split(content, ":") if len(p) != 2 { logs.Info("API vlan watcher ips invaild", content) continue } nid, ips := p[0], p[1] vethName := fmt.Sprintf("%s%s.%d", common.VLAN_PREFIX, nid, seq) if network.AddVLan(vethName, ips, cid) { gore.NewCommand("LPUSH", feedKey, fmt.Sprintf("1|%s|%s|%s", cid, vethName, ips)).Run(report) continue } gore.NewCommand("LPUSH", feedKey, "0|||").Run(report) } } }