Skip to content

Commit

Permalink
Merge pull request #42 from NodeFactoryIo/dev
Browse files Browse the repository at this point in the history
Release 0.2.0
  • Loading branch information
mpetrunic authored Oct 21, 2020
2 parents ac2b197 + 3569181 commit c90869d
Show file tree
Hide file tree
Showing 16 changed files with 188 additions and 196 deletions.
4 changes: 1 addition & 3 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,8 @@ jobs:

- name: Build All
shell: bash
env:
SENTRY_DSN: ${{ secrets.SENTRY_DSN }}
run: |
SENTRY_DSN=$SENTRY_DSN make buildAll
make buildAll
- name: Get Changelog Entry
id: changelog_reader
Expand Down
2 changes: 1 addition & 1 deletion .version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
version=0.1.2
version=0.2.0
7 changes: 5 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
# Changelog

## [unreleased](https://github.com/NodeFactoryIo/vedran-daemon/tree/HEAD)
## [v0.2.0](https://github.com/NodeFactoryIo/vedran-daemon/tree/v0.2.0)

[Full Changelog](https://github.com/NodeFactoryIo/vedran-daemon/compare/v0.1.2...HEAD)
[Full Changelog](https://github.com/NodeFactoryIo/vedran-daemon/compare/v0.1.2...v0.2.0)

### Fixed

### Added
- Added banner on starting vedran-daemon [\#32](https://github.com/NodeFactoryIo/vedran-daemon/pull/32) ([mpetrun5](https://github.com/mpetrun5))
- Added integration with load balancer http tunnel [\#39](https://github.com/NodeFactoryIo/vedran-daemon/pull/39) ([mpetrun5](https://github.com/mpetrun5))

### Changed
- Remove sentry [\#35](https://github.com/NodeFactoryIo/vedran-daemon/pull/35) ([mpetrun5](https://github.com/mpetrun5))

## [v0.1.2](https://github.com/NodeFactoryIo/vedran-daemon/tree/v0.1.2)

Expand Down
7 changes: 6 additions & 1 deletion cmd/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/NodeFactoryIo/vedran-daemon/internal/node"
"github.com/NodeFactoryIo/vedran-daemon/internal/run"
"github.com/NodeFactoryIo/vedran-daemon/internal/telemetry"
"github.com/NodeFactoryIo/vedran-daemon/internal/tunnel"
"github.com/NodeFactoryIo/vedran-daemon/pkg/logger"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
Expand Down Expand Up @@ -85,15 +86,19 @@ func start(cmd *cobra.Command, _ []string) error {
lbClient := lb.NewClient(lbURL)
nodeClient := node.NewClient(rpcURL, metricsURL)
telemetry := telemetry.NewTelemetry()
tunnel := &tunnel.Tunnel{
NodeRPCURL: rpcURL,
}

err := run.Start(lbClient, nodeClient, telemetry, id, payoutAddress)
err := run.Start(tunnel, lbClient, nodeClient, telemetry, id, payoutAddress)
if err != nil {
return fmt.Errorf("Failed starting vedran daemon because: %v", err)
}

return nil
}

// Execute runs command
func Execute() {
if err := startCmd.Execute(); err != nil {
fmt.Println(err)
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module github.com/NodeFactoryIo/vedran-daemon
go 1.15

require (
github.com/getsentry/sentry-go v0.7.0
github.com/NodeFactoryIo/vedran v0.1.2-0.20201012103221-1832974f2e38
github.com/go-co-op/gocron v0.3.1
github.com/mitchellh/mapstructure v1.3.3
github.com/prometheus/common v0.13.0
Expand Down
201 changes: 72 additions & 129 deletions go.sum

Large diffs are not rendered by default.

30 changes: 0 additions & 30 deletions internal/config/config.go

This file was deleted.

21 changes: 10 additions & 11 deletions internal/lb/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,13 @@ const (
type RegisterRequest struct {
ID string `json:"id"`
ConfigHash string `json:"config_hash"`
NodeURL string `json:"node_url"`
PayoutAddress string `json:"payout_address"`
}

// TokenResponse from lb register endpoint
type TokenResponse struct {
Token string `json:"token"`
// RegisterResponse from lb register endpoint
type RegisterResponse struct {
Token string `json:"token"`
TunnelServerAddress string `json:"tunnel_server_address"`
}

// Client used to communicate with vedran load balancer
Expand All @@ -49,22 +49,21 @@ func NewClient(baseURL *url.URL) *Client {
}

// Register daemon with load balancer and store token in client
func (c *Client) Register(id string, nodeURL string, payoutAddress string, configHash string) error {
func (c *Client) Register(id string, payoutAddress string, configHash string) (*RegisterResponse, error) {
body := &RegisterRequest{
ID: id,
NodeURL: nodeURL,
PayoutAddress: payoutAddress,
ConfigHash: configHash,
}
req, _ := c.newRequest(http.MethodPost, registerEndpoint, body)
tokenResponse := new(TokenResponse)
_, err := c.do(req, tokenResponse)
registerResponse := new(RegisterResponse)
_, err := c.do(req, registerResponse)

if tokenResponse.Token != "" {
c.Token = tokenResponse.Token
if registerResponse.Token != "" {
c.Token = registerResponse.Token
}

return err
return registerResponse, err
}

// newRequest creates an API request. A relative URL should be provided in urlStr, which will be resolved to the
Expand Down
12 changes: 5 additions & 7 deletions internal/lb/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,6 @@ func TestClient_Register(t *testing.T) {
}
type args struct {
id string
nodeURL string
payoutAddress string
configHash string
}
Expand All @@ -86,7 +85,7 @@ func TestClient_Register(t *testing.T) {
}{
{
name: "Returns error if client does not return 200",
args: args{"test-id", "http://localhost:3000", "0xtestaddress", "config-hash"},
args: args{"test-id", "0xtestaddress", "config-hash"},
fields: fields{http.DefaultClient, "valid", ""},
wantErr: true,
want: "",
Expand All @@ -95,12 +94,12 @@ func TestClient_Register(t *testing.T) {
}},
{
name: "Sets token on client if request valid",
args: args{"test-id", "http://localhost:3000", "0xtestaddress", "config-hash"},
args: args{"test-id", "0xtestaddress", "config-hash"},
fields: fields{http.DefaultClient, "valid", ""},
wantErr: false,
want: "test-token",
handleFunc: func(w http.ResponseWriter, r *http.Request) {
_, _ = io.WriteString(w, `{"token": "test-token"}`)
_, _ = io.WriteString(w, `{"token": "test-token", "tunnel_url": "192.168.1.31:5223"}`)
}},
}
for _, tt := range tests {
Expand All @@ -120,16 +119,15 @@ func TestClient_Register(t *testing.T) {
}
mux.HandleFunc("/api/v1/nodes", tt.handleFunc)

err := c.Register(tt.args.id, tt.args.nodeURL, tt.args.payoutAddress, tt.args.configHash)
registerResponse, err := c.Register(tt.args.id, tt.args.payoutAddress, tt.args.configHash)

if (err != nil) != tt.wantErr {
t.Errorf("Client.Register() error = %v, wantErr %v", err, tt.wantErr)
}

if tt.want != c.Token {
if tt.want != c.Token || tt.want != registerResponse.Token {
t.Errorf("Client.Register() token = %s, want %s", c.Token, tt.want)
}

})

teardown()
Expand Down
3 changes: 0 additions & 3 deletions internal/lb/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"net/http"

"github.com/NodeFactoryIo/vedran-daemon/internal/node"
"github.com/getsentry/sentry-go"
log "github.com/sirupsen/logrus"
)

Expand All @@ -25,7 +24,6 @@ func (ms *metricsService) Send(client node.Client) (*http.Response, error) {
metrics, err := client.GetMetrics()
if err != nil {
log.Errorf("Failed sending metrics to load balancer because of: %v", err)
sentry.CaptureException(err)
return nil, err
}

Expand All @@ -35,7 +33,6 @@ func (ms *metricsService) Send(client node.Client) (*http.Response, error) {

if err != nil {
log.Errorf("Failed sending metrics to load balancer because of: %v", err)
sentry.CaptureException(err)
return nil, err
}

Expand Down
2 changes: 0 additions & 2 deletions internal/lb/ping.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"net/http"
"time"

"github.com/getsentry/sentry-go"
log "github.com/sirupsen/logrus"
)

Expand Down Expand Up @@ -36,7 +35,6 @@ func (ps *pingService) Send() (*http.Response, error) {

if err != nil {
log.Errorf("Failed sending ping to load balancer because of: %v", err)
sentry.CaptureException(err)
return nil, err
}

Expand Down
7 changes: 5 additions & 2 deletions internal/run/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,15 @@ import (
"github.com/NodeFactoryIo/vedran-daemon/internal/lb"
"github.com/NodeFactoryIo/vedran-daemon/internal/node"
"github.com/NodeFactoryIo/vedran-daemon/internal/telemetry"
"github.com/NodeFactoryIo/vedran-daemon/internal/tunnel"
"github.com/go-co-op/gocron"
log "github.com/sirupsen/logrus"
)

var sleep = time.Sleep

// Start registers to load balancer and starts sending telemetry
func Start(lbClient *lb.Client, nodeClient node.Client, telemetry telemetry.Telemetry, id string, payoutAddress string) error {
func Start(tunnel tunnel.Tunneler, lbClient *lb.Client, nodeClient node.Client, telemetry telemetry.Telemetry, id string, payoutAddress string) error {
var configHash hash.Hash32
for {
var err error
Expand All @@ -28,12 +29,14 @@ func Start(lbClient *lb.Client, nodeClient node.Client, telemetry telemetry.Tele
sleep(time.Second * 5)
}

err := lbClient.Register(id, nodeClient.GetRPCURL(), payoutAddress, base64.StdEncoding.EncodeToString(configHash.Sum(nil)[:]))
registerResponse, err := lbClient.Register(id, payoutAddress, base64.StdEncoding.EncodeToString(configHash.Sum(nil)[:]))
if err != nil {
return err
}
log.Infof("Registered to load balancer %s", lbClient.BaseURL.String())

go tunnel.StartTunnel(id, registerResponse.TunnelServerAddress, registerResponse.Token)

scheduler := gocron.NewScheduler(time.UTC)
telemetry.StartSendingTelemetry(scheduler, lbClient, nodeClient)
return nil
Expand Down
14 changes: 10 additions & 4 deletions internal/run/run_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"github.com/NodeFactoryIo/vedran-daemon/internal/lb"
nodeMocks "github.com/NodeFactoryIo/vedran-daemon/mocks/node"
telemetryMocks "github.com/NodeFactoryIo/vedran-daemon/mocks/telemetry"
tunnelMocks "github.com/NodeFactoryIo/vedran-daemon/mocks/tunnel"
"github.com/stretchr/testify/mock"
)

Expand Down Expand Up @@ -60,6 +61,7 @@ func TestStart(t *testing.T) {
firstGetConfigHashError error
secondGetConfigHashResult hash.Hash32
secondGetConfigHashError error
startTunnelCallCount int
}{
{
name: "Retries get config hash if get config hash fails and returns error if register fails",
Expand All @@ -71,16 +73,18 @@ func TestStart(t *testing.T) {
firstGetConfigHashError: fmt.Errorf("Error"),
firstGetConfigHashResult: nil,
secondGetConfigHashError: nil,
secondGetConfigHashResult: testHash},
secondGetConfigHashResult: testHash,
startTunnelCallCount: 0},
{
name: "Returns nil if startSendingTelemetry succeeds",
args: args{lbClient, "test-id", "0xtestpayoutaddress"},
wantErr: false,
handleFunc: func(w http.ResponseWriter, r *http.Request) {
_, _ = io.WriteString(w, `{"token": "test-token"}`)
_, _ = io.WriteString(w, `{"token": "test-token", "tunnel_url": "url:5223"}`)
},
firstGetConfigHashError: nil,
firstGetConfigHashResult: testHash},
firstGetConfigHashResult: testHash,
startTunnelCallCount: 1},
}

for _, tt := range tests {
Expand All @@ -89,6 +93,8 @@ func TestStart(t *testing.T) {

telemetryMock := &telemetryMocks.Telemetry{}
telemetryMock.On("StartSendingTelemetry", mock.Anything, mock.Anything, mock.Anything).Return()
tunnelMock := &tunnelMocks.Tunneler{}
tunnelMock.On("StartTunnel", mock.Anything, mock.Anything, mock.Anything, mock.Anything).Return()
nodeClient.On("GetRPCURL").Return("http://localhost:9933")
nodeClient.On("GetConfigHash").Once().Return(tt.firstGetConfigHashResult, tt.firstGetConfigHashError)
nodeClient.On("GetConfigHash").Once().Return(tt.secondGetConfigHashResult, tt.secondGetConfigHashError)
Expand All @@ -97,7 +103,7 @@ func TestStart(t *testing.T) {
lbClient.BaseURL = url
mux.HandleFunc("/api/v1/nodes", tt.handleFunc)

err := Start(tt.args.client, nodeClient, telemetryMock, tt.args.id, tt.args.payoutAddress)
err := Start(tunnelMock, tt.args.client, nodeClient, telemetryMock, tt.args.id, tt.args.payoutAddress)

if (err != nil) != tt.wantErr {
t.Errorf("Start() error = %v, wantErr %v", err, tt.wantErr)
Expand Down
1 change: 1 addition & 0 deletions internal/scheduler/scheduler.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package scheduler

import "github.com/go-co-op/gocron"

// Scheduler schedules go cron jobs
type Scheduler interface {
Every(interval uint64) *gocron.Scheduler
Do(jobFun interface{}, params ...interface{}) (*gocron.Job, error)
Expand Down
56 changes: 56 additions & 0 deletions internal/tunnel/tunnel.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package tunnel

import (
"net/url"
"time"

"github.com/NodeFactoryIo/vedran/pkg/http-tunnel/client"
log "github.com/sirupsen/logrus"
)

const (
DefaultBackoffInterval = 500 * time.Millisecond
DefaultBackoffMultiplier = 1.5
DefaultBackoffMaxInterval = 60 * time.Second
DefaultBackoffMaxTime = 15 * time.Minute
Protocol = "tcp"
RemoteAddr = "0.0.0.0:AUTO"
)

// Tunnel is tunnel connection with load balancer
type Tunnel struct {
NodeRPCURL *url.URL
}

// Tunneler defines methods for connecting to load balancer tunnel
type Tunneler interface {
// StartTunnel connects to load balancer tunnel port and creates connection.
// nodeID is id that is passed in daemon,
// tunnelServerAddress is public address of load balancer tunnel server and
// token is jwt token given when registering with load balancer.
StartTunnel(nodeID string, tunnelServerAddress string, token string)
}

func (t *Tunnel) StartTunnel(nodeID string, tunnelServerAddress string, token string) {
c, err := client.NewClient(&client.ClientConfig{
ServerAddress: tunnelServerAddress,
Tunnels: map[string]*client.Tunnel{
"default": {
Protocol: Protocol,
Addr: t.NodeRPCURL.Host,
RemoteAddr: RemoteAddr,
},
},
Logger: log.NewEntry(log.New()),
AuthToken: token,
IdName: nodeID,
})
if err != nil {
log.Fatal("Failed to connect to tunnel: ", err)
}

err = c.Start()
if err != nil {
log.Fatal("Failed to start tunnels: ", err)
}
}
Loading

0 comments on commit c90869d

Please sign in to comment.