Skip to content

Commit 8278ed9

Browse files
committed
0.0.1 version finished.
1 parent 2a14359 commit 8278ed9

10 files changed

+512
-0
lines changed

README.md

+130
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
# kubesql
2+
3+
kubesql is a tool to use sql to query the resources of kubernetes.
4+
5+
The resources of kubernetes such as nodes, pods and so on are handled as the
6+
7+
For example, all pods are easily to list from apiserver. But the number of pods on each node is not easy to caculate.
8+
9+
With kubesql, a sql statement can achieve it like this.
10+
11+
```
12+
[root@localhost kubesql]# kubesql "select hostIp, count(*) from pods group by hostIp"
13+
+----------+----------------+
14+
| count(*) | hostIP |
15+
+----------+----------------+
16+
| 9 | None |
17+
| 4 | 22.2.22.222 |
18+
| 14 | 11.1.111.11 |
19+
+----------+----------------+
20+
```
21+
22+
How many pod are pending
23+
24+
```
25+
[root@localhost kubesql]# kubesql "select count(*) from pods where phase = 'Pending'"
26+
+----------+
27+
| count(*) |
28+
+----------+
29+
| 29 |
30+
+----------+
31+
```
32+
33+
34+
# compoments
35+
36+
kubesql has three compoments.
37+
38+
- kubesql-watch: Watch the events from kube-apiserver, and write it to sqlite3.
39+
- kubesql-server: Provide a http api for query. Accepts the sql query , execute the query in sqlite3 and return the query result.
40+
- kubesql-client: Send the query sql to kubesql-server and get the result, then print the result in table format.
41+
42+
```
43+
+----------------+ watch +---------------+ +---------+
44+
| kube-apiserver | -------> | kubesql-watch | --> | sqlite3 |
45+
+----------------+ +---------------+ +---------+
46+
^
47+
|
48+
|
49+
+----------------+ http +---------------+ |
50+
| kubesql-client | -------> | kubsql-server | ------+
51+
+----------------+ +---------------+
52+
```
53+
54+
# install and deploy
55+
56+
## manualy install and deploy
57+
58+
install
59+
60+
```
61+
//check out the code
62+
pip install requirements.txt
63+
python setup.py install
64+
cp -r etc/kubesql /etc
65+
```
66+
67+
check the config of `/etc/kubesql/config`, and modify the kubeconfig. kubeconfig is for kubesql-watch to connect to the apiserver.
68+
69+
```
70+
nohup kubesql-watch &
71+
nohup kubesql-server &
72+
```
73+
74+
75+
# Usage
76+
77+
kubesql command is short for kubesql-client. It is used to send the query and show the result in table.
78+
79+
```
80+
[root@localhost kubesql]# kubesql -h
81+
usage: kubesql [-h] [-t TABLE] [-a] [sql]
82+
83+
positional arguments:
84+
sql execte the sql.
85+
86+
optional arguments:
87+
-h, --help show this help message and exit
88+
-t TABLE, --table TABLE
89+
increase output verbosity
90+
-a, --all show all tables
91+
```
92+
93+
`kubesql -a` can list the tables currently supported.
94+
95+
```
96+
[root@localhost kubesql]# kubesql -a
97+
+------------+
98+
| table_name |
99+
+------------+
100+
| pods |
101+
| nodes |
102+
+------------+
103+
```
104+
105+
And `kubesql -t {table_name}` can list the columns for `table_name` currently supported.
106+
107+
```
108+
[root@localhost kubesql]# kubesql -t nodes
109+
+-------------------------+-----+------------+---------+----+-----------+
110+
| name | cid | dflt_value | notnull | pk | type |
111+
+-------------------------+-----+------------+---------+----+-----------+
112+
| name | 0 | None | 0 | 0 | char(200) |
113+
| uid | 1 | None | 0 | 0 | char(200) |
114+
| creationTimestamp | 2 | None | 0 | 0 | datetime |
115+
| deletionTimestamp | 3 | None | 0 | 0 | datetime |
116+
| zone | 4 | None | 0 | 0 | char(200) |
117+
| allocatable_cpu | 5 | None | 0 | 0 | char(200) |
118+
| allocatable_memory | 6 | None | 0 | 0 | char(200) |
119+
| allocatable_pods | 7 | None | 0 | 0 | char(200) |
120+
| capacity_cpu | 8 | None | 0 | 0 | char(200) |
121+
| capacity_memory | 9 | None | 0 | 0 | char(200) |
122+
| capacity_pods | 10 | None | 0 | 0 | char(200) |
123+
| architecture | 11 | None | 0 | 0 | char(200) |
124+
| containerRuntimeVersion | 12 | None | 0 | 0 | char(200) |
125+
| kubeProxyVersion | 13 | None | 0 | 0 | char(200) |
126+
| kubeletVersion | 14 | None | 0 | 0 | char(200) |
127+
| operatingSystem | 15 | None | 0 | 0 | char(200) |
128+
| osImage | 16 | None | 0 | 0 | char(200) |
129+
+-------------------------+-----+------------+---------+----+-----------+
130+
```

etc/kubesql/config

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
[kubesql]
2+
port=415
3+
ip=127.0.0.1
4+
db_path=/dev/shm/kubesql.db
5+
param_path=/etc/kubesql/params
6+
kubeconfig_path=/etc/kubeconfig

etc/kubesql/params

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
---
2+
pod
3+
metadata.name
4+
metadata.uid
5+
metadata.namespace
6+
metadata.creationTimestamp
7+
metadata.deletionTimestamp
8+
spec.nodeName
9+
spec.schedulerName
10+
status.hostIP
11+
status.phase
12+
status.podIP
13+
status.reason
14+
status.startTime
15+
---
16+
node
17+
metadata.name
18+
metadata.uid
19+
metadata.creationTimestamp
20+
metadata.deletionTimestamp
21+
status.allocatable.cpu allocatable_cpu
22+
status.allocatable.memory allocatable_memory
23+
status.allocatable.pods allocatable_pods
24+
status.capacity.cpu capacity_cpu
25+
status.capacity.memory capacity_memory
26+
status.capacity.pods capacity_pods
27+
status.nodeInfo.architecture
28+
status.nodeInfo.containerRuntimeVersion
29+
status.nodeInfo.kubeProxyVersion
30+
status.nodeInfo.kubeletVersion
31+
status.nodeInfo.operatingSystem
32+
status.nodeInfo.osImage

kubesql/__init__.py

Whitespace-only changes.

kubesql/client.py

+56
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
import prettytable as pt
2+
import argparse
3+
import httplib
4+
import json
5+
from kubesql import utils
6+
7+
cfg = utils.load_config()
8+
9+
def get_kube_sql(sql):
10+
conn = httplib.HTTPConnection("%s:%s" % (cfg.get("ip"), cfg.get("port")))
11+
params = {'sql': sql}
12+
headers = {'Content-type': 'application/json'}
13+
conn.request("POST", "/sql", headers=headers, body=json.dumps(params))
14+
try:
15+
response = conn.getresponse()
16+
# print params, response.status, response.reason
17+
data = response.read()
18+
return json.loads(data)
19+
except Exception, e:
20+
print e, params
21+
conn.close()
22+
23+
24+
def print_json_as_table(result):
25+
if result:
26+
tb = pt.PrettyTable()
27+
row = result[0]
28+
tb.field_names = row.keys()
29+
for row in result:
30+
row_value = []
31+
for field_name in tb.field_names:
32+
row_value.append(row.get(field_name))
33+
tb.add_row(row_value)
34+
tb.align = "l"
35+
print(tb)
36+
37+
38+
parser = argparse.ArgumentParser()
39+
parser.add_argument("sql", nargs="?", type=str, help="execte the sql.")
40+
parser.add_argument("-t", "--table", help="increase output verbosity")
41+
parser.add_argument("-a", "--all", action='store_true', help="show all tables")
42+
args = parser.parse_args()
43+
44+
45+
def main():
46+
if args.sql:
47+
result = get_kube_sql(args.sql)
48+
elif args.table:
49+
result = get_kube_sql('PRAGMA table_info(%s)' % args.table)
50+
elif args.all:
51+
result = get_kube_sql('SELECT name as table_name FROM sqlite_master WHERE type="table"')
52+
print_json_as_table(result)
53+
54+
55+
if __name__ == '__main__':
56+
main()

0 commit comments

Comments
 (0)