Skip to content

Commit

Permalink
feat: add diff of route resource (#3)
Browse files Browse the repository at this point in the history
  • Loading branch information
starsz authored Aug 22, 2023
1 parent e2711ca commit 69d38b9
Show file tree
Hide file tree
Showing 9 changed files with 382 additions and 19 deletions.
61 changes: 61 additions & 0 deletions .github/workflows/unit_test.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
name: Run Unit Test Suites

on:
push:
branches:
- main
- release/**
pull_request:
branches:
- main
- release/**
permissions: read-all
concurrency:
group: ${{ github.workflow }}-${{ github.event.pull_request.number || github.ref }}
cancel-in-progress: true
jobs:
changes:
runs-on: ubuntu-latest
timeout-minutes: 5
outputs:
src: ${{ steps.filter.outputs.src }}
steps:
- name: Checkout
uses: actions/checkout@v3
- uses: dorny/paths-filter@v2
id: filter
with:
token: ${{ secrets.GITHUB_TOKEN }}
filters: |
src:
- '*.go'
- '**/*.go'
- 'go.mod'
- 'go.sum'
- 'Makefile'
- '.github/**'
- 'internal/**'
run-test:
needs: changes
if: |
(needs.changes.outputs.src == 'true')
runs-on: ubuntu-latest
timeout-minutes: 20
steps:
- uses: actions/checkout@v3
- uses: actions/cache@v3
with:
path: |
~/.cache/go-build
~/go/pkg/mod
key: ${{ runner.os }}-go-${{ hashFiles('**/go.sum') }}
restore-keys: |
${{ runner.os }}-go-
- name: Setup Go Environment
uses: actions/setup-go@v3
with:
go-version: '1.20.3'
- name: Run Unit Test Suites
working-directory: ./
run: |
make test
4 changes: 4 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,7 @@ help: ## Display this help
build: ## Build adc
@go build -o bin/adc main.go
.PHONY: build

test:
@go test -v ./...
.PHONY: test
2 changes: 2 additions & 0 deletions adc.yaml
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
name: "test"
version: "1.0.0"
services:
- name: svc1
hosts:
Expand Down
35 changes: 31 additions & 4 deletions internal/pkg/db/memdb.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,15 @@ var schema = &memdb.DBSchema{
Unique: true,
Indexer: &memdb.StringFieldIndex{Field: "ID"},
},
"name": {
Name: "name",
},
},
"routes": {
Name: "routes",
Indexes: map[string]*memdb.IndexSchema{
"id": {
Name: "id",
Unique: true,
Indexer: &memdb.StringFieldIndex{Field: "Name"},
Indexer: &memdb.StringFieldIndex{Field: "ID"},
},
},
},
Expand All @@ -47,8 +52,17 @@ func NewMemDB(configure *data.Configuration) (*DB, error) {
if service.ID == "" {
service.ID = service.Name
}
err = txn.Insert("services", service)
if err != nil {
return nil, err
}
}

err := txn.Insert("services", service)
for _, routes := range configure.Routes {
if routes.ID == "" {
routes.ID = routes.Name
}
err = txn.Insert("routes", routes)
if err != nil {
return nil, err
}
Expand All @@ -70,3 +84,16 @@ func (db *DB) GetServiceByID(id string) (*data.Service, error) {

return obj.(*data.Service), err
}

func (db *DB) GetRouteByID(id string) (*data.Route, error) {
obj, err := db.memDB.Txn(false).First("routes", "id", id)
if err != nil {
return nil, err
}

if obj == nil {
return nil, NotFound
}

return obj.(*data.Route), err
}
45 changes: 41 additions & 4 deletions internal/pkg/db/memdb_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package db

import (
"net/http"
"testing"

"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -36,6 +37,15 @@ var (
},
UpstreamInUse: "upstream1",
}

route = &data.Route{
ID: "route",
Name: "route",
Labels: []string{"lable1", "label2"},
Methods: []string{http.MethodGet},
Paths: []string{"/get"},
ServiceID: "svc",
}
)

func TestGetServiceByID(t *testing.T) {
Expand All @@ -54,14 +64,41 @@ func TestGetServiceByID(t *testing.T) {
assert.Equal(t, NotFound, err, "check the error")

// Test Case 3: Service don't have id
svc.ID = ""
svc.Name = "test"
svc1 := *svc
svc1.ID = ""
config = data.Configuration{
Services: []*data.Service{svc},
Services: []*data.Service{&svc1},
}

db, _ = NewMemDB(&config)
service, err = db.GetServiceByID("test")
service, err = db.GetServiceByID("svc")
assert.Nil(t, err, "check the error")
assert.Equal(t, svc, service, "check the service")
}

func TestGetRouteByID(t *testing.T) {
// Test Case 1: get route by id
config := data.Configuration{
Routes: []*data.Route{route},
}

db, _ := NewMemDB(&config)
route1, err := db.GetRouteByID("route")
assert.Nil(t, err, "check the error")
assert.Equal(t, route, route1, "check the route")

// Test Case 2: get route by id (not found)
route1, err = db.GetRouteByID("not-found")
assert.Equal(t, NotFound, err, "check the error")

// Test Case 3: Route don't have id
route.ID = ""
config = data.Configuration{
Routes: []*data.Route{route},
}

db, _ = NewMemDB(&config)
route1, err = db.GetRouteByID("route")
assert.Nil(t, err, "check the error")
assert.Equal(t, route, route1, "check the route")
}
65 changes: 61 additions & 4 deletions internal/pkg/differ/differ.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,19 +35,23 @@ func (d *Differ) Diff() ([]*data.Event, error) {
var events []*data.Event
var err error

serviceEvents, err := d.diffService()
serviceEvents, err := d.diffServices()
if err != nil {
return nil, err
}
events = append(events, serviceEvents...)

// TODO: diff routes
routeEvents, err := d.diffRoutes()
if err != nil {
return nil, err
}
events = append(events, serviceEvents...)
events = append(events, routeEvents...)

return events, nil
}

// diffService compares the services between local and remote.
func (d *Differ) diffService() ([]*data.Event, error) {
func (d *Differ) diffServices() ([]*data.Event, error) {
var events []*data.Event
var mark = make(map[string]bool)

Expand Down Expand Up @@ -98,3 +102,56 @@ func (d *Differ) diffService() ([]*data.Event, error) {

return events, nil
}

// diffRoutes compares the routes between local and remote.
func (d *Differ) diffRoutes() ([]*data.Event, error) {
var events []*data.Event
var mark = make(map[string]bool)

for _, remoteRoute := range d.remoteConfig.Routes {
localRoute, err := d.localDB.GetRouteByID(remoteRoute.ID)
if err != nil {
// If we can't find the route in local, it means the route should be deleted.
// So we add a delete event and the value is the route from remote.
if err == db.NotFound {
e := data.Event{
ResourceType: data.RouteResourceType,
Option: data.DeleteOption,
Value: remoteRoute,
}
events = append(events, &e)
continue
}

return nil, err
}

mark[localRoute.ID] = true
// If the route is equal, we don't need to add an event.
// Else, we use the local routes to update the remote routes.
if equal := reflect.DeepEqual(localRoute, remoteRoute); equal {
continue
}

events = append(events, &data.Event{
ResourceType: data.RouteResourceType,
Option: data.UpdateOption,
Value: localRoute,
})
}

// If the route is not in the remote configuration, it means the route should be created.
for _, route := range d.localConfig.Routes {
if mark[route.ID] {
continue
}

events = append(events, &data.Event{
ResourceType: data.RouteResourceType,
Option: data.CreateOption,
Value: route,
})
}

return events, nil
}
Loading

0 comments on commit 69d38b9

Please sign in to comment.