Skip to content

Commit

Permalink
Merge pull request skydive-project#2297 from lebauce/graffiti-node-ed…
Browse files Browse the repository at this point in the history
…ge-api

graffiti: import node and edge APIs
  • Loading branch information
safchain authored Dec 14, 2020
2 parents 4e8043e + e2507e6 commit c73aee7
Show file tree
Hide file tree
Showing 44 changed files with 1,316 additions and 1,114 deletions.
2 changes: 1 addition & 1 deletion .mk/check.mk
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ check: lint
echo "===> You should comment you code <==="; \
exit 1; \
fi
unwanteddeps=$$(git grep --text github.com/skydive-project/skydive graffiti | grep -v github.com/skydive-project/skydive/graffiti | grep -v graffiti/gendecoder/main.go); \
unwanteddeps=$$(git grep --text github.com/skydive-project/skydive graffiti | grep -v github.com/skydive-project/skydive/graffiti | grep -v graffiti/gendecoder/main.go | grep -v graffiti/README.md); \
test -z "$$unwanteddeps" || \
(echo -e "Graffiti should not import Skydive only packages:\n$$unwanteddeps" && /bin/false)

Expand Down
2 changes: 1 addition & 1 deletion .mk/swagger.mk
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ SWAGGER_JSON_FILES = $(patsubst %.go,%_swagger.json,$(shell git grep //go:genera
.PHONY: swagger
swagger: $(SWAGGER_JSON_FILES)
go run github.com/go-swagger/go-swagger/cmd/swagger generate spec -m -o /tmp/swagger.json
for def in `ls api/server/*_swagger.json`; do \
for def in `ls api/server/*_swagger.json` `ls graffiti/api/server/*_swagger.json`; do \
jq -s '.[0] * .[1] * {tags: (.[0].tags + .[1].tags)}' /tmp/swagger.json $$def > swagger.json; \
cp swagger.json /tmp; \
done
Expand Down
5 changes: 3 additions & 2 deletions analyzer/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ import (
"github.com/skydive-project/skydive/flow/server"
"github.com/skydive-project/skydive/flow/storage"
"github.com/skydive-project/skydive/graffiti/api/rest"
gapi "github.com/skydive-project/skydive/graffiti/api/server"
etcdclient "github.com/skydive-project/skydive/graffiti/etcd/client"
etcdserver "github.com/skydive-project/skydive/graffiti/etcd/server"
"github.com/skydive-project/skydive/graffiti/graph"
Expand Down Expand Up @@ -333,7 +334,7 @@ func NewServerFromConfig() (*Server, error) {

s.piClient = packetinjector.NewOnDemandInjectionClient(g, piAPIHandler, hub.PodServer(), hub.SubscriberServer(), etcdClient)

_, err = api.RegisterNodeAPI(apiServer, g, apiAuthBackend)
_, err = gapi.RegisterNodeAPI(apiServer, g, apiAuthBackend)
if err != nil {
return nil, err
}
Expand All @@ -343,7 +344,7 @@ func NewServerFromConfig() (*Server, error) {
return nil, err
}

_, err = api.RegisterEdgeAPI(apiServer, g, apiAuthBackend)
_, err = gapi.RegisterEdgeAPI(apiServer, g, apiAuthBackend)
if err != nil {
return nil, err
}
Expand Down
6 changes: 5 additions & 1 deletion api/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,5 +46,9 @@ func NewRestClientFromConfig(authOptions *shttp.AuthenticationOpts) (*shttp.Rest
return nil, err
}

return shttp.NewRestClient(config.GetURL("http", sa.Addr, sa.Port, "/api/"), authOptions, tlsConfig), nil
url, err := config.GetURL("http", sa.Addr, sa.Port, "/api/")
if err != nil {
return nil, err
}
return shttp.NewRestClient(url, authOptions, tlsConfig), nil
}
147 changes: 6 additions & 141 deletions api/client/gremlin.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,159 +18,22 @@
package client

import (
"bytes"
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"net/http"

"github.com/skydive-project/skydive/api/types"
"github.com/skydive-project/skydive/flow"
"github.com/skydive-project/skydive/graffiti/graph"
gclient "github.com/skydive-project/skydive/graffiti/api/client"
shttp "github.com/skydive-project/skydive/graffiti/http"
"github.com/skydive-project/skydive/gremlin"
"github.com/skydive-project/skydive/sflow"
"github.com/skydive-project/skydive/topology"
"github.com/skydive-project/skydive/topology/probes/socketinfo"
)

// ErrNoResult is returned when a query returned no result
var ErrNoResult = errors.New("no result")
var ErrNoResult = gclient.ErrNoResult

// GremlinQueryHelper describes a gremlin query request query helper mechanism
type GremlinQueryHelper struct {
restClient *shttp.RestClient
authOptions *shttp.AuthenticationOpts
}

// Request send a Gremlin request to the topology API
func (g *GremlinQueryHelper) Request(query interface{}, header http.Header) (*http.Response, error) {
gq := types.TopologyParams{GremlinQuery: gremlin.NewQueryStringFromArgument(query).String()}
s, err := json.Marshal(gq)
if err != nil {
return nil, err
}

contentReader := bytes.NewReader(s)

return g.restClient.Request("POST", "topology", contentReader, header)
}

// Query queries the topology API
func (g *GremlinQueryHelper) Query(query interface{}) ([]byte, error) {
resp, err := g.Request(query, nil)
if err != nil {
return nil, err
}
defer resp.Body.Close()

data, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}
if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("%s: %s", resp.Status, string(data))
}

return data, nil
}

// GetInt64 parse the query result as int64
func (g *GremlinQueryHelper) GetInt64(query interface{}) (int64, error) {
data, err := g.Query(query)
if err != nil {
return 0, err
}

var i int64
if err := json.Unmarshal(data, &i); err != nil {
return 0, err
}

return i, nil
}

// GetNodes from the Gremlin query
func (g *GremlinQueryHelper) GetNodes(query interface{}) ([]*graph.Node, error) {
data, err := g.Query(query)
if err != nil {
return nil, err
}

var result []json.RawMessage
if err := json.Unmarshal(data, &result); err != nil {
return nil, err
}

var nodes []*graph.Node
for _, obj := range result {
// hacky stuff to know how to decode
switch obj[0] {
case '[':
var n []*graph.Node
if err := json.Unmarshal(obj, &n); err != nil {
return nil, err
}
nodes = append(nodes, n...)
case '{':
var n graph.Node
if err := json.Unmarshal(obj, &n); err != nil {
return nil, err
}
nodes = append(nodes, &n)
}
}

return nodes, nil
}

// GetNode from the Gremlin query
func (g *GremlinQueryHelper) GetNode(query interface{}) (node *graph.Node, _ error) {
nodes, err := g.GetNodes(query)
if err != nil {
return nil, err
}

if len(nodes) > 0 {
return nodes[0], nil
}

return nil, ErrNoResult
}

// GetEdges from the Gremlin query
func (g *GremlinQueryHelper) GetEdges(query interface{}) ([]*graph.Edge, error) {
data, err := g.Query(query)
if err != nil {
return nil, err
}

var result []json.RawMessage
if err := json.Unmarshal(data, &result); err != nil {
return nil, err
}

var edges []*graph.Edge
for _, obj := range result {
// hacky stuff to know how to decode
switch obj[0] {
case '[':
var e []*graph.Edge
if err := json.Unmarshal(obj, &e); err != nil {
return nil, err
}
edges = append(edges, e...)
case '{':
var e graph.Edge
if err := json.Unmarshal(obj, &e); err != nil {
return nil, err
}
edges = append(edges, &e)
}
}

return edges, nil
*gclient.GremlinQueryHelper
}

// GetFlows from the Gremlin query
Expand Down Expand Up @@ -320,7 +183,9 @@ func (g *GremlinQueryHelper) GetSockets(query interface{}) (sockets map[string][

// NewGremlinQueryHelper creates a new Gremlin query helper based on authentication
func NewGremlinQueryHelper(restClient *shttp.RestClient) *GremlinQueryHelper {
return &GremlinQueryHelper{restClient: restClient}
return &GremlinQueryHelper{
GremlinQueryHelper: gclient.NewGremlinQueryHelper(restClient),
}
}

// NewGremlinQueryHelperFromConfig creates a new Gremlin query helper based on authentication based on configuration file
Expand Down
2 changes: 1 addition & 1 deletion api/server/workflow_runtime.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ import (
"github.com/skydive-project/skydive/js"

"github.com/robertkrimen/otto"
"github.com/skydive-project/skydive/api/types"
api "github.com/skydive-project/skydive/graffiti/api/server"
"github.com/skydive-project/skydive/graffiti/api/types"
"github.com/skydive-project/skydive/graffiti/graph"
"github.com/skydive-project/skydive/graffiti/graph/traversal"
)
Expand Down
54 changes: 3 additions & 51 deletions api/types/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (
"time"

"github.com/skydive-project/skydive/graffiti/api/rest"
gtypes "github.com/skydive-project/skydive/graffiti/api/types"
"github.com/skydive-project/skydive/graffiti/graph"
"github.com/skydive-project/skydive/graffiti/schema"
)
Expand Down Expand Up @@ -134,29 +135,8 @@ func NewCapture(query string, bpfFilter string) *Capture {
}

// Edge object
// easyjson:json
// swagger:model
type Edge graph.Edge

// GetID returns the edge ID
func (e *Edge) GetID() string {
return string(e.ID)
}

// SetID sets the node ID
func (e *Edge) SetID(i string) {
e.ID = graph.Identifier(i)
}

// GetName returns the edge resource name
func (e *Edge) GetName() string {
return "Edge"
}

// Validate integrity of the resource
func (e *Edge) Validate() error {
return nil
}
type Edge = gtypes.Edge

// EdgeRule object
//
Expand Down Expand Up @@ -193,29 +173,8 @@ func (e *EdgeRule) Validate() error {
}

// Node object
// easyjson:json
// swagger:model
type Node graph.Node

// GetID returns the node ID
func (n *Node) GetID() string {
return string(n.ID)
}

// SetID sets the resource ID
func (n *Node) SetID(i string) {
n.ID = graph.Identifier(i)
}

// GetName returns the node resource name
func (n *Node) GetName() string {
return "Node"
}

// Validate integrity of the resource
func (n *Node) Validate() error {
return nil
}
type Node = gtypes.Node

// NodeRule object
//
Expand Down Expand Up @@ -330,13 +289,6 @@ func (pi *PacketInjection) Validate() error {
return nil
}

// TopologyParams topology query parameters
// easyjson:json
// swagger:model
type TopologyParams struct {
GremlinQuery string `json:"GremlinQuery,omitempty" valid:"isGremlinExpr" yaml:"GremlinQuery"`
}

// WorkflowChoice describes one value within a choice
// easyjson:json
// swagger:model
Expand Down
Loading

0 comments on commit c73aee7

Please sign in to comment.