gpt4 book ai didi

kubernetes - Kubernetes Python API用于创建自定义对象

转载 作者:行者123 更新时间:2023-12-02 12:08:40 24 4
gpt4 key购买 nike

是否可以使用python API在kubernetes中创建“自定义对象”?

编辑:

通过自定义对象,我指的是this

谢谢。

最佳答案

尚无现成的API。这是使用https://github.com/kubernetes-incubator/client-python的基本示例

import json
from kubernetes import client, config

class ThingyApi(object):
def __init__(self):
config = client.Configuration()
if not config.api_client:
config.api_client = client.ApiClient()
self.api_client = config.api_client

def create_thingy(self, body, namespace='default'):
resource_path = '/apis/example.com/v1/namespaces/' + namespace + '/thingies'
header_params = {}
header_params['Accept'] = self.api_client.select_header_accept(['application/json'])
header_params['Content-Type'] = self.api_client.select_header_content_type(['*/*'])

(resp, code, header) = self.api_client.call_api(
resource_path, 'POST', {'namespace': namespace}, {}, header_params, body, [], _preload_content=False)

return json.loads(resp.data.decode('utf-8'))

config.load_kube_config()

# Create the ThirdPartyResource (Thingy.example.com)
resp = client.ExtensionsV1beta1Api().create_third_party_resource(body={
'apiVersion': 'extensions/v1beta1',
'kind': 'ThirdPartyResource',
'metadata': {'name': 'thingy.example.com'},
'description': 'For storage of Thingy objects',
'versions': [{'name': 'v1'}]})
print("ThirdPartyResource created")
print(str(resp))

# Create 3 Thingy objects (mything-{1,2,3})
thingyapi = ThingyApi()
for i in range(1, 4):
obj = {'apiVersion': 'example.com/v1',
'metadata': {'name': 'mythingy-'+str(i)},
'kind': 'Thingy',
# Arbitrary contents
'key': 'value',
'array': [40, 2],
'object': {'foo': 'bar'}}
resp = thingyapi.create_thingy(body=obj, namespace='default')
print(str(resp))

输出将是这样的:
$ bin/python test.py                                                                                                                                                                    
ThirdPartyResource created
{'api_version': 'extensions/v1beta1',
'description': 'For storage of Thingy objects',
'kind': 'ThirdPartyResource',
'metadata': {'annotations': None,
'cluster_name': None,
'creation_timestamp': u'2017-03-14T13:57:07Z',
'deletion_grace_period_seconds': None,
'deletion_timestamp': None,
'finalizers': None,
'generate_name': None,
'generation': None,
'labels': None,
'name': 'thingy.example.com',
'namespace': None,
'owner_references': None,
'resource_version': '59942',
'self_link': '/apis/extensions/v1beta1/thirdpartyresourcesthingy.example.com',
'uid': '1c596824-08be-11e7-9a5f-5254000f561a'},
'versions': [{'name': 'v1'}]}
{u'kind': u'Thingy', u'object': {u'foo': u'bar'}, u'apiVersion': u'example.com/v1', u'key': u'value', u'array': [40, 2], u'metadata': {u'name': u'mythingy-1', u'namespace': u'default', u'resourceVersion': u'59943', u'creationTimestamp': u'2017-03-14T13:57:07Z', u'selfLink': u'/apis/example.com/v1/namespaces/default/thingies/mythingy-1', u'uid': u'1c59f7ae-08be-11e7-9a5f-5254000f561a'}}
{u'kind': u'Thingy', u'object': {u'foo': u'bar'}, u'apiVersion': u'example.com/v1', u'key': u'value', u'array': [40, 2], u'metadata': {u'name': u'mythingy-2', u'namespace': u'default', u'resourceVersion': u'59944', u'creationTimestamp': u'2017-03-14T13:57:07Z', u'selfLink': u'/apis/example.com/v1/namespaces/default/thingies/mythingy-2', u'uid': u'1c5be2a7-08be-11e7-9a5f-5254000f561a'}}
{u'kind': u'Thingy', u'object': {u'foo': u'bar'}, u'apiVersion': u'example.com/v1', u'key': u'value', u'array': [40, 2], u'metadata': {u'name': u'mythingy-3', u'namespace': u'default', u'resourceVersion': u'59945', u'creationTimestamp': u'2017-03-14T13:57:07Z', u'selfLink': u'/apis/example.com/v1/namespaces/default/thingies/mythingy-3', u'uid': u'1c5c390e-08be-11e7-9a5f-5254000f561a'}}

不要忘记在以下时间运行:
kubectl delete thingy --all
kubectl delete thirdpartyresource thingy.example.com

关于kubernetes - Kubernetes Python API用于创建自定义对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42766441/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com