-
Notifications
You must be signed in to change notification settings - Fork 12
Add support for static domain-to-IP mapping in k8s_dns_server for DNSChaos #31
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,7 +2,6 @@ package kubernetes | |
|
||
import ( | ||
"context" | ||
|
||
"github.com/coredns/coredns/plugin" | ||
"github.com/coredns/coredns/request" | ||
|
||
|
@@ -22,10 +21,23 @@ func (k Kubernetes) ServeDNS(ctx context.Context, w dns.ResponseWriter, r *dns.M | |
|
||
records, extra, zone, err := k.getRecords(ctx, state) | ||
log.Debugf("records: %v, err: %v", records, err) | ||
|
||
if k.needChaos(chaosPod, records, state.QName()) { | ||
if k.needChaos(chaosPod, records, state.QName()) && chaosPod.Action != ActionStatic { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Are there any considerations not to move the logic of Theoretically, we need to handle this part of the logic in the function to keep the code clean. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The reason for not moving the chaosPod. Action != ActionStatic logic into k.needChaos() is to avoid changing the original logic, which might introduce unexpected issues or affect existing behavior. Since this part of the code has been stable, I chose to keep the separation to ensure functional consistency and reduce the risk of regression. Once the overall behavior is verified to be stable, we can consider refactoring this logic into k.needChaos() in a follow-up PR to improve code clarity. |
||
return k.chaosDNS(ctx, w, r, state, chaosPod) | ||
} | ||
// Check if chaos testing is needed and the action type is static IP. | ||
if k.needChaos(chaosPod, records, state.QName()) && chaosPod.Action == ActionStatic { | ||
log.Debugf("need chaos, but action is static") | ||
// Get the domain-IP mapping for the specific namespace and pod name. | ||
domainMap := k.domainAndIPMap[chaosPod.Namespace][chaosPod.Name] | ||
// Check if the domain-IP mapping exists. | ||
if domainMap != nil { | ||
// Check if the requested domain exists in the mapping. | ||
if _, ok := domainMap[state.Name()]; ok { | ||
// Generate DNS records using the domain-IP mapping and return the result. | ||
return generateDNSRecords(state, domainMap, r, w) | ||
} | ||
} | ||
} | ||
|
||
if k.IsNameError(err) { | ||
if len(zone) == 0 { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,13 +5,13 @@ import ( | |
"context" | ||
"errors" | ||
"fmt" | ||
"github.com/chaos-mesh/k8s_dns_chaos/pb" | ||
"math/rand" | ||
"net" | ||
"strings" | ||
"sync" | ||
"time" | ||
|
||
"github.com/chaos-mesh/k8s_dns_chaos/pb" | ||
"github.com/coredns/coredns/plugin" | ||
"github.com/coredns/coredns/plugin/etcd/msg" | ||
"github.com/coredns/coredns/plugin/kubernetes/object" | ||
|
@@ -68,6 +68,8 @@ type Kubernetes struct { | |
podMap map[string]map[string]*PodInfo | ||
|
||
ipPodMap map[string]*PodInfo | ||
|
||
domainAndIPMap map[string]map[string]map[string]string | ||
|
||
} | ||
|
||
// New returns a initialized Kubernetes. It default interfaceAddrFunc to return 127.0.0.1. All other | ||
|
@@ -81,6 +83,7 @@ func New(zones []string) *Kubernetes { | |
k.chaosMap = make(map[string]*pb.SetDNSChaosRequest) | ||
k.podMap = make(map[string]map[string]*PodInfo) | ||
k.ipPodMap = make(map[string]*PodInfo) | ||
k.domainAndIPMap = make(map[string]map[string]map[string]string) | ||
rand.Seed(time.Now().UnixNano()) | ||
|
||
return k | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This type is not used?