LoginSignup
6
4

More than 5 years have passed since last update.

小ネタ Elixirからkubernetesのapiを叩きはじめた

Posted at

kubernetes環境がわらわら立ち始めてきたので、プログラミングしながら触ってみたいな、ということでよさ気なライブラリを探しましたがまだ更新系はそんなにないみたいです。

kubectlを叩くのもいいのですが、標準出力されるメタデータの形式をシェル等でいじるのも面倒です。。

そこで、REST APIを叩いてみることにしました。

なお、rest 一覧は、localhost:8080 でk8sを立てたら、

で確認が可能になります。

httpoisonとpoisonがあればいけるので、mix.exsを、以下のようにして

mix.exs
  def application do
    [applications: [:logger, :httpoison]]
  end

  defp deps do
    [
      {:httpoison, "~> 0.8.0"},
      {:poison, "~> 2.0"}
    ]

例えば下記みたいなことをしてみました。

def ExK8S

  @base_url  "localhost:8080/apis/extensions/v1beta1/namespaces/default" 

  def show_deployments do
    url = @base_url <> "/deployments"

    case HTTPoison.get(url) do
      {:ok, response} ->
        IO.puts(response.body)

      {:error, error} ->
        IO.inspect(error.reason)
    end
  end

上記を実行すると、

{
  "kind": "DeploymentList",
  "apiVersion": "extensions/v1beta1",
  "metadata": {
    "selfLink": "/apis/extensions/v1beta1/namespaces/default/deployments",
    "resourceVersion": "543"
  },
  "items": [
    {
      "metadata": {
        "name": "nginx",
        "namespace": "default",
        "selfLink": "/apis/extensions/v1beta1/namespaces/default/deployments/nginx",
        "uid": "2403844c-02e8-11e6-9089-9c5c8e718ea8",
        "resourceVersion": "267",
        "generation": 2,
        "creationTimestamp": "2016-04-15T08:57:54Z",
        "labels": {
          "run": "nginx"
        },
        "annotations": {
          "deployment.kubernetes.io/revision": "1"
        }
      },
      "spec": {
        "replicas": 1,
        "selector": {
          "matchLabels": {
            "run": "nginx"
          }
        },
        "template": {
          "metadata": {
            "creationTimestamp": null,
            "labels": {
              "run": "nginx"
            }
          },
          "spec": {
            "containers": [
              {
                "name": "nginx",
                "image": "nginx",
                "ports": [
                  {
                    "containerPort": 80,
                    "protocol": "TCP"
                  }
                ],
                "resources": {},
                "terminationMessagePath": "/dev/termination-log",
                "imagePullPolicy": "Always"
              }
            ],
            "restartPolicy": "Always",
            "terminationGracePeriodSeconds": 30,
            "dnsPolicy": "ClusterFirst",
            "securityContext": {}
          }
        },
        "strategy": {
          "type": "RollingUpdate",
          "rollingUpdate": {
            "maxUnavailable": 1,
            "maxSurge": 1
          }
        }
      },
      "status": {
        "observedGeneration": 2,
        "replicas": 1,
        "updatedReplicas": 1,
        "availableReplicas": 1
      }
    }
  ]
}

と、Jsonがそのまま取れました。

お、いけそうじゃん、ということで、rolling update等をよしなに実行できるかを今後は実験していきたいと思います。


本日は以上です。

6
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
6
4