Skip to content

Commit

Permalink
Enhance "kubectl wrapper
Browse files Browse the repository at this point in the history
The requirements discussed in tektoncd#233
- action: ['get', 'create', 'apply', 'delete', 'replace', 'patch'] -- the action to perform to the resource
- merge_strategy: ['strategic', 'merge', 'json'] -- the strategy used to merge a patch, defaults to "strategic"
- success_condition: a label selector expression which describes the success condition
- failure_condition: a label selector expression which describes the failure condition
- manifest: the kubernetes manifest"
  • Loading branch information
vincent-pli committed Apr 29, 2020
1 parent db51b07 commit a1f0bd1
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 9 deletions.
35 changes: 32 additions & 3 deletions kubectl/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,22 @@ kubectl apply -f https://raw.githubusercontent.com/tektoncd/catalog/master/kubec

### Parameters

* **manifest:**: The content of the resource to deploy
* **action**: The action to perform to the resource, support `get`, `create`, `apply`, `delete`, `replace`, `patch`.
* **manifest**: The content of the resource to deploy.
* **success-condition/failure-condition**: SuccessCondition and failureCondition are optional expressions which are evaluated upon every update of the resource. If failureCondition is ever evaluated to true, the step is considered failed. Likewise, if successCondition is ever evaluated to true the step is considered successful. It uses kubernetes label selection syntax and can be applied against any field of the resource (not just labels). Multiple AND conditions can be represented by comma delimited expressions. For more details, see: https://kubernetes.io/docs/concepts/overview/working-with-objects/labels/.
* **merge-strategy**: The strategy used to merge a patch, defaults to `strategic`, supported `strategic`, `merge` and `json`.
* **output**: Extracted from fields of the resource, only support jsonpath. Should define as a `yaml` array(array even if only one item):
```
- name: output
value: |
- name: job-name
valueFrom: '{.metadata.name}'
- name: job-namespace
valueFrom: '{.metadata.namespace}'
```
The extracted value will be write to`/tekton/results/$(name)`.
* **set-ownerreference**: Set the `ownerReferences` for the resource as pod of `step`, default to false.


## Usage

Expand All @@ -34,17 +49,31 @@ spec:
taskRef:
name: kubectl-deploy-pod
params:
- name: action
value: create
- name: success-condition
value: status.phase == Running
- name: failure-condition
value: status.phase in (Failed, Error)
- name: output
value: |
- name: job-name
valueFrom: '{.metadata.name}'
- name: job-namespace
valueFrom: '{.metadata.namespace}'
- name: set-ownerreference
value: "true"
- name: manifest
value: |
apiVersion: v1
kind: Pod
metadata:
name: myapp-pod
generateName: myapp-pod-
labels:
app: myapp
spec:
containers:
- name: myapp-container
image: docker
command: ['sh', '-c', 'echo Hello Kubernetes! && sleep 3600']
command: ['sh', '-c', 'echo Hello Kubernetes! && sleep 30']
```
48 changes: 44 additions & 4 deletions kubectl/kubectl-deploy.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,54 @@ metadata:
namespace: default
spec:
params:
- name: action
description: Action on the resource
- name: merge-strategy
default: strategic
description: Merge strategy when using action patch
- name: manifest
description: Content of the resource to deploy
- name: success-condition
default: ""
description: A label selector express to decide if the action on resource is success.
- name: failure-condition
default: ""
description: A label selector express to decide if the action on resource is failure.
- name: output
default: ""
description: An express to retrieval data from resource.
- name: set-ownerreference
default: "false"
description: Enable set owner reference for created resource.
- name: image
default: gcr.io/cloud-builders/kubectl # it is huge
default: index.docker.io/aipipeline/kubeclient:v0.0.2 # it is huge
description: Kubectl wrapper image
steps:
- name: kubeconfig
image: $(params.image)
script: |
echo "$(params.manifest)" > /tmp/resource.yaml
kubectl create -f /tmp/resource.yaml
args:
- --action=$(params.action)
- --merge-strategy=$(params.merge-strategy)
- --manifest=$(params.manifest)
- --success-condition=$(params.success-condition)
- --failure-condition=$(params.failure-condition)
- --output=$(params.output)
- --set-ownerreference=$(params.set-ownerreference)
env:
- name: POD_NAME
valueFrom:
fieldRef:
fieldPath: metadata.name
- name: POD_NAMESPACE
valueFrom:
fieldRef:
fieldPath: metadata.namespace
- name: POD_UID
valueFrom:
fieldRef:
fieldPath: metadata.uid
- name: echo
image: busybox
script: cat /tekton/results/job-name


18 changes: 16 additions & 2 deletions kubectl/taskrun.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,30 @@ spec:
taskRef:
name: kubectl-deploy-pod
params:
- name: action
value: create
- name: success-condition
value: status.phase == Running
- name: failure-condition
value: status.phase in (Failed, Error)
- name: output
value: |
- name: job-name
valueFrom: '{.metadata.name}'
- name: job-namespace
valueFrom: '{.metadata.namespace}'
- name: set-ownerreference
value: "true"
- name: manifest
value: |
apiVersion: v1
kind: Pod
metadata:
name: myapp-pod
generateName: myapp-pod-
labels:
app: myapp
spec:
containers:
- name: myapp-container
image: docker
command: ['sh', '-c', 'echo Hello Kubernetes! && sleep 3600']
command: ['sh', '-c', 'echo Hello Kubernetes! && sleep 30']

0 comments on commit a1f0bd1

Please sign in to comment.