diff --git a/plugin/kubernetes/autopath.go b/plugin/kubernetes/autopath.go index e87389768..f20f03c7c 100644 --- a/plugin/kubernetes/autopath.go +++ b/plugin/kubernetes/autopath.go @@ -33,15 +33,12 @@ func (k *Kubernetes) AutoPath(state request.Request) []string { return nil } - search := make([]string, 3) + totalSize := 3 + len(k.autoPathSearch) + 1 // +1 for sentinel + search := make([]string, 0, totalSize) if zone == "." { - search[0] = pod.Namespace + ".svc." - search[1] = "svc." - search[2] = "." + search = append(search, pod.Namespace+".svc.", "svc.", ".") } else { - search[0] = pod.Namespace + ".svc." + zone - search[1] = "svc." + zone - search[2] = zone + search = append(search, pod.Namespace+".svc."+zone, "svc."+zone, zone) } search = append(search, k.autoPathSearch...) diff --git a/plugin/kubernetes/autopath_test.go b/plugin/kubernetes/autopath_test.go new file mode 100644 index 000000000..ad4b01915 --- /dev/null +++ b/plugin/kubernetes/autopath_test.go @@ -0,0 +1,77 @@ +package kubernetes + +import ( + "context" + "testing" + + "github.com/coredns/coredns/plugin/kubernetes/object" + "github.com/coredns/coredns/plugin/test" + "github.com/coredns/coredns/request" + + "github.com/miekg/dns" + api "k8s.io/api/core/v1" +) + +// Mock data for benchmarks +var ( + mockPod = &object.Pod{ + Namespace: "test-namespace", + } + mockAutoPathSearch = []string{"example.com", "internal.example.com", "cluster.local"} +) + +// Mock API connector for testing +type mockAPIConnector struct{} + +func (m *mockAPIConnector) PodIndex(ip string) []*object.Pod { + return []*object.Pod{mockPod} +} + +// Minimal implementation of other required methods +func (m *mockAPIConnector) ServiceList() []*object.Service { return nil } +func (m *mockAPIConnector) EndpointsList() []*object.Endpoints { return nil } +func (m *mockAPIConnector) ServiceImportList() []*object.ServiceImport { return nil } +func (m *mockAPIConnector) SvcIndex(s string) []*object.Service { return nil } +func (m *mockAPIConnector) SvcIndexReverse(s string) []*object.Service { return nil } +func (m *mockAPIConnector) SvcExtIndexReverse(s string) []*object.Service { return nil } +func (m *mockAPIConnector) SvcImportIndex(s string) []*object.ServiceImport { return nil } +func (m *mockAPIConnector) EpIndex(s string) []*object.Endpoints { return nil } +func (m *mockAPIConnector) EpIndexReverse(s string) []*object.Endpoints { return nil } +func (m *mockAPIConnector) McEpIndex(s string) []*object.MultiClusterEndpoints { return nil } +func (m *mockAPIConnector) GetNodeByName(ctx context.Context, name string) (*api.Node, error) { + return nil, nil +} +func (m *mockAPIConnector) GetNamespaceByName(name string) (*object.Namespace, error) { + return nil, nil +} +func (m *mockAPIConnector) Run() {} +func (m *mockAPIConnector) HasSynced() bool { return true } +func (m *mockAPIConnector) Stop() error { return nil } +func (m *mockAPIConnector) Modified(ModifiedMode) int64 { return 0 } + +func BenchmarkAutoPath(b *testing.B) { + k := &Kubernetes{ + Zones: []string{"cluster.local."}, + autoPathSearch: mockAutoPathSearch, + podMode: podModeVerified, + opts: dnsControlOpts{ + initPodCache: true, + }, + APIConn: &mockAPIConnector{}, + } + + // Create a mock DNS request + req := &dns.Msg{} + req.SetQuestion("test.cluster.local.", dns.TypeA) + + // Create a request state with a mock ResponseWriter + state := request.Request{W: &test.ResponseWriter{}, Req: req} + + b.ReportAllocs() + b.ResetTimer() + + for i := 0; i < b.N; i++ { + result := k.AutoPath(state) + _ = result + } +}