Skip to content

Commit 30d1e76

Browse files
committed
align and doc server answers
1 parent 1d71077 commit 30d1e76

7 files changed

Lines changed: 65 additions & 52 deletions

File tree

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,12 @@ The REST servers offers an API (API version v1) for looking up
139139

140140
The answer is always a single JSON document.
141141

142+
- `error` *string*: in case of an error, this is the only field set.
143+
- `infos` *[]info*: the field infos provides a list of informational answers.
144+
145+
For FQDN resolutions the list contains one entry, with a possible empty list of records.
146+
For an IP resolutions it may include multiple entries describeing all possible matches with all possible FQDNS.
147+
142148
#### `error`
143149

144150
In case of an error a JSON document with a field `error` is returned, which contain the error message.

internal/controller/server/servercomp/comp_ips.go

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -43,41 +43,41 @@ func (c *Component) handleIPs(w http.ResponseWriter, r *http.Request) {
4343
c.SendError(fmt.Errorf("name not in domain"), w, http.StatusBadRequest)
4444
return
4545
}
46-
var answers []*v1.Answer
46+
var answer v1.Answer
4747

48-
for _, info := range infos {
49-
var answer v1.Answer
50-
answer.Names = info.EntryNames
51-
if info.Zone != nil {
48+
for _, i := range infos {
49+
var info v1.Info
50+
info.Names = i.EntryNames
51+
if i.Zone != nil {
5252
var zone corednsv1alpha1.HostedZone
53-
err := info.Zone.GetSource().(cluster.Cluster).Get(context.Background(), info.Zone.GetKey().NamespacedName, &zone)
53+
err := i.Zone.GetSource().(cluster.Cluster).Get(context.Background(), i.Zone.GetKey().NamespacedName, &zone)
5454
if err != nil {
5555
c.SendError(err, w, http.StatusInternalServerError)
5656
return
5757
}
58-
answer.Zone.Names = info.ZoneNames
59-
answer.Zone.NameServers = zone.Status.NameServers
60-
answer.Zone.EMail = zone.Spec.EMail
61-
answer.Zone.MinimumTTL = zone.Spec.MinimumTTL
62-
answer.Zone.Expire = zone.Spec.Expire
63-
answer.Zone.Refresh = zone.Spec.Refresh
58+
info.Zone.Names = i.ZoneNames
59+
info.Zone.NameServers = zone.Status.NameServers
60+
info.Zone.EMail = zone.Spec.EMail
61+
info.Zone.MinimumTTL = zone.Spec.MinimumTTL
62+
info.Zone.Expire = zone.Spec.Expire
63+
info.Zone.Refresh = zone.Spec.Refresh
6464
}
65-
for _, r := range info.Entries {
66-
CopyR(r.Spec.NS, &answer.Records.NS)
67-
CopyR(r.Spec.A, &answer.Records.A)
68-
CopyR(r.Spec.AAAA, &answer.Records.AAAA)
69-
CopyR(r.Spec.TXT, &answer.Records.TXT)
70-
if answer.Records.CNAME == "" {
71-
answer.Records.CNAME = r.Spec.CNAME
65+
for _, r := range i.Entries {
66+
CopyR(r.Spec.NS, &info.Records.NS)
67+
CopyR(r.Spec.A, &info.Records.A)
68+
CopyR(r.Spec.AAAA, &info.Records.AAAA)
69+
CopyR(r.Spec.TXT, &info.Records.TXT)
70+
if info.Records.CNAME == "" {
71+
info.Records.CNAME = r.Spec.CNAME
7272
}
7373
if r.Spec.SRV != nil {
74-
answer.Records.SRV = append(answer.Records.SRV, *r.Spec.SRV)
74+
info.Records.SRV = append(info.Records.SRV, *r.Spec.SRV)
7575
}
7676
}
77-
answers = append(answers, &answer)
77+
answer.Infos = append(answer.Infos, info)
7878
}
7979

80-
d, _ := json.Marshal(answers)
80+
d, _ := json.Marshal(&answer)
8181
_, err = io.Copy(w, bytes.NewReader(d))
8282
if err != nil {
8383
c.logger.Error("%s", err.Error())

internal/controller/server/servercomp/comp_names.go

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"net/http"
1010
"strings"
1111

12+
"github.com/mandelsoft/goutils/sliceutils"
1213
"github.com/mandelsoft/kubecrtutils/cluster"
1314
corednsv1alpha1 "github.com/mandelsoft/kubedns/api/coredns/v1alpha1"
1415
"github.com/mandelsoft/kubedns/internal/controller/server/zonemodel"
@@ -43,37 +44,39 @@ func (c *Component) handleNames(w http.ResponseWriter, r *http.Request) {
4344
c.SendError(fmt.Errorf("name not in domain"), w, http.StatusBadRequest)
4445
return
4546
}
46-
var answer v1.Answer
4747

48-
answer.Names = info.EntryNames
48+
var i v1.Info
49+
50+
i.Names = info.EntryNames
4951
if info.Zone != nil {
5052
var zone corednsv1alpha1.HostedZone
5153
err := info.Zone.GetSource().(cluster.Cluster).Get(context.Background(), info.Zone.GetKey().NamespacedName, &zone)
5254
if err != nil {
5355
c.SendError(err, w, http.StatusInternalServerError)
5456
return
5557
}
56-
answer.Zone.Names = info.ZoneNames
57-
answer.Zone.NameServers = zone.Status.NameServers
58-
answer.Zone.EMail = zone.Spec.EMail
59-
answer.Zone.MinimumTTL = zone.Spec.MinimumTTL
60-
answer.Zone.Expire = zone.Spec.Expire
61-
answer.Zone.Refresh = zone.Spec.Refresh
58+
i.Zone.Names = info.ZoneNames
59+
i.Zone.NameServers = zone.Status.NameServers
60+
i.Zone.EMail = zone.Spec.EMail
61+
i.Zone.MinimumTTL = zone.Spec.MinimumTTL
62+
i.Zone.Expire = zone.Spec.Expire
63+
i.Zone.Refresh = zone.Spec.Refresh
6264
}
6365
for _, r := range info.Entries {
64-
CopyR(r.Spec.NS, &answer.Records.NS)
65-
CopyR(r.Spec.A, &answer.Records.A)
66-
CopyR(r.Spec.AAAA, &answer.Records.AAAA)
67-
CopyR(r.Spec.TXT, &answer.Records.TXT)
68-
if answer.Records.CNAME == "" {
69-
answer.Records.CNAME = r.Spec.CNAME
66+
CopyR(r.Spec.NS, &i.Records.NS)
67+
CopyR(r.Spec.A, &i.Records.A)
68+
CopyR(r.Spec.AAAA, &i.Records.AAAA)
69+
CopyR(r.Spec.TXT, &i.Records.TXT)
70+
if i.Records.CNAME == "" {
71+
i.Records.CNAME = r.Spec.CNAME
7072
}
7173
if r.Spec.SRV != nil {
72-
answer.Records.SRV = append(answer.Records.SRV, *r.Spec.SRV)
74+
i.Records.SRV = append(i.Records.SRV, *r.Spec.SRV)
7375
}
7476
}
7577

76-
d, _ := json.Marshal(&answer)
78+
answer := &v1.Answer{Infos: sliceutils.AsSlice(i)}
79+
d, _ := json.Marshal(answer)
7780
_, err = io.Copy(w, bytes.NewReader(d))
7881
if err != nil {
7982
c.logger.Error("%s", err.Error())

internal/controller/server/servercomp/component.go

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,10 @@ import (
88
"io"
99
"net/http"
1010

11+
"github.com/mandelsoft/goutils/generics"
1112
"github.com/mandelsoft/kubecrtutils/cacheindex"
1213
"github.com/mandelsoft/kubecrtutils/cluster"
14+
"github.com/mandelsoft/kubecrtutils/cluster/clustercontext"
1315
"github.com/mandelsoft/kubecrtutils/component"
1416
corednsv1alpha1 "github.com/mandelsoft/kubedns/api/coredns/v1alpha1"
1517
"github.com/mandelsoft/kubedns/internal/controller/server/zonemodel"
@@ -43,14 +45,14 @@ func (c *Component) GetModel() *zonemodel.Model {
4345
return c.model
4446
}
4547

46-
func (c *Component) LookupRelativeDomainName(ctx context.Context, zk zonemodel.ZoneKey, rel string) ([]corednsv1alpha1.CoreDNSEntry, error) {
48+
func (c *Component) LookupRelativeDomainName(src zonemodel.Source, zk zonemodel.ZoneKey, rel string) ([]corednsv1alpha1.CoreDNSEntry, error) {
4749
c.Info("lookup entries for zone {{key}} rdn {{rdn}}", "key", zk, "rdn", rel)
48-
return c.indexByName.GetTyped(ctx, zk.Namespace, IndexKey(zk, rel))
50+
return c.indexByName.GetTyped(clustercontext.WithCluster(context.Background(), generics.Cast[cluster.Cluster](src)), zk.Namespace, IndexKey(zk, rel))
4951
}
5052

51-
func (c *Component) LookupIP(ctx context.Context, zk zonemodel.ZoneKey, ip string) ([]corednsv1alpha1.CoreDNSEntry, error) {
53+
func (c *Component) LookupIP(src zonemodel.Source, zk zonemodel.ZoneKey, ip string) ([]corednsv1alpha1.CoreDNSEntry, error) {
5254
c.Info("lookup entries for zone {{key}} ip {{ip}}", "key", zk, "ip", ip)
53-
return c.indexByIP.GetTyped(ctx, zk.Namespace, IndexKey(zk, ip))
55+
return c.indexByIP.GetTyped(clustercontext.WithCluster(context.Background(), generics.Cast[cluster.Cluster](src)), zk.Namespace, IndexKey(zk, ip))
5456
}
5557

5658
func IndexKey(zk zonemodel.ZoneKey, rdn string) string {

internal/controller/server/zonemodel/api/v1/api.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,15 @@ import (
44
corednsv1alpha1 "github.com/mandelsoft/kubedns/api/coredns/v1alpha1"
55
)
66

7+
type Answer struct {
8+
Error `json:",inline"`
9+
Infos []Info `json:"infos"`
10+
}
711
type Error struct {
8-
Error string `json:"error"`
12+
Error string `json:"error,omitempty"`
913
}
1014

11-
type Answer struct {
15+
type Info struct {
1216
Zone Zone `json:"zone"`
1317
Names []string `json:"names"`
1418
Records Records `json:"records"`

internal/controller/server/zonemodel/model.go

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package zonemodel
22

33
import (
4-
"context"
54
"fmt"
65
"sync"
76

@@ -59,8 +58,8 @@ type Source interface {
5958
}
6059

6160
type Index interface {
62-
LookupRelativeDomainName(ctx context.Context, zone ZoneKey, rel string) ([]corednsv1alpha1.CoreDNSEntry, error)
63-
LookupIP(ctx context.Context, zone ZoneKey, ip string) ([]corednsv1alpha1.CoreDNSEntry, error)
61+
LookupRelativeDomainName(src Source, zone ZoneKey, rel string) ([]corednsv1alpha1.CoreDNSEntry, error)
62+
LookupIP(src Source, zone ZoneKey, ip string) ([]corednsv1alpha1.CoreDNSEntry, error)
6463
}
6564

6665
type Model struct {
@@ -218,7 +217,7 @@ nextForward:
218217
cur = Join(l, cur)
219218
rel = Rdn(Join(l, rel))
220219
logger.Info("lookup NS record for {{current}}/{{relative}}", "current", cur, "relative", rel)
221-
list, err = zone.model.index.LookupRelativeDomainName(nil, zone.key, rel)
220+
list, err = zone.model.index.LookupRelativeDomainName(zone.source, zone.key, rel)
222221
if err != nil {
223222
return nil, err
224223
}
@@ -250,7 +249,7 @@ func (z *Zone) ResolveIP(logger logging.Logger, ip string) ([]*Info, error) {
250249

251250
func (z *Zone) resolveIP(fqdns []string, logger logging.Logger, ip string) ([]*Info, error) {
252251

253-
list, err := z.model.index.LookupIP(nil, z.key, ip)
252+
list, err := z.model.index.LookupIP(z.source, z.key, ip)
254253
if err != nil {
255254
return nil, err
256255
}

internal/controller/server/zonemodel/model_test.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package zonemodel_test
22

33
import (
4-
"context"
54
"slices"
65

76
"github.com/mandelsoft/goutils/sliceutils"
@@ -25,15 +24,15 @@ func NewIndex() *Index {
2524
return &Index{entries: make(map[zonemodel.ZoneKey]map[string][]corednsv1alpha1.CoreDNSEntry)}
2625
}
2726

28-
func (i *Index) LookupRelativeDomainName(ctx context.Context, zone zonemodel.ZoneKey, rel string) ([]corednsv1alpha1.CoreDNSEntry, error) {
27+
func (i *Index) LookupRelativeDomainName(src zonemodel.Source, zone zonemodel.ZoneKey, rel string) ([]corednsv1alpha1.CoreDNSEntry, error) {
2928
zoneentries := i.entries[zone]
3029
if zoneentries == nil {
3130
return nil, nil
3231
}
3332
return zoneentries[rel], nil
3433
}
3534

36-
func (i *Index) LookupIP(ctx context.Context, zone zonemodel.ZoneKey, ip string) ([]corednsv1alpha1.CoreDNSEntry, error) {
35+
func (i *Index) LookupIP(src zonemodel.Source, zone zonemodel.ZoneKey, ip string) ([]corednsv1alpha1.CoreDNSEntry, error) {
3736
zoneentries := i.entries[zone]
3837
if zoneentries == nil {
3938
return nil, nil

0 commit comments

Comments
 (0)