Skip to content

Commit fb7e794

Browse files
committed
nighthawk package init
Signed-off-by: kumarabd <[email protected]>
1 parent b81dc18 commit fb7e794

File tree

10 files changed

+4651
-6
lines changed

10 files changed

+4651
-6
lines changed

.github/workflows/build-and-publish.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ jobs:
3333
strategy:
3434
max-parallel: 10
3535
matrix:
36-
os: [ubuntu-latest]
36+
os: [ubuntu-latest, macos-latest]
3737
architecture: [amd64]
3838
distribution: [client, server, test_server, nighthawk_output_transform]
3939
runs-on: ${{ matrix.os }}

go.mod

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,13 @@ go 1.14
44

55
require (
66
fortio.org/fortio v1.6.8
7-
github.com/kr/text v0.2.0 // indirect
8-
github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e // indirect
7+
github.com/envoyproxy/go-control-plane v0.9.8
8+
github.com/envoyproxy/protoc-gen-validate v0.1.0
9+
github.com/golang/protobuf v1.4.2
10+
github.com/layer5io/meshkit v0.2.7
911
github.com/pkg/errors v0.9.1
10-
github.com/sirupsen/logrus v1.6.0
11-
github.com/stretchr/testify v1.6.1 // indirect
12-
gopkg.in/check.v1 v1.0.0-20200227125254-8fa46927fb4f // indirect
12+
github.com/sirupsen/logrus v1.7.0
13+
google.golang.org/genproto v0.0.0-20200804131852-c06518451d9c
14+
google.golang.org/grpc v1.31.0
15+
google.golang.org/protobuf v1.25.0
1316
)

go.sum

Lines changed: 820 additions & 0 deletions
Large diffs are not rendered by default.

pkg/client/error.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package nighthawk
2+
3+
import (
4+
"github.com/layer5io/meshkit/errors"
5+
)
6+
7+
const (
8+
ErrGRPCDialCode = "1000"
9+
ErrInvalidEndpointCode = "1001"
10+
)
11+
12+
var (
13+
ErrInvalidEndpoint = errors.NewDefault(ErrInvalidEndpointCode, "Endpoint not reachable")
14+
)
15+
16+
func ErrGRPCDial(err error) error {
17+
return errors.NewDefault(ErrGRPCDialCode, "Error creating nighthawk client", err.Error())
18+
}

pkg/client/nighthawk.go

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package nighthawk
2+
3+
import (
4+
"fmt"
5+
6+
"google.golang.org/grpc"
7+
8+
"github.com/layer5io/meshkit/utils"
9+
nighthawk_client "github.com/layer5io/nighthawk-go/pkg/proto"
10+
)
11+
12+
// Options argument for customizing the client
13+
type Options struct {
14+
ServerHost string
15+
ServerPort int32
16+
}
17+
18+
// Client holds the nighthawk client information
19+
type Client struct {
20+
Handler nighthawk_client.NighthawkServiceClient
21+
connection *grpc.ClientConn
22+
}
23+
24+
// New creates a new instance of the nighthawk client connection
25+
func New(opts Options) (*Client, error) {
26+
27+
if !utils.TcpCheck(&utils.HostPort{
28+
Address: opts.ServerHost,
29+
Port: opts.ServerPort,
30+
}, nil) {
31+
return nil, ErrInvalidEndpoint
32+
}
33+
34+
var dial_options []grpc.DialOption
35+
dial_options = append(dial_options, grpc.WithInsecure())
36+
37+
conn, err := grpc.Dial(fmt.Sprintf("%s:%d", opts.ServerHost, opts.ServerPort), dial_options...)
38+
if err != nil {
39+
return nil, ErrGRPCDial(err)
40+
}
41+
42+
return &Client{
43+
Handler: nighthawk_client.NewNighthawkServiceClient(conn),
44+
connection: conn,
45+
}, nil
46+
}
47+
48+
// Close closes the client connection
49+
func (c *Client) Close() error {
50+
if c.connection != nil {
51+
return c.connection.Close()
52+
}
53+
return nil
54+
}

pkg/client/nighthawk_test.go

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package nighthawk
2+
3+
import (
4+
"reflect"
5+
"testing"
6+
)
7+
8+
func TestNew(t *testing.T) {
9+
type args struct {
10+
opts Options
11+
}
12+
tests := []struct {
13+
name string
14+
args args
15+
want *Client
16+
wantErr bool
17+
}{
18+
// TODO: Add test cases.
19+
{
20+
name: "In case of blank URL",
21+
args: args{
22+
opts: Options{
23+
ServerHost: "",
24+
ServerPort: 0,
25+
},
26+
},
27+
want: nil,
28+
wantErr: true,
29+
},
30+
}
31+
for _, tt := range tests {
32+
t.Run(tt.name, func(t *testing.T) {
33+
got, err := New(tt.args.opts)
34+
if (err != nil) != tt.wantErr {
35+
t.Errorf("New() error = %v, wantErr %v", err, tt.wantErr)
36+
return
37+
}
38+
if !reflect.DeepEqual(got, tt.want) {
39+
t.Errorf("New() = %v, want %v", got, tt.want)
40+
}
41+
})
42+
}
43+
}

0 commit comments

Comments
 (0)