Skip to content

Commit a320ec1

Browse files
committed
Only considering pods in ready state for DNS resolution (this fixes #5)
Now suporting multiple pods having the same hostname. Also taking the ready state of a pod into account. This means: * only allowing lookup of a pod when all containers are ready * and when it is not being deleted. Readiness detection using the pod status ready status. Deletion detection using the metadata.deletionTimestamp field.
1 parent 546a71d commit a320ec1

File tree

14 files changed

+241
-50
lines changed

14 files changed

+241
-50
lines changed

cmd/dns-server/performance_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ func BenchmarkCreateNetworks(b *testing.B) {
2424
fmt.Sprintf("pod%d", ipod),
2525
[]model.Hostname{model.Hostname(fmt.Sprintf("host%d", j))},
2626
[]model.NetworkId{model.NetworkId(fmt.Sprintf("network%d", i))},
27+
true,
2728
)
2829
assert.Nil(b, err)
2930
pods.AddOrUpdate(pod)

internal/admissioncontroller/admissioncontroller_test.go

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -221,13 +221,10 @@ func (s *MutatorTestSuite) Test_DuplicateHost() {
221221
},
222222
"20.21.22.23")
223223
response := s.mutator.Handle(s.ctx, request)
224-
s.False(response.Allowed)
225-
226-
klog.V(3).Infof("Message: %s", response.Result.Message)
227-
s.True(strings.Contains(response.Result.Message, "already mapped to"))
224+
s.True(response.Allowed)
228225

229226
s.NotNil(s.pods.Get("kubedock", "db"))
230-
s.Nil(s.pods.Get("kubedock", "db2"))
227+
s.NotNil(s.pods.Get("kubedock", "db2"))
231228
}
232229

233230
func (s *MutatorTestSuite) Test_SecondHost() {

internal/dns/dns_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ func (dnsFunc DnsFunc) Resolve(r *dns.Msg) *dns.Msg {
3333

3434
func (s *DNSTestSuite) newPod(ip model.IPAddress, namespace string, name string, hostAliases []model.Hostname,
3535
networks []model.NetworkId) *model.Pod {
36-
pod, err := model.NewPod(ip, namespace, name, hostAliases, networks)
36+
pod, err := model.NewPod(ip, namespace, name, hostAliases, networks, true)
3737
s.Nil(err)
3838
s.NotNil(pod)
3939
return pod

internal/model/networks.go

Lines changed: 22 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,11 @@ type Pod struct {
2727
Name string
2828
HostAliases []Hostname
2929
Networks []NetworkId
30+
Ready bool
3031
}
3132

3233
func NewPod(ip IPAddress, namespace string, name string, hostAliases []Hostname,
33-
networks []NetworkId) (*Pod, error) {
34+
networks []NetworkId, ready bool) (*Pod, error) {
3435

3536
hostAliases = slices.Clone(hostAliases)
3637
slices.Sort(hostAliases)
@@ -53,6 +54,7 @@ func NewPod(ip IPAddress, namespace string, name string, hostAliases []Hostname,
5354
Name: name,
5455
HostAliases: hostAliases,
5556
Networks: networks,
57+
Ready: ready,
5658
}, nil
5759
}
5860

@@ -67,36 +69,33 @@ func (pod *Pod) Copy() *Pod {
6769
Name: pod.Name,
6870
HostAliases: slices.Clone(pod.HostAliases),
6971
Networks: slices.Clone(pod.Networks),
72+
Ready: pod.Ready,
7073
}
7174
}
7275

7376
type Network struct {
74-
Id NetworkId
75-
IPToPod map[IPAddress]*Pod
76-
HostAliasToPod map[Hostname]*Pod
77+
Id NetworkId
78+
IPToPod map[IPAddress]*Pod
79+
HostAliasToPods map[Hostname][]*Pod
7780
}
7881

7982
func NewNetwork(id NetworkId) *Network {
8083
network := Network{
81-
Id: id,
82-
IPToPod: make(map[IPAddress]*Pod),
83-
HostAliasToPod: make(map[Hostname]*Pod),
84+
Id: id,
85+
IPToPod: make(map[IPAddress]*Pod),
86+
HostAliasToPods: make(map[Hostname][]*Pod),
8487
}
8588
return &network
8689
}
8790

8891
func (net *Network) Add(pod *Pod) error {
89-
for _, hostAlias := range pod.HostAliases {
90-
existingPod := net.HostAliasToPod[hostAlias]
91-
if existingPod != nil && !(existingPod.Namespace == pod.Namespace && existingPod.Name == pod.Name) {
92-
return fmt.Errorf("Pod %s/%s: hostAlias %s in network %s already mapped to %s/%s",
93-
pod.Namespace, pod.Name, hostAlias, net.Id, existingPod.Namespace, existingPod.Name)
94-
}
95-
}
96-
9792
net.IPToPod[pod.IP] = pod
9893
for _, hostAlias := range pod.HostAliases {
99-
net.HostAliasToPod[hostAlias] = pod
94+
pods := net.HostAliasToPods[hostAlias]
95+
// when building the network from the pods, each pod is added in turn,
96+
// so we do not need to check for duplicate additions of pods.
97+
pods = append(pods, pod)
98+
net.HostAliasToPods[hostAlias] = pods
10099
}
101100
return nil
102101
}
@@ -170,7 +169,7 @@ func (net *Networks) Log() {
170169
for networkId, network := range net.NameToNetwork {
171170
klog.Infof("Network %s", networkId)
172171
for ip, pod := range network.IPToPod {
173-
klog.Infof(" Pod: %s/%s", pod.Namespace, pod.Name)
172+
klog.Infof(" Pod: %s/%s ready %v", pod.Namespace, pod.Name, pod.Ready)
174173
klog.Infof(" IP: %s", ip)
175174
for _, hostAlias := range pod.HostAliases {
176175
klog.Infof(" Hostalias: %s", hostAlias)
@@ -191,9 +190,11 @@ func (net *Networks) Lookup(sourceIp IPAddress, hostname Hostname) []IPAddress {
191190
return make([]IPAddress, 0)
192191
}
193192
for _, network := range networks {
194-
pod := network.HostAliasToPod[hostname]
195-
if pod != nil {
196-
res = append(res, pod.IP)
193+
pods := network.HostAliasToPods[hostname]
194+
for _, pod := range pods {
195+
if pod.Ready {
196+
res = append(res, pod.IP)
197+
}
197198
}
198199
}
199200
return res
@@ -214,7 +215,7 @@ func (net *Networks) ReverseLookup(sourceIp IPAddress, ip IPAddress) []Hostname
214215
for _, network := range networks {
215216
klog.V(3).Infof("Trying %s %v", network.Id, network)
216217
pod := network.IPToPod[ip]
217-
if pod != nil {
218+
if pod != nil && pod.Ready {
218219
klog.V(3).Infof("Found hostaliases %v", pod.HostAliases)
219220
return pod.HostAliases
220221
}

0 commit comments

Comments
 (0)