Skip to content

Commit

Permalink
Merge pull request #2273 from YaoZengzeng/filter-stats
Browse files Browse the repository at this point in the history
bugfix: only list container stats which are created by cri
  • Loading branch information
fuweid authored Sep 30, 2018
2 parents fa85aa5 + ee3640c commit 6719cc6
Show file tree
Hide file tree
Showing 4 changed files with 114 additions and 0 deletions.
18 changes: 18 additions & 0 deletions cri/utils/utils.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package utils

// Move the public utility methods which are used by both v1alpha1
// and v1alpha2 here to reduce the code duplication.

// MatchLabelSelector returns true if labels cover selector.
func MatchLabelSelector(selector, labels map[string]string) bool {
for k, v := range selector {
if val, ok := labels[k]; ok {
if v != val {
return false
}
} else {
return false
}
}
return true
}
66 changes: 66 additions & 0 deletions cri/utils/utils_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package utils

import (
"testing"
)

func Test_MatchLabelSelector(t *testing.T) {
type args struct {
selector map[string]string
labels map[string]string
}
tests := []struct {
name string
args args
want bool
}{
{
name: "Normal Test",
args: args{
selector: map[string]string{
"a1": "b1",
"a2": "b2",
},
labels: map[string]string{
"a1": "b1",
"a2": "b2",
},
},
want: true,
},
{
name: "Uncovered Test",
args: args{
selector: map[string]string{
"a1": "b1",
"a2": "b2",
},
labels: map[string]string{
"a2": "b2",
},
},
want: false,
},
{
name: "Unmatched Test",
args: args{
selector: map[string]string{
"a1": "b0",
"a2": "b2",
},
labels: map[string]string{
"a1": "b1",
"a2": "b2",
},
},
want: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := MatchLabelSelector(tt.args.selector, tt.args.labels); got != tt.want {
t.Errorf("matchLabelSelector() = %v, want %v", got, tt.want)
}
})
}
}
15 changes: 15 additions & 0 deletions cri/v1alpha1/cri.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
anno "github.com/alibaba/pouch/cri/annotations"
cni "github.com/alibaba/pouch/cri/ocicni"
"github.com/alibaba/pouch/cri/stream"
criutils "github.com/alibaba/pouch/cri/utils"
"github.com/alibaba/pouch/daemon/config"
"github.com/alibaba/pouch/daemon/mgr"
"github.com/alibaba/pouch/pkg/errtypes"
Expand Down Expand Up @@ -827,6 +828,20 @@ func (c *CriManager) ContainerStats(ctx context.Context, r *runtime.ContainerSta
func (c *CriManager) ListContainerStats(ctx context.Context, r *runtime.ListContainerStatsRequest) (*runtime.ListContainerStatsResponse, error) {
opts := &mgr.ContainerListOption{All: true}
filter := func(c *mgr.Container) bool {
if c.Config.Labels[containerTypeLabelKey] != containerTypeLabelContainer {
return false
}

if r.GetFilter().GetId() != "" && c.ID != r.GetFilter().GetId() {
return false
}
if r.GetFilter().GetPodSandboxId() != "" && c.Config.Labels[sandboxIDLabelKey] != r.GetFilter().GetPodSandboxId() {
return false
}
if r.GetFilter().GetLabelSelector() != nil &&
!criutils.MatchLabelSelector(r.GetFilter().GetLabelSelector(), c.Config.Labels) {
return false
}
return true
}
opts.FilterFunc = filter
Expand Down
15 changes: 15 additions & 0 deletions cri/v1alpha2/cri.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
runtime "github.com/alibaba/pouch/cri/apis/v1alpha2"
cni "github.com/alibaba/pouch/cri/ocicni"
"github.com/alibaba/pouch/cri/stream"
criutils "github.com/alibaba/pouch/cri/utils"
"github.com/alibaba/pouch/daemon/config"
"github.com/alibaba/pouch/daemon/mgr"
"github.com/alibaba/pouch/pkg/errtypes"
Expand Down Expand Up @@ -923,6 +924,20 @@ func (c *CriManager) ContainerStats(ctx context.Context, r *runtime.ContainerSta
func (c *CriManager) ListContainerStats(ctx context.Context, r *runtime.ListContainerStatsRequest) (*runtime.ListContainerStatsResponse, error) {
opts := &mgr.ContainerListOption{All: true}
filter := func(c *mgr.Container) bool {
if c.Config.Labels[containerTypeLabelKey] != containerTypeLabelContainer {
return false
}

if r.GetFilter().GetId() != "" && c.ID != r.GetFilter().GetId() {
return false
}
if r.GetFilter().GetPodSandboxId() != "" && c.Config.Labels[sandboxIDLabelKey] != r.GetFilter().GetPodSandboxId() {
return false
}
if r.GetFilter().GetLabelSelector() != nil &&
!criutils.MatchLabelSelector(r.GetFilter().GetLabelSelector(), c.Config.Labels) {
return false
}
return true
}
opts.FilterFunc = filter
Expand Down

0 comments on commit 6719cc6

Please sign in to comment.