2019-03-14 14:56:06 +00:00
|
|
|
package ingress
|
2019-02-21 22:08:05 +00:00
|
|
|
|
|
|
|
import (
|
2021-04-29 14:20:03 +00:00
|
|
|
"context"
|
2024-02-07 16:14:07 +00:00
|
|
|
"errors"
|
2019-02-21 22:08:05 +00:00
|
|
|
"testing"
|
2020-11-19 13:32:03 +00:00
|
|
|
"time"
|
2019-02-21 22:08:05 +00:00
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
2020-11-19 13:32:03 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
2020-09-15 11:48:32 +00:00
|
|
|
corev1 "k8s.io/api/core/v1"
|
2023-04-03 08:06:06 +00:00
|
|
|
netv1 "k8s.io/api/networking/v1"
|
|
|
|
kerror "k8s.io/apimachinery/pkg/api/errors"
|
2020-11-19 13:32:03 +00:00
|
|
|
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
2023-04-03 08:06:06 +00:00
|
|
|
kschema "k8s.io/apimachinery/pkg/runtime/schema"
|
|
|
|
kversion "k8s.io/apimachinery/pkg/version"
|
|
|
|
discoveryfake "k8s.io/client-go/discovery/fake"
|
2020-11-19 13:32:03 +00:00
|
|
|
kubefake "k8s.io/client-go/kubernetes/fake"
|
2019-02-21 22:08:05 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestTranslateNotFoundError(t *testing.T) {
|
|
|
|
testCases := []struct {
|
|
|
|
desc string
|
|
|
|
err error
|
|
|
|
expectedExists bool
|
|
|
|
expectedError error
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
desc: "kubernetes not found error",
|
2023-04-03 08:06:06 +00:00
|
|
|
err: kerror.NewNotFound(kschema.GroupResource{}, "foo"),
|
2019-02-21 22:08:05 +00:00
|
|
|
expectedExists: false,
|
|
|
|
expectedError: nil,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "nil error",
|
|
|
|
err: nil,
|
|
|
|
expectedExists: true,
|
|
|
|
expectedError: nil,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "not a kubernetes not found error",
|
2024-02-07 16:14:07 +00:00
|
|
|
err: errors.New("bar error"),
|
2019-02-21 22:08:05 +00:00
|
|
|
expectedExists: false,
|
2024-02-07 16:14:07 +00:00
|
|
|
expectedError: errors.New("bar error"),
|
2019-02-21 22:08:05 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, test := range testCases {
|
|
|
|
test := test
|
|
|
|
t.Run(test.desc, func(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
exists, err := translateNotFoundError(test.err)
|
|
|
|
assert.Equal(t, test.expectedExists, exists)
|
|
|
|
assert.Equal(t, test.expectedError, err)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2020-09-15 11:48:32 +00:00
|
|
|
|
|
|
|
func TestIsLoadBalancerIngressEquals(t *testing.T) {
|
|
|
|
testCases := []struct {
|
|
|
|
desc string
|
2023-04-03 08:06:06 +00:00
|
|
|
aSlice []netv1.IngressLoadBalancerIngress
|
|
|
|
bSlice []netv1.IngressLoadBalancerIngress
|
2020-09-15 11:48:32 +00:00
|
|
|
expectedEqual bool
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
desc: "both slices are empty",
|
|
|
|
expectedEqual: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "not the same length",
|
2023-04-03 08:06:06 +00:00
|
|
|
bSlice: []netv1.IngressLoadBalancerIngress{
|
2020-09-15 11:48:32 +00:00
|
|
|
{IP: "192.168.1.1", Hostname: "traefik"},
|
|
|
|
},
|
|
|
|
expectedEqual: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "same ordered content",
|
2023-04-03 08:06:06 +00:00
|
|
|
aSlice: []netv1.IngressLoadBalancerIngress{
|
2020-09-15 11:48:32 +00:00
|
|
|
{IP: "192.168.1.1", Hostname: "traefik"},
|
|
|
|
},
|
2023-04-03 08:06:06 +00:00
|
|
|
bSlice: []netv1.IngressLoadBalancerIngress{
|
2020-09-15 11:48:32 +00:00
|
|
|
{IP: "192.168.1.1", Hostname: "traefik"},
|
|
|
|
},
|
|
|
|
expectedEqual: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "same unordered content",
|
2023-04-03 08:06:06 +00:00
|
|
|
aSlice: []netv1.IngressLoadBalancerIngress{
|
2020-09-15 11:48:32 +00:00
|
|
|
{IP: "192.168.1.1", Hostname: "traefik"},
|
|
|
|
{IP: "192.168.1.2", Hostname: "traefik2"},
|
|
|
|
},
|
2023-04-03 08:06:06 +00:00
|
|
|
bSlice: []netv1.IngressLoadBalancerIngress{
|
2020-09-15 11:48:32 +00:00
|
|
|
{IP: "192.168.1.2", Hostname: "traefik2"},
|
|
|
|
{IP: "192.168.1.1", Hostname: "traefik"},
|
|
|
|
},
|
|
|
|
expectedEqual: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "different ordered content",
|
2023-04-03 08:06:06 +00:00
|
|
|
aSlice: []netv1.IngressLoadBalancerIngress{
|
2020-09-15 11:48:32 +00:00
|
|
|
{IP: "192.168.1.1", Hostname: "traefik"},
|
|
|
|
{IP: "192.168.1.2", Hostname: "traefik2"},
|
|
|
|
},
|
2023-04-03 08:06:06 +00:00
|
|
|
bSlice: []netv1.IngressLoadBalancerIngress{
|
2020-09-15 11:48:32 +00:00
|
|
|
{IP: "192.168.1.1", Hostname: "traefik"},
|
|
|
|
{IP: "192.168.1.2", Hostname: "traefik"},
|
|
|
|
},
|
|
|
|
expectedEqual: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "different unordered content",
|
2023-04-03 08:06:06 +00:00
|
|
|
aSlice: []netv1.IngressLoadBalancerIngress{
|
2020-09-15 11:48:32 +00:00
|
|
|
{IP: "192.168.1.1", Hostname: "traefik"},
|
|
|
|
{IP: "192.168.1.2", Hostname: "traefik2"},
|
|
|
|
},
|
2023-04-03 08:06:06 +00:00
|
|
|
bSlice: []netv1.IngressLoadBalancerIngress{
|
2020-09-15 11:48:32 +00:00
|
|
|
{IP: "192.168.1.2", Hostname: "traefik3"},
|
|
|
|
{IP: "192.168.1.1", Hostname: "traefik"},
|
|
|
|
},
|
|
|
|
expectedEqual: false,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, test := range testCases {
|
|
|
|
test := test
|
|
|
|
t.Run(test.desc, func(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
gotEqual := isLoadBalancerIngressEquals(test.aSlice, test.bSlice)
|
|
|
|
assert.Equal(t, test.expectedEqual, gotEqual)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2020-11-20 10:30:07 +00:00
|
|
|
|
2020-11-19 13:32:03 +00:00
|
|
|
func TestClientIgnoresHelmOwnedSecrets(t *testing.T) {
|
|
|
|
secret := &corev1.Secret{
|
|
|
|
ObjectMeta: metav1.ObjectMeta{
|
|
|
|
Namespace: "default",
|
|
|
|
Name: "secret",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
helmSecret := &corev1.Secret{
|
|
|
|
ObjectMeta: metav1.ObjectMeta{
|
|
|
|
Namespace: "default",
|
|
|
|
Name: "helm-secret",
|
|
|
|
Labels: map[string]string{
|
|
|
|
"owner": "helm",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
kubeClient := kubefake.NewSimpleClientset(helmSecret, secret)
|
|
|
|
|
2023-04-03 08:06:06 +00:00
|
|
|
discovery, _ := kubeClient.Discovery().(*discoveryfake.FakeDiscovery)
|
|
|
|
discovery.FakedServerVersion = &kversion.Info{
|
2021-03-15 10:16:04 +00:00
|
|
|
GitVersion: "v1.19",
|
|
|
|
}
|
|
|
|
|
2020-11-19 13:32:03 +00:00
|
|
|
client := newClientImpl(kubeClient)
|
|
|
|
|
|
|
|
stopCh := make(chan struct{})
|
|
|
|
|
|
|
|
eventCh, err := client.WatchAll(nil, stopCh)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
select {
|
|
|
|
case event := <-eventCh:
|
|
|
|
secret, ok := event.(*corev1.Secret)
|
|
|
|
require.True(t, ok)
|
|
|
|
|
|
|
|
assert.NotEqual(t, "helm-secret", secret.Name)
|
|
|
|
case <-time.After(50 * time.Millisecond):
|
|
|
|
assert.Fail(t, "expected to receive event for secret")
|
|
|
|
}
|
|
|
|
|
|
|
|
select {
|
|
|
|
case <-eventCh:
|
|
|
|
assert.Fail(t, "received more than one event")
|
|
|
|
case <-time.After(50 * time.Millisecond):
|
|
|
|
}
|
|
|
|
|
|
|
|
_, found, err := client.GetSecret("default", "secret")
|
|
|
|
require.NoError(t, err)
|
|
|
|
assert.True(t, found)
|
|
|
|
|
|
|
|
_, found, err = client.GetSecret("default", "helm-secret")
|
|
|
|
require.NoError(t, err)
|
|
|
|
assert.False(t, found)
|
|
|
|
}
|
2021-03-15 10:16:04 +00:00
|
|
|
|
2021-04-29 14:20:03 +00:00
|
|
|
func TestClientIgnoresEmptyEndpointUpdates(t *testing.T) {
|
|
|
|
emptyEndpoint := &corev1.Endpoints{
|
|
|
|
ObjectMeta: metav1.ObjectMeta{
|
|
|
|
Name: "empty-endpoint",
|
|
|
|
Namespace: "test",
|
|
|
|
ResourceVersion: "1244",
|
|
|
|
Annotations: map[string]string{
|
|
|
|
"test-annotation": "_",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
filledEndpoint := &corev1.Endpoints{
|
|
|
|
ObjectMeta: metav1.ObjectMeta{
|
|
|
|
Name: "filled-endpoint",
|
|
|
|
Namespace: "test",
|
|
|
|
ResourceVersion: "1234",
|
|
|
|
},
|
|
|
|
Subsets: []corev1.EndpointSubset{{
|
|
|
|
Addresses: []corev1.EndpointAddress{{
|
|
|
|
IP: "10.13.37.1",
|
|
|
|
}},
|
|
|
|
Ports: []corev1.EndpointPort{{
|
|
|
|
Name: "testing",
|
|
|
|
Port: 1337,
|
|
|
|
Protocol: "tcp",
|
|
|
|
}},
|
|
|
|
}},
|
|
|
|
}
|
|
|
|
|
|
|
|
kubeClient := kubefake.NewSimpleClientset(emptyEndpoint, filledEndpoint)
|
|
|
|
|
2023-04-03 08:06:06 +00:00
|
|
|
discovery, _ := kubeClient.Discovery().(*discoveryfake.FakeDiscovery)
|
|
|
|
discovery.FakedServerVersion = &kversion.Info{
|
2021-04-29 14:20:03 +00:00
|
|
|
GitVersion: "v1.19",
|
|
|
|
}
|
|
|
|
|
|
|
|
client := newClientImpl(kubeClient)
|
|
|
|
|
|
|
|
stopCh := make(chan struct{})
|
|
|
|
|
|
|
|
eventCh, err := client.WatchAll(nil, stopCh)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
select {
|
|
|
|
case event := <-eventCh:
|
|
|
|
ep, ok := event.(*corev1.Endpoints)
|
|
|
|
require.True(t, ok)
|
|
|
|
|
|
|
|
assert.True(t, ep.Name == "empty-endpoint" || ep.Name == "filled-endpoint")
|
|
|
|
case <-time.After(50 * time.Millisecond):
|
|
|
|
assert.Fail(t, "expected to receive event for endpoints")
|
|
|
|
}
|
|
|
|
|
|
|
|
emptyEndpoint, err = kubeClient.CoreV1().Endpoints("test").Get(context.TODO(), "empty-endpoint", metav1.GetOptions{})
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
// Update endpoint annotation and resource version (apparently not done by fake client itself)
|
|
|
|
// to show an update that should not trigger an update event on our eventCh.
|
|
|
|
// This reflects the behavior of kubernetes controllers which use endpoint annotations for leader election.
|
|
|
|
emptyEndpoint.Annotations["test-annotation"] = "___"
|
|
|
|
emptyEndpoint.ResourceVersion = "1245"
|
|
|
|
_, err = kubeClient.CoreV1().Endpoints("test").Update(context.TODO(), emptyEndpoint, metav1.UpdateOptions{})
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
select {
|
|
|
|
case event := <-eventCh:
|
|
|
|
ep, ok := event.(*corev1.Endpoints)
|
|
|
|
require.True(t, ok)
|
|
|
|
|
|
|
|
assert.Fail(t, "didn't expect to receive event for empty endpoint update", ep.Name)
|
|
|
|
case <-time.After(50 * time.Millisecond):
|
|
|
|
}
|
|
|
|
|
|
|
|
filledEndpoint, err = kubeClient.CoreV1().Endpoints("test").Get(context.TODO(), "filled-endpoint", metav1.GetOptions{})
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
filledEndpoint.Subsets[0].Addresses[0].IP = "10.13.37.2"
|
|
|
|
filledEndpoint.ResourceVersion = "1235"
|
|
|
|
_, err = kubeClient.CoreV1().Endpoints("test").Update(context.TODO(), filledEndpoint, metav1.UpdateOptions{})
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
select {
|
|
|
|
case event := <-eventCh:
|
|
|
|
ep, ok := event.(*corev1.Endpoints)
|
|
|
|
require.True(t, ok)
|
|
|
|
|
|
|
|
assert.Equal(t, "filled-endpoint", ep.Name)
|
|
|
|
case <-time.After(50 * time.Millisecond):
|
|
|
|
assert.Fail(t, "expected to receive event for filled endpoint")
|
|
|
|
}
|
|
|
|
|
|
|
|
select {
|
|
|
|
case <-eventCh:
|
|
|
|
assert.Fail(t, "received more than one event")
|
|
|
|
case <-time.After(50 * time.Millisecond):
|
|
|
|
}
|
|
|
|
}
|