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

add icmp-server cmd #221

Closed
wants to merge 7 commits into from
Closed
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 .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Dockerfile
31 changes: 31 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
FROM golang:alpine as build

WORKDIR /go/src/github.com/networkservicemesh/sdk

COPY go.mod go.sum ./
COPY ./pkg/imports/ ./pkg/imports/
RUN go build ./pkg/imports/

COPY . .
RUN go install -v github.com/networkservicemesh/sdk/cmd/icmp-server
RUN go install -v github.com/networkservicemesh/sdk/cmd/icmp-client
RUN go install -v github.com/networkservicemesh/sdk/cmd/healthcheck
RUN go install -v github.com/networkservicemesh/sdk/cmd/nsmgr
RUN go install -v github.com/networkservicemesh/sdk/cmd/registry

FROM alpine as runtime

RUN apk update
RUN apk add tmux

RUN mkdir -p /run/networkservicemesh/

COPY --from=build /go/bin/icmp-server /bin/icmp-server
COPY --from=build /go/bin/icmp-client /bin/icmp-client
COPY --from=build /go/bin/healthcheck /bin/healthcheck
COPY --from=build /go/bin/nsmgr /bin/nsmgr
COPY --from=build /go/bin/registry /bin/registry
COPY --from=build /go/src/github.com/networkservicemesh/sdk/run.sh /bin/run.sh
RUN chmod +x /bin/run.sh

CMD /bin/icmp-server
84 changes: 84 additions & 0 deletions cmd/healthcheck/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
package main

import (
"context"
"github.com/networkservicemesh/api/pkg/api/networkservice"
"github.com/networkservicemesh/sdk/pkg/tools/flags"
"github.com/networkservicemesh/sdk/pkg/tools/log"
"github.com/networkservicemesh/sdk/pkg/tools/spiffeutils"
"github.com/spf13/pflag"
"github.com/spiffe/go-spiffe/spiffe"
"google.golang.org/grpc"
healthgrpc "google.golang.org/grpc/health/grpc_health_v1"
healthpb "google.golang.org/grpc/health/grpc_health_v1"
"net/url"
"os"
"sync"
"time"
)

func main() {
ctx := context.Background()

flagSet := pflag.FlagSet{}
Flags(&flagSet)

populateFromEnv := flags.FromEnv(flags.EnvPrefix, flags.EnvReplacer, &flagSet)
populateFromEnv()

err := flagSet.Parse(os.Args)
if err != nil {
log.Entry(ctx).Fatalln(err)
}

tlsPeer, err := spiffeutils.NewTLSPeer(spiffe.WithWorkloadAPIAddr("unix:/run/spire/sockets/agent.sock"))

monitor, err := grpc.DialContext(ctx,"unix://" + ListenOnURL.String(),spiffeutils.WithSpiffe(tlsPeer,10 * time.Second), grpc.WithBlock())

hC := healthgrpc.NewHealthClient(monitor)

wg := &sync.WaitGroup{}
wg.Add(1)

go func() {
for {
resp, err := hC.Check(ctx, &healthpb.HealthCheckRequest{
Service: "connection.MonitorConnection",
})
if err != nil {
log.Entry(ctx).Fatalf("hC check failed", &hC, err)
}
log.Entry(ctx).Println("resp status", resp)

time.Sleep(5 * time.Second)
}
}()

monitorClient := networkservice.NewMonitorConnectionClient(monitor)

mCC, err := monitorClient.MonitorConnections(ctx, &networkservice.MonitorScopeSelector{
PathSegments: nil,
})
if err != nil {
log.Entry(ctx).Fatalf("mcc failed", &mCC, err)
}

go func() {
log.Entry(ctx).Println("attempting to monitor")
log.Entry(ctx).Println(mCC.Recv())
}()
wg.Wait()
}

var Name string
var BaseDir string
var ListenOnURL url.URL
var ConnectToURL url.URL

func Flags(f *pflag.FlagSet) {
// Standard NSM flags
f.StringVarP(&Name, flags.NameKey, flags.NameShortHand, "icmp-responder", flags.NameUsageDefault)
f.StringVarP(&BaseDir, flags.BaseDirKey, flags.BaseDirShortHand, flags.BaseDirDefault, flags.BaseDirUsageDefault)
flags.URLVarP(f, &ListenOnURL, flags.ListenOnURLKey, flags.ListenOnURLShortHand, &url.URL{Scheme: flags.ListenOnURLSchemeDefault, Path: flags.ListenOnURLPathDefault}, flags.ListenOnURLUsageDefault)
flags.URLVarP(f, &ConnectToURL, flags.ConnectToURLKey, flags.ConnectToURLShortHand, &url.URL{Scheme: flags.ConnectToURLSchemeDefault, Path: flags.ConnectToURLPathDefault}, flags.ConnectToURLUsageDefault)
}
154 changes: 154 additions & 0 deletions cmd/icmp-client/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,154 @@
package main

import (
"context"
"github.com/networkservicemesh/api/pkg/api/networkservice"
"github.com/networkservicemesh/sdk/pkg/networkservice/chains/client"
"github.com/networkservicemesh/sdk/pkg/tools/flags"
"github.com/spf13/pflag"
"github.com/spiffe/go-spiffe/spiffe"
"net/url"
"os"
"strings"
"time"

"google.golang.org/grpc"

"github.com/networkservicemesh/sdk/pkg/tools/log"
"github.com/networkservicemesh/sdk/pkg/tools/spiffeutils"
)

var Name string
var BaseDir string
var ListenOnURL url.URL
var ConnectToURL url.URL

var CidrPrefix string

func main() {
ctx := context.Background()

flagSet := pflag.FlagSet{}
Flags(&flagSet)

populateFromEnv := flags.FromEnv(flags.EnvPrefix, flags.EnvReplacer, &flagSet)
populateFromEnv()

err := flagSet.Parse(os.Args)
if err != nil {
log.Entry(ctx).Fatalln(err)
}

log.Entry(ctx).Printf("Args: %s", os.Args)
log.Entry(ctx).Printf("Name: %s", Name)
log.Entry(ctx).Printf("BaseDir: %s", BaseDir)
log.Entry(ctx).Printf("ListenOnURL: %s", ListenOnURL)
log.Entry(ctx).Printf("ConnectToURL: %s", ConnectToURL)
log.Entry(ctx).Printf("CIDR Prefix: %s", CidrPrefix)

log.Entry(ctx).Println()
for _, e := range os.Environ() {
pair := strings.SplitN(e, "=", 2)
log.Entry(ctx).Printf("ENV: %q", pair)
}

tlsPeer, err := spiffeutils.NewTLSPeer(spiffe.WithWorkloadAPIAddr("unix:/run/spire/sockets/agent.sock"))
if err != nil {
log.Entry(ctx).Fatalf("Error attempting to create spiffeutils.TLSPeer %+v", err)
}
tlsPeer.WaitUntilReady(ctx)
cert, err := tlsPeer.GetCertificate()
if err != nil {
log.Entry(ctx).Fatalf("Error attempting to create spiffeutils.TLSPeer %+v", err)
}
svid, err := spiffeutils.SpiffeIDFromTLS(cert)
log.Entry(ctx).Println("svid: ", svid)
log.Entry(ctx).Println("cert id: ", svid)

cc, err := grpc.DialContext(ctx, ConnectToURL.String(), spiffeutils.WithSpiffe(tlsPeer, 10*time.Second), grpc.WithBlock())
if err != nil {
log.Entry(ctx).Fatalf("failed to connect on %q: %+v", &ConnectToURL, err)
}

defer cc.Close()

//nsc := networkservice.NewNetworkServiceClient(cc)
nsc := client.NewClient(ctx, "", nil, spiffeutils.SpiffeJWTTokenGeneratorFunc(tlsPeer.GetCertificate, 10*time.Second), cc)

request := &networkservice.NetworkServiceRequest{
Connection: &networkservice.Connection{
NetworkService: "icmp-responder",
},
}

nsc.Request(ctx, request)
//request := &networkservice.NetworkServiceRequest{
// Connection: &networkservice.Connection{
// Id: "nsc",
// NetworkService: "icmp-responder",
// Path: &networkservice.Path{
// Index: 0,
// PathSegments: networkservice.PathSegment{
// Name: "",
// Id: "",
// Token: "",
// Expires: nil,
// Metrics: nil,
// XXX_NoUnkeyedLiteral: struct{}{},
// XXX_unrecognized: nil,
// XXX_sizecache: 0,
// },
// },
// },
//}
//conn, err := nsc.Request(ctx, request)
//if err != nil {
// log.Entry(ctx).Fatalln("unable to request network service:", err)
//}
//defer nsc.Close(ctx, conn)

if ctx.Err() != nil {
log.Entry(ctx).Warnf(ctx.Err().Error())
}
log.Entry(ctx).Warnf("complete!")
}

func Flags(f *pflag.FlagSet) {
// Standard NSM flags
f.StringVarP(&Name, flags.NameKey, flags.NameShortHand, "icmp-responder", flags.NameUsageDefault)
f.StringVarP(&BaseDir, flags.BaseDirKey, flags.BaseDirShortHand, flags.BaseDirDefault, flags.BaseDirUsageDefault)
flags.URLVarP(f, &ListenOnURL, flags.ListenOnURLKey, flags.ListenOnURLShortHand, &url.URL{Scheme: flags.ListenOnURLSchemeDefault, Path: flags.ListenOnURLPathDefault}, flags.ListenOnURLUsageDefault)
flags.URLVarP(f, &ConnectToURL, flags.ConnectToURLKey, flags.ConnectToURLShortHand, &url.URL{Scheme: flags.ConnectToURLSchemeDefault, Path: flags.ConnectToURLPathDefault}, flags.ConnectToURLUsageDefault)

// icmp-server specific flags
f.StringVarP(&CidrPrefix, "CIDR_PREFIX", "p", "169.254.0.0/16", "CIDR Prefix to assign IPs from")
}

// TODO Remove endpointServer, NewServer and Register when nsmgr is updated with timeout

//type endpointServer struct {
// networkservice.NetworkServiceServer
// networkservice.MonitorConnectionServer
//}

//// NewServer - returns a NetworkServiceMesh client as a chain of the standard Client pieces plus whatever
//// additional functionality is specified
//// - name - name of the NetworkServiceServer
//// - tokenGenerator - token.GeneratorFunc - generates tokens for use in Path
//// - additionalFunctionality - any additional NetworkServiceServer chain elements to be included in the chain
//func NewServer(name string, authzPolicy *rego.PreparedEvalQuery, tokenGenerator token.GeneratorFunc, additionalFunctionality ...networkservice.NetworkServiceServer) endpoint.Endpoint {
// rv := &endpointServer{}
// rv.NetworkServiceServer = chain.NewNetworkServiceServer(
// append([]networkservice.NetworkServiceServer{
// authorize.NewServer(authzPolicy),
// setid.NewServer(name),
// monitor.NewServer(&rv.MonitorConnectionServer),
// updatepath.NewServer(name, tokenGenerator),
// }, additionalFunctionality...)...)
// return rv
//}

//func (e *endpointServer) Register(s *grpc.Server) {
// networkservice.RegisterNetworkServiceServer(s, e)
// networkservice.RegisterMonitorConnectionServer(s, e)
//}
Loading