Skip to content

Commit

Permalink
bugfix: only list container stats which are created by cri
Browse files Browse the repository at this point in the history
Signed-off-by: YaoZengzeng <[email protected]>
  • Loading branch information
YaoZengzeng committed Sep 25, 2018
1 parent 7addef1 commit 3372df1
Show file tree
Hide file tree
Showing 6 changed files with 176 additions and 0 deletions.
13 changes: 13 additions & 0 deletions cri/v1alpha1/cri.go
Original file line number Diff line number Diff line change
Expand Up @@ -822,6 +822,19 @@ 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 &&
!matchLabelSelector(r.GetFilter().GetLabelSelector(), c.Config.Labels) {
return false
}
return true
}
opts.FilterFunc = filter
Expand Down
14 changes: 14 additions & 0 deletions cri/v1alpha1/cri_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -787,6 +787,20 @@ func filterCRIContainers(containers []*runtime.Container, filter *runtime.Contai
return filtered
}

// 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
}

// containerNetns returns the network namespace of the given container.
func containerNetns(container *mgr.Container) string {
pid := container.State.Pid
Expand Down
61 changes: 61 additions & 0 deletions cri/v1alpha1/cri_utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1199,6 +1199,67 @@ func Test_toCriContainer(t *testing.T) {
}
}

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)
}
})
}
}

func Test_containerNetns(t *testing.T) {
type args struct {
container *mgr.Container
Expand Down
13 changes: 13 additions & 0 deletions cri/v1alpha2/cri.go
Original file line number Diff line number Diff line change
Expand Up @@ -918,6 +918,19 @@ 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 &&
!matchLabelSelector(r.GetFilter().GetLabelSelector(), c.Config.Labels) {
return false
}
return true
}
opts.FilterFunc = filter
Expand Down
14 changes: 14 additions & 0 deletions cri/v1alpha2/cri_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -814,6 +814,20 @@ func filterCRIContainers(containers []*runtime.Container, filter *runtime.Contai
return filtered
}

// 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
}

// containerNetns returns the network namespace of the given container.
func containerNetns(container *mgr.Container) string {
pid := container.State.Pid
Expand Down
61 changes: 61 additions & 0 deletions cri/v1alpha2/cri_utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1186,6 +1186,67 @@ func Test_toCriContainer(t *testing.T) {
}
}

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)
}
})
}
}

func Test_containerNetns(t *testing.T) {
type args struct {
container *mgr.Container
Expand Down

0 comments on commit 3372df1

Please sign in to comment.