项目作者: travelping

项目描述 :
Kubernetes API client library
高级语言: Erlang
项目地址: git://github.com/travelping/ekub.git
创建时间: 2019-03-15T14:57:57Z
项目社区:https://github.com/travelping/ekub

开源协议:Apache License 2.0

下载


Ekub

License: Apache-2.0
GitHub Release Badge
Erlang Releases Badge

An Erlang client library to work with Kubernetes via Kubernetes API.

Usage

Get into an Erlang shell with everything needed (Erlang should be installed
before) downloaded, built and loaded:

  1. $ make shell

Read access from the current kubeconfig or service account folder (in case of
running from a pod):

  1. {ok, Access} = ekub_access:read().

Load current cluster API:

  1. {ok, Api} = ekub_api:load(Access).

In one go:

  1. {ok, {Api, Access}} = ekub:init().

Resource YAML file:

  1. # deployment.yaml:
  2. apiVersion: extensions/v1beta1
  3. kind: Deployment
  4. metadata:
  5. name: ekub-example
  6. spec:
  7. selector:
  8. matchLabels:
  9. app: ekub-example
  10. template:
  11. metadata:
  12. labels:
  13. app: ekub-example
  14. spec:
  15. containers:
  16. - image: alpine
  17. name: ekub-example
  18. command:
  19. - sh
  20. - -c
  21. - |
  22. echo "Sleeping..."
  23. sleep 1000000
  24. terminationGracePeriodSeconds: 0

This example describes Deployment, but can be whatever other resource supported
by the Kubernetes API. Or list of resources separated by “—-“.

Create deployment from the YAML file:

  1. {ok, [Deployment|_]} = ekub_yaml:read("deployment.yaml").
  2. {ok, Object1} = ekub:create(Deployment, {Api, Access}).

Get all the pod names in the current namespace:

  1. {ok, PodList1} = ekub:read(pod, {Api, Access}).
  2. PodNames = [Name || #{<<"metadata">> := #{<<"name">> := Name}}
  3. <- maps:get(<<"items">>, PodList1)].

Get our example pod name:

  1. Query = [{label_selector, "app=ekub-example"}],
  2. {ok, PodList2} = ekub:read(pod, Query, {Api, Access}).
  3. PodName = hd([Name || #{<<"metadata">> := #{<<"name">> := Name}}
  4. <- maps:get(<<"items">>, PodList2)]).

Execute a command within the pod:

  1. ekub:exec(PodName, "ls -l", {Api, Access}).

Get the pod logs:

  1. ekub:logs(PodName, {Api, Access}).

Watch all the pods in the current namespace for changes:

  1. start_watch({Api, Access}) ->
  2. case ekub:watch(pods, {Api, Access}) of
  3. {ok, Ref} -> continue_watch(Ref, {Api, Access});
  4. {error, Reason} -> {error, Reason}
  5. end.
  6. continue_watch(Ref, {Api, Access}) ->
  7. case ekub:watch(Ref) of
  8. {ok, done} -> start_watch({Api, Access});
  9. {ok, Events} -> <Process Events>, continue_watch(Ref, {Api, Access});
  10. {error, timeout} -> continue_watch(Ref, {Api, Access});
  11. {error, req_not_found} -> start_watch({Api, Access});
  12. {error, Reason} -> {error, Reason}
  13. end.

Update the deployment by replacing its body. The new body (note the container
name change):

  1. # deployment_to.yaml:
  2. apiVersion: extensions/v1beta1
  3. kind: Deployment
  4. metadata:
  5. name: ekub-example
  6. spec:
  7. selector:
  8. matchLabels:
  9. app: ekub-example
  10. template:
  11. metadata:
  12. labels:
  13. app: ekub-example
  14. spec:
  15. containers:
  16. - image: alpine
  17. name: ekub-example-sleep
  18. command:
  19. - sh
  20. - -c
  21. - |
  22. echo "Sleeping..."
  23. sleep 1000000
  24. terminationGracePeriodSeconds: 0

Replace:

  1. {ok, [DeploymentTo|_]} = ekub_yaml:read("deployment_to.yaml").
  2. {ok, Object2} = ekub:replace(DeploymentTo, {Api, Access}).

Update the deployment with a patch. The patch YAML file:

  1. # patch.yaml:
  2. spec:
  3. template:
  4. metadata:
  5. labels:
  6. purpose: test

Patch:

  1. {ok, [Patch|_]} = ekub_yaml:read("patch.yaml").
  2. ekub:patch(deployment, "ekub-example", Patch, {Api, Access}).

Delete the deployment:

  1. ekub:delete(deployment, "ekub-example", [{propagation_policy, 'Foreground'}], {Api, Access}).

Or:

  1. ekub:delete(Deployment, [{propagation_policy, 'Foreground'}], {Api, Access}).

Deploy set of resources from a remote manifest:

  1. {ok, MetalLB} = ekub_yaml:read("https://raw.githubusercontent.com/google/metallb/v0.7.3/manifests/metallb.yaml").
  2. [ekub:create(Piece, {Api, Access}) || Piece <- MetalLB].

Delete the MetalLB pieces in reverse order:

  1. [ekub:delete(Piece, {Api, Access}) || Piece <- lists:reverse(MetalLB)].

License

Copyright 2018-2019 Travelping GmbH

Licensed under the Apache License, Version 2.0 (the “License”);
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

  1. http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an “AS IS” BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.