OpenShift 4 - 通过 REST API 操作 OpenShift
- 说明
- 信息获取当前用户
- 获取Project或namespace列表
- 新建my-project项目
- 获得my-project项目或namespace的信息
- 获取cakephp-mysql-example模板
- 根据cakephp-mysql-example模板创建应用
- 获取my-project项目中所有Pod
- 获取my-project项目中名为XXX的Pod
- 删除my-project项目中名为XXX的Pod
- 获取my-project项目中所有BuildConfig
- 获取my-project项目中名为XXX的BuildConfig
- 创建mysecret的Secret
- 删除my-project项目
- 其它API参考
说明
- 以下REST API是针对OpenShift 4的,不一定都适合OpenShift 3.11。
- 所有操作都针对my-project项目。
- 首先用USER/PASSWORD登录OpenShift的API服务器(地址是OCP_API_URL),然后获取用户登录的TOKEN字符串。然后设置TOKEN和OCP_API_URL环境变量。
$ oc login -u <USER> -p <PASSWROD> https://<OCP_API_URL>
$ oc whoami -t
wMUsqoy2ecJoTpK5MtnjoCEy1nBXo86ADvFmYOO8BtU
$ export TOKEN=$(oc whoami -t)
$ export API_SERVER=$(oc whoami --show-server)
信息获取当前用户
$ curl -k -H "Authorization: Bearer $TOKEN" $API_SERVER/apis/user.openshift.io/v1/users/~
获取Project或namespace列表
$ curl -kX GET -H "Authorization: Bearer $TOKEN" $API_SERVER/apis/project.openshift.io/v1/projects | grep projects
$ curl -kX GET -H "Authorization: Bearer $TOKEN" $API_SERVER/api/v1/namespaces | grep namespaces
新建my-project项目
- 方法1
$ curl -kX POST \
-d @- \
-H "Authorization: Bearer $TOKEN" \
-H 'Accept: application/json' \
-H 'Content-Type: application/json' \
$API_SERVER/apis/project.openshift.io/v1/projectrequests <<'EOF'
{
"kind": "ProjectRequest",
"apiVersion": "project.openshift.io/v1",
"metadata": {
"name": "my-project"
}
}
EOF
- 方法2
$ curl -LO https://raw.githubusercontent.com/liuxiaoyu-git/OpenShift-HOL/master/myproject1.json
$ curl -kX POST -H "Accept: application/json" -H "Content-Type: application/json" -H "Authorization: Bearer $TOKEN" $API_SERVER/apis/project.openshift.io/v1/projects -d @my-project1.json
获得my-project项目或namespace的信息
$ curl -kX GET -H "Authorization: Bearer $TOKEN" $API_SERVER/apis/project.openshift.io/v1/projects/my-project
获取cakephp-mysql-example模板
$ curl -k -H "Authorization: Bearer $TOKEN" $API_SERVER/apis/template.openshift.io/v1/namespaces/openshift/templates/cakephp-mysql-example
根据cakephp-mysql-example模板创建应用
curl -kX POST \
-d @- \
-H "Authorization: Bearer $TOKEN" \
-H 'Accept: application/json' \
-H 'Content-Type: application/json' \
$API_SERVER/apis/template.openshift.io/v1/namespaces/my-project/templateinstances <<EOF
{
"kind": "TemplateInstance",
"apiVersion": "template.openshift.io/v1",
"metadata": {
"name": "my-templateinstance"
},
"spec": {
"template": $(curl -k \
-H "Authorization: Bearer $TOKEN" \
-H 'Accept: application/json' \
$API_SERVER/apis/template.openshift.io/v1/namespaces/openshift/templates/cakephp-mysql-example)
}
}
EOF
获取my-project项目中所有Pod
curl -k -H "Authorization: Bearer $TOKEN" $API_SERVER/api/v1/namespaces/my-project/pods
获取my-project项目中名为XXX的Pod
curl -k -H "Authorization: Bearer $TOKEN" $API_SERVER/api/v1/namespaces/my-project/pods/XXX
删除my-project项目中名为XXX的Pod
curl -kX DELETE \
-d @- \
-H "Authorization: Bearer $TOKEN" \
-H 'Accept: application/json' \
-H 'Content-Type: application/json' \
$API_SERVER/api/v1/namespaces/my-project/pods/XXX <<'EOF'
{
}
EOF
获取my-project项目中所有BuildConfig
curl -kX GET -H "Authorization: Bearer $TOKEN" $API_SERVER/apis/build.openshift.io/v1/namespaces/my-project/buildconfigs
获取my-project项目中名为XXX的BuildConfig
curl -kX GET -H "Authorization: Bearer $TOKEN" $API_SERVER/apis/build.openshift.io/v1/namespaces/my-project/buildconfigs/XXX
创建mysecret的Secret
curl -k \
-X POST \
-d @- \
-H "Authorization: Bearer $TOKEN" \
-H 'Accept: application/json' \
-H 'Content-Type: application/json' \
$API_SERVER/api/v1/namespaces/my-project/secrets <<'EOF'
{
"kind": "Secret",
"apiVersion": "v1",
"metadata": {
"name": "my-secret"
},
"stringData": {
"NAME": "example"
}
}
EOF
删除my-project项目
$ curl -kX DELETE -H "Authorization: Bearer $TOKEN" $API_SERVER/apis/project.openshift.io/v1/projects/my-project
$ curl -kX DELETE -H "Authorization: Bearer $TOKEN" $API_SERVER/api/v1/namespaces/my-project
其它API参考
- OpenShift的Rest API说明可参见:
https://docs.okd.io/latest/rest_api/index.html
https://docs.openshift.com/container-platform/3.11/rest_api/examples.html
- Java Client
- C# Client
- Python Client