Skip to content

Commit 25eac69

Browse files
committed
fix: use informer for istio gateways
Signed-off-by: Lukas Wöhrl <[email protected]>
1 parent f46676f commit 25eac69

File tree

2 files changed

+27
-33
lines changed

2 files changed

+27
-33
lines changed

source/istio_virtualservice.go

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ type virtualServiceSource struct {
5555
ignoreHostnameAnnotation bool
5656
serviceInformer coreinformers.ServiceInformer
5757
virtualserviceInformer networkingv1alpha3informer.VirtualServiceInformer
58+
gatewayInformer networkingv1alpha3informer.GatewayInformer
5859
}
5960

6061
// NewIstioVirtualServiceSource creates a new virtualServiceSource with the given config.
@@ -79,6 +80,7 @@ func NewIstioVirtualServiceSource(
7980
serviceInformer := informerFactory.Core().V1().Services()
8081
istioInformerFactory := istioinformers.NewSharedInformerFactoryWithOptions(istioClient, 0, istioinformers.WithNamespace(namespace))
8182
virtualServiceInformer := istioInformerFactory.Networking().V1alpha3().VirtualServices()
83+
gatewayInformer := istioInformerFactory.Networking().V1alpha3().Gateways()
8284

8385
// Add default resource event handlers to properly initialize informer.
8486
serviceInformer.Informer().AddEventHandler(
@@ -97,6 +99,14 @@ func NewIstioVirtualServiceSource(
9799
},
98100
)
99101

102+
gatewayInformer.Informer().AddEventHandler(
103+
cache.ResourceEventHandlerFuncs{
104+
AddFunc: func(obj interface{}) {
105+
log.Debug("gateway added")
106+
},
107+
},
108+
)
109+
100110
informerFactory.Start(ctx.Done())
101111
istioInformerFactory.Start(ctx.Done())
102112

@@ -118,6 +128,7 @@ func NewIstioVirtualServiceSource(
118128
ignoreHostnameAnnotation: ignoreHostnameAnnotation,
119129
serviceInformer: serviceInformer,
120130
virtualserviceInformer: virtualServiceInformer,
131+
gatewayInformer: gatewayInformer,
121132
}, nil
122133
}
123134

@@ -186,7 +197,7 @@ func (sc *virtualServiceSource) AddEventHandler(ctx context.Context, handler fun
186197
sc.virtualserviceInformer.Informer().AddEventHandler(eventHandlerFunc(handler))
187198
}
188199

189-
func (sc *virtualServiceSource) getGateway(ctx context.Context, gatewayStr string, virtualService *networkingv1alpha3.VirtualService) (*networkingv1alpha3.Gateway, error) {
200+
func (sc *virtualServiceSource) getGateway(_ context.Context, gatewayStr string, virtualService *networkingv1alpha3.VirtualService) (*networkingv1alpha3.Gateway, error) {
190201
if gatewayStr == "" || gatewayStr == IstioMeshGateway {
191202
// This refers to "all sidecars in the mesh"; ignore.
192203
return nil, nil
@@ -201,7 +212,7 @@ func (sc *virtualServiceSource) getGateway(ctx context.Context, gatewayStr strin
201212
namespace = virtualService.Namespace
202213
}
203214

204-
gateway, err := sc.istioClient.NetworkingV1alpha3().Gateways(namespace).Get(ctx, name, metav1.GetOptions{})
215+
gateway, err := sc.gatewayInformer.Lister().Gateways(namespace).Get(name)
205216
if errors.IsNotFound(err) {
206217
log.Warnf("VirtualService (%s/%s) references non-existent gateway: %s ", virtualService.Namespace, virtualService.Name, gatewayStr)
207218
return nil, nil

source/istio_virtualservice_test.go

Lines changed: 14 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,10 @@ import (
2929
istionetworking "istio.io/api/networking/v1alpha3"
3030
networkingv1alpha3 "istio.io/client-go/pkg/apis/networking/v1alpha3"
3131
istiofake "istio.io/client-go/pkg/clientset/versioned/fake"
32-
fakenetworking3 "istio.io/client-go/pkg/clientset/versioned/typed/networking/v1alpha3/fake"
3332
v1 "k8s.io/api/core/v1"
3433
networkv1 "k8s.io/api/networking/v1"
3534
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
36-
"k8s.io/apimachinery/pkg/runtime"
3735
"k8s.io/client-go/kubernetes/fake"
38-
k8sclienttesting "k8s.io/client-go/testing"
3936

4037
"sigs.k8s.io/external-dns/endpoint"
4138
)
@@ -700,6 +697,20 @@ func testEndpointsFromVirtualServiceConfig(t *testing.T) {
700697
t.Run(ti.title, func(t *testing.T) {
701698
t.Parallel()
702699

700+
for i := range ti.ingresses {
701+
if ti.ingresses[i].namespace == "" {
702+
ti.ingresses[i].namespace = "test"
703+
}
704+
}
705+
706+
if ti.gwconfig.namespace == "" {
707+
ti.gwconfig.namespace = "test"
708+
}
709+
710+
if ti.vsconfig.namespace == "" {
711+
ti.vsconfig.namespace = "test"
712+
}
713+
703714
if source, err := newTestVirtualServiceSource(ti.lbServices, ti.ingresses, []fakeGatewayConfig{ti.gwconfig}); err != nil {
704715
require.NoError(t, err)
705716
} else if endpoints, err := source.endpointsFromVirtualService(context.Background(), ti.vsconfig.Config()); err != nil {
@@ -2182,34 +2193,6 @@ func TestVirtualServiceSourceGetGateway(t *testing.T) {
21822193
Spec: istionetworking.Gateway{},
21832194
Status: v1alpha1.IstioStatus{},
21842195
}, expectedErrStr: ""},
2185-
{name: "ErrorGettingGateway", fields: fields{
2186-
virtualServiceSource: func() *virtualServiceSource {
2187-
istioFake := istiofake.NewSimpleClientset()
2188-
istioFake.NetworkingV1alpha3().(*fakenetworking3.FakeNetworkingV1alpha3).PrependReactor("get", "gateways", func(action k8sclienttesting.Action) (handled bool, ret runtime.Object, err error) {
2189-
return true, &networkingv1alpha3.Gateway{}, fmt.Errorf("error getting gateway")
2190-
})
2191-
vs, _ := NewIstioVirtualServiceSource(
2192-
context.TODO(),
2193-
fake.NewSimpleClientset(),
2194-
istioFake,
2195-
"",
2196-
"",
2197-
"{{.Name}}",
2198-
false,
2199-
false,
2200-
)
2201-
return vs.(*virtualServiceSource)
2202-
}(),
2203-
}, args: args{
2204-
ctx: context.TODO(),
2205-
gatewayStr: "foo/bar",
2206-
virtualService: &networkingv1alpha3.VirtualService{
2207-
TypeMeta: metav1.TypeMeta{},
2208-
ObjectMeta: metav1.ObjectMeta{Name: "gateway", Namespace: "error"},
2209-
Spec: istionetworking.VirtualService{},
2210-
Status: v1alpha1.IstioStatus{},
2211-
},
2212-
}, want: nil, expectedErrStr: "error getting gateway"},
22132196
}
22142197
for _, tt := range tests {
22152198
t.Run(tt.name, func(t *testing.T) {

0 commit comments

Comments
 (0)