fix: use informer for istio gateways

Signed-off-by: Lukas Wöhrl <lukas.woehrl@plentymarkets.com>
This commit is contained in:
Lukas Wöhrl 2024-06-03 11:42:53 +02:00
parent f46676fdbb
commit 25eac69705
2 changed files with 27 additions and 33 deletions

View File

@ -55,6 +55,7 @@ type virtualServiceSource struct {
ignoreHostnameAnnotation bool
serviceInformer coreinformers.ServiceInformer
virtualserviceInformer networkingv1alpha3informer.VirtualServiceInformer
gatewayInformer networkingv1alpha3informer.GatewayInformer
}
// NewIstioVirtualServiceSource creates a new virtualServiceSource with the given config.
@ -79,6 +80,7 @@ func NewIstioVirtualServiceSource(
serviceInformer := informerFactory.Core().V1().Services()
istioInformerFactory := istioinformers.NewSharedInformerFactoryWithOptions(istioClient, 0, istioinformers.WithNamespace(namespace))
virtualServiceInformer := istioInformerFactory.Networking().V1alpha3().VirtualServices()
gatewayInformer := istioInformerFactory.Networking().V1alpha3().Gateways()
// Add default resource event handlers to properly initialize informer.
serviceInformer.Informer().AddEventHandler(
@ -97,6 +99,14 @@ func NewIstioVirtualServiceSource(
},
)
gatewayInformer.Informer().AddEventHandler(
cache.ResourceEventHandlerFuncs{
AddFunc: func(obj interface{}) {
log.Debug("gateway added")
},
},
)
informerFactory.Start(ctx.Done())
istioInformerFactory.Start(ctx.Done())
@ -118,6 +128,7 @@ func NewIstioVirtualServiceSource(
ignoreHostnameAnnotation: ignoreHostnameAnnotation,
serviceInformer: serviceInformer,
virtualserviceInformer: virtualServiceInformer,
gatewayInformer: gatewayInformer,
}, nil
}
@ -186,7 +197,7 @@ func (sc *virtualServiceSource) AddEventHandler(ctx context.Context, handler fun
sc.virtualserviceInformer.Informer().AddEventHandler(eventHandlerFunc(handler))
}
func (sc *virtualServiceSource) getGateway(ctx context.Context, gatewayStr string, virtualService *networkingv1alpha3.VirtualService) (*networkingv1alpha3.Gateway, error) {
func (sc *virtualServiceSource) getGateway(_ context.Context, gatewayStr string, virtualService *networkingv1alpha3.VirtualService) (*networkingv1alpha3.Gateway, error) {
if gatewayStr == "" || gatewayStr == IstioMeshGateway {
// This refers to "all sidecars in the mesh"; ignore.
return nil, nil
@ -201,7 +212,7 @@ func (sc *virtualServiceSource) getGateway(ctx context.Context, gatewayStr strin
namespace = virtualService.Namespace
}
gateway, err := sc.istioClient.NetworkingV1alpha3().Gateways(namespace).Get(ctx, name, metav1.GetOptions{})
gateway, err := sc.gatewayInformer.Lister().Gateways(namespace).Get(name)
if errors.IsNotFound(err) {
log.Warnf("VirtualService (%s/%s) references non-existent gateway: %s ", virtualService.Namespace, virtualService.Name, gatewayStr)
return nil, nil

View File

@ -29,13 +29,10 @@ import (
istionetworking "istio.io/api/networking/v1alpha3"
networkingv1alpha3 "istio.io/client-go/pkg/apis/networking/v1alpha3"
istiofake "istio.io/client-go/pkg/clientset/versioned/fake"
fakenetworking3 "istio.io/client-go/pkg/clientset/versioned/typed/networking/v1alpha3/fake"
v1 "k8s.io/api/core/v1"
networkv1 "k8s.io/api/networking/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/client-go/kubernetes/fake"
k8sclienttesting "k8s.io/client-go/testing"
"sigs.k8s.io/external-dns/endpoint"
)
@ -700,6 +697,20 @@ func testEndpointsFromVirtualServiceConfig(t *testing.T) {
t.Run(ti.title, func(t *testing.T) {
t.Parallel()
for i := range ti.ingresses {
if ti.ingresses[i].namespace == "" {
ti.ingresses[i].namespace = "test"
}
}
if ti.gwconfig.namespace == "" {
ti.gwconfig.namespace = "test"
}
if ti.vsconfig.namespace == "" {
ti.vsconfig.namespace = "test"
}
if source, err := newTestVirtualServiceSource(ti.lbServices, ti.ingresses, []fakeGatewayConfig{ti.gwconfig}); err != nil {
require.NoError(t, err)
} else if endpoints, err := source.endpointsFromVirtualService(context.Background(), ti.vsconfig.Config()); err != nil {
@ -2182,34 +2193,6 @@ func TestVirtualServiceSourceGetGateway(t *testing.T) {
Spec: istionetworking.Gateway{},
Status: v1alpha1.IstioStatus{},
}, expectedErrStr: ""},
{name: "ErrorGettingGateway", fields: fields{
virtualServiceSource: func() *virtualServiceSource {
istioFake := istiofake.NewSimpleClientset()
istioFake.NetworkingV1alpha3().(*fakenetworking3.FakeNetworkingV1alpha3).PrependReactor("get", "gateways", func(action k8sclienttesting.Action) (handled bool, ret runtime.Object, err error) {
return true, &networkingv1alpha3.Gateway{}, fmt.Errorf("error getting gateway")
})
vs, _ := NewIstioVirtualServiceSource(
context.TODO(),
fake.NewSimpleClientset(),
istioFake,
"",
"",
"{{.Name}}",
false,
false,
)
return vs.(*virtualServiceSource)
}(),
}, args: args{
ctx: context.TODO(),
gatewayStr: "foo/bar",
virtualService: &networkingv1alpha3.VirtualService{
TypeMeta: metav1.TypeMeta{},
ObjectMeta: metav1.ObjectMeta{Name: "gateway", Namespace: "error"},
Spec: istionetworking.VirtualService{},
Status: v1alpha1.IstioStatus{},
},
}, want: nil, expectedErrStr: "error getting gateway"},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {