2018年1月13日土曜日

VMware Admiral で API を使ってテンプレートを操作する方法

概要

前回 VMware Admiral の REST API を試してみました

今回は更にテンプレートの操作も API でやってみたのでその方法を紹介します

環境

  • macOS 10.13.2
  • docker 17.12.0-ce
  • Admiral v1.3.0

テンプレートを追加する

  • リクエスト
curl -X POST \
'http://localhost:8282/resources/composite-descriptions' \
-H 'x-project: /projects/default-project' \
-H 'content-type: application/json' \
-H 'x-xenon-auth-token: eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJ4biIsInN1YiI6Ii9jb3JlL2F1dGh6L3VzZXJzL1lXUnRhVzVBWVdSdGFYSmhiQzVzYjJOaGJBXHUwMDNkXHUwMDNkIiwiZXhwIjoxNTE1NzM1Mjg2fQ.j03awiWf4Esmc_Wb9Tar6fZFVv3g3i48k_JqytPevoA' \
-d '{"name":"test","descriptionLinks":[]}'
  • レスポンス
{
  "name": "test",
  "descriptionLinks": [],
  "tenantLinks": [
    "/projects/default-project"
  ],
  "documentVersion": 0,
  "documentEpoch": 0,
  "documentKind": "com:vmware:admiral:compute:container:CompositeDescriptionService:CompositeDescription",
  "documentSelfLink": "/resources/composite-descriptions/4d9e8d32664228755628ccbb0b7c8",
  "documentUpdateTimeMicros": 1515731735853001,
  "documentUpdateAction": "POST",
  "documentExpirationTimeMicros": 0,
  "documentOwner": "3d0223ea-5e52-4dd6-8c68-5e9ddfb8a443",
  "documentAuthPrincipalLink": "/core/authz/users/YWRtaW5AYWRtaXJhbC5sb2NhbA=="
}

name は実は任意パラメータです
指定しないとランダムの文字列でテンプレート名を作成します
プロジェクトやクラスタは name が必須なのですがテンプレートは違うようです

テンプレートの一覧を取得する

  • リクエスト
curl -X GET \
'http://localhost:8282/templates?q=*' \
-H 'x-project: /projects/default-project' \
-H 'x-xenon-auth-token: eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJ4biIsInN1YiI6Ii9jb3JlL2F1dGh6L3VzZXJzL1lXUnRhVzVBWVdSdGFYSmhiQzVzYjJOaGJBXHUwMDNkXHUwMDNkIiwiZXhwIjoxNTE1NzM1Mjg2fQ.j03awiWf4Esmc_Wb9Tar6fZFVv3g3i48k_JqytPevoA'
  • レスポンス
{
  "results": [
    {
      "templateType": "COMPOSITE_DESCRIPTION",
      "name": "test",
      "descriptionLinks": [],
      "tenantLinks": [
        "/projects/default-project"
      ],
      "documentVersion": 0,
      "documentEpoch": 0,
      "documentKind": "com:vmware:admiral:compute:container:CompositeDescriptionService:CompositeDescription",
      "documentSelfLink": "/resources/composite-descriptions/4d9e8d32664228755628ccbb0b7c8",
      "documentUpdateTimeMicros": 1515732089628001,
      "documentUpdateAction": "POST",
      "documentExpirationTimeMicros": 0,
      "documentOwner": "3d0223ea-5e52-4dd6-8c68-5e9ddfb8a443",
      "documentAuthPrincipalLink": "/core/authz/users/YWRtaW5AYWRtaXJhbC5sb2NhbA=="
    }
  ],
  "isPartialResult": false
}

q は必須パラメータです
x-project でプロジェクトを指定しなかった場合はすべてのプロジェクトからテンプレートを取得します

GET /resources/composite-descriptions でも取得できるようですが name などの情報は取得できません

テンプレートの詳細を取得する

  • リクエスト
curl -X GET \
'http://localhost:8282/resources/composite-descriptions/4d9e8d32664228755628ccbb0b7c8' \
-H 'x-xenon-auth-token: eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJ4biIsInN1YiI6Ii9jb3JlL2F1dGh6L3VzZXJzL1lXUnRhVzVBWVdSdGFYSmhiQzVzYjJOaGJBXHUwMDNkXHUwMDNkIiwiZXhwIjoxNTE1NzM1Mjg2fQ.j03awiWf4Esmc_Wb9Tar6fZFVv3g3i48k_JqytPevoA'
  • レスポンス
{
  "name": "test",
  "descriptionLinks": [],
  "tenantLinks": [
    "/projects/default-project"
  ],
  "documentVersion": 0,
  "documentEpoch": 0,
  "documentKind": "com:vmware:admiral:compute:container:CompositeDescriptionService:CompositeDescription",
  "documentSelfLink": "/resources/composite-descriptions/4d9e8d32664228755628ccbb0b7c8",
  "documentUpdateTimeMicros": 1515732089628001,
  "documentUpdateAction": "POST",
  "documentExpirationTimeMicros": 0,
  "documentOwner": "3d0223ea-5e52-4dd6-8c68-5e9ddfb8a443",
  "documentAuthPrincipalLink": "/core/authz/users/YWRtaW5AYWRtaXJhbC5sb2NhbA=="
}

テンプレートを削除する

  • リクエスト
curl -X DELETE \
'http://localhost:8282/resources/composite-descriptions/4d9e8d32664228755628ccbb0b7c8' \
-H 'x-xenon-auth-token: eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJ4biIsInN1YiI6Ii9jb3JlL2F1dGh6L3VzZXJzL1lXUnRhVzVBWVdSdGFYSmhiQzVzYjJOaGJBXHUwMDNkXHUwMDNkIiwiZXhwIjoxNTE1NzM1Mjg2fQ.j03awiWf4Esmc_Wb9Tar6fZFVv3g3i48k_JqytPevoA'
  • レスポンス
{
  "name": "test",
  "descriptionLinks": [],
  "tenantLinks": ["/projects/default-project"],
  "documentVersion": 1,
  "documentEpoch": 0,
  "documentKind": "com:vmware:admiral:compute:container:CompositeDescriptionService:CompositeDescription",
  "documentSelfLink": "/resources/composite-descriptions/4d9e8d32664228755628ccbb0b7c8",
  "documentUpdateTimeMicros": 1515731950045000,
  "documentUpdateAction": "DELETE",
  "documentExpirationTimeMicros": 0,
  "documentOwner": "3d0223ea-5e52-4dd6-8c68-5e9ddfb8a443",
  "documentAuthPrincipalLink": "/core/authz/users/YWRtaW5AYWRtaXJhbC5sb2NhbA=="
}

コンテナ定義の追加

テンプレートは作成しただけではコンテナのプロビジョニングができません
コンテナ定義を追加する必要があります
他にもボリューム定義やネットワーク定義、クロージャ定義の追加があります
以下はコンテナ定義を追加するリクエストです

  • リクエスト
curl -X POST 'http://localhost:8282/resources/container-descriptions' \
-H 'content-type:application/json' \
-H 'x-xenon-auth-token: ' \
-H 'x-project:/projects/default-project' \
-d '{"image":"registry.hub.docker.com/library/nginx","name":"nginx","command":[],"links":[],"deploymentPolicyId":null,"portBindings":[{"hostPort":"","containerPort":"80"}],"publishAll":true,"hostname":null,"networkMode":null,"networks":{},"volumes":[],"volumesFrom":[],"workingDir":null,"_cluster":1,"restartPolicy":"no","maximumRetryCount":null,"cpuShares":null,"memoryLimit":null,"memorySwapLimit":null,"affinity":[],"env":[],"customProperties":{},"healthConfig":{},"logConfig":{"type":null,"config":{}}}'

基本的にはコンテナを run するための種となる情報を定義します
イメージの情報やポートの情報、環境変数などが定義できます

  • レスポンス
{
  "image": "registry.hub.docker.com/library/nginx",
  "command": [],
  "instanceAdapterReference": "/adapters/docker-service",
  "affinity": [],
  "_cluster": 1,
  "env": [],
  "volumes": [],
  "publishAll": true,
  "portBindings": [{
    "hostPort": "",
    "containerPort": "80"
  }],
  "logConfig": {
    "config": {}
  },
  "links": [],
  "volumesFrom": [],
  "restartPolicy": "no",
  "networks": {},
  "name": "nginx",
  "customProperties": {},
  "tenantLinks": ["/projects/default-project"],
  "documentVersion": 0,
  "documentEpoch": 0,
  "documentKind": "com:vmware:admiral:compute:container:ContainerDescriptionService:ContainerDescription",
  "documentSelfLink": "/resources/container-descriptions/4d9e8d32664228755628f1dad72a8",
  "documentUpdateTimeMicros": 1515741701305001,
  "documentUpdateAction": "POST",
  "documentExpirationTimeMicros": 0,
  "documentOwner": "3d0223ea-5e52-4dd6-8c68-5e9ddfb8a443",
  "documentAuthPrincipalLink": "/core/authz/users/YWRtaW5AYWRtaXJhbC5sb2NhbA=="
}

_cluster の値を大きくすることで最低限存在しなければならないコンテナ数を設定することができます

コンテナ定義とテンプレートを紐付ける

  • リクエスト
curl -X PATCH 'http://localhost:8282/resources/composite-descriptions/4d9e8d32664228755628dff8ade3a' \
-H 'x-xenon-auth-token: eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJ4biIsInN1YiI6Ii9jb3JlL2F1dGh6L3VzZXJzL1lXUnRhVzVBWVdSdGFYSmhiQzVzYjJOaGJBXHUwMDNkXHUwMDNkIiwiZXhwIjoxNTE1NzQ1Mjc4fQ.u8_SThTzX0CiiFGVq6NeqDUBKcMmmKOr_7ql5FufZng' \
-H 'x-project:/projects/default-project' \
-H 'content-type:application/json' \
-d '{"name":"","descriptionLinks":["/resources/container-descriptions/4d9e8d32664228755628f1dad72a8"],"tenantLinks":["/projects/default-project"],"documentVersion":8,"documentEpoch":0,"documentKind":"com:vmware:admiral:compute:container:CompositeDescriptionService:CompositeDescription","documentSelfLink":"/resources/composite-descriptions/4d9e8d32664228755628dff8ade3a","documentUpdateTimeMicros":1515741517583001,"documentUpdateAction":"PATCH","documentExpirationTimeMicros":0,"documentOwner":"3d0223ea-5e52-4dd6-8c68-5e9ddfb8a443","documentAuthPrincipalLink":"/core/authz/users/YWRtaW5AYWRtaXJhbC5sb2NhbA=="}'
  • レスポンス
{
  "name": "",
  "descriptionLinks": [
    "/resources/container-descriptions/4d9e8d32664228755628f1dad72a8"
  ],
  "tenantLinks": [
    "/projects/default-project"
  ],
  "documentVersion": 11,
  "documentEpoch": 0,
  "documentKind": "com:vmware:admiral:compute:container:CompositeDescriptionService:CompositeDescription",
  "documentSelfLink": "/resources/composite-descriptions/4d9e8d32664228755628dff8ade3a",
  "documentUpdateTimeMicros": 1515743075171001,
  "documentUpdateAction": "PATCH",
  "documentExpirationTimeMicros": 0,
  "documentOwner": "3d0223ea-5e52-4dd6-8c68-5e9ddfb8a443",
  "documentAuthPrincipalLink": "/core/authz/users/YWRtaW5AYWRtaXJhbC5sb2NhbA=="
}

テンプレートが作成された段階では descriptionLinks が空の状態です
ここを作成したコンテナ定義の ID (4d9e8d32664228755628f1dad72a8) で更新してあげることで紐付けを行うことができます
この作業をしないと admiral の管理画面でテンプレートからコンテナ定義を確認することができません

コンテナ定義の一覧の取得

  • リクエスト
curl -X GET 'http://localhost:8282/resources/container-descriptions' \
-H 'x-xenon-auth-token: eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJ4biIsInN1YiI6Ii9jb3JlL2F1dGh6L3VzZXJzL1lXUnRhVzVBWVdSdGFYSmhiQzVzYjJOaGJBXHUwMDNkXHUwMDNkIiwiZXhwIjoxNTE1NzQ1Mjc4fQ.u8_SThTzX0CiiFGVq6NeqDUBKcMmmKOr_7ql5FufZng' \
-H 'x-project:/projects/default-project'
  • レスポンス
{
  "totalCount": 1,
  "documentLinks": [
    "/resources/container-descriptions/4d9e8d32664228755628f1dad72a8"
  ],
  "documents": {
    "/resources/container-descriptions/4d9e8d32664228755628f1dad72a8": {
      "image": "registry.hub.docker.com/library/nginx",
      "command": [],
      "instanceAdapterReference": "/adapters/docker-service",
      "affinity": [],
      "_cluster": 1,
      "env": [],
      "volumes": [],
      "publishAll": true,
      "portBindings": [
        {
          "hostPort": "",
          "containerPort": "80"
        }
      ],
      "logConfig": {
        "config": {}
      },
      "links": [],
      "volumesFrom": [],
      "restartPolicy": "no",
      "networks": {},
      "name": "nginx",
      "customProperties": {},
      "tenantLinks": [
        "/projects/default-project"
      ],
      "documentVersion": 0,
      "documentEpoch": 0,
      "documentKind": "com:vmware:admiral:compute:container:ContainerDescriptionService:ContainerDescription",
      "documentSelfLink": "/resources/container-descriptions/4d9e8d32664228755628f1dad72a8",
      "documentUpdateTimeMicros": 1515741701305001,
      "documentUpdateAction": "POST",
      "documentExpirationTimeMicros": 0,
      "documentOwner": "3d0223ea-5e52-4dd6-8c68-5e9ddfb8a443",
      "documentAuthPrincipalLink": "/core/authz/users/YWRtaW5AYWRtaXJhbC5sb2NhbA=="
    }
  },
  "documentCount": 1,
  "queryTimeMicros": 997,
  "documentVersion": 0,
  "documentUpdateTimeMicros": 0,
  "documentExpirationTimeMicros": 0,
  "documentOwner": "3d0223ea-5e52-4dd6-8c68-5e9ddfb8a443"
}

コンテナ定義の削除

  • リクエスト
curl -X DELETE 'http://localhost:8282/resources/container-descriptions/4d9e8d32664228755628f1dad72a8' \
-H 'x-xenon-auth-token: eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJ4biIsInN1YiI6Ii9jb3JlL2F1dGh6L3VzZXJzL1lXUnRhVzVBWVdSdGFYSmhiQzVzYjJOaGJBXHUwMDNkXHUwMDNkIiwiZXhwIjoxNTE1NzQ1Mjc4fQ.u8_SThTzX0CiiFGVq6NeqDUBKcMmmKOr_7ql5FufZng' \
-H 'x-project:/projects/default-project'
  • レスポンス
{
  "image": "registry.hub.docker.com/library/nginx",
  "command": [],
  "instanceAdapterReference": "/adapters/docker-service",
  "affinity": [],
  "_cluster": 1,
  "env": [],
  "volumes": [],
  "publishAll": true,
  "portBindings": [
    {
      "hostPort": "",
      "containerPort": "80"
    }
  ],
  "logConfig": {
    "config": {}
  },
  "links": [],
  "volumesFrom": [],
  "restartPolicy": "no",
  "networks": {},
  "name": "nginx",
  "customProperties": {},
  "tenantLinks": [
    "/projects/default-project"
  ],
  "documentVersion": 1,
  "documentEpoch": 0,
  "documentKind": "com:vmware:admiral:compute:container:ContainerDescriptionService:ContainerDescription",
  "documentSelfLink": "/resources/container-descriptions/4d9e8d32664228755628f1dad72a8",
  "documentUpdateTimeMicros": 1515742131728000,
  "documentUpdateAction": "DELETE",
  "documentExpirationTimeMicros": 0,
  "documentOwner": "3d0223ea-5e52-4dd6-8c68-5e9ddfb8a443",
  "documentAuthPrincipalLink": "/core/authz/users/YWRtaW5AYWRtaXJhbC5sb2NhbA=="
}

最後に

VMware admiral の API を使ってテンプレートの操作をしてみました
/resources/composite-descriptions/templates を使い分けるようです
ざっと見た感じだと templates は DockerHub で公開されているイメージも検索することが可能で composite-descriptions は自信で作成したテンプレートに関しての操作かなと思います
あとはテンプレートは作成しただけでは特に効果はなく、その中にコンテナやボリュームの定義を追加することでアプリのデプロイなどが行なえます

admiral の UI でリクエストしている情報をベースに作成しているので実際にはどちらかだけを使えば OK なのかもしれません

0 件のコメント:

コメントを投稿