diff --git a/task/kubernetes-actions/0.1/README.md b/task/kubernetes-actions/0.1/README.md new file mode 100644 index 0000000000..7b6c4be0d9 --- /dev/null +++ b/task/kubernetes-actions/0.1/README.md @@ -0,0 +1,74 @@ +# kubernetes actions + +This is a generic task used to perform kubernetes actions such as `kubectl get deployment` or `kubectl create -f filename.yaml`. For more commands [see](https://kubernetes.io/docs/reference/kubectl/overview/). + +## Install the task + +``` +kubectl apply -f https://raw.githubusercontent.com/tektoncd/catalog/master/task/kubernetes-actions/0.1/kubernetes-actions.yaml +``` + +## Inputs + +### Parameters + +- **script**: script of `kubectl` commands to execute e.g. `kubectl get pod $1 -0 yaml`. This will take the first value of ARGS as pod name (_default_: `kubectl $@`) +- **args**: args to execute which are appended to `kubectl` e.g. `start-build myapp` (_default_: `help`) +- **image**: Default image being `gcr.io/cloud-builders/kubectl`. If somebody wants to use their own image then they can provide it as a part of params. For example an image avilable is `lachlanevenson/k8s-kubectl` + +### Workspaces + +- **kubeconfig-dir**: If you want to deploy you application to another cluster then you can mount your `kubeconfig` file via this `workspace`. (Default: _emptyDir:{}_ in case `kubeconfig` is not mounted) +- **manifest-dir**: Manifest files can be provided via the workspaces.(Default: _emptyDir:{}_ in case no manifest is provided) + +## Usage + +In case no manifests are mounted + +```yaml +apiVersion: tekton.dev/v1beta1 +kind: TaskRun +metadata: + name: kubectl-run +spec: + taskRef: + name: kubectl-actions + params: + - name: SCRIPT + value: | + kubectl get pods + echo "-----------" + kubectl get deploy + workspaces: + - name: kubeconfig-dir + emptyDir: {} + - name: manifest-dir + emptyDir: {} +``` + +In case manifest is present on `GitHub` : + +```yaml +apiVersion: tekton.dev/v1beta1 +kind: TaskRun +metadata: + name: kubectl-run +spec: + taskRef: + name: kubectl-actions + params: + - name: script + value: | + kubectl apply -f https://raw.githubusercontent.com/vinamra28/social-client/master/k8s/deployment.yaml + ---------- + kubectl get deployment + workspaces: + - name: kubeconfig-dir + emptyDir: {} + - name: manifest-dir + emptyDir: {} +``` + +## Kubectl Patch Deployment Image Example + +If you have existing deployment and after period of time image of the application is being updated. So to update the container image in the deployment, this task can be used as this task will patch the image with the new image in the existing deployment. The TaskRun for this scenario can be found [here](./samples/update-deployment-image-taskrun.yaml) diff --git a/task/kubernetes-actions/0.1/kubernetes-actions.yaml b/task/kubernetes-actions/0.1/kubernetes-actions.yaml new file mode 100644 index 0000000000..7b417571a5 --- /dev/null +++ b/task/kubernetes-actions/0.1/kubernetes-actions.yaml @@ -0,0 +1,45 @@ +apiVersion: tekton.dev/v1beta1 +kind: Task +metadata: + name: kubernetes-actions + labels: + app.kubernetes.io/version: "0.1" + annotations: + tekton.dev/pipelines.minVersion: "0.12.1" + tekton.dev/tags: CLI, kubectl, generic + tekton.dev/displayName: "kubernetes actions" +spec: + description: >- + This task is the generic kubectl CLI task which can be used + to run all kinds of k8s commands + workspaces: + - name: manifest-dir + - name: kubeconfig-dir + params: + - name: script + description: The Kubernetes CLI script to run + type: string + default: "kubectl $@" + - name: args + description: The Kubernetes CLI arguments to run + type: array + default: + - "help" + - name: image + default: gcr.io/cloud-builders/kubectl #image is huge + description: Kubectl wrapper image + steps: + - name: kubectl + image: $(params.image) + workingDir: $(workspaces.manifest-dir.path) + script: | + #!/usr/bin/env bash + + if [[ -f $(workspaces.kubeconfig-dir.path)/kubeconfig ]]; then + export KUBECONFIG=$(workspaces.kubeconfig-dir.path)/kubeconfig + fi + + $(params.script) + + args: + - "$(params.args)" diff --git a/task/kubernetes-actions/0.1/samples/kubectl-actions-run.yaml b/task/kubernetes-actions/0.1/samples/kubectl-actions-run.yaml new file mode 100644 index 0000000000..fe6b713649 --- /dev/null +++ b/task/kubernetes-actions/0.1/samples/kubectl-actions-run.yaml @@ -0,0 +1,18 @@ +apiVersion: tekton.dev/v1beta1 +kind: TaskRun +metadata: + name: kubectl-run +spec: + taskRef: + name: kubernetes-actions + params: + - name: script + value: | + kubectl get pods + echo "---------" + kubectl get deploy + workspaces: + - name: kubeconfig-dir + emptyDir: {} + - name: manifest-dir + emptyDir: {} diff --git a/task/kubernetes-actions/0.1/samples/update-deployment-image-taskrun.yaml b/task/kubernetes-actions/0.1/samples/update-deployment-image-taskrun.yaml new file mode 100644 index 0000000000..0599ebe2f7 --- /dev/null +++ b/task/kubernetes-actions/0.1/samples/update-deployment-image-taskrun.yaml @@ -0,0 +1,26 @@ +--- +apiVersion: tekton.dev/v1beta1 +kind: TaskRun +metadata: + name: update-deployment-image-taskrun +spec: + taskRef: + name: kubernetes-actions + workspaces: + - name: kubeconfig-dir + emptyDir: {} + - name: manifest-dir + emptyDir: {} + params: + - name: script + value: | + kubectl patch deployment $1 --patch='{"spec":{"template":{"spec":{ + "containers":[{ + "name": $1, + "image":$2 + }] + }}}}' + - name: args + value: + - my-client-v1 + - quay.io/vinamra2807/social-client:v2