首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >Python客户端,用于OpenShift API和执行oc登录。

Python客户端,用于OpenShift API和执行oc登录。
EN

Stack Overflow用户
提问于 2018-03-02 18:04:14
回答 1查看 2.8K关注 0票数 1

我已经安装了这个python API:

https://github.com/openshift/openshift-restclient-python

我正在尝试理解如何将其用于基本操作,这些操作通常是使用open shift客户端在命令行上执行的,例如:

oc login [my-openshift-instance]
oc project [my-project]

但是在上面的API中我似乎找不到这些基本的操作。该API是为其他目的而设计的,还是我只是错过了这些调用?

我试过了:

from openshift import client, config
config.load_kube_config()
oapi = client.OapiApi()
project_list = oapi.list_project()
for project in project_list.items:
    print (project.metadata.name)

但它不会让我指定打开的shift实例和user/pass。

EN

回答 1

Stack Overflow用户

发布于 2018-06-29 04:22:10

这主要遵循dynamic client usage中的配置,但我使用了kubernetes.config.new_client_from_config() (以支持多个客户机),其中配置文件名是/root/.kube/config中的open shift配置(危险:王国的密钥,相应地进行保护)

$ python
Python 2.7.13 (default, Jul 18 2017, 09:17:00) 
[GCC 4.2.1 Compatible Apple LLVM 8.1.0 (clang-802.0.42)] on darwin
Type "help", "copyright", "credits" or "license" for more information.

>>> import kubernetes
>>> import openshift.dynamic
>>> import yaml

>>> k8s_client = kubernetes.config.new_client_from_config('config/k8s.10.150.114.10.config')
>>> dyn_client = openshift.dynamic.DynamicClient(k8s_client)
>>> v1_services = dyn_client.resources.get(api_version='extensions/v1beta1', kind='Deployment')

>>> service_data = yaml.load(open('squawker_stage.yaml'))

>>> print service_data
{'kind': 'Deployment', 'spec': {'template': {'spec': {'containers': 
[{'image': 'docker.com/lrmcfarland/squawker', 'name': 'squawker', 
'ports': [{'containerPort': 8000}]}]}, 'metadata': {'labels': {'app': 
'squawker'}}}, 'replicas': 3}, 'apiVersion': 'extensions/v1beta1', 
'metadata': {'namespace': 'squawker-staging', 'name': 'squawker'}}

此时,我需要在GUI中创建项目squawker-staging,但将其留空。我仍然在阅读如何做到这一点的优秀手册,并将在我这样做的时候更新。

>>> resp = v1_services.create(body=service_data, namespace='squawker-staging')

>>> print(resp.metadata)
{u'creationTimestamp': u'2018-06-28T18:23:14Z',
 u'generation': 1,
 u'labels': {u'app': u'squawker'},
 u'name': u'squawker',
 u'namespace': u'squawker-staging',
 u'resourceVersion': u'1855021',
 u'selfLink': u'/apis/extensions/v1beta1/namespaces/squawker- 
 staging/deployments/squawker',
 u'uid': u'5268fa31-7b00-11e8-9e65-005056887570'}

Squawker是我用于容器通信测试的简单flask服务器。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/49066856

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档