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

feat: Integrate Traefik API proxy with Aperture #1977

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
23 changes: 23 additions & 0 deletions integrations/traefik-config/dynamic-rule-config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
http:
routers:
Router0:
entryPoints:
- http
service: service-whoami
rule: Path(`/whoami`)
middlewares:
- my-plugin


services:
service-whoami:
loadBalancer:
servers:
- url: http://localhost:8080/
passHostHeader: true

middlewares:
my-plugin:
plugin:
example:
ControlPoint: "awesomeFeature"
21 changes: 21 additions & 0 deletions integrations/traefik-config/static-config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Static configuration

api:
dashboard: true
insecure: true

experimental:
localPlugins:
example:
moduleName: github.com/fluxninja/aperture-traefik-plugin

entryPoints:
http:
address: ":8000"
forwardedHeaders:
insecure: true


providers:
file:
filename: dyanmic-rule-config.yaml
9 changes: 9 additions & 0 deletions integrations/traefik-plugin/.traefik.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
displayName: Aperture traefik plugin
type: middleware

import: github.com/fluxninja/aperture-traefik-plugin

summary: 'Integrate Traefik API proxy with Aperture'

testData:
ControlPoint: "awesomeFeature"
33 changes: 33 additions & 0 deletions integrations/traefik-plugin/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
module github.com/fluxninja/aperture-traefik-plugin

go 1.20

replace github.com/fluxninja/aperture-go/v2 => /home/sachinmaurya/fluxninja/test/aperture/sdks/aperture-go

require (
github.com/fluxninja/aperture-go/v2 v2.0.0
github.com/go-logr/stdr v1.2.2
google.golang.org/grpc v1.55.0
)

require (
github.com/cenkalti/backoff/v4 v4.2.1 // indirect
github.com/envoyproxy/protoc-gen-validate v0.10.0 // indirect
github.com/go-logr/logr v1.2.4 // indirect
github.com/golang/protobuf v1.5.3 // indirect
github.com/gorilla/mux v1.8.0 // indirect
github.com/grpc-ecosystem/grpc-gateway/v2 v2.15.2 // indirect
go.opentelemetry.io/otel v1.15.1 // indirect
go.opentelemetry.io/otel/exporters/otlp/internal/retry v1.15.1 // indirect
go.opentelemetry.io/otel/exporters/otlp/otlptrace v1.15.1 // indirect
go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracegrpc v1.15.1 // indirect
go.opentelemetry.io/otel/sdk v1.15.1 // indirect
go.opentelemetry.io/otel/trace v1.15.1 // indirect
go.opentelemetry.io/proto/otlp v0.19.0 // indirect
golang.org/x/exp v0.0.0-20230510235704-dd950f8aeaea // indirect
golang.org/x/net v0.10.0 // indirect
golang.org/x/sys v0.8.0 // indirect
golang.org/x/text v0.9.0 // indirect
google.golang.org/genproto v0.0.0-20230410155749-daa745c078e1 // indirect
google.golang.org/protobuf v1.30.0 // indirect
)
450 changes: 450 additions & 0 deletions integrations/traefik-plugin/go.sum

Large diffs are not rendered by default.

75 changes: 75 additions & 0 deletions integrations/traefik-plugin/traefik.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
package traefikplugin

import (
"context"
"log"
"net"
"net/http"
"strings"
"time"

aperture "github.com/fluxninja/aperture-go/v2/sdk"

"github.com/go-logr/stdr"
)

type Config struct {
ControlPoint string
//Labels map[string]string
}

type TraefikPlugin struct {
next http.Handler
ControlPoint string
//Labels map[string]string
}

func CreateConfig() *Config {
return &Config{}
}

func New(ctx context.Context, next http.Handler, config *Config, name string) (http.Handler, error) {
return &TraefikPlugin{
next: next,
ControlPoint: config.ControlPoint,
//Labels: config.Labels,
}, nil
}

func (a TraefikPlugin) ServeHTTP(rw http.ResponseWriter, r *http.Request) {

agentHost := getEnvOrDefault("FN_AGENT_HOST", defaultAgentHost)
agentPort := getEnvOrDefault("FN_AGENT_PORT", defaultAgentPort)

ctx := context.Background()

apertureAgentGRPCClient, err := grpcClient(ctx, net.JoinHostPort(agentHost, agentPort))
if err != nil {
log.Fatalf("failed to create flow control client: %v", err)
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🤖 OpenAI

There is a potential issue with the use of log.Fatalf here. If there's an error creating the gRPC client, it will cause the entire process to exit. Instead, you should return an error and let the caller handle it.

Suggested change
ctx := context.Background()
apertureAgentGRPCClient, err := grpcClient(ctx, net.JoinHostPort(agentHost, agentPort))
if err != nil {
log.Fatalf("failed to create flow control client: %v", err)
}
func (a *TraefikPlugin) ServeHTTP(rw http.ResponseWriter, r *http.Request) {
// ...
apertureAgentGRPCClient, err := grpcClient(ctx, net.JoinHostPort(agentHost, agentPort))
if err != nil {
rw.WriteHeader(http.StatusInternalServerError)
rw.Write([]byte("failed to create flow control client"))
return
}
// ...
}


// Initialize the logger
logger := stdr.New(log.Default()).WithName("aperture-traefik-plugin")

opts := aperture.Options{
ApertureAgentGRPCClientConn: apertureAgentGRPCClient,
CheckTimeout: 200 * time.Millisecond,
Logger: &logger,
}

//initialize Aperture Client with the provided options.
apertureClient, err := aperture.NewClient(ctx, opts)
if err != nil {
log.Fatalf("failed to create client: %v", err)
}
Comment on lines +62 to +67
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🤖 OpenAI

Similar to the previous issue, using log.Fatalf here will cause the entire process to exit if there's an error creating the Aperture client. You should return an error and let the caller handle it.

Suggested change
//initialize Aperture Client with the provided options.
apertureClient, err := aperture.NewClient(ctx, opts)
if err != nil {
log.Fatalf("failed to create client: %v", err)
}
func (a *TraefikPlugin) ServeHTTP(rw http.ResponseWriter, r *http.Request) {
// ...
apertureClient, err := aperture.NewClient(ctx, opts)
if err != nil {
rw.WriteHeader(http.StatusInternalServerError)
rw.Write([]byte("failed to create client"))
return
}
// ...
}


labels := aperture.LabelsFromCtx(r.Context())

for key, value := range r.Header {
if strings.HasPrefix(key, ":") {
continue
}
labels[key] = strings.Join(value, ",")
}
a.next.ServeHTTP(rw, r)
}
37 changes: 37 additions & 0 deletions integrations/traefik-plugin/utils.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package traefikplugin

import (
"context"
"os"
"time"

"google.golang.org/grpc"
"google.golang.org/grpc/backoff"
"google.golang.org/grpc/credentials/insecure"
)

const (
defaultAgentHost = "localhost"
defaultAgentPort = "8080"
)

func grpcClient(ctx context.Context, address string) (*grpc.ClientConn, error) {
// creating a gRPC client connection is essential to allow the Aperture client to communicate with the Flow Control Service.
var grpcDialOptions []grpc.DialOption
grpcDialOptions = append(grpcDialOptions, grpc.WithConnectParams(grpc.ConnectParams{
Backoff: backoff.DefaultConfig,
MinConnectTimeout: time.Second * 10,
}))
grpcDialOptions = append(grpcDialOptions, grpc.WithUserAgent("aperture-traefik-plugin"))
grpcDialOptions = append(grpcDialOptions, grpc.WithTransportCredentials(insecure.NewCredentials()))

return grpc.DialContext(ctx, address, grpcDialOptions...)
}

func getEnvOrDefault(envName, defaultValue string) string {
val := os.Getenv(envName)
if val == "" {
return defaultValue
}
return val
}