Skip to content

Commit 7f31332

Browse files
committed
fully migrate DNS to proto
1 parent cbaebfa commit 7f31332

File tree

9 files changed

+192
-32
lines changed

9 files changed

+192
-32
lines changed

pkg/roles/dns/record.go

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,12 @@ package dns
22

33
import (
44
"context"
5-
"encoding/json"
65
"net"
76
"strings"
87

98
"beryju.io/gravity/pkg/roles"
109
"beryju.io/gravity/pkg/roles/dns/types"
10+
"beryju.io/gravity/pkg/storage"
1111
"github.com/miekg/dns"
1212
"go.etcd.io/etcd/api/v3/mvccpb"
1313
clientv3 "go.etcd.io/etcd/client/v3"
@@ -43,13 +43,7 @@ func (z *ZoneContext) recordFromKV(kv *mvccpb.KeyValue) (*RecordContext, error)
4343
}
4444
rec.recordKey = strings.TrimSuffix(fullRecordKey, "/"+rec.uid)
4545

46-
// Try loading protobuf first
47-
err := proto.Unmarshal(kv.Value, rec.Record)
48-
if err == nil {
49-
return rec, nil
50-
}
51-
// Otherwise try json
52-
err = json.Unmarshal(kv.Value, &rec)
46+
_, err := storage.Parse(kv.Value, rec.Record)
5347
if err != nil {
5448
return rec, err
5549
}

pkg/roles/dns/role.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ type Role struct {
2424
ctx context.Context
2525
zones map[string]*ZoneContext
2626

27-
cfg *RoleConfig
27+
cfg *types.RoleConfig
2828
log *zap.Logger
2929
servers []*dns.Server
3030
zonesM sync.RWMutex

pkg/roles/dns/role_config.go

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,41 +2,38 @@ package dns
22

33
import (
44
"context"
5-
"encoding/json"
65

76
instanceTypes "beryju.io/gravity/pkg/instance/types"
7+
"beryju.io/gravity/pkg/storage"
88
"go.uber.org/zap"
9+
"google.golang.org/protobuf/proto"
910

1011
"beryju.io/gravity/pkg/roles/dns/types"
1112
"github.com/swaggest/usecase"
1213
"github.com/swaggest/usecase/status"
1314
)
1415

15-
type RoleConfig struct {
16-
Port int32 `json:"port"`
17-
}
18-
19-
func (r *Role) decodeRoleConfig(raw []byte) *RoleConfig {
20-
def := RoleConfig{
16+
func (r *Role) decodeRoleConfig(raw []byte) *types.RoleConfig {
17+
def := types.RoleConfig{
2118
Port: 53,
2219
}
2320
if len(raw) < 1 {
2421
return &def
2522
}
26-
err := json.Unmarshal(raw, &def)
23+
conf, err := storage.Parse(raw, &def)
2724
if err != nil {
2825
r.log.Warn("failed to decode role config", zap.Error(err))
2926
}
30-
return &def
27+
return conf
3128
}
3229

3330
type APIRoleConfigOutput struct {
34-
Config RoleConfig `json:"config" required:"true"`
31+
Config *types.RoleConfig `json:"config" required:"true"`
3532
}
3633

3734
func (r *Role) APIRoleConfigGet() usecase.Interactor {
3835
u := usecase.NewInteractor(func(ctx context.Context, input struct{}, output *APIRoleConfigOutput) error {
39-
output.Config = *r.cfg
36+
output.Config = r.cfg
4037
return nil
4138
})
4239
u.SetName("dns.get_role_config")
@@ -46,12 +43,12 @@ func (r *Role) APIRoleConfigGet() usecase.Interactor {
4643
}
4744

4845
type APIRoleConfigInput struct {
49-
Config RoleConfig `json:"config" required:"true"`
46+
Config *types.RoleConfig `json:"config" required:"true"`
5047
}
5148

5249
func (r *Role) APIRoleConfigPut() usecase.Interactor {
5350
u := usecase.NewInteractor(func(ctx context.Context, input APIRoleConfigInput, output *struct{}) error {
54-
jc, err := json.Marshal(input.Config)
51+
jc, err := proto.Marshal(input.Config)
5552
if err != nil {
5653
return status.Wrap(err, status.InvalidArgument)
5754
}

pkg/roles/dns/role_config_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55

66
"beryju.io/gravity/pkg/instance"
77
"beryju.io/gravity/pkg/roles/dns"
8+
"beryju.io/gravity/pkg/roles/dns/types"
89
"beryju.io/gravity/pkg/tests"
910
"github.com/stretchr/testify/assert"
1011
)
@@ -31,7 +32,7 @@ func TestAPIRoleConfigPut(t *testing.T) {
3132
defer role.Stop()
3233

3334
assert.NoError(t, role.APIRoleConfigPut().Interact(ctx, dns.APIRoleConfigInput{
34-
Config: dns.RoleConfig{
35+
Config: &types.RoleConfig{
3536
Port: 1054,
3637
},
3738
}, &struct{}{}))

pkg/roles/dns/role_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,13 @@ import (
55

66
"beryju.io/gravity/pkg/instance"
77
"beryju.io/gravity/pkg/roles/dns"
8+
"beryju.io/gravity/pkg/roles/dns/types"
89
"beryju.io/gravity/pkg/tests"
910
"github.com/stretchr/testify/assert"
1011
)
1112

1213
func RoleConfig() []byte {
13-
return []byte(tests.MustJSON(dns.RoleConfig{
14+
return []byte(tests.MustPB(&types.RoleConfig{
1415
Port: 1054,
1516
}))
1617
}

pkg/roles/dns/types/role_dns_config.pb.go

Lines changed: 143 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pkg/roles/dns/zone.go

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package dns
22

33
import (
44
"context"
5-
"encoding/json"
65
"fmt"
76
"strings"
87
"sync"
@@ -12,6 +11,7 @@ import (
1211
"beryju.io/gravity/pkg/roles/dns/types"
1312
"beryju.io/gravity/pkg/roles/dns/utils"
1413
tsdbTypes "beryju.io/gravity/pkg/roles/tsdb/types"
14+
"beryju.io/gravity/pkg/storage"
1515
"github.com/getsentry/sentry-go"
1616
"github.com/miekg/dns"
1717
"go.etcd.io/etcd/api/v3/mvccpb"
@@ -139,13 +139,7 @@ func (r *Role) zoneFromKV(raw *mvccpb.KeyValue) (*ZoneContext, error) {
139139
z.log = r.log.With(zap.String("zone", name))
140140
z.etcdKey = string(raw.Key)
141141

142-
// Try loading protobuf first
143-
err := proto.Unmarshal(raw.Value, z.Zone)
144-
if err == nil {
145-
return z, nil
146-
}
147-
// Otherwise try json
148-
err = json.Unmarshal(raw.Value, &z)
142+
_, err := storage.Parse(raw.Value, z.Zone)
149143
if err != nil {
150144
return nil, err
151145
}

pkg/storage/utils.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package storage
2+
3+
import (
4+
"encoding/json"
5+
6+
"google.golang.org/protobuf/proto"
7+
"google.golang.org/protobuf/reflect/protoreflect"
8+
)
9+
10+
// Parse Attempt to parse data as protobuf first, otherwise try json
11+
func Parse[T protoreflect.ProtoMessage](raw []byte, v T) (T, error) {
12+
// Try loading protobuf first
13+
err := proto.Unmarshal(raw, v)
14+
if err == nil {
15+
return v, nil
16+
}
17+
// Otherwise try json
18+
err = json.Unmarshal(raw, &v)
19+
if err == nil {
20+
return v, nil
21+
}
22+
return v, err
23+
}

protobuf/role_dns_config.proto

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
syntax = "proto3";
2+
3+
option go_package = "pkg/roles/dns/types";
4+
5+
message RoleConfig {
6+
int32 port = 1;
7+
}

0 commit comments

Comments
 (0)