-
Notifications
You must be signed in to change notification settings - Fork 25
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
Closed
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
) |
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||
// 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
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🤖 OpenAI Similar to the previous issue, using
Suggested change
|
||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||
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) | ||||||||||||||||||||||||||||||||||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.