-Fix the name could cause a conflict if an object with the same name is created in a different namespace

This commit is contained in:
Guoliang Wang 2018-04-18 12:16:32 +08:00
parent 47a1aac931
commit df49a4b8c6
7 changed files with 29 additions and 24 deletions

View File

@ -117,12 +117,12 @@ func (c compareStrategy) ComparePods(pods, waitingPods []*v1.Pod, nodeinfos map[
func (c compareStrategy) ComparePdbs(pdbs []*policy.PodDisruptionBudget, pdbCache map[string]*policy.PodDisruptionBudget) (missed, redundant []string) { func (c compareStrategy) ComparePdbs(pdbs []*policy.PodDisruptionBudget, pdbCache map[string]*policy.PodDisruptionBudget) (missed, redundant []string) {
actual := []string{} actual := []string{}
for _, pdb := range pdbs { for _, pdb := range pdbs {
actual = append(actual, string(pdb.Name)) actual = append(actual, string(pdb.UID))
} }
cached := []string{} cached := []string{}
for pdbName := range pdbCache { for pdbUID := range pdbCache {
cached = append(cached, pdbName) cached = append(cached, pdbUID)
} }
return compareStrings(actual, cached) return compareStrings(actual, cached)

View File

@ -202,17 +202,17 @@ func TestComparePdbs(t *testing.T) {
for _, test := range tests { for _, test := range tests {
pdbs := []*policy.PodDisruptionBudget{} pdbs := []*policy.PodDisruptionBudget{}
for _, name := range test.actual { for _, uid := range test.actual {
pdb := &policy.PodDisruptionBudget{} pdb := &policy.PodDisruptionBudget{}
pdb.Name = name pdb.UID = types.UID(uid)
pdbs = append(pdbs, pdb) pdbs = append(pdbs, pdb)
} }
cache := make(map[string]*policy.PodDisruptionBudget) cache := make(map[string]*policy.PodDisruptionBudget)
for _, name := range test.cached { for _, uid := range test.cached {
pdb := &policy.PodDisruptionBudget{} pdb := &policy.PodDisruptionBudget{}
pdb.Name = name pdb.UID = types.UID(uid)
cache[name] = pdb cache[uid] = pdb
} }
m, r := compare.ComparePdbs(pdbs, cache) m, r := compare.ComparePdbs(pdbs, cache)

View File

@ -432,7 +432,7 @@ func (cache *schedulerCache) AddPDB(pdb *policy.PodDisruptionBudget) error {
defer cache.mu.Unlock() defer cache.mu.Unlock()
// Unconditionally update cache. // Unconditionally update cache.
cache.pdbs[pdb.Name] = pdb cache.pdbs[string(pdb.UID)] = pdb
return nil return nil
} }
@ -444,7 +444,7 @@ func (cache *schedulerCache) RemovePDB(pdb *policy.PodDisruptionBudget) error {
cache.mu.Lock() cache.mu.Lock()
defer cache.mu.Unlock() defer cache.mu.Unlock()
delete(cache.pdbs, pdb.Name) delete(cache.pdbs, string(pdb.UID))
return nil return nil
} }

View File

@ -1137,13 +1137,14 @@ func setupCacheWithAssumedPods(b *testing.B, podNum int, assumedTime time.Time)
return cache return cache
} }
func makePDB(name, namespace string, labels map[string]string, minAvailable int) *v1beta1.PodDisruptionBudget { func makePDB(name, namespace string, uid types.UID, labels map[string]string, minAvailable int) *v1beta1.PodDisruptionBudget {
intstrMin := intstr.FromInt(minAvailable) intstrMin := intstr.FromInt(minAvailable)
pdb := &v1beta1.PodDisruptionBudget{ pdb := &v1beta1.PodDisruptionBudget{
ObjectMeta: metav1.ObjectMeta{ ObjectMeta: metav1.ObjectMeta{
Namespace: namespace, Namespace: namespace,
Name: name, Name: name,
Labels: labels, Labels: labels,
UID: uid,
}, },
Spec: v1beta1.PodDisruptionBudgetSpec{ Spec: v1beta1.PodDisruptionBudgetSpec{
MinAvailable: &intstrMin, MinAvailable: &intstrMin,
@ -1158,14 +1159,14 @@ func makePDB(name, namespace string, labels map[string]string, minAvailable int)
func TestPDBOperations(t *testing.T) { func TestPDBOperations(t *testing.T) {
ttl := 10 * time.Second ttl := 10 * time.Second
testPDBs := []*v1beta1.PodDisruptionBudget{ testPDBs := []*v1beta1.PodDisruptionBudget{
makePDB("pdb0", "ns1", map[string]string{"tkey1": "tval1"}, 3), makePDB("pdb0", "ns1", "uid0", map[string]string{"tkey1": "tval1"}, 3),
makePDB("pdb1", "ns1", map[string]string{"tkey1": "tval1", "tkey2": "tval2"}, 1), makePDB("pdb1", "ns1", "uid1", map[string]string{"tkey1": "tval1", "tkey2": "tval2"}, 1),
makePDB("pdb2", "ns3", map[string]string{"tkey3": "tval3", "tkey2": "tval2"}, 10), makePDB("pdb2", "ns3", "uid2", map[string]string{"tkey3": "tval3", "tkey2": "tval2"}, 10),
} }
updatedPDBs := []*v1beta1.PodDisruptionBudget{ updatedPDBs := []*v1beta1.PodDisruptionBudget{
makePDB("pdb0", "ns1", map[string]string{"tkey4": "tval4"}, 8), makePDB("pdb0", "ns1", "uid0", map[string]string{"tkey4": "tval4"}, 8),
makePDB("pdb1", "ns1", map[string]string{"tkey1": "tval1"}, 1), makePDB("pdb1", "ns1", "uid1", map[string]string{"tkey1": "tval1"}, 1),
makePDB("pdb2", "ns3", map[string]string{"tkey3": "tval3", "tkey1": "tval1", "tkey2": "tval2"}, 10), makePDB("pdb2", "ns3", "uid2", map[string]string{"tkey3": "tval3", "tkey1": "tval1", "tkey2": "tval2"}, 10),
} }
tests := []struct { tests := []struct {
pdbsToAdd []*v1beta1.PodDisruptionBudget pdbsToAdd []*v1beta1.PodDisruptionBudget
@ -1225,7 +1226,7 @@ func TestPDBOperations(t *testing.T) {
found := false found := false
// find it among the cached ones // find it among the cached ones
for _, cpdb := range cachedPDBs { for _, cpdb := range cachedPDBs {
if pdb.Name == cpdb.Name { if pdb.UID == cpdb.UID {
found = true found = true
if !reflect.DeepEqual(pdb, cpdb) { if !reflect.DeepEqual(pdb, cpdb) {
t.Errorf("%v is not equal to %v", pdb, cpdb) t.Errorf("%v is not equal to %v", pdb, cpdb)
@ -1234,7 +1235,7 @@ func TestPDBOperations(t *testing.T) {
} }
} }
if !found { if !found {
t.Errorf("PDB with name '%v' was not found in the cache.", pdb.Name) t.Errorf("PDB with uid '%v' was not found in the cache.", pdb.UID)
} }
} }

View File

@ -51,6 +51,7 @@ go_test(
"//vendor/k8s.io/apimachinery/pkg/api/resource:go_default_library", "//vendor/k8s.io/apimachinery/pkg/api/resource:go_default_library",
"//vendor/k8s.io/apimachinery/pkg/apis/meta/v1:go_default_library", "//vendor/k8s.io/apimachinery/pkg/apis/meta/v1:go_default_library",
"//vendor/k8s.io/apimachinery/pkg/labels:go_default_library", "//vendor/k8s.io/apimachinery/pkg/labels:go_default_library",
"//vendor/k8s.io/apimachinery/pkg/types:go_default_library",
"//vendor/k8s.io/apimachinery/pkg/util/diff:go_default_library", "//vendor/k8s.io/apimachinery/pkg/util/diff:go_default_library",
"//vendor/k8s.io/apimachinery/pkg/util/intstr:go_default_library", "//vendor/k8s.io/apimachinery/pkg/util/intstr:go_default_library",
"//vendor/k8s.io/apimachinery/pkg/util/sets:go_default_library", "//vendor/k8s.io/apimachinery/pkg/util/sets:go_default_library",

View File

@ -28,6 +28,7 @@ import (
"k8s.io/apimachinery/pkg/api/resource" "k8s.io/apimachinery/pkg/api/resource"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/labels" "k8s.io/apimachinery/pkg/labels"
"k8s.io/apimachinery/pkg/types"
"k8s.io/apimachinery/pkg/util/intstr" "k8s.io/apimachinery/pkg/util/intstr"
"k8s.io/apimachinery/pkg/util/wait" "k8s.io/apimachinery/pkg/util/wait"
utilfeature "k8s.io/apiserver/pkg/util/feature" utilfeature "k8s.io/apiserver/pkg/util/feature"
@ -496,7 +497,7 @@ func TestNominatedNodeCleanUp(t *testing.T) {
} }
} }
func mkMinAvailablePDB(name, namespace string, minAvailable int, matchLabels map[string]string) *policy.PodDisruptionBudget { func mkMinAvailablePDB(name, namespace string, uid types.UID, minAvailable int, matchLabels map[string]string) *policy.PodDisruptionBudget {
intMinAvailable := intstr.FromInt(minAvailable) intMinAvailable := intstr.FromInt(minAvailable)
return &policy.PodDisruptionBudget{ return &policy.PodDisruptionBudget{
ObjectMeta: metav1.ObjectMeta{ ObjectMeta: metav1.ObjectMeta{
@ -546,7 +547,7 @@ func TestPDBInPreemption(t *testing.T) {
description: "A non-PDB violating pod is preempted despite its higher priority", description: "A non-PDB violating pod is preempted despite its higher priority",
nodes: []*nodeConfig{{name: "node-1", res: defaultNodeRes}}, nodes: []*nodeConfig{{name: "node-1", res: defaultNodeRes}},
pdbs: []*policy.PodDisruptionBudget{ pdbs: []*policy.PodDisruptionBudget{
mkMinAvailablePDB("pdb-1", context.ns.Name, 2, map[string]string{"foo": "bar"}), mkMinAvailablePDB("pdb-1", context.ns.Name, types.UID("pdb-1-uid"), 2, map[string]string{"foo": "bar"}),
}, },
existingPods: []*v1.Pod{ existingPods: []*v1.Pod{
initPausePod(context.clientSet, &pausePodConfig{ initPausePod(context.clientSet, &pausePodConfig{
@ -588,7 +589,7 @@ func TestPDBInPreemption(t *testing.T) {
{name: "node-2", res: defaultNodeRes}, {name: "node-2", res: defaultNodeRes},
}, },
pdbs: []*policy.PodDisruptionBudget{ pdbs: []*policy.PodDisruptionBudget{
mkMinAvailablePDB("pdb-1", context.ns.Name, 2, map[string]string{"foo": "bar"}), mkMinAvailablePDB("pdb-1", context.ns.Name, types.UID("pdb-1-uid"), 2, map[string]string{"foo": "bar"}),
}, },
existingPods: []*v1.Pod{ existingPods: []*v1.Pod{
initPausePod(context.clientSet, &pausePodConfig{ initPausePod(context.clientSet, &pausePodConfig{
@ -626,8 +627,8 @@ func TestPDBInPreemption(t *testing.T) {
{name: "node-3", res: defaultNodeRes}, {name: "node-3", res: defaultNodeRes},
}, },
pdbs: []*policy.PodDisruptionBudget{ pdbs: []*policy.PodDisruptionBudget{
mkMinAvailablePDB("pdb-1", context.ns.Name, 2, map[string]string{"foo1": "bar"}), mkMinAvailablePDB("pdb-1", context.ns.Name, types.UID("pdb-1-uid"), 2, map[string]string{"foo1": "bar"}),
mkMinAvailablePDB("pdb-2", context.ns.Name, 2, map[string]string{"foo2": "bar"}), mkMinAvailablePDB("pdb-2", context.ns.Name, types.UID("pdb-2-uid"), 2, map[string]string{"foo2": "bar"}),
}, },
existingPods: []*v1.Pod{ existingPods: []*v1.Pod{
initPausePod(context.clientSet, &pausePodConfig{ initPausePod(context.clientSet, &pausePodConfig{

View File

@ -29,6 +29,7 @@ import (
"k8s.io/apimachinery/pkg/api/resource" "k8s.io/apimachinery/pkg/api/resource"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/labels" "k8s.io/apimachinery/pkg/labels"
"k8s.io/apimachinery/pkg/types"
"k8s.io/apimachinery/pkg/util/diff" "k8s.io/apimachinery/pkg/util/diff"
"k8s.io/apimachinery/pkg/util/intstr" "k8s.io/apimachinery/pkg/util/intstr"
"k8s.io/apimachinery/pkg/util/sets" "k8s.io/apimachinery/pkg/util/sets"
@ -671,6 +672,7 @@ func TestPDBCache(t *testing.T) {
ObjectMeta: metav1.ObjectMeta{ ObjectMeta: metav1.ObjectMeta{
Namespace: context.ns.Name, Namespace: context.ns.Name,
Name: "test-pdb", Name: "test-pdb",
UID: types.UID("test-pdb-uid"),
Labels: map[string]string{"tkey1": "tval1", "tkey2": "tval2"}, Labels: map[string]string{"tkey1": "tval1", "tkey2": "tval2"},
}, },
Spec: policy.PodDisruptionBudgetSpec{ Spec: policy.PodDisruptionBudgetSpec{