Skip to content

Commit

Permalink
common: use more precise errors and move them to their respective pac…
Browse files Browse the repository at this point in the history
…kage
  • Loading branch information
lebauce committed Jul 8, 2020
1 parent 26b9ab9 commit 2ece159
Show file tree
Hide file tree
Showing 42 changed files with 115 additions and 128 deletions.
13 changes: 6 additions & 7 deletions analyzer/server.go
Expand Up @@ -296,7 +296,6 @@ func NewServerFromConfig() (*Server, error) {
StatusReporter: s,
TLSConfig: tlsConfig,
Peers: peers,
EtcdKeysAPI: etcdClient.KeysAPI,
TopologyMarshallers: api.TopologyMarshallers,
}

Expand Down Expand Up @@ -339,13 +338,13 @@ func NewServerFromConfig() (*Server, error) {
flowSubscriberWSServer := ws.NewStructServer(config.NewWSServer(hub.HTTPServer(), "/ws/subscriber/flow", apiAuthBackend))
flowSubscriberEndpoint := server.NewFlowSubscriberEndpoint(flowSubscriberWSServer)

captureAPIHandler, err := api.RegisterCaptureAPI(hub.APIServer(), g, apiAuthBackend)
captureAPIHandler, err := api.RegisterCaptureAPI(hub.APIServer(), etcdClient.KeysAPI, g, apiAuthBackend)
if err != nil {
return nil, err
}

apiServer := hub.APIServer()
piAPIHandler, err := api.RegisterPacketInjectorAPI(g, apiServer, apiAuthBackend)
piAPIHandler, err := api.RegisterPacketInjectorAPI(g, apiServer, etcdClient.KeysAPI, apiAuthBackend)
if err != nil {
return nil, err
}
Expand All @@ -357,7 +356,7 @@ func NewServerFromConfig() (*Server, error) {
return nil, err
}

nodeRuleAPIHandler, err := api.RegisterNodeRuleAPI(apiServer, g, apiAuthBackend)
nodeRuleAPIHandler, err := api.RegisterNodeRuleAPI(apiServer, etcdClient.KeysAPI, g, apiAuthBackend)
if err != nil {
return nil, err
}
Expand All @@ -367,18 +366,18 @@ func NewServerFromConfig() (*Server, error) {
return nil, err
}

edgeRuleAPIHandler, err := api.RegisterEdgeRuleAPI(apiServer, g, apiAuthBackend)
edgeRuleAPIHandler, err := api.RegisterEdgeRuleAPI(apiServer, etcdClient.KeysAPI, g, apiAuthBackend)
if err != nil {
return nil, err
}

s.topologyManager = usertopology.NewTopologyManager(etcdClient, nodeRuleAPIHandler, edgeRuleAPIHandler, g)

if _, err = api.RegisterAlertAPI(apiServer, apiAuthBackend); err != nil {
if _, err = api.RegisterAlertAPI(apiServer, etcdClient.KeysAPI, apiAuthBackend); err != nil {
return nil, err
}

if _, err := api.RegisterWorkflowAPI(apiServer, apiAuthBackend); err != nil {
if _, err := api.RegisterWorkflowAPI(apiServer, etcdClient.KeysAPI, apiAuthBackend); err != nil {
return nil, err
}

Expand Down
9 changes: 6 additions & 3 deletions api/client/gremlin.go
Expand Up @@ -20,12 +20,12 @@ package client
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"net/http"

"github.com/skydive-project/skydive/api/types"
"github.com/skydive-project/skydive/common"
"github.com/skydive-project/skydive/flow"
"github.com/skydive-project/skydive/graffiti/graph"
shttp "github.com/skydive-project/skydive/graffiti/http"
Expand All @@ -35,6 +35,9 @@ import (
"github.com/skydive-project/skydive/topology/probes/socketinfo"
)

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

// GremlinQueryHelper describes a gremlin query request query helper mechanism
type GremlinQueryHelper struct {
authOptions *shttp.AuthenticationOpts
Expand Down Expand Up @@ -137,7 +140,7 @@ func (g *GremlinQueryHelper) GetNode(query interface{}) (node *graph.Node, _ err
return nodes[0], nil
}

return nil, common.ErrNotFound
return nil, ErrNoResult
}

// GetFlows from the Gremlin query
Expand Down Expand Up @@ -206,7 +209,7 @@ func (g *GremlinQueryHelper) GetFlowMetrics(query interface{}) (map[string][]*fl
}

if len(result) == 0 {
return nil, common.ErrNotFound
return nil, ErrNoResult
}

return result[0], nil
Expand Down
5 changes: 3 additions & 2 deletions api/server/alert.go
Expand Up @@ -23,6 +23,7 @@ package server
import (
"time"

etcd "github.com/coreos/etcd/client"
"github.com/skydive-project/skydive/api/types"
"github.com/skydive-project/skydive/graffiti/api/rest"
api "github.com/skydive-project/skydive/graffiti/api/server"
Expand Down Expand Up @@ -52,11 +53,11 @@ func (a *AlertResourceHandler) Name() string {
}

// RegisterAlertAPI registers an Alert's API to a designated API Server
func RegisterAlertAPI(apiServer *api.Server, authBackend shttp.AuthenticationBackend) (*AlertAPIHandler, error) {
func RegisterAlertAPI(apiServer *api.Server, kapi etcd.KeysAPI, authBackend shttp.AuthenticationBackend) (*AlertAPIHandler, error) {
alertAPIHandler := &AlertAPIHandler{
BasicAPIHandler: rest.BasicAPIHandler{
ResourceHandler: &AlertResourceHandler{},
EtcdKeyAPI: apiServer.EtcdKeyAPI,
EtcdKeyAPI: kapi,
},
}
if err := apiServer.RegisterAPIHandler(alertAPIHandler, authBackend); err != nil {
Expand Down
5 changes: 3 additions & 2 deletions api/server/capture.go
Expand Up @@ -23,6 +23,7 @@ package server
import (
"fmt"

etcd "github.com/coreos/etcd/client"
"github.com/skydive-project/skydive/api/types"
"github.com/skydive-project/skydive/flow"
"github.com/skydive-project/skydive/flow/probes"
Expand Down Expand Up @@ -146,11 +147,11 @@ func (c *CaptureAPIHandler) Create(r rest.Resource, opts *rest.CreateOptions) er
}

// RegisterCaptureAPI registers an new resource, capture
func RegisterCaptureAPI(apiServer *api.Server, g *graph.Graph, authBackend shttp.AuthenticationBackend) (*CaptureAPIHandler, error) {
func RegisterCaptureAPI(apiServer *api.Server, kapi etcd.KeysAPI, g *graph.Graph, authBackend shttp.AuthenticationBackend) (*CaptureAPIHandler, error) {
captureAPIHandler := &CaptureAPIHandler{
BasicAPIHandler: rest.BasicAPIHandler{
ResourceHandler: &CaptureResourceHandler{},
EtcdKeyAPI: apiServer.EtcdKeyAPI,
EtcdKeyAPI: kapi,
},
Graph: g,
}
Expand Down
3 changes: 1 addition & 2 deletions api/server/edge.go
Expand Up @@ -24,7 +24,6 @@ import (
"time"

"github.com/skydive-project/skydive/api/types"
"github.com/skydive-project/skydive/common"
"github.com/skydive-project/skydive/graffiti/api/rest"
api "github.com/skydive-project/skydive/graffiti/api/server"
"github.com/skydive-project/skydive/graffiti/graph"
Expand Down Expand Up @@ -110,7 +109,7 @@ func (h *EdgeAPIHandler) Delete(id string) error {

edge := h.g.GetEdge(graph.Identifier(id))
if edge == nil {
return common.ErrNotFound
return rest.ErrNotFound
}

return h.g.DelEdge(edge)
Expand Down
5 changes: 3 additions & 2 deletions api/server/edgerule.go
Expand Up @@ -21,6 +21,7 @@
package server

import (
etcd "github.com/coreos/etcd/client"
"github.com/skydive-project/skydive/api/types"
"github.com/skydive-project/skydive/graffiti/api/rest"
api "github.com/skydive-project/skydive/graffiti/api/server"
Expand Down Expand Up @@ -50,11 +51,11 @@ func (erh *EdgeRuleResourceHandler) New() rest.Resource {
}

// RegisterEdgeRuleAPI registers an EdgeRule's API to a designated API Server
func RegisterEdgeRuleAPI(apiServer *api.Server, g *graph.Graph, authBackend shttp.AuthenticationBackend) (*EdgeRuleAPI, error) {
func RegisterEdgeRuleAPI(apiServer *api.Server, kapi etcd.KeysAPI, g *graph.Graph, authBackend shttp.AuthenticationBackend) (*EdgeRuleAPI, error) {
era := &EdgeRuleAPI{
BasicAPIHandler: rest.BasicAPIHandler{
ResourceHandler: &EdgeRuleResourceHandler{},
EtcdKeyAPI: apiServer.EtcdKeyAPI,
EtcdKeyAPI: kapi,
},
Graph: g,
}
Expand Down
3 changes: 1 addition & 2 deletions api/server/node.go
Expand Up @@ -24,7 +24,6 @@ import (
"time"

"github.com/skydive-project/skydive/api/types"
"github.com/skydive-project/skydive/common"
"github.com/skydive-project/skydive/graffiti/api/rest"
api "github.com/skydive-project/skydive/graffiti/api/server"
"github.com/skydive-project/skydive/graffiti/graph"
Expand Down Expand Up @@ -110,7 +109,7 @@ func (h *NodeAPIHandler) Delete(id string) error {

node := h.g.GetNode(graph.Identifier(id))
if node == nil {
return common.ErrNotFound
return rest.ErrNotFound
}

return h.g.DelNode(node)
Expand Down
5 changes: 3 additions & 2 deletions api/server/noderule.go
Expand Up @@ -21,6 +21,7 @@
package server

import (
etcd "github.com/coreos/etcd/client"
"github.com/skydive-project/skydive/api/types"
"github.com/skydive-project/skydive/graffiti/api/rest"
api "github.com/skydive-project/skydive/graffiti/api/server"
Expand Down Expand Up @@ -50,11 +51,11 @@ func (nrh *NodeRuleResourceHandler) New() rest.Resource {
}

// RegisterNodeRuleAPI register a new node rule api handler
func RegisterNodeRuleAPI(apiServer *api.Server, g *graph.Graph, authBackend shttp.AuthenticationBackend) (*NodeRuleAPI, error) {
func RegisterNodeRuleAPI(apiServer *api.Server, kapi etcd.KeysAPI, g *graph.Graph, authBackend shttp.AuthenticationBackend) (*NodeRuleAPI, error) {
nra := &NodeRuleAPI{
BasicAPIHandler: rest.BasicAPIHandler{
ResourceHandler: &NodeRuleResourceHandler{},
EtcdKeyAPI: apiServer.EtcdKeyAPI,
EtcdKeyAPI: kapi,
},
Graph: g,
}
Expand Down
5 changes: 3 additions & 2 deletions api/server/packet_injector_common.go
Expand Up @@ -21,6 +21,7 @@
package server

import (
etcd "github.com/coreos/etcd/client"
"github.com/skydive-project/skydive/api/types"
"github.com/skydive-project/skydive/graffiti/api/rest"
api "github.com/skydive-project/skydive/graffiti/api/server"
Expand Down Expand Up @@ -76,11 +77,11 @@ func (pi *PacketInjectorAPI) getNode(gremlinQuery string) *graph.Node {
}

// RegisterPacketInjectorAPI registers a new packet injector resource in the API
func RegisterPacketInjectorAPI(g *graph.Graph, apiServer *api.Server, authBackend shttp.AuthenticationBackend) (*PacketInjectorAPI, error) {
func RegisterPacketInjectorAPI(g *graph.Graph, apiServer *api.Server, kapi etcd.KeysAPI, authBackend shttp.AuthenticationBackend) (*PacketInjectorAPI, error) {
pia := &PacketInjectorAPI{
BasicAPIHandler: rest.BasicAPIHandler{
ResourceHandler: &packetInjectorResourceHandler{},
EtcdKeyAPI: apiServer.EtcdKeyAPI,
EtcdKeyAPI: kapi,
},
Graph: g,
}
Expand Down
5 changes: 3 additions & 2 deletions api/server/workflow.go
Expand Up @@ -25,6 +25,7 @@ import (

yaml "gopkg.in/yaml.v2"

etcd "github.com/coreos/etcd/client"
"github.com/skydive-project/skydive/api/types"
"github.com/skydive-project/skydive/graffiti/api/rest"
api "github.com/skydive-project/skydive/graffiti/api/server"
Expand Down Expand Up @@ -110,11 +111,11 @@ func (w *WorkflowAPIHandler) Index() map[string]rest.Resource {
}

// RegisterWorkflowAPI registers a new workflow api handler
func RegisterWorkflowAPI(apiServer *api.Server, authBackend shttp.AuthenticationBackend) (*WorkflowAPIHandler, error) {
func RegisterWorkflowAPI(apiServer *api.Server, kapi etcd.KeysAPI, authBackend shttp.AuthenticationBackend) (*WorkflowAPIHandler, error) {
workflowAPIHandler := &WorkflowAPIHandler{
BasicAPIHandler: rest.BasicAPIHandler{
ResourceHandler: &WorkflowResourceHandler{},
EtcdKeyAPI: apiServer.EtcdKeyAPI,
EtcdKeyAPI: kapi,
},
}
if err := apiServer.RegisterAPIHandler(workflowAPIHandler, authBackend); err != nil {
Expand Down
17 changes: 0 additions & 17 deletions common/types.go
Expand Up @@ -17,23 +17,6 @@

package common

import (
"errors"
)

var (
// ErrCantCompareInterface error can't compare interface
ErrCantCompareInterface = errors.New("Can't compare interface")
// ErrFieldWrongType error field has wrong type
ErrFieldWrongType = errors.New("Field has wrong type")
// ErrNotFound error no result was found
ErrNotFound = errors.New("No result found")
// ErrTimeout network timeout
ErrTimeout = errors.New("Timeout")
// ErrNotImplemented unimplemented feature
ErrNotImplemented = errors.New("Not implemented")
)

// SortOrder describes ascending or descending order
type SortOrder string

Expand Down
10 changes: 6 additions & 4 deletions flow/no_bpf.go
Expand Up @@ -20,20 +20,22 @@
package flow

import (
"errors"

"github.com/google/gopacket/layers"
"golang.org/x/net/bpf"

"github.com/skydive-project/skydive/common"
)

var errNeedPcap = errors.New("BPF filtering needs libpcap support")

// BPF describes a filter
type BPF struct {
vm *bpf.VM
}

// BPFFilterToRaw creates a raw binary filter from a BPF expression
func BPFFilterToRaw(linkType layers.LinkType, captureLength uint32, filter string) ([]bpf.RawInstruction, error) {
return nil, common.ErrNotImplemented
return nil, errNeedPcap
}

// Matches returns true data match the filter
Expand All @@ -43,5 +45,5 @@ func (b *BPF) Matches(data []byte) bool {

// NewBPF creates a new BPF filter
func NewBPF(linkType layers.LinkType, captureLength uint32, filter string) (*BPF, error) {
return nil, common.ErrNotImplemented
return nil, errNeedPcap
}
3 changes: 1 addition & 2 deletions flow/probes/gopacket/no_gopacket.go
Expand Up @@ -20,12 +20,11 @@
package gopacket

import (
"github.com/skydive-project/skydive/common"
"github.com/skydive-project/skydive/flow/probes"
"github.com/skydive-project/skydive/probe"
)

// NewProbe returns a new GoPacket probe
func NewProbe(ctx probes.Context, bundle *probe.Bundle) (probes.FlowProbeHandler, error) {
return nil, common.ErrNotImplemented
return nil, probe.ErrNotImplemented
}
3 changes: 1 addition & 2 deletions flow/probes/ovsmirror/no_ovsmirror.go
Expand Up @@ -20,12 +20,11 @@
package ovsmirror

import (
"github.com/skydive-project/skydive/common"
"github.com/skydive-project/skydive/flow/probes"
"github.com/skydive-project/skydive/probe"
)

// NewProbe returns an empty flow probe handler and an error
func NewProbe(ctx probes.Context, bundle *probe.Bundle) (probes.FlowProbeHandler, error) {
return nil, common.ErrNotImplemented
return nil, probe.ErrNotImplemented
}
3 changes: 1 addition & 2 deletions flow/probes/targets/no_erspan.go
Expand Up @@ -22,7 +22,6 @@ package targets
import (
"github.com/google/gopacket"
"github.com/skydive-project/skydive/api/types"
"github.com/skydive-project/skydive/common"
"github.com/skydive-project/skydive/flow"
"github.com/skydive-project/skydive/graffiti/graph"
)
Expand All @@ -45,5 +44,5 @@ func (ers *ERSpanTarget) Stop() {

// NewERSpanTarget returns a new ERSpan target
func NewERSpanTarget(g *graph.Graph, n *graph.Node, capture *types.Capture) (*ERSpanTarget, error) {
return nil, common.ErrNotImplemented
return nil, errrors.ErrNotImplemented
}
3 changes: 3 additions & 0 deletions graffiti/api/rest/kv.go
Expand Up @@ -127,6 +127,9 @@ func (h *BasicAPIHandler) Delete(id string) error {
etcdPath := fmt.Sprintf("/%s/%s", h.ResourceHandler.Name(), id)

_, err := h.EtcdKeyAPI.Delete(context.Background(), etcdPath, nil)
if err, ok := err.(etcd.Error); ok && err.Code == etcd.ErrorCodeKeyNotFound {
return ErrNotFound
}
return err
}

Expand Down
5 changes: 4 additions & 1 deletion graffiti/api/rest/rest.go
Expand Up @@ -23,7 +23,10 @@ import (
)

// ErrDuplicatedResource is returned when a resource is duplicated
var ErrDuplicatedResource = errors.New("Duplicated resource")
var ErrDuplicatedResource = errors.New("duplicated resource")

// ErrNotFound is returned when a resource could not be found
var ErrNotFound = errors.New("resource not found")

// Resource used as interface resources for each API
type Resource interface {
Expand Down

0 comments on commit 2ece159

Please sign in to comment.