Skip to content

Commit c7e9b7e

Browse files
committed
refactoring: updated examples/kv to latest cartridge
1 parent a5c46b7 commit c7e9b7e

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

48 files changed

+648
-958
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
1515
- Bump operator-sdk version (and other dependencies)
1616
- Refactor project structure, all helm charts are collected in one place
1717
- Update crds api version from `apiextensions.k8s.io/v1beta1` to `apiextensions.k8s.io/v1`
18+
- Updated kv example to the most recent cartridge version
1819

1920
### Fixed
2021

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ ReplicasetTemplate.
2020
* [Resource ownership](#resource-ownership)
2121
* [Documentation](#documentation)
2222
* [Deploying the Tarantool operator on minikube](#deploying-the-tarantool-operator-on-minikube)
23-
* [Example: key-value storage](#example-key-value-storage)
23+
* [Example: key-value storage](#example-application-key-value-storage)
2424
* [Application topology](#application-topology)
2525
* [Running the application](#running-the-application)
2626
* [Scaling the application](#scaling-the-application)

examples/kv/.cartridge.yml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
---
2+
# Here you can specify default parameters for local running, such as:
3+
4+
# cfg: path-to-cfg-file
5+
# log-dir: path-to-log-dir
6+
# run-dir: path-to-run-dir
7+
# data-dir: path-to-data-dir
8+
9+
stateboard: false # If no arguments are supplied, --stateboard flag is true

examples/kv/.gitignore

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
.rocks
2+
.swo
3+
.swp
4+
CMakeCache.txt
5+
CMakeFiles
6+
cmake_install.cmake
7+
*.dylib
8+
*.idea
9+
__pycache__
10+
*pyc
11+
.cache
12+
.pytest_cache
13+
.vagrant
14+
.DS_Store
15+
*.xlog
16+
*.snap
17+
*.rpm
18+
*.deb
19+
*.tar.gz
20+
node_modules
21+
/tmp/*
22+
!/tmp/.keep

examples/kv/.luacheckrc

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
include_files = {'**/*.lua', '*.luacheckrc', '*.rockspec'}
2+
exclude_files = {'.rocks/', 'tmp/'}
3+
max_line_length = 120
4+
redefined = false

examples/kv/.luacov

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
2+
statsfile = 'tmp/luacov.stats.out'
3+
reportfile = 'tmp/luacov.report.out'
4+
exclude = {
5+
'/test/',
6+
}

examples/kv/Dockerfile

Lines changed: 0 additions & 22 deletions
This file was deleted.
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# Simple Dockerfile
2+
# Used by "pack" command as a base for build image
3+
# when --use-docker option is specified
4+
#
5+
# Image based on centos:7 is expected to be used
6+
FROM centos:7
7+
8+
# Here you can install some packages required
9+
# for your application build
10+
#
11+
# RUN set -x \
12+
# && curl -sL https://rpm.nodesource.com/setup_10.x | bash - \
13+
# && yum -y install nodejs

examples/kv/Dockerfile.cartridge

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# Simple Dockerfile
2+
# Used by "pack docker" command as a base for runtime image
3+
#
4+
# Image based on centos:7 is expected to be used
5+
FROM centos:7
6+
7+
# Here you can install some packages required
8+
# for your application in runtime
9+
#
10+
#
11+
# For example, if you need to install some python packages,
12+
# you can do it this way:
13+
#
14+
# COPY requirements.txt /tmp
15+
# RUN yum install -y python3-pip
16+
# RUN pip3 install -r /tmp/requirements.txt

examples/kv/Makefile

Lines changed: 0 additions & 17 deletions
This file was deleted.

examples/kv/app/admin.lua

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
local cli_admin = require('cartridge-cli-extensions.admin')
2+
3+
-- register admin function probe to use it with "cartridge admin"
4+
local function init()
5+
cli_admin.init()
6+
7+
local probe = {
8+
usage = 'Probe instance',
9+
args = {
10+
uri = {
11+
type = 'string',
12+
usage = 'Instance URI',
13+
},
14+
},
15+
call = function(opts)
16+
opts = opts or {}
17+
18+
if opts.uri == nil then
19+
return nil, "Please, pass instance URI via --uri flag"
20+
end
21+
22+
local cartridge_admin = require('cartridge.admin')
23+
local ok, err = cartridge_admin.probe_server(opts.uri)
24+
25+
if not ok then
26+
return nil, err.err
27+
end
28+
29+
return {
30+
string.format('Probe %q: OK', opts.uri),
31+
}
32+
end,
33+
}
34+
35+
local ok, err = cli_admin.register('probe', probe.usage, probe.args, probe.call)
36+
assert(ok, err)
37+
end
38+
39+
return {init = init}

examples/kv/app/roles/router.lua

Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
local cartridge = require('cartridge')
2+
local router_api = require('app.router.api')
3+
4+
local function handle_create(req)
5+
local status, body = pcall(function() return req:json() end)
6+
7+
if type(body) == 'string' then
8+
local resp = req:render{json = { info = msg }}
9+
resp.status = 500
10+
return resp
11+
end
12+
13+
if body['key'] == nil then
14+
local resp = req:render{json = { info = msg }}
15+
resp.status = 400
16+
return resp
17+
end
18+
19+
if body['value'] == nil then
20+
local resp = req:render{json = { info = msg }}
21+
resp.status = 400
22+
return resp
23+
end
24+
25+
local data, err = router_api.create(body['key'], body['value'])
26+
if err ~= nil then
27+
local resp = req:render({json = {error = err}})
28+
resp.status = 500
29+
return resp
30+
end
31+
32+
local resp = req:render({json = data})
33+
resp.status = 201
34+
35+
return resp
36+
end
37+
38+
local function handle_get(req)
39+
local key = req:stash('key')
40+
local data, err = router_api.get(key)
41+
42+
if err ~= nil then
43+
local resp = req:render{json = { info = "Key doesn't exist", msg = err and err.msg }}
44+
resp.status = 400
45+
return resp
46+
end
47+
48+
return req:render({json = data['value']})
49+
end
50+
51+
local function handle_update(req)
52+
local key = req:stash('key')
53+
local status, value = pcall(function() return req:json() end)
54+
55+
if value == nil or type(value) == 'string' then
56+
local resp = req:render{json = { info = msg }}
57+
resp.status = 500
58+
return resp
59+
end
60+
61+
local data, err = router_api.update(key, value)
62+
if err ~= nil then
63+
local resp = req:render({json = {error = err}})
64+
resp.status = 500
65+
return resp
66+
end
67+
68+
local resp = req:render({json = data})
69+
resp.status = 201
70+
71+
return resp
72+
end
73+
74+
local function handle_delete(req)
75+
local key = req:stash('key')
76+
77+
local data, err = router_api.delete(key)
78+
79+
if err ~= nil then
80+
local resp = req:render{json = { info = "Key doesn't exist", msg = err and err.msg }}
81+
resp.status = 400
82+
return resp
83+
end
84+
85+
local resp = req:render{json = { info = "Successfully deleted" }}
86+
resp.status = 200
87+
return resp
88+
end
89+
90+
local function init(opts) -- luacheck: no unused args
91+
local httpd = cartridge.service_get('httpd')
92+
if httpd ~= nil then
93+
httpd:route({ path = '/kv/', method = 'POST', public = true }, handle_create)
94+
httpd:route({ path = '/kv/:key', method = 'GET', public = true }, handle_get)
95+
httpd:route({ path = '/kv/:key', method = 'DELETE', public = true }, handle_delete)
96+
httpd:route({ path = '/kv/:key', method = 'PUT ', public = true }, handle_update)
97+
end
98+
99+
rawset(_G, 'app', {router = {api = router_api}})
100+
for name, _ in pairs(router_api) do
101+
box.schema.func.create('app.router.api.' .. name, { setuid = true, if_not_exists = true })
102+
box.schema.user.grant('admin', 'execute', 'function', 'app.router.api.' .. name, { if_not_exists = true })
103+
end
104+
105+
return true
106+
end
107+
108+
local function stop()
109+
return true
110+
end
111+
112+
local function validate_config(conf_new, conf_old) -- luacheck: no unused args
113+
return true
114+
end
115+
116+
local function apply_config(conf, opts) -- luacheck: no unused args
117+
-- if opts.is_master then
118+
-- end
119+
120+
return true
121+
end
122+
123+
return {
124+
role_name = 'app.roles.router',
125+
init = init,
126+
stop = stop,
127+
validate_config = validate_config,
128+
apply_config = apply_config,
129+
dependencies = {'cartridge.roles.vshard-router'},
130+
}

examples/kv/app/roles/storage.lua

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
local cartridge = require('cartridge')
2+
local storage_api = require('app.storage.api')
3+
4+
local function init(opts) -- luacheck: no unused args
5+
if opts.is_master then
6+
local kv_store = box.schema.space.create(
7+
'kv_store',
8+
{ if_not_exists = true }
9+
)
10+
11+
kv_store:format({
12+
{ name = 'key', type = 'string' },
13+
{ name = 'value', type = '*' },
14+
})
15+
16+
kv_store:create_index('primary',
17+
{ type = 'hash', parts = {1, 'string'}, if_not_exists = true }
18+
)
19+
20+
end
21+
22+
rawset(_G, 'app', {storage = {api = storage_api}})
23+
for name, func in pairs(storage_api) do
24+
box.schema.func.create('app.storage.api.' .. name, { setuid = true, if_not_exists = true })
25+
box.schema.user.grant('admin', 'execute', 'function', 'app.storage.api.' .. name, { if_not_exists = true })
26+
end
27+
28+
return true
29+
end
30+
31+
local function stop()
32+
return true
33+
end
34+
35+
local function validate_config(conf_new, conf_old) -- luacheck: no unused args
36+
return true
37+
end
38+
39+
local function apply_config(conf, opts) -- luacheck: no unused args
40+
-- if opts.is_master then
41+
-- end
42+
43+
return true
44+
end
45+
46+
return {
47+
role_name = 'app.roles.storage',
48+
init = init,
49+
stop = stop,
50+
validate_config = validate_config,
51+
apply_config = apply_config,
52+
dependencies = {'cartridge.roles.vshard-storage'},
53+
}

examples/kv/app/router/api.lua

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
local vshard = require('vshard')
2+
3+
local function create(key, value)
4+
local bucket_id = vshard.router.bucket_id(key)
5+
local data, err = vshard.router.callrw(bucket_id, 'app.storage.api.insert', { key, value })
6+
return data, err
7+
end
8+
9+
local function get(key)
10+
local bucket_id = vshard.router.bucket_id(key)
11+
local data, err = vshard.router.callro(bucket_id, 'app.storage.api.get', { key })
12+
return data, err
13+
end
14+
15+
local function update(key, value)
16+
local bucket_id = vshard.router.bucket_id(key)
17+
local data, err = vshard.router.callro(bucket_id, 'app.storage.api.update', { key, value })
18+
return data, err
19+
end
20+
21+
local function delete(key)
22+
local bucket_id = vshard.router.bucket_id(key)
23+
local data, err = vshard.router.callrw(bucket_id, 'app.storage.api.delete', { key })
24+
return data, err
25+
end
26+
27+
return {
28+
create = create,
29+
get = get,
30+
update = update,
31+
delete = delete,
32+
}

0 commit comments

Comments
 (0)