Sync Hostname and IP address from service to ingress status (#7464)

* Change statusSync.runningAddresses() return type

Previously, this method returning a string slice containing the resolved
IP addresses / FQDNs to sync onto the Ingress. It was then converted
just before use into a slice of LoadBalancerIngresses.

This commit changes this logic so that this method generates
LoadBalancerIngress objects directly, and returns these. This has two
main benefits:
- Future work in syncing _both_ hostname and IP, or any other fields
  that may be used in future (eg Ports), is now supported.
- There is less need to rely on net.ParseIP() to determine if a value is
  an IP address or Hostname, as this can be correctly assigned at
  generation time based on where each value came from.

* Sync both IP and Hostname to Ingress Status

Previously, if the IP address was set on a PublishService's
LoadBalancerIngress entries, only that would be synced. Hostname was
only synced as a fallback when the IP address was missing.

Now, both fields are checked independantly and both are synced if
present.
This commit is contained in:
Emily L Shepherd 2021-09-07 18:41:16 +01:00 committed by GitHub
parent 33061b8cdf
commit 3c86f838d4
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 101 additions and 68 deletions

View file

@ -382,7 +382,7 @@ func TestKeyfunc(t *testing.T) {
func TestRunningAddressesWithPublishService(t *testing.T) {
testCases := map[string]struct {
fakeClient *testclient.Clientset
expected []string
expected []apiv1.LoadBalancerIngress
errExpected bool
}{
"service type ClusterIP": {
@ -416,7 +416,9 @@ func TestRunningAddressesWithPublishService(t *testing.T) {
},
},
),
[]string{"1.1.1.1"},
[]apiv1.LoadBalancerIngress{
{IP: "1.1.1.1"},
},
false,
},
"service type NodePort": {
@ -435,7 +437,9 @@ func TestRunningAddressesWithPublishService(t *testing.T) {
},
},
),
[]string{"1.1.1.1"},
[]apiv1.LoadBalancerIngress{
{IP: "1.1.1.1"},
},
false,
},
"service type ExternalName": {
@ -454,7 +458,9 @@ func TestRunningAddressesWithPublishService(t *testing.T) {
},
},
),
[]string{"foo.bar"},
[]apiv1.LoadBalancerIngress{
{Hostname: "foo.bar"},
},
false,
},
"service type LoadBalancer": {
@ -478,6 +484,10 @@ func TestRunningAddressesWithPublishService(t *testing.T) {
IP: "",
Hostname: "foo",
},
{
IP: "10.0.0.2",
Hostname: "10-0-0-2.cloudprovider.example.net",
},
},
},
},
@ -485,7 +495,14 @@ func TestRunningAddressesWithPublishService(t *testing.T) {
},
},
),
[]string{"10.0.0.1", "foo"},
[]apiv1.LoadBalancerIngress{
{IP: "10.0.0.1"},
{Hostname: "foo"},
{
IP: "10.0.0.2",
Hostname: "10-0-0-2.cloudprovider.example.net",
},
},
false,
},
"service type LoadBalancer with same externalIP and ingress IP": {
@ -513,7 +530,9 @@ func TestRunningAddressesWithPublishService(t *testing.T) {
},
},
),
[]string{"10.0.0.1"},
[]apiv1.LoadBalancerIngress{
{IP: "10.0.0.1"},
},
false,
},
"invalid service type": {
@ -549,7 +568,7 @@ func TestRunningAddressesWithPublishService(t *testing.T) {
}
if ra == nil {
t.Fatalf("returned nil but expected valid []string")
t.Fatalf("returned nil but expected valid []apiv1.LoadBalancerIngress")
}
if !reflect.DeepEqual(tc.expected, ra) {
@ -565,15 +584,15 @@ func TestRunningAddressesWithPods(t *testing.T) {
r, _ := fk.runningAddresses()
if r == nil {
t.Fatalf("returned nil but expected valid []string")
t.Fatalf("returned nil but expected valid []apiv1.LoadBalancerIngress")
}
rl := len(r)
if len(r) != 1 {
t.Fatalf("returned %v but expected %v", rl, 1)
}
rv := r[0]
if rv != "11.0.0.2" {
t.Errorf("returned %v but expected %v", rv, "11.0.0.2")
if rv.IP != "11.0.0.2" {
t.Errorf("returned %v but expected %v", rv, apiv1.LoadBalancerIngress{IP: "11.0.0.2"})
}
}
@ -583,15 +602,15 @@ func TestRunningAddressesWithPublishStatusAddress(t *testing.T) {
ra, _ := fk.runningAddresses()
if ra == nil {
t.Fatalf("returned nil but expected valid []string")
t.Fatalf("returned nil but expected valid []apiv1.LoadBalancerIngress")
}
rl := len(ra)
if len(ra) != 1 {
t.Errorf("returned %v but expected %v", rl, 1)
}
rv := ra[0]
if rv != "127.0.0.1" {
t.Errorf("returned %v but expected %v", rv, "127.0.0.1")
if rv.IP != "127.0.0.1" {
t.Errorf("returned %v but expected %v", rv, apiv1.LoadBalancerIngress{IP: "127.0.0.1"})
}
}
@ -601,7 +620,7 @@ func TestRunningAddressesWithPublishStatusAddresses(t *testing.T) {
ra, _ := fk.runningAddresses()
if ra == nil {
t.Fatalf("returned nil but expected valid []string")
t.Fatalf("returned nil but expected valid []apiv1.LoadBalancerIngress")
}
rl := len(ra)
if len(ra) != 2 {
@ -609,11 +628,11 @@ func TestRunningAddressesWithPublishStatusAddresses(t *testing.T) {
}
rv := ra[0]
rv2 := ra[1]
if rv != "127.0.0.1" {
t.Errorf("returned %v but expected %v", rv, "127.0.0.1")
if rv.IP != "127.0.0.1" {
t.Errorf("returned %v but expected %v", rv, apiv1.LoadBalancerIngress{IP: "127.0.0.1"})
}
if rv2 != "1.1.1.1" {
t.Errorf("returned %v but expected %v", rv2, "1.1.1.1")
if rv2.IP != "1.1.1.1" {
t.Errorf("returned %v but expected %v", rv2, apiv1.LoadBalancerIngress{IP: "1.1.1.1"})
}
}
@ -623,7 +642,7 @@ func TestRunningAddressesWithPublishStatusAddressesAndSpaces(t *testing.T) {
ra, _ := fk.runningAddresses()
if ra == nil {
t.Fatalf("returned nil but expected valid []string")
t.Fatalf("returned nil but expected valid []apiv1.LoadBalancerIngresst")
}
rl := len(ra)
if len(ra) != 2 {
@ -631,22 +650,22 @@ func TestRunningAddressesWithPublishStatusAddressesAndSpaces(t *testing.T) {
}
rv := ra[0]
rv2 := ra[1]
if rv != "127.0.0.1" {
t.Errorf("returned %v but expected %v", rv, "127.0.0.1")
if rv.IP != "127.0.0.1" {
t.Errorf("returned %v but expected %v", rv, apiv1.LoadBalancerIngress{IP: "127.0.0.1"})
}
if rv2 != "1.1.1.1" {
t.Errorf("returned %v but expected %v", rv2, "1.1.1.1")
if rv2.IP != "1.1.1.1" {
t.Errorf("returned %v but expected %v", rv2, apiv1.LoadBalancerIngress{IP: "1.1.1.1"})
}
}
func TestSliceToStatus(t *testing.T) {
fkEndpoints := []string{
"10.0.0.1",
"2001:db8::68",
"opensource-k8s-ingress",
func TestStandardizeLoadBalancerIngresses(t *testing.T) {
fkEndpoints := []apiv1.LoadBalancerIngress{
{IP: "2001:db8::68"},
{IP: "10.0.0.1"},
{Hostname: "opensource-k8s-ingress"},
}
r := sliceToStatus(fkEndpoints)
r := standardizeLoadBalancerIngresses(fkEndpoints)
if r == nil {
t.Fatalf("returned nil but expected a valid []apiv1.LoadBalancerIngress")