# OpenShift python client
[![Build Status](https://travis-ci.org/openshift/openshift-restclient-python.svg?branch=master)](https://travis-ci.org/openshift/openshift-restclient-python)
[![Coverage Status](https://coveralls.io/repos/github/openshift/openshift-restclient-python/badge.svg?branch=master)](https://coveralls.io/github/openshift/openshift-restclient-python?branch=master)
Python client for the [OpenShift](http://openshift.redhat.com/) API.
## Installation
From source:
```
git clone https://github.com/openshift/openshift-restclient-python.git
cd openshift-restclient-python
python setup.py install
```
From [PyPi](https://pypi.python.org/pypi/openshift/) directly:
```
pip install openshift
```
Using [Dockerfile](Dockerfile):
```
docker build -t openshift-restclient-python -f Dockerfile .
```
## Usage and examples
The OpenShift client depends on the [Kubernetes Python client](https://github.com/kubernetes-incubator/client-python.git), and as part of the installation process, the Kubernetes (K8s) client is automatically installed.
In the case you are using Docker, you will likely need to share your `.kube/config` with the `openshift-restclient-python` container:
```
docker run -it -v $HOME/.kube/config:/root/.kube/config:z openshift-restclient-python python
```
There are two ways this project interacts with the OpenShift API. The first, now deprecated, is to use models and functions generated with swagger from the API spec. The second, new approach, is
to use a single model and client to generically interact with all resources on the server. The dynamic client also works with
resources that are defined by aggregated API servers or Custom Resource Definitions.
### Dynamic client usage
To work with the dynamic client, you will need an instantiated kubernetes client object. For example, the following uses the dynamic client to create a new Service object:
```python
import yaml
from kubernetes import client, config
from openshift.dynamic import DynamicClient
k8s_client = config.new_client_from_config()
dyn_client = DynamicClient(k8s_client)
v1_services = dyn_client.resources.get(api_version='v1', kind='Service')
service = """
kind: Service
apiVersion: v1
metadata:
name: my-service
spec:
selector:
app: MyApp
ports:
- protocol: TCP
port: 8080
targetPort: 9376
"""
service_data = yaml.load(service)
resp = v1_services.create(body=service_data, namespace='default')
# resp is a ResourceInstance object
print(resp.metadata)
```
Now in the following example, we use the dynamic client to create a Route object, and associate it with the new Service:
```python
import yaml
from kubernetes import client, config
from openshift.dynamic import DynamicClient
k8s_client = config.new_client_from_config()
dyn_client = DynamicClient(k8s_client)
v1_routes = dyn_client.resources.get(api_version='route.openshift.io/v1', kind='Route')
route = """
apiVersion: route.openshift.io/v1
kind: Route
metadata:
name: frontend
spec:
host: www.example.com
to:
kind: Service
name: my-service
"""
route_data = yaml.load(route)
resp = v1_routes.create(body=route_data, namespace='default')
# resp is a ResourceInstance object
print(resp.metadata)
```
And finally, the following uses the dynamic client to list Projects the user can access:
```python
from kubernetes import client, config
from openshift.dynamic import DynamicClient
k8s_client = config.new_client_from_config()
dyn_client = DynamicClient(k8s_client)
v1_projects = dyn_client.resources.get(api_version='project.openshift.io/v1', kind='Project')
project_list = v1_projects.get()
for project in project_list.items:
print(project.metadata.name)
```
#### Available methods for resources
The generic Resource class supports the following methods, though every resource kind does not support every method.
#### `get(name=None, namespace=None, label_selector=None, field_selector=None, **kwargs)`
Query for a resource in the cluster. Will return a `ResourceInstance` object or raise a `NotFoundError`
```python
v1_services = dyn_client.resources.get(api_version='v1', kind='Service')
# Gets the specific Service named 'example' from the 'test' namespace
v1_services.get(name='example', namespace='test')
# Lists all Services in the 'test' namespace
v1_services.get(namespace='test')
# Lists all Services in the cluster (requires high permission level)
v1_services.get()
# Gets all Services in the 'test' namespace with the 'app' label set to 'foo'
v1_services.get(namespace='test', label_selector='app=foo')
# Gets all Services except for those in the 'default' namespace
v1_services.get(field_selector='metadata.namespace!=default')
```
#### `get(body=None, namespace=None, **kwargs)`
Query for a resource in the cluster. Will return a `ResourceInstance` object or raise a `NotFoundError`
For List kind resources (ie, the resource name ends in `List`), the `get` implementation is slightly different.
Rather than taking a name, they take a `*List` kind definition and call `get` for each definition in the list.
```python
v1_service_list = dyn_client.resources.get(api_version='v1', kind='ServiceList')
body = {
'kind': 'ServiceList',
'apiVersion': 'v1',
'items': [
'metadata': {'name': 'my-service'},
'spec': {
'selector': {'app': 'MyApp'},
'ports': [{
'protocol': 'TCP',
'port': '8080',
'targetPort': '9376'
}]
}
],
# More definitions would go here
}
# Gets the specified Service(s) from the 'test' namespace
v1_service_list.get(body=body, namespace='test')
# Lists all Services in the 'test' namespace
v1_service_list.get(namespace='test')
# Lists all Services in the cluster (requires high permission level)
v1_service_list.get()
```
#### `create(body=None, namespace=None, **kwargs)`
```python
v1_services = dyn_client.resources.get(api_version='v1', kind='Service')
body = {
'kind': 'Service',
'apiVersion': 'v1',
'metadata': {'name': 'my-service'},
'spec': {
'selector': {'app': 'MyApp'},
'ports': [{
'protocol': 'TCP',
'port': '8080',
'targetPort': '9376'
}]
}
}
# Creates the above service in the 'test' namespace
v1_services.create(body=body, namespace='test')
```
The `create` implementation is the same for `*List` kinds, except that each definition in the list will be created separately.
If the resource is namespaced (ie, not cluster-level), then one of `namespace`, `label_selector`, or `field_selector` is required.
If the resource is cluster-level, then one of `name`, `label_selector`, or `field_selector` is required.
#### `delete(name=None, namespace=None, label_selector=None, field_selector=None, **kwargs)`
```python
v1_services = dyn_client.resources.get(api_version='v1', kind='Service')
# Deletes the specific Service named 'example' from the 'test' namespace
v1_services.delete(name='my-service', namespace='test')
# Deletes all Services in the 'test' namespace
v1_services.delete(namespace='test')
# Deletes all Services in the 'test' namespace with the 'app' label set to 'foo'
v1_services.delete(namespace='test', label_selector='app=foo')
# Deletes all Services except for those in the 'default' namespace
v1_services.delete(field_selector='metadata.namespace!=default')
```
#### `delete(body=None, namespace=None, **kwargs)`
For List kind resources (ie, the resource name ends in `List`), the `delete` implementation is slightly different.
Rather than taking a name, they take a `*List` kind definition and call `delete` for each definition in the list.
```python
v1_service_list = dyn_client.resources.get(api_version='v1', kind='ServiceList')
body = {
'kind': 'ServiceList',
'apiVersion': 'v1',
'items': [
'metadata': {'name': 'my-service'},
'spec': {
'selector': {'app': 'MyApp'},
'ports': [{
'protocol': 'TCP',
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
PyPI 官网下载 | openshift-0.8.4.tar.gz (494个子文件)
setup.cfg 722B
custom_objects_spec.json 17KB
README.md 14KB
PKG-INFO 889B
PKG-INFO 889B
oapi_api.py 1.7MB
authorization_openshift_io_v1_api.py 307KB
image_openshift_io_v1_api.py 236KB
network_openshift_io_v1_api.py 220KB
template_openshift_io_v1_api.py 209KB
oauth_openshift_io_v1_api.py 201KB
build_openshift_io_v1_api.py 186KB
user_openshift_io_v1_api.py 171KB
apps_openshift_io_v1_api.py 129KB
quota_openshift_io_v1_api.py 99KB
route_openshift_io_v1_api.py 90KB
security_openshift_io_v1_api.py 86KB
project_openshift_io_v1_api.py 55KB
test_oapi_api.py 43KB
v1_security_context_constraints.py 34KB
ansible.py 33KB
client.py 30KB
base.py 24KB
v1_local_subject_access_review.py 20KB
v1_subject_access_review.py 20KB
v1_build_config_spec.py 19KB
v1_o_auth_client.py 19KB
__init__.py 18KB
v1_image.py 18KB
docstrings.py 18KB
v1_build_request.py 17KB
v1_local_resource_access_review.py 17KB
v1_build_status.py 17KB
v1_resource_access_review.py 17KB
v1_o_auth_access_token.py 17KB
preprocess_spec.py 17KB
v1_o_auth_authorize_token.py 16KB
v1_image_signature.py 16KB
v1_build_spec.py 15KB
v1_deployment_config_spec.py 15KB
v1_deployment_config_status.py 15KB
__init__.py 15KB
v1_rolling_deployment_strategy_params.py 15KB
v1_docker_build_strategy.py 14KB
v1_build_source.py 14KB
v1_cluster_role_binding.py 14KB
v1_cluster_network.py 14KB
v1_role_binding.py 14KB
v1_image_stream_tag.py 13KB
v1_deployment_strategy.py 13KB
template_openshift_io_api.py 13KB
v1_template.py 13KB
v1_tag_reference.py 13KB
v1_policy_rule.py 13KB
v1_custom_build_strategy.py 12KB
v1_tls_config.py 12KB
v1_parameter.py 12KB
v1_route_spec.py 12KB
v1_o_auth_client_authorization.py 12KB
v1_identity.py 12KB
v1_host_subnet.py 12KB
v1_deployment_config_rollback_spec.py 12KB
v1_deployment_request.py 12KB
v1_build_trigger_cause.py 12KB
v1_net_namespace.py 12KB
test_authorization_openshift_io_v1_api.py 11KB
v1_user.py 11KB
v1_source_build_strategy.py 11KB
v1_deployment_config_rollback.py 11KB
v1_tag_event_condition.py 11KB
v1_cluster_role.py 11KB
v1_applied_cluster_resource_quota.py 11KB
v1_deployment_condition.py 11KB
v1_signature_condition.py 10KB
v1_build_trigger_policy.py 10KB
v1_deployment_trigger_image_change_params.py 10KB
v1_cluster_resource_quota.py 10KB
v1_image_stream_import.py 10KB
v1_build_strategy.py 10KB
v1_template_instance.py 10KB
v1_build_config.py 10KB
v1_project_request.py 10KB
v1_deployment_config.py 10KB
v1_template_instance_condition.py 10KB
v1_route_ingress.py 10KB
v1_image_stream_mapping.py 10KB
v1_user_identity_mapping.py 10KB
v1_image_import_spec.py 10KB
v1_image_stream.py 10KB
v1_pod_security_policy_self_subject_review.py 10KB
v1_route.py 10KB
v1_pod_security_policy_subject_review.py 10KB
v1_project.py 10KB
v1_route_ingress_condition.py 10KB
v1_security_context_constraints_list.py 10KB
v1_build.py 10KB
v1_applied_cluster_resource_quota_list.py 10KB
v1_o_auth_client_authorization_list.py 10KB
v1_pod_security_policy_review.py 10KB
v1_role_binding_restriction_list.py 10KB
共 494 条
- 1
- 2
- 3
- 4
- 5
资源评论
挣扎的蓝藻
- 粉丝: 12w+
- 资源: 15万+
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
- 126641091882064LSPatch.apk
- 5G网络基础培训课件.zip
- 2024-spring-HIT-CS-大作业
- yolo目标检测项目实验
- downloadFile-1.hc
- C++课程设计:基于Qt的航班信息管理系统
- ADS7822UVerilog驱动,前面传的有点问题
- 基于python的高性能爬虫程序,使用了多线程+缓存+xpath实现的,这里以彼-岸图库为例,实现,仅用于学习交流
- 中分辨率成像光谱仪(MODIS)烧毁面积产品信息MODIS-C6-BA-User-Guide-1.2.pdf
- Screenshot_20240427_172613_com.huawei.browser.jpg
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功