diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..a6a54b1 --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +.idea +.git +.DS_Store \ No newline at end of file diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..9198541 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,26 @@ +FROM alpine:3.10.2 +MAINTAINER Serhiy Mitrovtsiy + +LABEL name="kubectl" +LABEL version="1.0.0" +LABEL repository="https://github.com/exelban/gcloud" +LABEL homepage="https://github.com/exelban/gcloud" +LABEL maintainer="Serhiy Mytrovtsiy " + +LABEL com.github.actions.name="Kuberentes (k8s) cli - kubectl" +LABEL com.github.actions.description="GitHub Action for working with kubectl (k8s)" +LABEL com.github.actions.icon="terminal" +LABEL com.github.actions.color="blue" + +ARG KUBE_VERSION="1.15.4" + +COPY entrypoint.sh /entrypoint.sh + +RUN chmod +x /entrypoint.sh && \ + apk add --no-cache --update openssl curl ca-certificates && \ + curl -L https://storage.googleapis.com/kubernetes-release/release/v$KUBE_VERSION/bin/linux/amd64/kubectl -o /usr/local/bin/kubectl && \ + chmod +x /usr/local/bin/kubectl && \ + rm -rf /var/cache/apk/* + +ENTRYPOINT ["/entrypoint.sh"] +CMD ["cluster-info"] \ No newline at end of file diff --git a/README.md b/README.md index 9c30723..724f644 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,117 @@ # kubectl -GitHub Action for working with kubectl (k8s) + +[![Preview](https://serhiy.s3.eu-central-1.amazonaws.com/Github_repo/kubectl/logo.png)](https://cloud.google.com) + +GitHub Action for working with kubectl ([k8s](https://kubernetes.io)) + +## Usage +To use kubectl put this step into your workflow: + +### Authorization with config file +```yaml +- uses: exelban/kubectl@master + env: + KUBE_CONFIG: ${{ secrets.KUBE_CONFIG }} + with: + args: get pods +``` + +### Authorization with credentials +```yaml +- uses: exelban/kubectl@master + env: + KUBE_HOST: ${{ secrets.KUBE_HOST }} + KUBE_USERNAME: ${{ secrets.KUBE_USERNAME }} + KUBE_PASSWORD: ${{ secrets.KUBE_PASSWORD }} + KUBE_CERTIFICATE: ${{ secrets.KUBE_CERTIFICATE }} + with: + args: get pods +``` + +## Environment variables +All these variables need to authorize to kubernetes cluster. +I recommend using secrets for this. + +### KUBECONFIG file +First options its to use [kubeconfig file](https://kubernetes.io/docs/concepts/configuration/organize-cluster-access-kubeconfig/). + +For this method `KUBE_CONFIG` required. +You can find it: `cat $HOME/.kube/config | base64 `. + +Optionally you can switch the [context](https://kubernetes.io/docs/tasks/access-application-cluster/configure-access-multiple-clusters/) (the cluster) if you have few in kubeconfig file. Passing specific context to `KUBE_CONTEXT`. To see the list of available contexts do: `kubectl config get-contexts`. + +| Variable | Type | +| --- | --- | +| KUBE_CONFIG | string (base64) | +| KUBE_CONTEXT | string | + +### KUBECONFIG file +Another way to authenticate in the cluster is [HTTP basic auth](https://kubernetes.io/docs/reference/access-authn-authz/authentication/). + +For this you need to pass: +- host (IP only, without protocol) +- username +- password +- cluster CA certificate + +| Variable | Type | +| --- | --- | +| KUBE_HOST | string | +| KUBE_USERNAME | string | +| KUBE_PASSWORD | string | +| KUBE_CERTIFICATE | string | + +## Example +```yaml +name: Get pods +on: [push] + +jobs: + deploy: + name: Deploy + runs-on: ubuntu-latest + + steps: + - uses: actions/checkout@v1 + - uses: exelban/kubectl@master + env: + KUBE_CONFIG: ${{ secrets.KUBE_CONFIG }} + with: + args: get pods +``` + +```yaml +name: Get pods +on: [push] + +jobs: + deploy: + name: Deploy + runs-on: ubuntu-latest + + steps: + - uses: actions/checkout@v1 + - uses: exelban/kubectl@master + env: + KUBE_CONFIG: ${{ secrets.KUBE_CONFIG }} + + - uses: exelban/kubectl@master + with: + args: get pods +``` + +## Versions +If you need a specific version of kubectl, make a PR with a specific version number. +After accepting PR the new release will be created. +To use a specific version of kubectl use: + +```yaml +- uses: exelban/kubectl@1.14.3 + env: + KUBE_CONFIG: ${{ secrets.KUBE_CONFIG }} + with: + args: get pods +``` + +## Licence +[MIT License](https://github.com/exelban/kubectl/blob/master/LICENSE) \ No newline at end of file diff --git a/entrypoint.sh b/entrypoint.sh new file mode 100644 index 0000000..eb33696 --- /dev/null +++ b/entrypoint.sh @@ -0,0 +1,34 @@ +#!/bin/sh + +set -e + +if [ ! -d "$HOME/.kube" ]; then + mkdir -p $HOME/.kube +fi + +if [ ! -f "$HOME/.kube/config" ]; then + if [ ! -z "${KUBE_CONFIG}" ]; then + + echo "$KUBE_CONFIG" | base64 -d > $HOME/.kube/config + + if [ ! -z "${KUBE_CONTEXT}" ]; then + kubectl config use-context $KUBE_CONTEXT + fi + + elif [ ! -z "${KUBE_HOST}" ]; then + + echo "$KUBE_CERTIFICATE" | base64 -d > $HOME/.kube/certificate + kubectl config set-cluster default --server=https://$KUBE_HOST --certificate-authority=$HOME/.kube/certificate > /dev/null + kubectl config set-credentials cluster-admin --username=$KUBE_USERNAME --password=$KUBE_PASSWORD > /dev/null + kubectl config set-context default --cluster=default --namespace=default --user=cluster-admin > /dev/null + kubectl config use-context default > /dev/null + + else + echo "No authorization data found. Please provide CONFIG or HTTPS variables. Exiting...." + exit 1 + fi +fi + +echo ::add-path::/usr/local/bin/kubectl + +kubectl $* \ No newline at end of file