Skip to content
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

Protobuf #460

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -62,3 +62,4 @@ jobs:
uses: golangci/golangci-lint-action@v3
with:
args: --timeout 5000s
skip-cache: true
8 changes: 6 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,6 @@ run:
export INSTANCE_LISTEN=0.0.0.0
export DEBUG=true
export LISTEN_ONLY=true
export SENTRY_ENVIRONMENT=testing
export SENTRY_ENABLED=true
$(eval LD_FLAGS := -X beryju.io/gravity/pkg/extconfig.Version=${VERSION} -X beryju.io/gravity/pkg/extconfig.BuildHash=dev-$(shell git rev-parse HEAD))
go run ${GO_FLAGS} . server

Expand All @@ -43,6 +41,12 @@ gen-build:
DEBUG=true go run ${GO_FLAGS} . generateSchema ${SCHEMA_FILE}
git add ${SCHEMA_FILE}

gen-proto:
protoc \
--proto_path . \
--go_out . \
protobuf/**

gen-clean:
rm -rf gen-ts-api/

Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ require (
golang.org/x/net v0.10.0
golang.org/x/oauth2 v0.8.0
golang.org/x/term v0.8.0
google.golang.org/protobuf v1.30.0
)

require (
Expand Down Expand Up @@ -204,7 +205,6 @@ require (
google.golang.org/appengine v1.6.7 // indirect
google.golang.org/genproto v0.0.0-20221227171554-f9683d7f8bef // indirect
google.golang.org/grpc v1.52.3 // indirect
google.golang.org/protobuf v1.30.0 // indirect
gopkg.in/DataDog/dd-trace-go.v1 v1.47.0 // indirect
gopkg.in/cheggaaa/pb.v1 v1.0.28 // indirect
gopkg.in/inf.v0 v0.9.1 // indirect
Expand Down
16 changes: 8 additions & 8 deletions pkg/roles/discovery/api_devices_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ func TestDeviceApplyDHCPWithDNS(t *testing.T) {
dnsTypes.KeyZones,
"gravity.beryju.io.",
).String(),
tests.MustJSON(dns.Zone{}),
tests.MustJSON(dns.ZoneContext{}),
)
inst.KV().Put(
ctx,
Expand All @@ -150,7 +150,7 @@ func TestDeviceApplyDHCPWithDNS(t *testing.T) {
dnsTypes.KeyZones,
"0.192.in-addr.arpa.",
).String(),
tests.MustJSON(dns.Zone{}),
tests.MustJSON(dns.ZoneContext{}),
)

// Create DHCP role to register events
Expand Down Expand Up @@ -210,7 +210,7 @@ func TestDeviceApplyDHCPWithDNS(t *testing.T) {
"test",
"A",
),
dns.Record{
dnsTypes.Record{
Data: "192.0.2.1",
},
)
Expand All @@ -224,7 +224,7 @@ func TestDeviceApplyDHCPWithDNS(t *testing.T) {
"1.2",
"PTR",
),
dns.Record{
dnsTypes.Record{
Data: "test.gravity.beryju.io.",
},
)
Expand Down Expand Up @@ -257,7 +257,7 @@ func TestDeviceApplyDNSWithReverse(t *testing.T) {
dnsTypes.KeyZones,
"gravity.beryju.io.",
).String(),
tests.MustJSON(dns.Zone{}),
tests.MustJSON(dns.ZoneContext{}),
)
inst.KV().Put(
ctx,
Expand All @@ -266,7 +266,7 @@ func TestDeviceApplyDNSWithReverse(t *testing.T) {
dnsTypes.KeyZones,
"0.192.in-addr.arpa.",
).String(),
tests.MustJSON(dns.Zone{}),
tests.MustJSON(dns.ZoneContext{}),
)

// Create DNS role to register events
Expand All @@ -291,7 +291,7 @@ func TestDeviceApplyDNSWithReverse(t *testing.T) {
"test",
"A",
),
dns.Record{
dnsTypes.Record{
Data: "192.0.2.1",
},
)
Expand All @@ -305,7 +305,7 @@ func TestDeviceApplyDNSWithReverse(t *testing.T) {
"1.2",
"PTR",
),
dns.Record{
dnsTypes.Record{
Data: "test.gravity.beryju.io.",
},
)
Expand Down
16 changes: 8 additions & 8 deletions pkg/roles/dns/api_records.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,10 +73,10 @@ func (r *Role) APIRecordsGet() usecase.Interactor {
FQDN: rec.Name + "." + zone.Name,
Type: rec.Type,
Data: rec.Data,
MXPreference: rec.MXPreference,
SRVPort: rec.SRVPort,
SRVPriority: rec.SRVPriority,
SRVWeight: rec.SRVWeight,
MXPreference: uint16(rec.MxPreference),
SRVPort: uint16(rec.SrvPort),
SRVPriority: uint16(rec.SrvPriority),
SRVWeight: uint16(rec.SrvWeight),
})
}
return nil
Expand Down Expand Up @@ -122,10 +122,10 @@ func (r *Role) APIRecordsPut() usecase.Interactor {
input.Data = utils.EnsureTrailingPeriod(input.Data)
}
rec.Data = input.Data
rec.MXPreference = input.MXPreference
rec.SRVPort = input.SRVPort
rec.SRVPriority = input.SRVPriority
rec.SRVWeight = input.SRVWeight
rec.MxPreference = uint32(input.MXPreference)
rec.SrvPort = uint32(input.SRVPort)
rec.SrvPriority = uint32(input.SRVPriority)
rec.SrvWeight = uint32(input.SRVWeight)
err = rec.put(ctx, -1)
if err != nil {
return status.Wrap(err, status.Internal)
Expand Down
12 changes: 6 additions & 6 deletions pkg/roles/dns/api_records_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ func TestAPIRecordsGet(t *testing.T) {
types.KeyZones,
zone,
).String(),
tests.MustJSON(dns.Zone{}),
tests.MustPB(&types.Zone{}),
)
inst.KV().Put(
ctx,
Expand All @@ -35,7 +35,7 @@ func TestAPIRecordsGet(t *testing.T) {
"test",
"A",
).String(),
tests.MustJSON(dns.Record{
tests.MustPB(&types.Record{
Data: "192.0.2.1",
}),
)
Expand All @@ -61,7 +61,7 @@ func TestAPIRecordsPut(t *testing.T) {
types.KeyZones,
name,
).String(),
tests.MustJSON(dns.Zone{}),
tests.MustPB(&types.Zone{}),
)
assert.NoError(t, role.APIRecordsPut().Interact(ctx, dns.APIRecordsPutInput{
Zone: name,
Expand All @@ -80,7 +80,7 @@ func TestAPIRecordsPut(t *testing.T) {
"test",
"A",
),
dns.Record{
types.Record{
Data: "192.0.2.1",
},
)
Expand All @@ -100,7 +100,7 @@ func TestAPIRecordsDelete(t *testing.T) {
types.KeyZones,
zone,
).String(),
tests.MustJSON(dns.Zone{}),
tests.MustPB(&types.Zone{}),
)
inst.KV().Put(
ctx,
Expand All @@ -111,7 +111,7 @@ func TestAPIRecordsDelete(t *testing.T) {
"test",
"A",
).String(),
tests.MustJSON(dns.Record{
tests.MustPB(&types.Record{
Data: "192.0.2.1",
}),
)
Expand Down
22 changes: 20 additions & 2 deletions pkg/roles/dns/api_zones.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/swaggest/usecase/status"
clientv3 "go.etcd.io/etcd/client/v3"
"go.uber.org/zap"
"google.golang.org/protobuf/types/known/structpb"
)

type APIZonesGetInput struct {
Expand Down Expand Up @@ -54,11 +55,19 @@ func (r *Role) APIZonesGet() usecase.Interactor {
if strings.Contains(_zone.Name, "/") {
continue
}
handlers := []map[string]string{}
for _, h := range _zone.HandlerConfigs {
hc := map[string]string{}
for key, value := range h.Fields {
hc[key] = value.GetStringValue()
}
handlers = append(handlers, hc)
}
output.Zones = append(output.Zones, APIZone{
Name: _zone.Name,
Authoritative: _zone.Authoritative,
DefaultTTL: _zone.DefaultTTL,
HandlerConfigs: _zone.HandlerConfigs,
HandlerConfigs: handlers,
})
}
return nil
Expand All @@ -85,7 +94,16 @@ func (r *Role) APIZonesPut() usecase.Interactor {
z.Name += "."
}
z.Authoritative = input.Authoritative
z.HandlerConfigs = input.HandlerConfigs
z.HandlerConfigs = []*structpb.Struct{}
for _, h := range input.HandlerConfigs {
fields := make(map[string]*structpb.Value)
for key, value := range h {
fields[key] = structpb.NewStringValue(value)
}
z.HandlerConfigs = append(z.HandlerConfigs, &structpb.Struct{
Fields: fields,
})
}
z.DefaultTTL = input.DefaultTTL
err := z.put(ctx)
if err != nil {
Expand Down
13 changes: 8 additions & 5 deletions pkg/roles/dns/api_zones_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"beryju.io/gravity/pkg/roles/dns/types"
"beryju.io/gravity/pkg/tests"
"github.com/stretchr/testify/assert"
"google.golang.org/protobuf/types/known/structpb"
)

func TestAPIZonesGet(t *testing.T) {
Expand All @@ -24,7 +25,7 @@ func TestAPIZonesGet(t *testing.T) {
types.KeyZones,
"test.",
).String(),
tests.MustJSON(dns.Zone{}),
tests.MustPB(&types.Zone{}),
)

var output dns.APIZonesGetOutput
Expand Down Expand Up @@ -57,11 +58,13 @@ func TestAPIZonesPut(t *testing.T) {
types.KeyZones,
name,
),
dns.Zone{
types.Zone{
Authoritative: true,
HandlerConfigs: []map[string]string{
HandlerConfigs: []*structpb.Struct{
{
"type": "etcd",
Fields: map[string]*structpb.Value{
"type": structpb.NewStringValue("etcd"),
},
},
},
},
Expand All @@ -83,7 +86,7 @@ func TestAPIZonesDelete(t *testing.T) {
types.KeyZones,
name,
).String(),
tests.MustJSON(dns.Zone{}),
tests.MustPB(&types.Zone{}),
)

assert.NoError(t, role.APIZonesDelete().Interact(ctx, dns.APIZonesDeleteInput{
Expand Down
6 changes: 3 additions & 3 deletions pkg/roles/dns/dns_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ import (
)

// Find a zone for the given fqdn
func (r *Role) FindZone(fqdn string) *Zone {
func (r *Role) FindZone(fqdn string) *ZoneContext {
lastLongest := 0
var longestZone *Zone
var longestZone *ZoneContext
for name, zone := range r.zones {
// Zone doesn't have the correct suffix for the question
if !strings.HasSuffix(fqdn, name) {
Expand All @@ -30,7 +30,7 @@ func (r *Role) FindZone(fqdn string) *Zone {

func (ro *Role) Handler(w dns.ResponseWriter, r *dns.Msg) {
lastLongest := 0
var longestZone *Zone
var longestZone *ZoneContext

span := sentry.StartTransaction(
context.TODO(),
Expand Down
6 changes: 3 additions & 3 deletions pkg/roles/dns/event_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,14 @@ func (r *Role) eventCreateForward(ev *roles.Event) {
r.log.Warn("failed to parse address to add dns record", zap.Error(err))
return
}
var rec *Record
var rec *RecordContext
if ip.Is4() {
rec = forwardZone.newRecord(hostname, types.DNSRecordTypeA)
} else {
rec = forwardZone.newRecord(hostname, types.DNSRecordTypeA)
}
rec.Data = ip.String()
rec.TTL = forwardZone.DefaultTTL
rec.Ttl = forwardZone.DefaultTTL
err = rec.put(ev.Context, 0, ev.Payload.RelatedObjectOptions...)
if err != nil {
r.log.Warn("failed to save dns record", zap.Error(err))
Expand Down Expand Up @@ -74,7 +74,7 @@ func (r *Role) eventCreateReverse(ev *roles.Event) {
relName := strings.TrimSuffix(rev, utils.EnsureLeadingPeriod(forwardZone.Name))
rec := forwardZone.newRecord(relName, types.DNSRecordTypePTR)
rec.Data = fqdn
rec.TTL = forwardZone.DefaultTTL
rec.Ttl = forwardZone.DefaultTTL
err = rec.put(ev.Context, 0, ev.Payload.RelatedObjectOptions...)
if err != nil {
r.log.Warn("failed to save dns record", zap.Error(err))
Expand Down
7 changes: 4 additions & 3 deletions pkg/roles/dns/handler_coredns.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,25 +12,26 @@ import (
"github.com/miekg/dns"
_ "github.com/ori-edge/k8s_gateway"
"go.uber.org/zap"
"google.golang.org/protobuf/types/known/structpb"
)

const CoreDNSType = "coredns"

type CoreDNS struct {
c map[string]string
c map[string]*structpb.Value
log *zap.Logger
instance *caddy.Instance
srv *dnsserver.Server
}

func NewCoreDNS(z *Zone, rawConfig map[string]string) *CoreDNS {
func NewCoreDNS(z *ZoneContext, rawConfig map[string]*structpb.Value) *CoreDNS {
core := &CoreDNS{
c: rawConfig,
}
core.log = z.log.With(zap.String("handler", core.Identifier()))
dnsserver.Quiet = true
corefile := caddy.CaddyfileInput{
Contents: []byte(core.c["config"]),
Contents: []byte(core.c["config"].GetStringValue()),
Filepath: "in-memory",
ServerTypeName: "dns",
}
Expand Down
11 changes: 7 additions & 4 deletions pkg/roles/dns/handler_coredns_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"beryju.io/gravity/pkg/roles/dns/types"
"beryju.io/gravity/pkg/tests"
"github.com/stretchr/testify/assert"
"google.golang.org/protobuf/types/known/structpb"
)

const CoreDNSConfig = `.:1342 {
Expand All @@ -29,11 +30,13 @@ func TestRoleDNSHandlerCoreDNS(t *testing.T) {
types.KeyZones,
".",
).String(),
tests.MustJSON(dns.Zone{
HandlerConfigs: []map[string]string{
tests.MustPB(&types.Zone{
HandlerConfigs: []*structpb.Struct{
{
"type": "coredns",
"config": CoreDNSConfig,
Fields: map[string]*structpb.Value{
"type": structpb.NewStringValue("coredns"),
"config": structpb.NewStringValue(CoreDNSConfig),
},
},
},
}),
Expand Down
Loading