-
Notifications
You must be signed in to change notification settings - Fork 262
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Refactor gateway package * fix lint errors --------- Co-authored-by: varungupta <[email protected]>
- Loading branch information
1 parent
4a53415
commit 4a6f9a6
Showing
19 changed files
with
259 additions
and
417 deletions.
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
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
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
File renamed without changes.
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,138 @@ | ||
/* | ||
Copyright 2024 The Aibrix Team. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package main | ||
|
||
import ( | ||
"context" | ||
"flag" | ||
"fmt" | ||
"log" | ||
"net" | ||
"os" | ||
"os/signal" | ||
"syscall" | ||
"time" | ||
|
||
"github.com/aibrix/aibrix/pkg/plugins/gateway" | ||
ratelimiter "github.com/aibrix/aibrix/pkg/plugins/gateway/rate_limiter" | ||
redis "github.com/redis/go-redis/v9" | ||
"google.golang.org/grpc" | ||
"k8s.io/client-go/kubernetes" | ||
"k8s.io/client-go/rest" | ||
"k8s.io/client-go/tools/clientcmd" | ||
|
||
extProcPb "github.com/envoyproxy/go-control-plane/envoy/service/ext_proc/v3" | ||
healthPb "google.golang.org/grpc/health/grpc_health_v1" | ||
) | ||
|
||
// Create Redis Client | ||
var ( | ||
grpc_port int | ||
redis_host = getEnv("REDIS_HOST", "localhost") | ||
redis_port = getEnv("REDIS_PORT", "6379") | ||
) | ||
|
||
func getEnv(key, defaultValue string) string { | ||
value := os.Getenv(key) | ||
if value == "" { | ||
return defaultValue | ||
} | ||
return value | ||
} | ||
|
||
//+kubebuilder:rbac:groups=core,resources=pods,verbs=get;list;watch;create;update;patch;delete | ||
|
||
func createClient(kubeconfigPath string) (kubernetes.Interface, error) { | ||
var kubeconfig *rest.Config | ||
|
||
if kubeconfigPath != "" { | ||
config, err := clientcmd.BuildConfigFromFlags("", kubeconfigPath) | ||
if err != nil { | ||
return nil, fmt.Errorf("unable to load kubeconfig from %s: %v", kubeconfigPath, err) | ||
} | ||
kubeconfig = config | ||
} else { | ||
config, err := rest.InClusterConfig() | ||
if err != nil { | ||
return nil, fmt.Errorf("unable to load in-cluster config: %v", err) | ||
} | ||
kubeconfig = config | ||
} | ||
|
||
client, err := kubernetes.NewForConfig(kubeconfig) | ||
if err != nil { | ||
return nil, fmt.Errorf("unable to create a client: %v", err) | ||
} | ||
|
||
return client, nil | ||
} | ||
|
||
// TODO (varun): one or multi plugin ext_proc | ||
func main() { | ||
kubeconfig := flag.String("kubeconfig", "", "absolute path to the kubeconfig file") | ||
flag.IntVar(&grpc_port, "port", 50052, "gRPC port") | ||
flag.Parse() | ||
|
||
// Connect to Redis | ||
client := redis.NewClient(&redis.Options{ | ||
Addr: redis_host + ":" + redis_port, | ||
DB: 0, // Default DB | ||
}) | ||
pong, err := client.Ping(context.Background()).Result() | ||
if err != nil { | ||
log.Fatal("Error connecting to Redis:", err) | ||
} | ||
fmt.Println("Connected to Redis:", pong) | ||
|
||
// Connect to K8s cluster | ||
k8sClient, err := createClient(*kubeconfig) | ||
if err != nil { | ||
log.Fatal("Error creating kubernetes client:", err) | ||
} | ||
|
||
// grpc server init | ||
lis, err := net.Listen("tcp", fmt.Sprintf(":%d", grpc_port)) | ||
if err != nil { | ||
log.Fatalf("failed to listen: %v", err) | ||
} | ||
|
||
s := grpc.NewServer() | ||
|
||
extProcPb.RegisterExternalProcessorServer(s, gateway.NewServer( | ||
ratelimiter.NewRedisAccountRateLimiter("aibrix", client, 1*time.Minute), | ||
k8sClient, | ||
)) | ||
healthPb.RegisterHealthServer(s, &gateway.HealthServer{}) | ||
|
||
log.Println("Starting gRPC server on port :50052") | ||
|
||
// shutdown | ||
var gracefulStop = make(chan os.Signal, 1) | ||
signal.Notify(gracefulStop, syscall.SIGTERM) | ||
signal.Notify(gracefulStop, syscall.SIGINT) | ||
go func() { | ||
sig := <-gracefulStop | ||
log.Printf("caught sig: %+v", sig) | ||
log.Println("Wait for 1 second to finish processing") | ||
time.Sleep(1 * time.Second) | ||
os.Exit(0) | ||
}() | ||
|
||
if err := s.Serve(lis); err != nil { | ||
panic(err) | ||
} | ||
} |
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
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
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
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,34 @@ | ||
## Multistage build | ||
FROM golang:1.22 as builder | ||
ARG TARGETOS | ||
ARG TARGETARCH | ||
|
||
WORKDIR /workspace | ||
# Copy the Go Modules manifests | ||
COPY go.mod go.mod | ||
COPY go.sum go.sum | ||
# cache deps before building and copying source so that we don't need to re-download as much | ||
# and so that source changes don't invalidate our downloaded layer | ||
RUN go mod download | ||
|
||
# Copy the go source | ||
COPY cmd/plugins/main.go cmd/main.go | ||
COPY api/ api/ | ||
COPY pkg/plugins/ pkg/plugins/ | ||
COPY pkg/utils/ pkg/utils/ | ||
|
||
# Build | ||
# the GOARCH has not a default value to allow the binary be built according to the host where the command | ||
# was called. For example, if we call make docker-build in a local env which has the Apple Silicon M1 SO | ||
# the docker BUILDPLATFORM arg will be linux/arm64 when for Apple x86 it will be linux/amd64. Therefore, | ||
# by leaving it empty we can ensure that the container and binary shipped on it will have the same platform. | ||
RUN CGO_ENABLED=0 GOOS=${TARGETOS:-linux} GOARCH=${TARGETARCH} go build -a -o plugins cmd/main.go | ||
|
||
# Use distroless as minimal base image to package the manager binary | ||
# Refer to https://github.com/GoogleContainerTools/distroless for more details | ||
FROM gcr.io/distroless/static:nonroot | ||
WORKDIR / | ||
COPY --from=builder /workspace/plugins . | ||
USER 65532:65532 | ||
|
||
ENTRYPOINT ["/plugins"] |
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
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
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.