Skip to content

Commit

Permalink
add list mesos agents filter
Browse files Browse the repository at this point in the history
  • Loading branch information
pwzgorilla committed Sep 13, 2017
1 parent a5237af commit 3663b12
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 11 deletions.
22 changes: 20 additions & 2 deletions api/mesos.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,14 @@ func (s *Server) listMesosAgents(w http.ResponseWriter, r *http.Request) {
return
}

writeJSON(w, http.StatusOK, agents)
if err := r.ParseForm(); err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}

filtered := s.filter(agents, r.Form)

writeJSON(w, http.StatusOK, filtered)
}

func (s *Server) getAgentLabels(w http.ResponseWriter, r *http.Request) {
Expand Down Expand Up @@ -197,7 +204,6 @@ func (s *Server) canOperated(label *types.MesosLabel) bool {
}

constraints := ver.Constraints
log.Println("===", constraints)
if len(constraints) == 0 {
continue
}
Expand All @@ -211,3 +217,15 @@ func (s *Server) canOperated(label *types.MesosLabel) bool {

return true
}

func (s *Server) filter(agents []*types.MesosAgent, filter map[string][]string) []*types.MesosAgent {
filtered := make([]*types.MesosAgent, 0)

for _, agent := range agents {
if agent.Match(filter) {
filtered = append(filtered, agent)
}
}

return filtered
}
4 changes: 2 additions & 2 deletions mesos/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,8 @@ func (s *Scheduler) updateMesosAgent() {

for _, agent := range s.agents {
a := &types.MesosAgent{
ID: agent.id,
IP: agent.hostname,
ID: agent.ID(),
IP: agent.IP(),
}

cpus, mem, disk, ports := agent.Resources()
Expand Down
17 changes: 10 additions & 7 deletions mesos/scheduler.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,13 +75,16 @@ type Scheduler struct {
// NewScheduler...
func NewScheduler(cfg *SchedulerConfig, db store.Store, clusterMaster *mole.Master) (*Scheduler, error) {
s := &Scheduler{
cfg: cfg,
quit: make(chan struct{}),
agents: make(map[string]*magent.Agent),
pendingTasks: make(map[string]*Task),
db: db,
strategy: strategy.NewBinPackStrategy(), // default strategy
filters: []filter.Filter{filter.NewConstraintsFilter(), filter.NewResourceFilter()},
cfg: cfg,
quit: make(chan struct{}),
agents: make(map[string]*magent.Agent),
pendingTasks: make(map[string]*Task),
db: db,
strategy: strategy.NewBinPackStrategy(), // default strategy
filters: []filter.Filter{
filter.NewConstraintsFilter(db),
filter.NewResourceFilter(),
},
eventmgr: NewEventManager(),
clusterMaster: clusterMaster,
sem: make(chan struct{}, 1), // allow only one offer acquirement at one time
Expand Down
1 change: 1 addition & 0 deletions types/compose.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ func (c *Compose) Valid() error {
if err := utils.LegalDomain(c.Name); err != nil {
return err
}

if c.Name == "default" {
return errors.New("compose name `default` is reserved")
}
Expand Down
23 changes: 23 additions & 0 deletions types/mesos.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,26 @@ type MesosAgent struct {
Ports int64 `json:"ports"`
Attrs map[string]string `json:"attrs"`
}

func (m *MesosAgent) Match(filter map[string][]string) bool {
var n int
for k, vals := range filter {
if v, ok := m.Attrs[k]; ok {
if in(v, vals) {
n++
}
}
}

return n == len(filter)
}

func in(v string, vals []string) bool {
for i := 0; i < len(vals); i++ {
if v == vals[i] {
return true
}
}

return false
}

0 comments on commit 3663b12

Please sign in to comment.