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

Add users CRUD API #181

Merged
merged 7 commits into from
Sep 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
4 changes: 2 additions & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ COPY go.sum go.sum
RUN go mod download

# Copy the go source
COPY cmd/controllers/main.go cmd/main.go
COPY cmd/ cmd/
COPY api/ api/
COPY pkg/controller/ pkg/controller/
COPY pkg/utils/ pkg/utils/
Expand All @@ -24,7 +24,7 @@ COPY pkg/client/ pkg/client/
# 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 manager cmd/main.go
RUN CGO_ENABLED=0 GOOS=${TARGETOS:-linux} GOARCH=${TARGETARCH} go build -a -o manager cmd/controllers/main.go

# Use distroless as minimal base image to package the manager binary
# Refer to https://github.com/GoogleContainerTools/distroless for more details
Expand Down
10 changes: 5 additions & 5 deletions gateway.Dockerfile → Dockerfile.gateway
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
## Multistage build
FROM golang:1.22 as builder
# Build the manager binary
FROM golang:1.22 AS builder
ARG TARGETOS
ARG TARGETARCH

Expand All @@ -12,7 +12,7 @@ COPY go.sum go.sum
RUN go mod download

# Copy the go source
COPY cmd/plugins/main.go cmd/main.go
COPY cmd/ cmd/
COPY api/ api/
COPY pkg/plugins/ pkg/plugins/
COPY pkg/utils/ pkg/utils/
Expand All @@ -24,7 +24,7 @@ COPY pkg/client/ pkg/client/
# 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
RUN CGO_ENABLED=0 GOOS=${TARGETOS:-linux} GOARCH=${TARGETARCH} go build -a -o plugins cmd/plugins/main.go

# Use distroless as minimal base image to package the manager binary
# Refer to https://github.com/GoogleContainerTools/distroless for more details
Expand All @@ -33,4 +33,4 @@ WORKDIR /
COPY --from=builder /workspace/plugins .
USER 65532:65532

ENTRYPOINT ["/plugins"]
ENTRYPOINT ["/plugins"]
36 changes: 36 additions & 0 deletions Dockerfile.users
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# Build the manager binary
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/ cmd/
COPY api/ api/
COPY pkg/users/ pkg/users/
COPY pkg/utils/ pkg/utils/
COPY pkg/cache/ pkg/cache/
COPY pkg/client/ pkg/client/

# 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 users cmd/users/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/users .
USER 65532:65532

ENTRYPOINT ["/users"]
13 changes: 12 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ AIBRIX_DOCKERHUB_NAMESPACE ?= aibrix
IMG ?= ${AIBRIX_DOCKERHUB_NAMESPACE}/controller-manager:${GIT_COMMIT_HASH}
PLUGINS_IMG ?= ${AIBRIX_DOCKERHUB_NAMESPACE}/plugins:${GIT_COMMIT_HASH}
RUNTIME_IMG ?= ${AIBRIX_DOCKERHUB_NAMESPACE}/runtime:${GIT_COMMIT_HASH}
USERS_IMG ?= ${AIBRIX_DOCKERHUB_NAMESPACE}/users:${GIT_COMMIT_HASH}
# ENVTEST_K8S_VERSION refers to the version of kubebuilder assets to be downloaded by envtest binary.
ENVTEST_K8S_VERSION = 1.29.0

Expand Down Expand Up @@ -126,14 +127,19 @@ docker-build: ## Build docker image with the manager.

.PHONY: docker-build-plugins
docker-build-plugins: ## Build docker image with the manager.
$(CONTAINER_TOOL) build -t ${PLUGINS_IMG} -f gateway.Dockerfile .
$(CONTAINER_TOOL) build -t ${PLUGINS_IMG} -f Dockerfile.gateway .
$(CONTAINER_TOOL) tag ${PLUGINS_IMG} ${AIBRIX_DOCKERHUB_NAMESPACE}/plugins:nightly

.PHONY: docker-build-runtime
docker-build-runtime: ## Build docker image with the AI Runime.
$(CONTAINER_TOOL) build -t ${RUNTIME_IMG} -f runtime.Dockerfile .
$(CONTAINER_TOOL) tag ${RUNTIME_IMG} ${AIBRIX_DOCKERHUB_NAMESPACE}/runtime:nightly

.PHONY: docker-build-users
docker-build-users: ## Build docker image with the manager.
$(CONTAINER_TOOL) build -t ${USERS_IMG} -f Dockerfile.users .
$(CONTAINER_TOOL) tag ${USERS_IMG} ${AIBRIX_DOCKERHUB_NAMESPACE}/users:nightly

.PHONY: docker-push
docker-push: ## Push docker image with the manager.
$(CONTAINER_TOOL) push ${IMG}
Expand All @@ -149,6 +155,11 @@ docker-push-runtime: ## Push docker image with the manager.
$(CONTAINER_TOOL) push ${RUNTIME_IMG}
$(CONTAINER_TOOL) push ${AIBRIX_DOCKERHUB_NAMESPACE}/runtime:nightly

.PHONY: docker-push-users
docker-push-users: ## Push docker image with the manager.
$(CONTAINER_TOOL) push ${USERS_IMG}
$(CONTAINER_TOOL) push ${AIBRIX_DOCKERHUB_NAMESPACE}/users:nightly

# PLATFORMS defines the target platforms for the manager image be built to provide support to multiple
# architectures. (i.e. make docker-buildx IMG=myregistry/mypoperator:0.0.1). To use this option you need to:
# - be able to use docker buildx. More info: https://docs.docker.com/build/buildx/
Expand Down
33 changes: 5 additions & 28 deletions cmd/plugins/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ limitations under the License.
package main

import (
"context"
"flag"
"fmt"
"log"
Expand All @@ -27,53 +26,34 @@ import (
"syscall"
"time"

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"

"github.com/aibrix/aibrix/pkg/cache"
"github.com/aibrix/aibrix/pkg/plugins/gateway"
ratelimiter "github.com/aibrix/aibrix/pkg/plugins/gateway/rate_limiter"
"github.com/aibrix/aibrix/pkg/utils"
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")
grpc_port int
)

func getEnv(key, defaultValue string) string {
value := os.Getenv(key)
if value == "" {
return defaultValue
}
return value
}

func main() {
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)
redisClient := utils.GetRedisClient()

fmt.Println("Starting cache")
stopCh := make(chan struct{})
defer close(stopCh)
var config *rest.Config
var err error

// ref: https://github.com/kubernetes-sigs/controller-runtime/issues/878#issuecomment-1002204308
kubeConfig := flag.Lookup("kubeconfig").Value.String()
Expand Down Expand Up @@ -105,10 +85,7 @@ func main() {

s := grpc.NewServer()

extProcPb.RegisterExternalProcessorServer(s, gateway.NewServer(
ratelimiter.NewRedisAccountRateLimiter("aibrix", client, 1*time.Minute),
k8sClient,
))
extProcPb.RegisterExternalProcessorServer(s, gateway.NewServer(redisClient, k8sClient))
healthPb.RegisterHealthServer(s, &gateway.HealthServer{})

log.Println("Starting gRPC server on port :50052")
Expand Down
32 changes: 32 additions & 0 deletions cmd/users/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*
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 (
"log"

"github.com/aibrix/aibrix/pkg/users"
"github.com/aibrix/aibrix/pkg/utils"
)

func main() {
redisClient := utils.GetRedisClient()

log.Println("Starting listening on port 8090")
srv := users.NewHTTPServer(":8090", redisClient)
log.Fatal(srv.ListenAndServe())
}
1 change: 1 addition & 0 deletions config/gateway/kustomization.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@ resources:
- gateway.yaml
- redis.yaml
- gateway-plugin.yaml
- users.yaml

52 changes: 52 additions & 0 deletions config/gateway/users.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
apiVersion: v1
kind: Service
metadata:
name: gateway-users
namespace: aibrix-system
spec:
selector:
app: gateway-users
ports:
- protocol: TCP
port: 8090
targetPort: 8090
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: gateway-users
namespace: aibrix-system
spec:
replicas: 1
selector:
matchLabels:
app: gateway-users
template:
metadata:
labels:
app: gateway-users
spec:
initContainers:
- name: init-c
image: busybox
command: ['sh', '-c', "until nslookup aibrix-redis-master.aibrix-system.svc.cluster.local; do echo waiting for service aibrix-redis-master; sleep 2; done"]
containers:
- name: golang-app-container
image: aibrix/users:nightly
imagePullPolicy: Always
ports:
- containerPort: 8090
env:
- name: REDIS_HOST
value: aibrix-redis-master
- name: REDIS_PORT
value: "6379"
- name: POD_NAME
valueFrom:
fieldRef:
fieldPath: metadata.name
- name: POD_NAMESPACE
valueFrom:
fieldRef:
fieldPath: metadata.namespace
serviceAccountName: aibrix-gateway-plugin
2 changes: 1 addition & 1 deletion docs/development/app/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ OR

- if only want to test gateway plugins

docker build -t aibrix/plugins:v0.1.0 -f gateway.Dockerfile .
docker build -t aibrix/plugins:v0.1.0 -f Dockerfile.gateway .
kind load docker-image aibrix/plugins:v0.1.0

kubectl -n aibrix-system apply -f docs/development/app/redis.yaml
Expand Down
52 changes: 52 additions & 0 deletions docs/development/app/users.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
apiVersion: v1
kind: Service
metadata:
name: aibrix-gateway-users
namespace: aibrix-system
spec:
selector:
app: gateway-users
ports:
- protocol: TCP
port: 8090
targetPort: 8090
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: aibrix-gateway-users
namespace: aibrix-system
spec:
replicas: 1
selector:
matchLabels:
app: gateway-users
template:
metadata:
labels:
app: gateway-users
spec:
initContainers:
- name: init-c
image: busybox
command: ['sh', '-c', "until nslookup aibrix-redis-master.aibrix-system.svc.cluster.local; do echo waiting for service aibrix-redis-master; sleep 2; done"]
containers:
- name: golang-app-container
image: aibrix/users:nightly
imagePullPolicy: Always
ports:
- containerPort: 8090
env:
- name: REDIS_HOST
value: aibrix-redis-master
- name: REDIS_PORT
value: "6379"
- name: POD_NAME
valueFrom:
fieldRef:
fieldPath: metadata.name
- name: POD_NAMESPACE
valueFrom:
fieldRef:
fieldPath: metadata.namespace
serviceAccountName: aibrix-gateway-plugin
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ toolchain go1.22.6

require (
github.com/envoyproxy/go-control-plane v0.12.0
github.com/gorilla/mux v1.8.1
github.com/onsi/ginkgo/v2 v2.14.0
github.com/onsi/gomega v1.30.0
github.com/ray-project/kuberay/ray-operator v1.1.1
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@ github.com/google/pprof v0.0.0-20240424215950-a892ee059fd6 h1:k7nVchz72niMH6YLQN
github.com/google/pprof v0.0.0-20240424215950-a892ee059fd6/go.mod h1:kf6iHlnVGwgKolg33glAes7Yg/8iWP8ukqeldJSO7jw=
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/gorilla/mux v1.8.1 h1:TuBL49tXwgrFYWhqrNgrUNEY92u81SPhu7sTdzQEiWY=
github.com/gorilla/mux v1.8.1/go.mod h1:AKf9I4AEqPTmMytcMc0KkNouC66V3BtZ4qD5fmWSiMQ=
github.com/josharian/intern v1.0.0 h1:vlS4z54oSdjm0bgjRigI+G1HpF+tI+9rE5LLzOg8HmY=
github.com/josharian/intern v1.0.0/go.mod h1:5DoeVV0s6jJacbCEi61lwdGj/aVlrQvzHFFd8Hwg//Y=
github.com/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnrnM=
Expand Down
Loading
Loading