Skip to content

Commit 411b159

Browse files
committed
feat(istio): rate limit example
1 parent c4c8040 commit 411b159

File tree

7 files changed

+79
-10
lines changed

7 files changed

+79
-10
lines changed
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
apiVersion: config.istio.io/v1alpha2
2+
kind: memquota
3+
metadata:
4+
name: handler
5+
namespace: istio-system
6+
spec:
7+
quotas:
8+
- name: requestcount.quota.istio-system
9+
maxAmount: 5000 # default max requests per second
10+
validDuration: 1s
11+
overrides:
12+
- dimensions:
13+
destination: github
14+
source: api
15+
sourceVersion: v2
16+
maxAmount: 1 # api v2 can send 1 request to github per seconds
17+
validDuration: 1s
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#!/bin/bash
2+
3+
DIR=$(pwd)
4+
5+
kubectl delete -f <(istioctl kube-inject -f $DIR/adapter.yml)
6+
kubectl delete -f <(istioctl kube-inject -f $DIR/instance.yml)
7+
kubectl delete -f <(istioctl kube-inject -f $DIR/rule.yml)
8+
9+
kubectl apply -f <(istioctl kube-inject -f $DIR/adapter.yml)
10+
kubectl apply -f <(istioctl kube-inject -f $DIR/instance.yml)
11+
kubectl apply -f <(istioctl kube-inject -f $DIR/rule.yml)
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
apiVersion: config.istio.io/v1alpha2
2+
kind: quota
3+
metadata:
4+
name: requestcount
5+
namespace: istio-system
6+
spec:
7+
dimensions:
8+
source: source.labels["app"] | source.service | "unknown"
9+
sourceVersion: source.labels["version"] | "unknown"
10+
destination: destination.labels["app"] | destination.service | "unknown"
11+
destinationVersion: destination.labels["version"] | "unknown"
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
apiVersion: config.istio.io/v1alpha2
2+
kind: rule
3+
metadata:
4+
name: quota
5+
namespace: istio-system
6+
spec:
7+
actions:
8+
- handler: handler.memquota
9+
instances:
10+
- requestcount.quota

istio_examples/Services/api/src/server.js

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,26 @@ app.get('/api', (req, res) => res.json({
1212
requestedVersion: req.headers.version,
1313
version: VERSION
1414
}))
15+
1516
app.get('/api/git', async (req, res) => {
1617
try {
17-
const response = await request({
18-
method: 'GET',
19-
uri: `http://github.default.svc.cluster.local:5000/api/v1`
20-
})
21-
return res.json(JSON.parse(response))
18+
return res.json(JSON.parse(
19+
await request({
20+
method: 'GET',
21+
uri: `http://github.default.svc.cluster.local:5000/api/hello`
22+
})))
23+
} catch (err) {
24+
console.log(err)
25+
return res.sendStatus(500)
26+
}
27+
})
28+
app.get('/api/git/repo', async (req, res) => {
29+
try {
30+
return res.json(JSON.parse(
31+
await request({
32+
method: 'GET',
33+
uri: `http://github.default.svc.cluster.local:5000/api/search/repo`
34+
})))
2235
} catch (err) {
2336
console.log(err)
2437
return res.sendStatus(500)

istio_examples/Services/github/src/server.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,10 @@ app.use(cors())
88
const PORT = process.env.PORT
99

1010
app.get('/healthz', (req, res) => res.sendStatus(200))
11-
app.get('/api/v1', async (req, res) => {
11+
app.get('/api/hello', (req, res) => res.json({
12+
response: 'Hello world from github service in cluster!'
13+
}))
14+
app.get('/api/search/repo', async (req, res) => {
1215
try {
1316
const {items} = await request({
1417
method: 'GET',

istio_examples/Services/ui/src/src/App.js

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,9 @@ class App extends Component {
1010
}
1111
this.callAPIv1 = this.callAPI.bind(this, 'api', {'version': 'v1'})
1212
this.callAPIv2 = this.callAPI.bind(this, 'api', {'version': 'v2'})
13-
this.callAPIgit = this.callAPI.bind(this, 'api/git', {'version': 'v1'})
13+
this.callAPIgit = this.callAPI.bind(this, '/api/git/repo', {'version': 'v1'})
14+
this.callAPIgitv1 = this.callAPI.bind(this, '/api/git', {'version': 'v1'})
15+
this.callAPIgitv2 = this.callAPI.bind(this, '/api/git', {'version': 'v2'})
1416
}
1517

1618
callAPI(endpoint, headers) {
@@ -36,9 +38,11 @@ class App extends Component {
3638

3739
return (
3840
<div className="App">
39-
<button onClick={this.callAPIv1}>Call API v1</button>
40-
<button onClick={this.callAPIv2}>Call API v2</button>
41-
<button onClick={this.callAPIgit}>Call API git</button>
41+
<button onClick={this.callAPIv1}>Call /api (v1)</button>
42+
<button onClick={this.callAPIv2}>Call /api (v2)</button>
43+
<button onClick={this.callAPIgitv1}>Call /api/git (v1)</button>
44+
<button onClick={this.callAPIgitv2}>Call /api/git (v2)</button>
45+
<button onClick={this.callAPIgit}>Call /api/git/repo (v1)</button>
4246
<p></p>
4347
{!this.state.error
4448
? _.map(data, (value, key) => {return(<div key={key}>{key}: <b>{value.toString()}</b></div>)})

0 commit comments

Comments
 (0)