Skip to content

Commit

Permalink
Middleware (dapr#906)
Browse files Browse the repository at this point in the history
* Enabling HTTP Pipeline

* fix comments

* rename HTTPPipeline to Pipeline

* fix for k8s configuration

* loader updates

* middleware registry updates

* middleware registry updates

* dep update

* remove sirupsen temporarily

* add sirupsen

* update golangci

* update golangci
  • Loading branch information
Haishi2016 authored and yaron2 committed Dec 17, 2019
1 parent 8ddf5e7 commit fe72ddf
Show file tree
Hide file tree
Showing 116 changed files with 3,628 additions and 1,300 deletions.
4 changes: 2 additions & 2 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -121,10 +121,10 @@ linters-settings:
list-type: blacklist
include-go-root: false
packages:
- github.com/sirupsen/logrus
- github.com/Sirupsen/logrus
packages-with-error-messages:
# specify an error message to output when a blacklisted package is used
github.com/sirupsen/logrus: "logging is allowed only by logutils.Log"
github.com/Sirupsen/logrus: "must use github.com/sirupsen/logrus"
misspell:
# Correct spellings using locale preferences for US or UK.
# Default is to use a neutral variety of English.
Expand Down
2 changes: 1 addition & 1 deletion cmd/daprd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (
"syscall"
"time"

log "github.com/Sirupsen/logrus"
log "github.com/sirupsen/logrus"
global_config "github.com/dapr/dapr/pkg/config"
"github.com/dapr/dapr/pkg/modes"
daprd "github.com/dapr/dapr/pkg/runtime"
Expand Down
2 changes: 1 addition & 1 deletion cmd/injector/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
"flag"
"time"

log "github.com/Sirupsen/logrus"
log "github.com/sirupsen/logrus"
"github.com/dapr/dapr/pkg/injector"
"github.com/dapr/dapr/pkg/signals"
"github.com/dapr/dapr/pkg/version"
Expand Down
2 changes: 1 addition & 1 deletion cmd/operator/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
"flag"
"time"

log "github.com/Sirupsen/logrus"
log "github.com/sirupsen/logrus"
scheme "github.com/dapr/dapr/pkg/client/clientset/versioned"
k8s "github.com/dapr/dapr/pkg/kubernetes"
"github.com/dapr/dapr/pkg/operator"
Expand Down
2 changes: 1 addition & 1 deletion cmd/placement/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
"os"
"os/signal"

log "github.com/Sirupsen/logrus"
log "github.com/sirupsen/logrus"
"github.com/dapr/dapr/pkg/placement"
"github.com/dapr/dapr/pkg/version"
)
Expand Down
10 changes: 3 additions & 7 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,8 @@ go 1.13
require (
github.com/AdhityaRamadhanus/fasthttpcors v0.0.0-20170121111917-d4c07198763a
github.com/DataDog/zstd v1.4.1 // indirect
github.com/Sirupsen/logrus v1.0.6
github.com/cenkalti/backoff v2.2.1+incompatible
github.com/dapr/components-contrib v0.0.0-20191128051247-c4bd2a47eae0
github.com/dapr/components-contrib v0.0.0-20191217174314-701f12e52994
github.com/eapache/go-resiliency v1.2.0 // indirect
github.com/emicklei/go-restful v2.10.0+incompatible // indirect
github.com/evanphx/json-patch v4.2.0+incompatible // indirect
Expand All @@ -30,20 +29,17 @@ require (
github.com/jcmturner/gofork v1.0.0 // indirect
github.com/json-iterator/go v1.1.7
github.com/kelseyhightower/envconfig v1.4.0
github.com/klauspost/compress v1.5.0 // indirect
github.com/klauspost/cpuid v1.2.1 // indirect
github.com/konsorten/go-windows-terminal-sequences v1.0.2 // indirect
github.com/mailru/easyjson v0.7.0 // indirect
github.com/minio/blake2b-simd v0.0.0-20160723061019-3f5f724cb5b1
github.com/mitchellh/mapstructure v1.1.2
github.com/onsi/ginkgo v1.10.1 // indirect
github.com/onsi/gomega v1.7.0 // indirect
github.com/phayes/freeport v0.0.0-20171002181615-b8543db493a5
github.com/pierrec/lz4 v2.3.0+incompatible // indirect
github.com/qiangxue/fasthttp-routing v0.0.0-20160225050629-6ccdc2a18d87
github.com/rcrowley/go-metrics v0.0.0-20190826022208-cac0b30c2563 // indirect
github.com/sirupsen/logrus v1.4.2
github.com/stretchr/testify v1.4.0
github.com/valyala/fasthttp v1.4.0
github.com/valyala/fasthttp v1.6.0
go.opencensus.io v0.22.1
golang.org/x/exp v0.0.0-20190927203820-447a159532ef // indirect
golang.org/x/sync v0.0.0-20190911185100-cd5d95a43a6e // indirect
Expand Down
112 changes: 68 additions & 44 deletions go.sum

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion pkg/actors/actors.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import (
"sync"
"time"

log "github.com/Sirupsen/logrus"
log "github.com/sirupsen/logrus"
"github.com/dapr/components-contrib/state"
"github.com/dapr/dapr/pkg/channel"
"github.com/dapr/dapr/pkg/channel/http"
Expand Down
1 change: 1 addition & 0 deletions pkg/apis/configuration/v1alpha1/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ type PipelineSpec struct {
// HandlerSpec defines a request handlers
type HandlerSpec struct {
Name string `json:"name"`
Type string `json:"type"`
SelectorSpec SelectorSpec `json:"selector,omitempty"`
}

Expand Down
2 changes: 1 addition & 1 deletion pkg/components/kubernetes_loader.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
"fmt"
"time"

log "github.com/Sirupsen/logrus"
log "github.com/sirupsen/logrus"
"github.com/cenkalti/backoff"
components_v1alpha1 "github.com/dapr/dapr/pkg/apis/components/v1alpha1"
config "github.com/dapr/dapr/pkg/config/modes"
Expand Down
8 changes: 7 additions & 1 deletion pkg/components/middleware/http/loader.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,15 @@ package http
import (
"strings"

middleware "github.com/dapr/components-contrib/middleware"
"github.com/dapr/components-contrib/middleware/http/oauth2"
http_middleware "github.com/dapr/dapr/pkg/middleware/http"
"github.com/valyala/fasthttp"
)

// Load HTTP middleware
func Load() {
RegisterMiddleware("uppercase", func() http_middleware.Middleware {
RegisterMiddleware("uppercase", func(metadata middleware.Metadata) http_middleware.Middleware {
return func(h fasthttp.RequestHandler) fasthttp.RequestHandler {
return func(ctx *fasthttp.RequestCtx) {
body := string(ctx.PostBody())
Expand All @@ -23,4 +25,8 @@ func Load() {
}
}
})
RegisterMiddleware("oauth2", func(metadata middleware.Metadata) http_middleware.Middleware {
handler, _ := oauth2.NewOAuth2Middleware().GetHandler(metadata)
return handler
})
}
13 changes: 7 additions & 6 deletions pkg/components/middleware/http/registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,17 @@ import (
"fmt"
"sync"

middleware "github.com/dapr/components-contrib/middleware"
http_middleware "github.com/dapr/dapr/pkg/middleware/http"
)

// Registry is the interface for callers to get registered HTTP middleware
type Registry interface {
CreateMiddleware(name string) (http_middleware.Middleware, error)
CreateMiddleware(name string, metadata middleware.Metadata) (http_middleware.Middleware, error)
}

type httpMiddlewareRegistry struct {
middleware map[string]func() http_middleware.Middleware
middleware map[string]func(middleware.Metadata) http_middleware.Middleware
}

var instance *httpMiddlewareRegistry
Expand All @@ -28,24 +29,24 @@ var once sync.Once
func NewRegistry() Registry {
once.Do(func() {
instance = &httpMiddlewareRegistry{
middleware: map[string]func() http_middleware.Middleware{},
middleware: map[string]func(middleware.Metadata) http_middleware.Middleware{},
}
})
return instance
}

// RegisterMiddleware registers a new HTTP middleware
func RegisterMiddleware(name string, factoryMethod func() http_middleware.Middleware) {
func RegisterMiddleware(name string, factoryMethod func(metadata middleware.Metadata) http_middleware.Middleware) {
instance.middleware[createFullName(name)] = factoryMethod
}

func createFullName(name string) string {
return fmt.Sprintf("middleware.http.%s", name)
}

func (p *httpMiddlewareRegistry) CreateMiddleware(name string) (http_middleware.Middleware, error) {
func (p *httpMiddlewareRegistry) CreateMiddleware(name string, metadata middleware.Metadata) (http_middleware.Middleware, error) {
if method, ok := p.middleware[name]; ok {
return method(), nil
return method(metadata), nil
}
return nil, fmt.Errorf("couldn't find HTTP middleware %s", name)
}
2 changes: 1 addition & 1 deletion pkg/components/standalone_loader.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
"fmt"
"io/ioutil"

log "github.com/Sirupsen/logrus"
log "github.com/sirupsen/logrus"
components_v1alpha1 "github.com/dapr/dapr/pkg/apis/components/v1alpha1"
config "github.com/dapr/dapr/pkg/config/modes"
"github.com/ghodss/yaml"
Expand Down
1 change: 1 addition & 0 deletions pkg/config/configuration.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ type PipelineSpec struct {

type HandlerSpec struct {
Name string `json:"name"`
Type string `json:"type"`
SelectorSpec SelectorSpec `json:"selector,omitempty"`
}

Expand Down
2 changes: 1 addition & 1 deletion pkg/discovery/mdns.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
"syscall"
"time"

log "github.com/Sirupsen/logrus"
log "github.com/sirupsen/logrus"
"github.com/grandcat/zeroconf"
)

Expand Down
2 changes: 1 addition & 1 deletion pkg/grpc/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
"fmt"
"net"

log "github.com/Sirupsen/logrus"
log "github.com/sirupsen/logrus"
"github.com/dapr/dapr/pkg/config"
diag "github.com/dapr/dapr/pkg/diagnostics"
dapr_pb "github.com/dapr/dapr/pkg/proto/dapr"
Expand Down
4 changes: 3 additions & 1 deletion pkg/http/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"github.com/dapr/components-contrib/bindings"
"github.com/dapr/components-contrib/exporters"
"github.com/dapr/components-contrib/exporters/stringexporter"
"github.com/dapr/components-contrib/middleware"
"github.com/dapr/components-contrib/state"
"github.com/dapr/dapr/pkg/actors"
"github.com/dapr/dapr/pkg/channel/http"
Expand Down Expand Up @@ -967,7 +968,7 @@ func buildHTTPPineline(spec config.PipelineSpec) http_middleware.Pipeline {
http_middleware_loader.Load()
var handlers []http_middleware.Middleware
for i := 0; i < len(spec.Handlers); i++ {
handler, err := registry.CreateMiddleware(spec.Handlers[i].Name)
handler, err := registry.CreateMiddleware(spec.Handlers[i].Type, middleware.Metadata{})
if err != nil {
return http_middleware.Pipeline{}
}
Expand Down Expand Up @@ -996,6 +997,7 @@ func TestSinglePipelineWithTracer(t *testing.T) {
pipeline := buildHTTPPineline(config.PipelineSpec{
Handlers: []config.HandlerSpec{
config.HandlerSpec{
Type: "middleware.http.uppercase",
Name: "middleware.http.uppercase",
},
},
Expand Down
2 changes: 1 addition & 1 deletion pkg/http/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
"strings"

cors "github.com/AdhityaRamadhanus/fasthttpcors"
log "github.com/Sirupsen/logrus"
log "github.com/sirupsen/logrus"
"github.com/dapr/dapr/pkg/config"
diag "github.com/dapr/dapr/pkg/diagnostics"
http_middleware "github.com/dapr/dapr/pkg/middleware/http"
Expand Down
2 changes: 1 addition & 1 deletion pkg/injector/injector.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
"net/http"
"time"

log "github.com/Sirupsen/logrus"
log "github.com/sirupsen/logrus"
"k8s.io/api/admission/v1beta1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
Expand Down
2 changes: 1 addition & 1 deletion pkg/injector/pod_patch.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
"strconv"
"strings"

log "github.com/Sirupsen/logrus"
log "github.com/sirupsen/logrus"
"k8s.io/api/admission/v1beta1"
corev1 "k8s.io/api/core/v1"
)
Expand Down
3 changes: 2 additions & 1 deletion pkg/operator/api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
"net/http"
"time"

log "github.com/Sirupsen/logrus"
log "github.com/sirupsen/logrus"
scheme "github.com/dapr/dapr/pkg/client/clientset/versioned"
"github.com/gorilla/mux"
meta_v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
Expand Down Expand Up @@ -45,6 +45,7 @@ type PipelineSpec struct {

type HandlerSpec struct {
Name string `json:"name"`
Type string `json:"type"`
SelectorSpec SelectorSpec `json:"selector,omitempty"`
}

Expand Down
2 changes: 1 addition & 1 deletion pkg/operator/handlers/components.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"fmt"
"time"

log "github.com/Sirupsen/logrus"
log "github.com/sirupsen/logrus"
components_v1alpha1 "github.com/dapr/dapr/pkg/apis/components/v1alpha1"
pb "github.com/dapr/dapr/pkg/proto/daprinternal"
"google.golang.org/grpc"
Expand Down
2 changes: 1 addition & 1 deletion pkg/operator/handlers/dapr_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"strings"
"sync"

log "github.com/Sirupsen/logrus"
log "github.com/sirupsen/logrus"
"github.com/dapr/dapr/pkg/kubernetes"
appsv1 "k8s.io/api/apps/v1"
corev1 "k8s.io/api/core/v1"
Expand Down
2 changes: 1 addition & 1 deletion pkg/operator/operator.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ package operator
import (
"context"

log "github.com/Sirupsen/logrus"
log "github.com/sirupsen/logrus"
scheme "github.com/dapr/dapr/pkg/client/clientset/versioned"
k8s "github.com/dapr/dapr/pkg/kubernetes"
"github.com/dapr/dapr/pkg/operator/api"
Expand Down
2 changes: 1 addition & 1 deletion pkg/placement/placement.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
"net"
"sync"

log "github.com/Sirupsen/logrus"
log "github.com/sirupsen/logrus"
daprinternal_pb "github.com/dapr/dapr/pkg/proto/daprinternal"
"google.golang.org/grpc"
"google.golang.org/grpc/metadata"
Expand Down
23 changes: 21 additions & 2 deletions pkg/runtime/runtime.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,11 @@ import (
"sync"
"time"

log "github.com/Sirupsen/logrus"
log "github.com/sirupsen/logrus"
"github.com/dapr/components-contrib/bindings"
"github.com/dapr/components-contrib/exporters"
"github.com/dapr/components-contrib/middleware"

"github.com/dapr/components-contrib/pubsub"
"github.com/dapr/components-contrib/secretstores"
"github.com/dapr/components-contrib/servicediscovery"
Expand Down Expand Up @@ -200,7 +202,14 @@ func (a *DaprRuntime) buildHTTPPipeline() (http_middleware.Pipeline, error) {
http_middleware_loader.Load()
var handlers []http_middleware.Middleware
for i := 0; i < len(a.globalConfig.Spec.HTTPPipelineSpec.Handlers); i++ {
handler, err := a.httpMiddlewareRegistry.CreateMiddleware(a.globalConfig.Spec.HTTPPipelineSpec.Handlers[i].Name)
component := a.getComponent(a.globalConfig.Spec.HTTPPipelineSpec.Handlers[i].Type, a.globalConfig.Spec.HTTPPipelineSpec.Handlers[i].Name)
if component == nil {
return http_middleware.Pipeline{}, fmt.Errorf("couldn't find middleware %s of type %s",
a.globalConfig.Spec.HTTPPipelineSpec.Handlers[i].Name,
a.globalConfig.Spec.HTTPPipelineSpec.Handlers[i].Type)
}
handler, err := a.httpMiddlewareRegistry.CreateMiddleware(a.globalConfig.Spec.HTTPPipelineSpec.Handlers[i].Type,
middleware.Metadata{Properties: a.convertMetadataItemsToProperties(component.Spec.Metadata)})
if err != nil {
return http_middleware.Pipeline{}, err
}
Expand Down Expand Up @@ -1048,3 +1057,13 @@ func (a *DaprRuntime) convertMetadataItemsToProperties(items []components_v1alph
}
return properties
}

func (a *DaprRuntime) getComponent(componentType string, name string) *components_v1alpha1.Component {
for _, c := range a.components {
fmt.Printf("COMPONENT TYPE: %s, NAME: %s\n", c.Spec.Type, c.ObjectMeta.Name)
if c.Spec.Type == componentType && c.ObjectMeta.Name == name {
return &c
}
}
return nil
}
2 changes: 1 addition & 1 deletion pkg/signals/signals.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
"os/signal"
"syscall"

log "github.com/Sirupsen/logrus"
log "github.com/sirupsen/logrus"
)

// Context returns a context which will be canceled when either the SIGINT or
Expand Down
2 changes: 1 addition & 1 deletion tests/platforms/kubernetes/appmanager.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
"os"
"time"

log "github.com/Sirupsen/logrus"
log "github.com/sirupsen/logrus"
appsv1 "k8s.io/api/apps/v1"
apiv1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/errors"
Expand Down
2 changes: 1 addition & 1 deletion tests/runner/kube_testplatform.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
"fmt"
"os"

log "github.com/Sirupsen/logrus"
log "github.com/sirupsen/logrus"
kube "github.com/dapr/dapr/tests/platforms/kubernetes"
)

Expand Down
Loading

0 comments on commit fe72ddf

Please sign in to comment.