Merge pull request #88143 from Huang-Wei/GetPodsInNamespace-bug

Fix a bug in e2epod function
This commit is contained in:
Kubernetes Prow Robot 2020-02-13 19:48:26 -08:00 committed by GitHub
commit 4c08717513
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 80 additions and 3 deletions

View File

@ -1,4 +1,4 @@
load("@io_bazel_rules_go//go:def.bzl", "go_library")
load("@io_bazel_rules_go//go:def.bzl", "go_library", "go_test")
go_library(
name = "go_default_library",
@ -47,3 +47,15 @@ filegroup(
tags = ["automanaged"],
visibility = ["//visibility:public"],
)
go_test(
name = "go_default_test",
srcs = ["resource_test.go"],
embed = [":go_default_library"],
deps = [
"//staging/src/k8s.io/api/core/v1:go_default_library",
"//staging/src/k8s.io/apimachinery/pkg/apis/meta/v1:go_default_library",
"//staging/src/k8s.io/apimachinery/pkg/runtime:go_default_library",
"//staging/src/k8s.io/client-go/kubernetes/fake:go_default_library",
],
)

View File

@ -533,8 +533,9 @@ func GetPodsInNamespace(c clientset.Interface, ns string, ignoreLabels map[strin
return []*v1.Pod{}, err
}
ignoreSelector := labels.SelectorFromSet(ignoreLabels)
filtered := []*v1.Pod{}
for _, p := range pods.Items {
var filtered []*v1.Pod
for i := range pods.Items {
p := pods.Items[i]
if len(ignoreLabels) != 0 && ignoreSelector.Matches(labels.Set(p.Labels)) {
continue
}

View File

@ -0,0 +1,64 @@
/*
Copyright 2020 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package pod
import (
"reflect"
"testing"
v1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
fakeclient "k8s.io/client-go/kubernetes/fake"
)
func TestGetPodsInNamespace(t *testing.T) {
tests := []struct {
name string
pods []runtime.Object
wantPods []*v1.Pod
expectErr bool
}{
{
name: "nil check",
},
{
name: "2 pods",
pods: []runtime.Object{
&v1.Pod{ObjectMeta: metav1.ObjectMeta{Name: "pod1"}},
&v1.Pod{ObjectMeta: metav1.ObjectMeta{Name: "pod2"}},
},
wantPods: []*v1.Pod{
{ObjectMeta: metav1.ObjectMeta{Name: "pod1"}},
{ObjectMeta: metav1.ObjectMeta{Name: "pod2"}},
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
cs := fakeclient.NewSimpleClientset(tt.pods...)
got, err := GetPodsInNamespace(cs, "", map[string]string{})
if (err != nil) != tt.expectErr {
t.Errorf("expectErr = %v, but got err = %v", tt.expectErr, err)
}
if !reflect.DeepEqual(got, tt.wantPods) {
t.Errorf("expect %v, got %v", tt.wantPods, got)
}
})
}
}