1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
|
def svc_get():
"""
查询一个service
:return:
"""
# 查询
for svc in core_api.list_namespaced_service(namespace="default").items:
print(svc.metadata.name)
def svc_delete():
"""
删除一个service
:return:
"""
namespace = "default"
name = "api-test"
body = client.V1DeleteOptions()
try:
core_api.delete_namespaced_service(namespace=namespace, name=name, body=body)
except Exception as e:
status = getattr(e, "status")
if status == 404:
print("没找到")
elif status == 403:
print("没权限")
elif status == 409:
print("冲突")
def svc_create():
"""
创建一个service,命名空间默认为default,如果要创建其他命名空间的,需要先创建命名空间
:return:
"""
namespace = "default"
name = "api-test"
selector = {'a': '1', 'b': '2'} # 不区分数据类型,都要加引号
port = 80
target_port = 80
type = "NodePort"
body = client.V1Service(
api_version="v1",
kind="Service",
metadata=client.V1ObjectMeta(
name=name
),
spec=client.V1ServiceSpec(
selector=selector,
ports=[client.V1ServicePort(
port=port,
target_port=target_port
)],
type=type
)
)
try:
core_api.create_namespaced_service(namespace=namespace, body=body)
except Exception as e:
status = getattr(e, "status")
if status == 400:
print(e)
print("格式错误")
elif status == 403:
print("没权限")
def svc_update():
"""
更新一个service
:return:
"""
namespace = "default"
name = "api-test"
body = client.V1Service(
api_version="v1",
kind="Service",
metadata=client.V1ObjectMeta(
name=name
),
spec=client.V1ServiceSpec(
selector={'a': '1', 'b': '2'},
ports=[client.V1ServicePort(
port=80,
target_port=8080
)],
type="NodePort"
)
)
try:
core_api.patch_namespaced_service(namespace=namespace, name=name, body=body)
except Exception as e:
status = getattr(e, "status")
if status == 400:
print(e)
print("格式错误")
elif status == 403:
print("没权限")
|