Skip to content

Commit

Permalink
Merge pull request #412 from digitalcrab/job-lables
Browse files Browse the repository at this point in the history
Filter jobs by tags
  • Loading branch information
Victor Castell authored Sep 4, 2018
2 parents 25c2f1f + 227b322 commit 6a254fc
Show file tree
Hide file tree
Showing 7 changed files with 63 additions and 13 deletions.
8 changes: 5 additions & 3 deletions dkron/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ import (
"net/http"
"strconv"

"github.com/sirupsen/logrus"
"github.com/abronan/valkeyrie/store"
gin "github.com/gin-gonic/gin"
"github.com/gin-gonic/gin"
"github.com/sirupsen/logrus"
)

const (
Expand Down Expand Up @@ -122,7 +122,9 @@ func (h *HTTPTransport) indexHandler(c *gin.Context) {
}

func (h *HTTPTransport) jobsHandler(c *gin.Context) {
jobs, err := h.agent.Store.GetJobs(&JobOptions{ComputeStatus: true})
jobTags := c.QueryMap("tags")

jobs, err := h.agent.Store.GetJobs(&JobOptions{ComputeStatus: true, Tags: jobTags})
if err != nil {
log.WithError(err).Error("api: Unable to get jobs, store not reachable.")
return
Expand Down
2 changes: 1 addition & 1 deletion dkron/job.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import (
"sync"
"time"

"github.com/sirupsen/logrus"
"github.com/abronan/valkeyrie/store"
"github.com/sirupsen/logrus"
"github.com/victorcoder/dkron/proto"
)

Expand Down
35 changes: 32 additions & 3 deletions dkron/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@ import (
"sort"
"time"

"github.com/sirupsen/logrus"
"github.com/abronan/valkeyrie"
"github.com/abronan/valkeyrie/store"
"github.com/abronan/valkeyrie/store/consul"
"github.com/abronan/valkeyrie/store/etcd/v2"
"github.com/abronan/valkeyrie/store/redis"
"github.com/abronan/valkeyrie/store/zookeeper"
"github.com/sirupsen/logrus"
"github.com/victorcoder/dkron/cron"
)

Expand Down Expand Up @@ -45,6 +45,7 @@ type Store struct {

type JobOptions struct {
ComputeStatus bool
Tags map[string]string `json:"tags"`
}

func init() {
Expand Down Expand Up @@ -220,6 +221,29 @@ func (s *Store) validateJob(job *Job) error {
return nil
}

func (s *Store) jobHasTags(job *Job, tags map[string]string) bool {
if job == nil || job.Tags == nil || len(job.Tags) == 0 {
return false
}

res := true
for k, v := range tags {
var found bool

if val, ok := job.Tags[k]; ok && v == val {
found = true
}

res = res && found

if !res {
break
}
}

return res
}

// GetJobs returns all jobs
func (s *Store) GetJobs(options *JobOptions) ([]*Job, error) {
res, err := s.Client.List(s.keyspace+"/jobs/", nil)
Expand All @@ -239,8 +263,13 @@ func (s *Store) GetJobs(options *JobOptions) ([]*Job, error) {
return nil, err
}
job.Agent = s.agent
if options != nil && options.ComputeStatus {
job.Status = job.GetStatus()
if options != nil {
if options.Tags != nil && len(options.Tags) > 0 && !s.jobHasTags(&job, options.Tags) {
continue
}
if options.ComputeStatus {
job.Status = job.GetStatus()
}
}
jobs = append(jobs, &job)
}
Expand Down
8 changes: 4 additions & 4 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,10 @@ services:
environment:
- GODEBUG=netdns=go
# Uncomment to use consul
command: scripts/run agent -server -backend=consul -backend-machine=consul:8500 -join=dkron:8946 -log-level=debug
command: scripts/run agent --server --backend=consul --backend-machine=consul:8500 --join=dkron:8946 --log-level=debug
# Uncomment to use etcd
# command: scripts/run agent -server -backend=etcd -backend-machine=etcd:2379 -join=dkron:8946 -log-level=debug
# command: scripts/run agent --server --backend=etcd --backend-machine=etcd:2379 --join=dkron:8946 --log-level=debug
# Uncomment to use zk
# command: scripts/run agent -server -backend=zk -backend-machine=zk:2181 -join=dkron:8946 -log-level=debug
# command: scripts/run agent --server --backend=zk --backend-machine=zk:2181 --join=dkron:8946 --log-level=debug
# Uncomment to use redis
# command: scripts/run agent -server -backend=redis -backend-machine=redis:6379 -join=dkron:8946 -log-level=debug
# command: scripts/run agent --server --backend=redis --backend-machine=redis:6379 --join=dkron:8946 --log-level=debug
5 changes: 4 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ require (
github.com/gin-contrib/multitemplate v0.0.0-20170922032617-bbc6daf6024b
github.com/gin-contrib/sse v0.0.0-20170109093832-22d885f9ecc7 // indirect
github.com/gin-gonic/autotls v0.0.0-20180426091246-be87bd5ef97b // indirect
github.com/gin-gonic/gin v0.0.0-20170702092826-d459835d2b07
github.com/gin-gonic/gin v1.3.0
github.com/gogo/protobuf v1.1.1 // indirect
github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b // indirect
github.com/golang/groupcache v0.0.0-20180513044358-24b0969c4cb7 // indirect
Expand Down Expand Up @@ -88,6 +88,7 @@ require (
github.com/jonboulle/clockwork v0.1.0 // indirect
github.com/jordan-wright/email v0.0.0-20180115032944-94ae17dedda2
github.com/joyent/triton-go v0.0.0-20180628001255-830d2b111e62 // indirect
github.com/json-iterator/go v1.1.5 // indirect
github.com/kardianos/osext v0.0.0-20170510131534-ae77be60afb1
github.com/kr/fs v0.1.0 // indirect
github.com/kr/pretty v0.1.0 // indirect
Expand All @@ -105,6 +106,8 @@ require (
github.com/mitchellh/go-wordwrap v0.0.0-20150314170334-ad45545899c7 // indirect
github.com/mitchellh/mapstructure v0.0.0-20171017171808-06020f85339e // indirect
github.com/mitchellh/reflectwalk v0.0.0-20170726202117-63d60e9d0dbc // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.1 // indirect
github.com/nicolai86/scaleway-sdk v1.11.1 // indirect
github.com/oklog/run v1.0.0 // indirect
github.com/olekukonko/tablewriter v0.0.0-20180506121414-d4647c9c7a84 // indirect
Expand Down
8 changes: 8 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,8 @@ github.com/gin-gonic/autotls v0.0.0-20180426091246-be87bd5ef97b h1:dm/NYytoj7p8J
github.com/gin-gonic/autotls v0.0.0-20180426091246-be87bd5ef97b/go.mod h1:vwfeXwKgEIWq63oVfwaBjoByS4dZzYbHHROHjV4IjNY=
github.com/gin-gonic/gin v0.0.0-20170702092826-d459835d2b07 h1:cZPJWzd2oNeoS0oJM2TlN9rl0OnCgUr10gC8Q4mH+6M=
github.com/gin-gonic/gin v0.0.0-20170702092826-d459835d2b07/go.mod h1:7cKuhb5qV2ggCFctp2fJQ+ErvciLZrIeoOSOm6mUr7Y=
github.com/gin-gonic/gin v1.3.0 h1:kCmZyPklC0gVdL728E6Aj20uYBJV93nj/TkwBTKhFbs=
github.com/gin-gonic/gin v1.3.0/go.mod h1:7cKuhb5qV2ggCFctp2fJQ+ErvciLZrIeoOSOm6mUr7Y=
github.com/go-ini/ini v1.25.4/go.mod h1:ByCAeIL28uOIIG0E3PJtZPDL8WnHpFKFOtgjp+3Ies8=
github.com/gogo/protobuf v1.1.1 h1:72R+M5VuhED/KujmZVcIquuo8mBgX4oVda//DQb3PXo=
github.com/gogo/protobuf v1.1.1/go.mod h1:r8qH/GZQm5c6nD/R0oafs1akxWv10x8SbQlK7atdtwQ=
Expand Down Expand Up @@ -174,6 +176,8 @@ github.com/jordan-wright/email v0.0.0-20180115032944-94ae17dedda2 h1:BkuA0hfZuy4
github.com/jordan-wright/email v0.0.0-20180115032944-94ae17dedda2/go.mod h1:1c7szIrayyPPB/987hsnvNzLushdWf4o/79s3P08L8A=
github.com/joyent/triton-go v0.0.0-20180628001255-830d2b111e62 h1:JHCT6xuyPUrbbgAPE/3dqlvUKzRHMNuTBKKUb6OeR/k=
github.com/joyent/triton-go v0.0.0-20180628001255-830d2b111e62/go.mod h1:U+RSyWxWd04xTqnuOQxnai7XGS2PrPY2cfGoDKtMHjA=
github.com/json-iterator/go v1.1.5 h1:gL2yXlmiIo4+t+y32d4WGwOjKGYcGOuyrg46vadswDE=
github.com/json-iterator/go v1.1.5/go.mod h1:+SdeFBvtyEkXs7REEP0seUULqWtbJapLOCVDaaPEHmU=
github.com/kardianos/osext v0.0.0-20170510131534-ae77be60afb1 h1:PJPDf8OUfOK1bb/NeTKd4f1QXZItOX389VN3B6qC8ro=
github.com/kardianos/osext v0.0.0-20170510131534-ae77be60afb1/go.mod h1:1NbS8ALrpOvjt0rHPNLyCIeMtbizbir8U//inJ+zuB8=
github.com/kr/fs v0.1.0 h1:Jskdu9ieNAYnjxsi0LbQp1ulIKZV1LAFgK1tWhpZgl8=
Expand Down Expand Up @@ -213,6 +217,10 @@ github.com/mitchellh/mapstructure v0.0.0-20171017171808-06020f85339e h1:PtGHLB3C
github.com/mitchellh/mapstructure v0.0.0-20171017171808-06020f85339e/go.mod h1:FVVH3fgwuzCH5S8UJGiWEs2h04kUh9fWfEaFds41c1Y=
github.com/mitchellh/reflectwalk v0.0.0-20170726202117-63d60e9d0dbc h1:gqYjvctjtX4GHzgfutJxZpvZ7XhGwQLGR5BASwhpO2o=
github.com/mitchellh/reflectwalk v0.0.0-20170726202117-63d60e9d0dbc/go.mod h1:mSTlrgnPZtwu0c4WaC2kGObEpuNDbx0jmZXqmk4esnw=
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd h1:TRLaZ9cD/w8PVh93nsPXa1VrQ6jlwL5oN8l14QlcNfg=
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
github.com/modern-go/reflect2 v1.0.1 h1:9f412s+6RmYXLWZSEzVVgPGK7C2PphHj5RJrvfx9AWI=
github.com/modern-go/reflect2 v1.0.1/go.mod h1:bx2lNnkwVCuqBIxFjflWJWanXIb3RllmbCylyMrvgv0=
github.com/nicolai86/scaleway-sdk v1.11.1 h1:iO97OP7oKcwQEHoX6RwcuKfw8PLKkjXLAsFNyumdh3M=
github.com/nicolai86/scaleway-sdk v1.11.1/go.mod h1:TLb2Sg7HQcgGdloNxkrmtgDNR9uVYF3lfdFIN4Ro6Sk=
github.com/oklog/run v1.0.0 h1:Ru7dDtJNOyC66gQ5dQmaCa0qIsAUFY3sFpK1Xk8igrw=
Expand Down
10 changes: 9 additions & 1 deletion website/content/usage/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,14 @@ json :
List jobs.


#### Parameters

|Type|Name|Description|Schema|
|---|---|---|---|
|**query**|**tags** <br>*optional*|Filter jobs by tags. Works as a logical and.|< string, string > map|



#### Responses

|HTTP Code|Description|Schema|
Expand All @@ -98,7 +106,7 @@ List jobs.

##### Request path
```
/jobs
/jobs?tags[foo]=val1&tags[bar]=val2
```


Expand Down

0 comments on commit 6a254fc

Please sign in to comment.