Enable reconcile on endpoint events

This commit is contained in:
Dan Markhasin 2025-02-02 23:42:50 +02:00
parent 8eb8ea3a1b
commit 87464189b3
5 changed files with 21 additions and 3 deletions

View File

@ -127,6 +127,7 @@ func main() {
IgnoreNonHostNetworkPods: cfg.IgnoreNonHostNetworkPods,
IgnoreIngressTLSSpec: cfg.IgnoreIngressTLSSpec,
IgnoreIngressRulesSpec: cfg.IgnoreIngressRulesSpec,
ListenEndpointEvents: cfg.ListenEndpointEvents,
GatewayNamespace: cfg.GatewayNamespace,
GatewayLabelFilter: cfg.GatewayLabelFilter,
Compatibility: cfg.Compatibility,

View File

@ -60,6 +60,7 @@ type Config struct {
IgnoreNonHostNetworkPods bool
IgnoreIngressTLSSpec bool
IgnoreIngressRulesSpec bool
ListenEndpointEvents bool
GatewayNamespace string
GatewayLabelFilter string
Compatibility string
@ -427,6 +428,7 @@ func App(cfg *Config) *kingpin.Application {
app.Flag("kubeconfig", "Retrieve target cluster configuration from a Kubernetes configuration file (default: auto-detect)").Default(defaultConfig.KubeConfig).StringVar(&cfg.KubeConfig)
app.Flag("request-timeout", "Request timeout when calling Kubernetes APIs. 0s means no timeout").Default(defaultConfig.RequestTimeout.String()).DurationVar(&cfg.RequestTimeout)
app.Flag("resolve-service-load-balancer-hostname", "Resolve the hostname of LoadBalancer-type Service object to IP addresses in order to create DNS A/AAAA records instead of CNAMEs").BoolVar(&cfg.ResolveServiceLoadBalancerHostname)
app.Flag("listen-endpoint-events", "Trigger a reconcile on changes to Endpoints, for Service source (default: false)").BoolVar(&cfg.ListenEndpointEvents)
// Flags related to cloud foundry
app.Flag("cf-api-endpoint", "The fully-qualified domain name of the cloud foundry instance you are targeting").Default(defaultConfig.CFAPIEndpoint).StringVar(&cfg.CFAPIEndpoint)

View File

@ -55,6 +55,7 @@ type serviceSource struct {
publishHostIP bool
alwaysPublishNotReadyAddresses bool
resolveLoadBalancerHostname bool
listenEndpointEvents bool
serviceInformer coreinformers.ServiceInformer
endpointsInformer coreinformers.EndpointsInformer
podInformer coreinformers.PodInformer
@ -64,7 +65,7 @@ type serviceSource struct {
}
// NewServiceSource creates a new serviceSource with the given config.
func NewServiceSource(ctx context.Context, kubeClient kubernetes.Interface, namespace, annotationFilter string, fqdnTemplate string, combineFqdnAnnotation bool, compatibility string, publishInternal bool, publishHostIP bool, alwaysPublishNotReadyAddresses bool, serviceTypeFilter []string, ignoreHostnameAnnotation bool, labelSelector labels.Selector, resolveLoadBalancerHostname bool) (Source, error) {
func NewServiceSource(ctx context.Context, kubeClient kubernetes.Interface, namespace, annotationFilter, fqdnTemplate string, combineFqdnAnnotation bool, compatibility string, publishInternal, publishHostIP, alwaysPublishNotReadyAddresses bool, serviceTypeFilter []string, ignoreHostnameAnnotation bool, labelSelector labels.Selector, resolveLoadBalancerHostname, listenEndpointEvents bool) (Source, error) {
tmpl, err := parseTemplate(fqdnTemplate)
if err != nil {
return nil, err
@ -718,10 +719,13 @@ func (sc *serviceSource) extractNodePortEndpoints(svc *v1.Service, hostname stri
return endpoints
}
func (sc *serviceSource) AddEventHandler(ctx context.Context, handler func()) {
func (sc *serviceSource) AddEventHandler(_ context.Context, handler func()) {
log.Debug("Adding event handler for service")
// Right now there is no way to remove event handler from informer, see:
// https://github.com/kubernetes/kubernetes/issues/79610
sc.serviceInformer.Informer().AddEventHandler(eventHandlerFunc(handler))
if sc.listenEndpointEvents {
sc.endpointsInformer.Informer().AddEventHandler(eventHandlerFunc(handler))
}
}

View File

@ -80,6 +80,7 @@ func (suite *ServiceSuite) SetupTest() {
false,
labels.Everything(),
false,
false,
)
suite.NoError(err, "should initialize service source")
}
@ -161,6 +162,7 @@ func testServiceSourceNewServiceSource(t *testing.T) {
false,
labels.Everything(),
false,
false,
)
if ti.expectError {
@ -1132,6 +1134,7 @@ func testServiceSourceEndpoints(t *testing.T) {
tc.ignoreHostnameAnnotation,
sourceLabel,
tc.resolveLoadBalancerHostname,
false,
)
require.NoError(t, err)
@ -1346,6 +1349,7 @@ func testMultipleServicesEndpoints(t *testing.T) {
tc.ignoreHostnameAnnotation,
labels.Everything(),
false,
false,
)
require.NoError(t, err)
@ -1649,6 +1653,7 @@ func TestClusterIpServices(t *testing.T) {
tc.ignoreHostnameAnnotation,
labelSelector,
false,
false,
)
require.NoError(t, err)
@ -2367,6 +2372,7 @@ func TestServiceSourceNodePortServices(t *testing.T) {
tc.ignoreHostnameAnnotation,
labels.Everything(),
false,
false,
)
require.NoError(t, err)
@ -3095,6 +3101,7 @@ func TestHeadlessServices(t *testing.T) {
tc.ignoreHostnameAnnotation,
labels.Everything(),
false,
false,
)
require.NoError(t, err)
@ -3554,6 +3561,7 @@ func TestHeadlessServicesHostIP(t *testing.T) {
tc.ignoreHostnameAnnotation,
labels.Everything(),
false,
false,
)
require.NoError(t, err)
@ -3732,6 +3740,7 @@ func TestExternalServices(t *testing.T) {
tc.ignoreHostnameAnnotation,
labels.Everything(),
false,
false,
)
require.NoError(t, err)
@ -3787,6 +3796,7 @@ func BenchmarkServiceEndpoints(b *testing.B) {
false,
labels.Everything(),
false,
false,
)
require.NoError(b, err)

View File

@ -53,6 +53,7 @@ type Config struct {
IgnoreNonHostNetworkPods bool
IgnoreIngressTLSSpec bool
IgnoreIngressRulesSpec bool
ListenEndpointEvents bool
GatewayNamespace string
GatewayLabelFilter string
Compatibility string
@ -220,7 +221,7 @@ func BuildWithConfig(ctx context.Context, source string, p ClientGenerator, cfg
if err != nil {
return nil, err
}
return NewServiceSource(ctx, client, cfg.Namespace, cfg.AnnotationFilter, cfg.FQDNTemplate, cfg.CombineFQDNAndAnnotation, cfg.Compatibility, cfg.PublishInternal, cfg.PublishHostIP, cfg.AlwaysPublishNotReadyAddresses, cfg.ServiceTypeFilter, cfg.IgnoreHostnameAnnotation, cfg.LabelFilter, cfg.ResolveLoadBalancerHostname)
return NewServiceSource(ctx, client, cfg.Namespace, cfg.AnnotationFilter, cfg.FQDNTemplate, cfg.CombineFQDNAndAnnotation, cfg.Compatibility, cfg.PublishInternal, cfg.PublishHostIP, cfg.AlwaysPublishNotReadyAddresses, cfg.ServiceTypeFilter, cfg.IgnoreHostnameAnnotation, cfg.LabelFilter, cfg.ResolveLoadBalancerHostname, cfg.ListenEndpointEvents)
case "ingress":
client, err := p.KubeClient()
if err != nil {