Stop publishing Pod ResourceVersion in Endpoints and EndpointSlice API

The field is not used anywhere and its value may be stale as Endpoints
and EndpointSlice won't be updated if there is only Pod ResourceVersion
change..
This commit is contained in:
Quan Tian 2022-02-17 14:10:49 +08:00
parent 422001df8b
commit 906e6d4670
5 changed files with 96 additions and 78 deletions

View File

@ -255,11 +255,10 @@ func podToEndpointAddressForService(svc *v1.Service, pod *v1.Pod) (*v1.EndpointA
IP: endpointIP, IP: endpointIP,
NodeName: &pod.Spec.NodeName, NodeName: &pod.Spec.NodeName,
TargetRef: &v1.ObjectReference{ TargetRef: &v1.ObjectReference{
Kind: "Pod", Kind: "Pod",
Namespace: pod.ObjectMeta.Namespace, Namespace: pod.ObjectMeta.Namespace,
Name: pod.ObjectMeta.Name, Name: pod.ObjectMeta.Name,
UID: pod.ObjectMeta.UID, UID: pod.ObjectMeta.UID,
ResourceVersion: pod.ObjectMeta.ResourceVersion,
}, },
}, nil }, nil
} }

View File

@ -63,9 +63,10 @@ func testPod(namespace string, id int, nPorts int, isReady bool, ipFamilies []v1
p := &v1.Pod{ p := &v1.Pod{
TypeMeta: metav1.TypeMeta{APIVersion: "v1"}, TypeMeta: metav1.TypeMeta{APIVersion: "v1"},
ObjectMeta: metav1.ObjectMeta{ ObjectMeta: metav1.ObjectMeta{
Namespace: namespace, Namespace: namespace,
Name: fmt.Sprintf("pod%d", id), Name: fmt.Sprintf("pod%d", id),
Labels: map[string]string{"foo": "bar"}, Labels: map[string]string{"foo": "bar"},
ResourceVersion: fmt.Sprint(id),
}, },
Spec: v1.PodSpec{ Spec: v1.PodSpec{
Containers: []v1.Container{{Ports: []v1.ContainerPort{}}}, Containers: []v1.Container{{Ports: []v1.ContainerPort{}}},
@ -1430,16 +1431,16 @@ func TestPodToEndpointAddressForService(t *testing.T) {
t.Fatalf("TargetRef.Kind: expected: %s, got: %s", "Pod", epa.TargetRef.Kind) t.Fatalf("TargetRef.Kind: expected: %s, got: %s", "Pod", epa.TargetRef.Kind)
} }
if epa.TargetRef.Namespace != pod.ObjectMeta.Namespace { if epa.TargetRef.Namespace != pod.ObjectMeta.Namespace {
t.Fatalf("TargetRef.Kind: expected: %s, got: %s", pod.ObjectMeta.Namespace, epa.TargetRef.Namespace) t.Fatalf("TargetRef.Namespace: expected: %s, got: %s", pod.ObjectMeta.Namespace, epa.TargetRef.Namespace)
} }
if epa.TargetRef.Name != pod.ObjectMeta.Name { if epa.TargetRef.Name != pod.ObjectMeta.Name {
t.Fatalf("TargetRef.Kind: expected: %s, got: %s", pod.ObjectMeta.Name, epa.TargetRef.Name) t.Fatalf("TargetRef.Name: expected: %s, got: %s", pod.ObjectMeta.Name, epa.TargetRef.Name)
} }
if epa.TargetRef.UID != pod.ObjectMeta.UID { if epa.TargetRef.UID != pod.ObjectMeta.UID {
t.Fatalf("TargetRef.Kind: expected: %s, got: %s", pod.ObjectMeta.UID, epa.TargetRef.UID) t.Fatalf("TargetRef.UID: expected: %s, got: %s", pod.ObjectMeta.UID, epa.TargetRef.UID)
} }
if epa.TargetRef.ResourceVersion != pod.ObjectMeta.ResourceVersion { if epa.TargetRef.ResourceVersion != "" {
t.Fatalf("TargetRef.Kind: expected: %s, got: %s", pod.ObjectMeta.ResourceVersion, epa.TargetRef.ResourceVersion) t.Fatalf("TargetRef.ResourceVersion: expected empty, got: %s", epa.TargetRef.ResourceVersion)
} }
}) })
} }

View File

@ -53,11 +53,10 @@ func podToEndpoint(pod *corev1.Pod, node *corev1.Node, service *corev1.Service,
Ready: &ready, Ready: &ready,
}, },
TargetRef: &corev1.ObjectReference{ TargetRef: &corev1.ObjectReference{
Kind: "Pod", Kind: "Pod",
Namespace: pod.ObjectMeta.Namespace, Namespace: pod.ObjectMeta.Namespace,
Name: pod.ObjectMeta.Name, Name: pod.ObjectMeta.Name,
UID: pod.ObjectMeta.UID, UID: pod.ObjectMeta.UID,
ResourceVersion: pod.ObjectMeta.ResourceVersion,
}, },
} }

View File

@ -262,11 +262,10 @@ func TestPodToEndpoint(t *testing.T) {
Conditions: discovery.EndpointConditions{Ready: utilpointer.BoolPtr(true)}, Conditions: discovery.EndpointConditions{Ready: utilpointer.BoolPtr(true)},
NodeName: utilpointer.StringPtr("node-1"), NodeName: utilpointer.StringPtr("node-1"),
TargetRef: &v1.ObjectReference{ TargetRef: &v1.ObjectReference{
Kind: "Pod", Kind: "Pod",
Namespace: ns, Namespace: ns,
Name: readyPod.Name, Name: readyPod.Name,
UID: readyPod.UID, UID: readyPod.UID,
ResourceVersion: readyPod.ResourceVersion,
}, },
}, },
}, },
@ -279,11 +278,10 @@ func TestPodToEndpoint(t *testing.T) {
Conditions: discovery.EndpointConditions{Ready: utilpointer.BoolPtr(true)}, Conditions: discovery.EndpointConditions{Ready: utilpointer.BoolPtr(true)},
NodeName: utilpointer.StringPtr("node-1"), NodeName: utilpointer.StringPtr("node-1"),
TargetRef: &v1.ObjectReference{ TargetRef: &v1.ObjectReference{
Kind: "Pod", Kind: "Pod",
Namespace: ns, Namespace: ns,
Name: readyPod.Name, Name: readyPod.Name,
UID: readyPod.UID, UID: readyPod.UID,
ResourceVersion: readyPod.ResourceVersion,
}, },
}, },
}, },
@ -296,11 +294,10 @@ func TestPodToEndpoint(t *testing.T) {
Conditions: discovery.EndpointConditions{Ready: utilpointer.BoolPtr(false)}, Conditions: discovery.EndpointConditions{Ready: utilpointer.BoolPtr(false)},
NodeName: utilpointer.StringPtr("node-1"), NodeName: utilpointer.StringPtr("node-1"),
TargetRef: &v1.ObjectReference{ TargetRef: &v1.ObjectReference{
Kind: "Pod", Kind: "Pod",
Namespace: ns, Namespace: ns,
Name: readyPod.Name, Name: readyPod.Name,
UID: readyPod.UID, UID: readyPod.UID,
ResourceVersion: readyPod.ResourceVersion,
}, },
}, },
}, },
@ -313,11 +310,10 @@ func TestPodToEndpoint(t *testing.T) {
Conditions: discovery.EndpointConditions{Ready: utilpointer.BoolPtr(true)}, Conditions: discovery.EndpointConditions{Ready: utilpointer.BoolPtr(true)},
NodeName: utilpointer.StringPtr("node-1"), NodeName: utilpointer.StringPtr("node-1"),
TargetRef: &v1.ObjectReference{ TargetRef: &v1.ObjectReference{
Kind: "Pod", Kind: "Pod",
Namespace: ns, Namespace: ns,
Name: readyPod.Name, Name: readyPod.Name,
UID: readyPod.UID, UID: readyPod.UID,
ResourceVersion: readyPod.ResourceVersion,
}, },
}, },
}, },
@ -332,11 +328,10 @@ func TestPodToEndpoint(t *testing.T) {
Zone: utilpointer.StringPtr("us-central1-a"), Zone: utilpointer.StringPtr("us-central1-a"),
NodeName: utilpointer.StringPtr("node-1"), NodeName: utilpointer.StringPtr("node-1"),
TargetRef: &v1.ObjectReference{ TargetRef: &v1.ObjectReference{
Kind: "Pod", Kind: "Pod",
Namespace: ns, Namespace: ns,
Name: readyPod.Name, Name: readyPod.Name,
UID: readyPod.UID, UID: readyPod.UID,
ResourceVersion: readyPod.ResourceVersion,
}, },
}, },
}, },
@ -351,11 +346,10 @@ func TestPodToEndpoint(t *testing.T) {
Zone: utilpointer.StringPtr("us-central1-a"), Zone: utilpointer.StringPtr("us-central1-a"),
NodeName: utilpointer.StringPtr("node-1"), NodeName: utilpointer.StringPtr("node-1"),
TargetRef: &v1.ObjectReference{ TargetRef: &v1.ObjectReference{
Kind: "Pod", Kind: "Pod",
Namespace: ns, Namespace: ns,
Name: readyPod.Name, Name: readyPod.Name,
UID: readyPod.UID, UID: readyPod.UID,
ResourceVersion: readyPod.ResourceVersion,
}, },
}, },
}, },
@ -371,11 +365,10 @@ func TestPodToEndpoint(t *testing.T) {
Zone: utilpointer.StringPtr("us-central1-a"), Zone: utilpointer.StringPtr("us-central1-a"),
NodeName: utilpointer.StringPtr("node-1"), NodeName: utilpointer.StringPtr("node-1"),
TargetRef: &v1.ObjectReference{ TargetRef: &v1.ObjectReference{
Kind: "Pod", Kind: "Pod",
Namespace: ns, Namespace: ns,
Name: readyPodHostname.Name, Name: readyPodHostname.Name,
UID: readyPodHostname.UID, UID: readyPodHostname.UID,
ResourceVersion: readyPodHostname.ResourceVersion,
}, },
}, },
}, },
@ -392,11 +385,10 @@ func TestPodToEndpoint(t *testing.T) {
}, },
NodeName: utilpointer.StringPtr("node-1"), NodeName: utilpointer.StringPtr("node-1"),
TargetRef: &v1.ObjectReference{ TargetRef: &v1.ObjectReference{
Kind: "Pod", Kind: "Pod",
Namespace: ns, Namespace: ns,
Name: readyPod.Name, Name: readyPod.Name,
UID: readyPod.UID, UID: readyPod.UID,
ResourceVersion: readyPod.ResourceVersion,
}, },
}, },
terminatingGateEnabled: true, terminatingGateEnabled: true,
@ -412,11 +404,10 @@ func TestPodToEndpoint(t *testing.T) {
}, },
NodeName: utilpointer.StringPtr("node-1"), NodeName: utilpointer.StringPtr("node-1"),
TargetRef: &v1.ObjectReference{ TargetRef: &v1.ObjectReference{
Kind: "Pod", Kind: "Pod",
Namespace: ns, Namespace: ns,
Name: readyPod.Name, Name: readyPod.Name,
UID: readyPod.UID, UID: readyPod.UID,
ResourceVersion: readyPod.ResourceVersion,
}, },
}, },
terminatingGateEnabled: false, terminatingGateEnabled: false,
@ -434,11 +425,10 @@ func TestPodToEndpoint(t *testing.T) {
}, },
NodeName: utilpointer.StringPtr("node-1"), NodeName: utilpointer.StringPtr("node-1"),
TargetRef: &v1.ObjectReference{ TargetRef: &v1.ObjectReference{
Kind: "Pod", Kind: "Pod",
Namespace: ns, Namespace: ns,
Name: readyPod.Name, Name: readyPod.Name,
UID: readyPod.UID, UID: readyPod.UID,
ResourceVersion: readyPod.ResourceVersion,
}, },
}, },
terminatingGateEnabled: true, terminatingGateEnabled: true,
@ -454,11 +444,10 @@ func TestPodToEndpoint(t *testing.T) {
}, },
NodeName: utilpointer.StringPtr("node-1"), NodeName: utilpointer.StringPtr("node-1"),
TargetRef: &v1.ObjectReference{ TargetRef: &v1.ObjectReference{
Kind: "Pod", Kind: "Pod",
Namespace: ns, Namespace: ns,
Name: readyPod.Name, Name: readyPod.Name,
UID: readyPod.UID, UID: readyPod.UID,
ResourceVersion: readyPod.ResourceVersion,
}, },
}, },
terminatingGateEnabled: false, terminatingGateEnabled: false,
@ -476,11 +465,10 @@ func TestPodToEndpoint(t *testing.T) {
}, },
NodeName: utilpointer.StringPtr("node-1"), NodeName: utilpointer.StringPtr("node-1"),
TargetRef: &v1.ObjectReference{ TargetRef: &v1.ObjectReference{
Kind: "Pod", Kind: "Pod",
Namespace: ns, Namespace: ns,
Name: readyPod.Name, Name: readyPod.Name,
UID: readyPod.UID, UID: readyPod.UID,
ResourceVersion: readyPod.ResourceVersion,
}, },
}, },
terminatingGateEnabled: true, terminatingGateEnabled: true,
@ -937,6 +925,7 @@ func newPod(n int, namespace string, ready bool, nPorts int, terminating bool) *
Name: fmt.Sprintf("pod%d", n), Name: fmt.Sprintf("pod%d", n),
Labels: map[string]string{"foo": "bar"}, Labels: map[string]string{"foo": "bar"},
DeletionTimestamp: deletionTimestamp, DeletionTimestamp: deletionTimestamp,
ResourceVersion: fmt.Sprint(n),
}, },
Spec: v1.PodSpec{ Spec: v1.PodSpec{
Containers: []v1.Container{{ Containers: []v1.Container{{

View File

@ -851,6 +851,28 @@ func TestEndpointsEqualBeyondHash(t *testing.T) {
}, },
expected: true, expected: true,
}, },
{
name: "Pod resourceVersion removed",
ep1: &discovery.Endpoint{
Conditions: discovery.EndpointConditions{
Ready: utilpointer.BoolPtr(true),
},
Addresses: []string{"10.0.0.1"},
TargetRef: &v1.ObjectReference{Kind: "Pod", Namespace: "default", Name: "pod0", ResourceVersion: "1"},
Zone: utilpointer.StringPtr("zone-1"),
NodeName: utilpointer.StringPtr("node-1"),
},
ep2: &discovery.Endpoint{
Conditions: discovery.EndpointConditions{
Ready: utilpointer.BoolPtr(true),
},
Addresses: []string{"10.0.0.1"},
TargetRef: &v1.ObjectReference{Kind: "Pod", Namespace: "default", Name: "pod0", ResourceVersion: ""},
Zone: utilpointer.StringPtr("zone-1"),
NodeName: utilpointer.StringPtr("node-1"),
},
expected: true,
},
} }
for _, tt := range tests { for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) { t.Run(tt.name, func(t *testing.T) {
@ -933,6 +955,14 @@ func TestEndpointSubsetsEqualIgnoreResourceVersion(t *testing.T) {
})}, })},
expected: true, expected: true,
}, },
{
name: "Pod ResourceVersion removed",
subsets1: []v1.EndpointSubset{*es1, *es2},
subsets2: []v1.EndpointSubset{*es1, *copyAndMutateEndpointSubset(es2, func(es *v1.EndpointSubset) {
es.Addresses[0].TargetRef.ResourceVersion = ""
})},
expected: true,
},
{ {
name: "Ports changed", name: "Ports changed",
subsets1: []v1.EndpointSubset{*es1, *es2}, subsets1: []v1.EndpointSubset{*es1, *es2},