Skip to content

Commit

Permalink
support select container by label
Browse files Browse the repository at this point in the history
  • Loading branch information
zhangxu19830126 committed Aug 12, 2019
1 parent fa155df commit 75ffa41
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 2 deletions.
5 changes: 3 additions & 2 deletions meta.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,14 +71,16 @@ type Resource interface {
Stale(other Resource) bool
Changed(other Resource) bool
Clone() Resource
Labels() []Pair

Marshal() ([]byte, error)
Unmarshal(data []byte) error
}

// Pair key value pair
type Pair struct {
Key, Value string
Key string `json:"key"`
Value string `json:"value"`
}

// Container is the resource container, the resource is running on the container
Expand All @@ -90,7 +92,6 @@ type Container interface {
ID() uint64
Labels() []Pair
State() State

Clone() Container

Marshal() ([]byte, error)
Expand Down
27 changes: 27 additions & 0 deletions scheduler_filter.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ func filterTarget(container *ContainerRuntime, filters []Filter) bool {
return false
}

type labelFilter struct {
labels []Pair
}

type stateFilter struct {
cfg *Cfg
}
Expand Down Expand Up @@ -112,6 +116,11 @@ func NewSnapshotCountFilter(cfg *Cfg) Filter {
return &snapshotCountFilter{cfg: cfg}
}

// NewLabelFilter returns label filter, if the container missing the labels, skip it.
func NewLabelFilter(labels []Pair) Filter {
return &labelFilter{labels: labels}
}

func (f *stateFilter) filter(container *ContainerRuntime) bool {
return !(container.IsUp() && container.Downtime() < f.cfg.MaxAllowContainerDownDuration)
}
Expand Down Expand Up @@ -198,3 +207,21 @@ func (f *snapshotCountFilter) FilterSource(container *ContainerRuntime) bool {
func (f *snapshotCountFilter) FilterTarget(container *ContainerRuntime) bool {
return f.filter(container)
}

func (f *labelFilter) filter(container *ContainerRuntime) bool {
for _, l := range f.labels {
if l.Value != container.GetLabelValue(l.Key) {
return true
}
}

return false
}

func (f *labelFilter) FilterSource(container *ContainerRuntime) bool {
return f.filter(container)
}

func (f *labelFilter) FilterTarget(container *ContainerRuntime) bool {
return f.filter(container)
}
1 change: 1 addition & 0 deletions scheduler_replica.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ func (r *replicaChecker) selectBestPeer(target *ResourceRuntime, allocPeerID boo
filters = append(filters, NewStateFilter(r.cfg))
filters = append(filters, NewStorageThresholdFilter(r.cfg))
filters = append(filters, NewExcludedFilter(nil, target.GetContainerIDs()))
filters = append(filters, NewLabelFilter(target.meta.Labels())) // make sure the container has the tags needed for the resource

var (
bestContainer *ContainerRuntime
Expand Down

0 comments on commit 75ffa41

Please sign in to comment.