Conversation
There was a problem hiding this comment.
Summary of Changes
Hello @Kuromesi, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!
I've implemented an experimental DNS proxy feature for Kmesh. This enhancement allows Kmesh to act as a DNS server, intercepting and resolving DNS queries for services, especially those managed by Istio ServiceEntry configurations. By redirecting DNS traffic to the Kmesh daemon, we can ensure that traffic policies are correctly applied to hostnames that might otherwise be unresolvable. This provides a more comprehensive traffic management solution within the mesh.
Highlights
- Experimental DNS Proxy: I've introduced an experimental DNS proxy feature within the Kmesh daemon. This allows Kmesh to intercept and handle DNS queries for services, particularly those defined via Istio
ServiceEntryresources. - DNS Traffic Redirection via eBPF: When enabled, all traffic destined for port 53 (DNS) will be redirected to the Kmesh daemon's internal DNS server. This redirection is managed by new eBPF programs attached to cgroups.
- Enhanced Service Entry Resolution: The proxy enables hostnames, such as those in
ServiceEntrydefinitions, to be resolved by Kmesh. This is crucial for applying traffic policies to services that are not natively discoverable via Kubernetes DNS. - Configurable Feature Flag: The feature is controlled by the
KMESH_ENABLE_DNS_PROXYenvironment variable, defaulting tofalse. This allows for a controlled rollout and testing of the new functionality. - Modular DNS Resolution Architecture: I've refactored the DNS resolution logic to be more modular, introducing
Resolverinterfaces and delegate resolvers. This allows for flexible chaining of resolution mechanisms, including a newServiceCacheResolverthat uses Kmesh's internal service cache and anUpstreamResolverfor external queries.
Using Gemini Code Assist
The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.
Invoking Gemini
You can request assistance from Gemini at any point in your pull request via creating an issue comment (i.e. comment on the pull request page) using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands.
| Feature | Command | Description |
|---|---|---|
| Code Review | /gemini review |
Performs a code review for the current pull request in its current state. |
| Pull Request Summary | /gemini summary |
Provides a summary of the current pull request in its current state. |
| Comment | @gemini-code-assist | Responds in comments when explicitly tagged, both in issue comments and review comments. |
| Help | /gemini help |
Displays a list of available commands. |
Customization
To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.
Limitations & Feedback
Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments or fill out our survey to provide feedback.
You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.
A hostname sought, a query sent, Through eBPF's path, its journey bent. To Kmesh it flies, a proxy's art, New addresses found, a fresh start.
Footnotes
-
Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution. ↩
There was a problem hiding this comment.
Code Review
This pull request introduces DNS proxy support as an experimental feature. The changes are extensive, touching BPF programs, Go controller logic, and adding a new DNS server implementation. My review focuses on correctness and maintainability. I've identified a critical bug in the BPF program attachment logic during restart, fragile logic in the DNS service cache resolver, and some code duplication and dead code that should be addressed.
pkg/dns/service_cache_resolver.go
Outdated
| case dns.TypeA: | ||
| ip := net.IP(svc.GetAddresses()[0].Address) | ||
| if ip == nil || ip.To4() == nil { | ||
| log.Errorf("Invalid IPv4 address in records for %s: %s", q.Name, ip.String()) | ||
| continue | ||
| } | ||
|
|
||
| rr, err := dns.NewRR(fmt.Sprintf("%s A %s", q.Name, ip.String())) | ||
| if err != nil { | ||
| log.Errorf("Error creating A record: %v", err) | ||
| continue | ||
| } | ||
| m.Answer = append(m.Answer, rr) |
There was a problem hiding this comment.
The logic for creating an A record assumes the IPv4 address is always at index [0] of the address slice. This is not a safe assumption and can lead to incorrect behavior or panics if the address list is empty, in a different order, or doesn't contain an IPv4 address at that position. You should iterate through the addresses to find a valid IPv4 address.
case dns.TypeA:
var ip net.IP
for _, addr := range svc.GetAddresses() {
maybeIP := net.IP(addr.Address)
if maybeIP != nil && maybeIP.To4() != nil {
ip = maybeIP
break
}
}
if ip == nil {
log.Errorf("No valid IPv4 address in records for %s", q.Name)
continue
}
rr, err := dns.NewRR(fmt.Sprintf("%s A %s", q.Name, ip.String()))
if err != nil {
log.Errorf("Error creating A record: %v", err)
continue
}
m.Answer = append(m.Answer, rr)
pkg/dns/service_cache_resolver.go
Outdated
| case dns.TypeAAAA: | ||
| if len(svc.GetAddresses()) < 2 { | ||
| log.Infof("no ipv6 address found") | ||
| continue | ||
| } | ||
| ip := net.IP(svc.GetAddresses()[1].Address) | ||
| if ip == nil || ip.To16() == nil { | ||
| log.Errorf("Invalid IP address: %s", ip.String()) | ||
| continue | ||
| } | ||
|
|
||
| rr, err := dns.NewRR(fmt.Sprintf("%s %d IN AAAA %s", q.Name, 60, ip.String())) | ||
| if err != nil { | ||
| log.Errorf("Error creating AAAA record: %v", err) | ||
| continue | ||
| } | ||
| m.Answer = append(m.Answer, rr) | ||
| } |
There was a problem hiding this comment.
The logic for creating an AAAA record is brittle. It checks if there are at least 2 addresses and assumes the one at index [1] is IPv6. This is not robust and will fail if the addresses are in a different order or if only an IPv6 address is present. You should iterate through the addresses to find a valid IPv6 address that is not an IPv4-mapped IPv6 address.
case dns.TypeAAAA:
var ip net.IP
for _, addr := range svc.GetAddresses() {
maybeIP := net.IP(addr.Address)
if maybeIP != nil && maybeIP.To16() != nil && maybeIP.To4() == nil {
ip = maybeIP
break
}
}
if ip == nil {
log.Infof("no ipv6 address found for %s", q.Name)
continue
}
rr, err := dns.NewRR(fmt.Sprintf("%s %d IN AAAA %s", q.Name, 60, ip.String()))
if err != nil {
log.Errorf("Error creating AAAA record: %v", err)
continue
}
m.Answer = append(m.Answer, rr)Signed-off-by: Kuromesi <blackfacepan@163.com>
bpf/kmesh/workload/cgroup_sock.c
Outdated
| } | ||
|
|
||
| ctx->user_ip4 = backend_v->addr.ip4; | ||
| ctx->user_port = bpf_htons(53); |
| return CGROUP_SOCK_OK; | ||
| } | ||
|
|
||
| ctx->user_ip4 = storage->sk_tuple.ipv4.daddr; |
There was a problem hiding this comment.
It rewriting the dst address a must in udp?
pkg/dns/service_cache_resolver.go
Outdated
There was a problem hiding this comment.
can you import or refer istio dns proxy implemention
hzxuzhonghu
left a comment
There was a problem hiding this comment.
Generally LG, LMK if you are gonna to optimize or later
Signed-off-by: Kuromesi <blackfacepan@163.com>
|
Could you please help me review the latest commit which implement the istio dns server? @hzxuzhonghu @LiZhenCheng9527 |
pkg/controller/controller.go
Outdated
| kdns "kmesh.net/kmesh/pkg/dns" | ||
|
|
||
| service_discovery_v3 "github.com/envoyproxy/go-control-plane/envoy/service/discovery/v3" | ||
| dnsClient "istio.io/istio/pkg/dns/client" |
There was a problem hiding this comment.
nit: camel case is not common in go import
There was a problem hiding this comment.
Sure I'll fix this! (This is copied from istio btw)
| } | ||
| } | ||
|
|
||
| // THIS FUNC IS MODIFIED BASED ON istio.io/istio/pkg/dns/server/name_table.go |
|
/retest |
|
I'll check what's wrong with the e2e |
|
seems related |
|
Cool, will have a last look |
|
[APPROVALNOTIFIER] This PR is APPROVED This pull-request has been approved by: hzxuzhonghu The full list of commands accepted by this bot can be found here. The pull request process is described here DetailsNeeds approval from an approver in each of these files:
Approvers can indicate their approval by writing |
What type of PR is this?
/kind feature
What this PR does / why we need it:
This experimental feature is controlled by env
KMESH_ENABLE_DNS_PROXY, defaults to false. When dns proxy is enabled, a dns server will be set up in kmesh daemon and all traffic with destination port 53 will be redirected to which.This feature can be illustrated and validated by the following example:
Without dns proxy, traffic to kmesh-fake.com will fail with could not resolve host error. When dns proxy is enabled, host can be resolved and traffic policies will be applied.
Which issue(s) this PR fixes:
This should resolve #1459.
Special notes for your reviewer:
Does this PR introduce a user-facing change?: