Skip to content

Commit

Permalink
feat: support custom etcd server and adapt to APISIX (#17)
Browse files Browse the repository at this point in the history
  • Loading branch information
AlinsRan authored Apr 19, 2023
1 parent e7acdaf commit 53513fd
Show file tree
Hide file tree
Showing 38 changed files with 1,003 additions and 232 deletions.
25 changes: 14 additions & 11 deletions .github/workflows/e2e.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: E2E test
name: E2E Test

on:
push:
Expand All @@ -19,25 +19,28 @@ jobs:
env:
ALLOW_EMPTY_PASSWORD: yes
steps:
- uses: actions/checkout@v2
- name: Checkout
uses: actions/checkout@v3
with:
submodules: recursive

- uses: actions/setup-go@v3
- name: Set up Go
uses: actions/setup-go@v3
with:
go-version: "1.17"
go-version: "1.20"

- name: Set up QEMU
uses: docker/setup-qemu-action@v2

- name: Set up Docker Buildx
id: buildx
uses: docker/setup-buildx-action@v2

- name: Install Ginkgo
run: go install github.com/onsi/ginkgo/v2/ginkgo@latest

- name: Build ETCD Adapter
run: go build -o ./test/e2e/etcd-adapter main.go

- name: Run test cases
working-directory: ./test/e2e
run: |
go mod download
ginkgo -r
run: make e2e-test

- name: Run tmate debugger
if: ${{ failure() }}
Expand Down
27 changes: 27 additions & 0 deletions .github/workflows/unit-test.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
name: Unit Test

on:
push:
branches:
- main
pull_request:
branches:
- main

jobs:
run-test:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v3

- name: Setup Go Env
uses: actions/setup-go@v3
with:
go-version: "1.20"

- name: Run unit test
working-directory: ./
run: |
make unit-test
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
.idea

etcd-adapter
24 changes: 24 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
FROM golang:1.20 AS build-env

ARG ENABLE_PROXY=false

WORKDIR /build
COPY go.* ./

RUN if [ "$ENABLE_PROXY" = "true" ] ; then go env -w GOPROXY=https://goproxy.cn,direct ; fi \
&& go mod download

COPY . .
RUN make build

FROM ubuntu:latest

WORKDIR /etcd-adapter

COPY --from=build-env /build/etcd-adapter /etcd-adapter

COPY conf/ /etcd-adapter/conf/

EXPOSE 12379

ENTRYPOINT ["/etcd-adapter/etcd-adapter", "-c", "/etcd-adapter/conf/config.yaml"]
28 changes: 26 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,38 @@
# limitations under the License.
#

test:
# contianer image registry
REGISTRY ?="api7"

# e2e
E2E_FOCUS ?=""

.PHONY: unit-test
unit-test:
@go test ./...

.PHONY: bench
bench:
@go test -bench '^Benchmark' ./...
@go test -run=none -bench=Benchmark ./...

.PHONY: gofmt
gofmt:
@find . -name "*.go" | xargs gofmt -w

.PHONY: lint
lint:
@golangci-lint run

.PHONY: build
build:
go build \
-o etcd-adapter \
main.go

.PHONY: build-image
build-image:
@docker build -t $(REGISTRY)/etcd-adapter:dev .

.PHONY: e2e-test
e2e-test: build build-image
cd test/e2e && go mod download && ginkgo -r --focus=$(E2E_FOCUS)
82 changes: 51 additions & 31 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ 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
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,
Expand All @@ -21,16 +21,18 @@ import (
"net"
"os"
"os/signal"
"strings"
"syscall"

"github.com/api7/gopkg/pkg/log"
"github.com/k3s-io/kine/pkg/server"
"github.com/spf13/cobra"
"go.uber.org/zap"

"github.com/api7/etcd-adapter/internal/adapter"
"github.com/api7/etcd-adapter/internal/backends/btree"
"github.com/api7/etcd-adapter/internal/backends/mysql"
"github.com/api7/etcd-adapter/internal/config"
"github.com/api7/etcd-adapter/pkg/adapter"
"github.com/api7/etcd-adapter/pkg/backends/btree"
"github.com/api7/etcd-adapter/pkg/backends/mysql"
"github.com/api7/etcd-adapter/pkg/config"
)

var configFile string
Expand All @@ -39,64 +41,74 @@ var rootCmd = &cobra.Command{
Use: "etcd-adapter",
Short: "The bridge between etcd protocol and other storage backends.",
Run: func(cmd *cobra.Command, args []string) {
// initialize logger
logger, err := log.NewLogger()

// initialize configuration
err = config.Init(configFile, logger)
err := config.Init(configFile)
if err != nil {
return
dief("failed to initialize configuration: %s", err)
}

// initialize log
log.DefaultLogger, err = log.NewLogger(
log.WithSkipFrames(3),
log.WithLogLevel(config.Config.Log.Level),
)
if err != nil {
dief("failed to initialize logging: %s", err)
}

// initialize backend
log.Infow("using backends type", zap.String("datasources", string(config.Config.DataSource.Type)))
var backend server.Backend
switch config.Config.DataSource.Type {
case "mysql":
case config.Mysql:
mysqlConfig := config.Config.DataSource.MySQL
backend, err = mysql.NewMySQLCache(context.TODO(), &mysql.Options{
DSN: fmt.Sprintf("%s:%s@tcp(%s:%s)/%s", mysqlConfig.Username, mysqlConfig.Password, mysqlConfig.Host, mysqlConfig.Port, mysqlConfig.Database),
})

if err != nil {
logger.Panic("failed to create mysql backend: ", err)
return
dief("failed to create mysql backend, err: %s", err)
}
case config.BTree:
backend = btree.NewBTreeCache()
default:
backend = btree.NewBTreeCache(logger)
dief("does not support backends from %s", config.Config.DataSource.Type)
}

// bootstrap etcd adapter
adapter := adapter.NewEtcdAdapter(backend, logger)
adapter := adapter.NewEtcdAdapter(&adapter.AdapterOptions{
Backend: backend,
})

log.Info("configuring listeners at ", config.Config.Server.Host, ":", config.Config.Server.Port, "")
ln, err := net.Listen("tcp", net.JoinHostPort(config.Config.Server.Host, config.Config.Server.Port))
if err != nil {
panic(err)
dief("failed create listenners, err:", err)

}
go func() {
log.Info("start etcd-adapter server")
if err := adapter.Serve(context.Background(), ln); err != nil {
logger.Panic(err)
return
dief("failed to start etcd-adapter server, err:", err)
}
}()

// graceful exit
quit := make(chan os.Signal, 1)
signal.Notify(quit, syscall.SIGINT, syscall.SIGTERM)

select {
case <-quit:
err := adapter.Shutdown(context.TODO())
if err != nil {
logger.Error("An error occurred while exiting: ", err)
return
}
err = logger.Sync()
if err != nil {
logger.Error("An error occurred while exiting: ", err)
return
}
logger.Info("See you next time!")
<-quit
err = adapter.Shutdown(context.TODO())
if err != nil {
log.Error("An error occurred while exiting: ", err)
return
}
err = log.DefaultLogger.Sync()
if err != nil {
log.Error("An error occurred while exiting: ", err)
return
}
log.Info("etcd-adapter exit")
},
}

Expand All @@ -111,3 +123,11 @@ func Execute() {
os.Exit(1)
}
}

func dief(template string, args ...interface{}) {
if !strings.HasSuffix(template, "\n") {
template += "\n"
}
fmt.Fprintf(os.Stderr, template, args...)
os.Exit(1)
}
18 changes: 18 additions & 0 deletions conf/config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
server:
host: 0.0.0.0
port: 12379
# tls:
# cert: /path/to/file.pem
# key: /path/to/file.pem

log:
level: info

datasource:
type: btree # btree of mysql
# mysql:
# host: 127.0.0.1
# port: 3306
# username: root
# password:
# database: apisix
18 changes: 0 additions & 18 deletions config/config.yaml

This file was deleted.

41 changes: 37 additions & 4 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module github.com/api7/etcd-adapter

go 1.16
go 1.20

require (
github.com/api7/gopkg v0.1.2
Expand All @@ -12,14 +12,47 @@ require (
github.com/spf13/viper v1.10.0
github.com/stretchr/testify v1.7.0
github.com/tmc/grpc-websocket-proxy v0.0.0-20201229170055-e5319fda7802
go.etcd.io/etcd v3.3.27+incompatible // indirect
go.etcd.io/etcd/api/v3 v3.5.2
go.etcd.io/etcd/client/v3 v3.5.2
go.uber.org/zap v1.19.1
golang.org/x/net v0.0.0-20220225172249-27dd8689420f
google.golang.org/grpc v1.42.0
)

replace (
go.etcd.io/etcd v0.0.0-20191023171146-3cf2f69b5738 => go.etcd.io/etcd v3.3.27+incompatible
require (
github.com/Rican7/retry v0.1.0 // indirect
github.com/coreos/go-semver v0.3.0 // indirect
github.com/coreos/go-systemd/v22 v22.3.2 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/fsnotify/fsnotify v1.5.1 // indirect
github.com/go-sql-driver/mysql v1.6.0 // indirect
github.com/gogo/protobuf v1.3.2 // indirect
github.com/golang/protobuf v1.5.2 // indirect
github.com/gorilla/websocket v1.4.2 // indirect
github.com/hashicorp/hcl v1.0.0 // indirect
github.com/inconshreveable/mousetrap v1.0.0 // indirect
github.com/magiconair/properties v1.8.5 // indirect
github.com/mitchellh/mapstructure v1.4.3 // indirect
github.com/pelletier/go-toml v1.9.4 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/sirupsen/logrus v1.7.0 // indirect
github.com/spf13/afero v1.6.0 // indirect
github.com/spf13/cast v1.4.1 // indirect
github.com/spf13/jwalterweatherman v1.1.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/subosito/gotenv v1.2.0 // indirect
go.etcd.io/etcd v3.3.27+incompatible // indirect
go.etcd.io/etcd/client/pkg/v3 v3.5.2 // indirect
go.uber.org/atomic v1.7.0 // indirect
go.uber.org/multierr v1.6.0 // indirect
golang.org/x/sys v0.0.0-20211216021012-1d35b9e2eb4e // indirect
golang.org/x/text v0.3.7 // indirect
google.golang.org/genproto v0.0.0-20211208223120-3a66f561d7aa // indirect
google.golang.org/protobuf v1.27.1 // indirect
gopkg.in/ini.v1 v1.66.2 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b // indirect
)

replace go.etcd.io/etcd v0.0.0-20191023171146-3cf2f69b5738 => go.etcd.io/etcd v3.3.27+incompatible
Loading

0 comments on commit 53513fd

Please sign in to comment.