Skip to content

Commit

Permalink
fully migrate DNS to proto
Browse files Browse the repository at this point in the history
  • Loading branch information
BeryJu committed May 31, 2023
1 parent cbaebfa commit 7f31332
Show file tree
Hide file tree
Showing 9 changed files with 192 additions and 32 deletions.
10 changes: 2 additions & 8 deletions pkg/roles/dns/record.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@ package dns

import (
"context"
"encoding/json"
"net"
"strings"

"beryju.io/gravity/pkg/roles"
"beryju.io/gravity/pkg/roles/dns/types"
"beryju.io/gravity/pkg/storage"
"github.com/miekg/dns"
"go.etcd.io/etcd/api/v3/mvccpb"
clientv3 "go.etcd.io/etcd/client/v3"
Expand Down Expand Up @@ -43,13 +43,7 @@ func (z *ZoneContext) recordFromKV(kv *mvccpb.KeyValue) (*RecordContext, error)
}
rec.recordKey = strings.TrimSuffix(fullRecordKey, "/"+rec.uid)

// Try loading protobuf first
err := proto.Unmarshal(kv.Value, rec.Record)
if err == nil {
return rec, nil
}
// Otherwise try json
err = json.Unmarshal(kv.Value, &rec)
_, err := storage.Parse(kv.Value, rec.Record)
if err != nil {
return rec, err
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/roles/dns/role.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ type Role struct {
ctx context.Context
zones map[string]*ZoneContext

cfg *RoleConfig
cfg *types.RoleConfig
log *zap.Logger
servers []*dns.Server
zonesM sync.RWMutex
Expand Down
23 changes: 10 additions & 13 deletions pkg/roles/dns/role_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,41 +2,38 @@ package dns

import (
"context"
"encoding/json"

instanceTypes "beryju.io/gravity/pkg/instance/types"
"beryju.io/gravity/pkg/storage"
"go.uber.org/zap"
"google.golang.org/protobuf/proto"

"beryju.io/gravity/pkg/roles/dns/types"
"github.com/swaggest/usecase"
"github.com/swaggest/usecase/status"
)

type RoleConfig struct {
Port int32 `json:"port"`
}

func (r *Role) decodeRoleConfig(raw []byte) *RoleConfig {
def := RoleConfig{
func (r *Role) decodeRoleConfig(raw []byte) *types.RoleConfig {
def := types.RoleConfig{
Port: 53,
}
if len(raw) < 1 {
return &def
}
err := json.Unmarshal(raw, &def)
conf, err := storage.Parse(raw, &def)
if err != nil {
r.log.Warn("failed to decode role config", zap.Error(err))
}
return &def
return conf
}

type APIRoleConfigOutput struct {
Config RoleConfig `json:"config" required:"true"`
Config *types.RoleConfig `json:"config" required:"true"`
}

func (r *Role) APIRoleConfigGet() usecase.Interactor {
u := usecase.NewInteractor(func(ctx context.Context, input struct{}, output *APIRoleConfigOutput) error {
output.Config = *r.cfg
output.Config = r.cfg
return nil
})
u.SetName("dns.get_role_config")
Expand All @@ -46,12 +43,12 @@ func (r *Role) APIRoleConfigGet() usecase.Interactor {
}

type APIRoleConfigInput struct {
Config RoleConfig `json:"config" required:"true"`
Config *types.RoleConfig `json:"config" required:"true"`
}

func (r *Role) APIRoleConfigPut() usecase.Interactor {
u := usecase.NewInteractor(func(ctx context.Context, input APIRoleConfigInput, output *struct{}) error {
jc, err := json.Marshal(input.Config)
jc, err := proto.Marshal(input.Config)
if err != nil {
return status.Wrap(err, status.InvalidArgument)
}
Expand Down
3 changes: 2 additions & 1 deletion pkg/roles/dns/role_config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (

"beryju.io/gravity/pkg/instance"
"beryju.io/gravity/pkg/roles/dns"
"beryju.io/gravity/pkg/roles/dns/types"
"beryju.io/gravity/pkg/tests"
"github.com/stretchr/testify/assert"
)
Expand All @@ -31,7 +32,7 @@ func TestAPIRoleConfigPut(t *testing.T) {
defer role.Stop()

assert.NoError(t, role.APIRoleConfigPut().Interact(ctx, dns.APIRoleConfigInput{
Config: dns.RoleConfig{
Config: &types.RoleConfig{
Port: 1054,
},
}, &struct{}{}))
Expand Down
3 changes: 2 additions & 1 deletion pkg/roles/dns/role_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,13 @@ import (

"beryju.io/gravity/pkg/instance"
"beryju.io/gravity/pkg/roles/dns"
"beryju.io/gravity/pkg/roles/dns/types"
"beryju.io/gravity/pkg/tests"
"github.com/stretchr/testify/assert"
)

func RoleConfig() []byte {
return []byte(tests.MustJSON(dns.RoleConfig{
return []byte(tests.MustPB(&types.RoleConfig{
Port: 1054,
}))
}
Expand Down
143 changes: 143 additions & 0 deletions pkg/roles/dns/types/role_dns_config.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 2 additions & 8 deletions pkg/roles/dns/zone.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package dns

import (
"context"
"encoding/json"
"fmt"
"strings"
"sync"
Expand All @@ -12,6 +11,7 @@ import (
"beryju.io/gravity/pkg/roles/dns/types"
"beryju.io/gravity/pkg/roles/dns/utils"
tsdbTypes "beryju.io/gravity/pkg/roles/tsdb/types"
"beryju.io/gravity/pkg/storage"
"github.com/getsentry/sentry-go"
"github.com/miekg/dns"
"go.etcd.io/etcd/api/v3/mvccpb"
Expand Down Expand Up @@ -139,13 +139,7 @@ func (r *Role) zoneFromKV(raw *mvccpb.KeyValue) (*ZoneContext, error) {
z.log = r.log.With(zap.String("zone", name))
z.etcdKey = string(raw.Key)

// Try loading protobuf first
err := proto.Unmarshal(raw.Value, z.Zone)
if err == nil {
return z, nil
}
// Otherwise try json
err = json.Unmarshal(raw.Value, &z)
_, err := storage.Parse(raw.Value, z.Zone)
if err != nil {
return nil, err
}
Expand Down
23 changes: 23 additions & 0 deletions pkg/storage/utils.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package storage

import (
"encoding/json"

"google.golang.org/protobuf/proto"
"google.golang.org/protobuf/reflect/protoreflect"
)

// Parse Attempt to parse data as protobuf first, otherwise try json
func Parse[T protoreflect.ProtoMessage](raw []byte, v T) (T, error) {
// Try loading protobuf first
err := proto.Unmarshal(raw, v)
if err == nil {
return v, nil
}
// Otherwise try json
err = json.Unmarshal(raw, &v)
if err == nil {
return v, nil
}
return v, err
}
7 changes: 7 additions & 0 deletions protobuf/role_dns_config.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
syntax = "proto3";

option go_package = "pkg/roles/dns/types";

message RoleConfig {
int32 port = 1;
}

0 comments on commit 7f31332

Please sign in to comment.