Do not use transformer when fqdnTemplate is set

This commit is contained in:
Valerian Roche 2025-07-07 21:30:53 -04:00
parent 59d54158ee
commit 2ed7bb3d7a
No known key found for this signature in database
GPG Key ID: DBDC687C250F4CD2
2 changed files with 165 additions and 98 deletions

View File

@ -78,9 +78,11 @@ func NewPodSource(
_, _ = podInformer.Informer().AddEventHandler(informers.DefaultEventHandler())
if fqdnTemplate == "" {
// Transformer is used to reduce the memory usage of the informer.
// The pod informer will otherwise store a full in-memory, go-typed copy of all pod schemas in the cluster.
// If watchList is not used it will not prevent memory bursts on the initial informer sync.
// When fqdnTemplate is used the entire pod needs to be provided to the rendering call, but the informer itself becomes unneeded.
podInformer.Informer().SetTransform(func(i interface{}) (interface{}, error) {
pod, ok := i.(*corev1.Pod)
if !ok {
@ -107,6 +109,8 @@ func NewPodSource(
},
}, nil
})
}
_, _ = nodeInformer.Informer().AddEventHandler(informers.DefaultEventHandler())
informerFactory.Start(ctx.Done())

View File

@ -1000,6 +1000,7 @@ func nodesFixturesIPv4() []*corev1.Node {
}
func TestPodTransformerInPodSource(t *testing.T) {
t.Run("transformer set", func(t *testing.T) {
ctx := t.Context()
fakeClient := fake.NewClientset()
@ -1078,4 +1079,66 @@ func TestPodTransformerInPodSource(t *testing.T) {
assert.Empty(t, retrieved.Status.HostIP)
assert.Equal(t, "127.0.0.1", retrieved.Status.PodIP)
assert.Empty(t, retrieved.Status.Conditions)
})
t.Run("transormer is not used when fqdnTemplate is set", func(t *testing.T) {
ctx := t.Context()
fakeClient := fake.NewClientset()
pod := &v1.Pod{
Spec: v1.PodSpec{
Containers: []v1.Container{{
Name: "test",
}},
Hostname: "test-hostname",
NodeName: "test-node",
HostNetwork: true,
},
ObjectMeta: metav1.ObjectMeta{
Namespace: "test-ns",
Name: "test-name",
Labels: map[string]string{
"label1": "value1",
"label2": "value2",
"label3": "value3",
},
Annotations: map[string]string{
"user-annotation": "value",
"external-dns.alpha.kubernetes.io/hostname": "test-hostname",
"external-dns.alpha.kubernetes.io/random": "value",
"other/annotation": "value",
},
UID: "someuid",
},
Status: v1.PodStatus{
PodIP: "127.0.0.1",
HostIP: "127.0.0.2",
Conditions: []v1.PodCondition{{
Type: v1.PodReady,
Status: v1.ConditionTrue,
}, {
Type: v1.ContainersReady,
Status: v1.ConditionFalse,
}},
},
}
_, err := fakeClient.CoreV1().Pods(pod.Namespace).Create(context.Background(), pod, metav1.CreateOptions{})
require.NoError(t, err)
// Should not error when creating the source
src, err := NewPodSource(ctx, fakeClient, "", "", false, "", "template", false, "", nil)
require.NoError(t, err)
ps, ok := src.(*podSource)
require.True(t, ok)
retrieved, err := ps.podInformer.Lister().Pods("test-ns").Get("test-name")
require.NoError(t, err)
// Metadata
assert.Equal(t, "test-name", retrieved.Name)
assert.Equal(t, "test-ns", retrieved.Namespace)
assert.NotEmpty(t, retrieved.UID)
assert.NotEmpty(t, retrieved.Labels)
})
}