Skip to content

Commit

Permalink
Add the mergeStrategy option to resource patching (#1269)
Browse files Browse the repository at this point in the history
* This adds the ability to pass a mergeStrategy to a patch resource.
  this is valuable because the default merge strategy for kubernetes is
  'strategic', which does not work with Custom Resources.
* This also updates the resource example to demonstrate how it is used
  • Loading branch information
ian-howell authored and jessesuen committed Mar 22, 2019
1 parent e610524 commit 73a37f2
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 1 deletion.
4 changes: 4 additions & 0 deletions api/openapi-spec/swagger.json
Original file line number Diff line number Diff line change
Expand Up @@ -538,6 +538,10 @@
"description": "Manifest contains the kubernetes manifest",
"type": "string"
},
"mergeStrategy": {
"description": "MergeStrategy is the strategy used to merge a patch. It defaults to \"strategic\" Must be one of: strategic, merge, json",
"type": "string"
},
"successCondition": {
"description": "SuccessCondition is a label selector expression which describes the conditions of the k8s resource in which it is acceptable to proceed to the following step",
"type": "string"
Expand Down
34 changes: 33 additions & 1 deletion examples/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1164,7 +1164,6 @@ spec:
- name: pi-tmpl
resource: # indicates that this is a resource template
action: create # can be any kubectl action (e.g. create, delete, apply, patch)
# Patch action will support only **json merge strategic**
# The successCondition and failureCondition are optional expressions.
# If failureCondition is true, the step is considered failed.
# If successCondition is true, the step is considered successful.
Expand Down Expand Up @@ -1194,6 +1193,39 @@ spec:
Resources created in this way are independent of the workflow. If you want the resource to be deleted when the workflow is deleted then you can use [Kubernetes garbage collection](https://kubernetes.io/docs/concepts/workloads/controllers/garbage-collection/) with the workflow resource as an owner reference ([example](./k8s-owner-reference.yaml)).
**Note:**
When patching, the resource will accept another attribute, `mergeStrategy`, which can either be `strategic`, `merge`, or `json`. If this attribute is not supplied, it will default to `strategic`. Keep in mind that Custom Resources cannot be patched with `strategic`, so a different strategy must be chosen. For example, suppose you have the [CronTab CustomResourceDefinition](https://kubernetes.io/docs/tasks/access-kubernetes-api/custom-resources/custom-resource-definitions/#create-a-customresourcedefinition) defined, and the following instance of a CronTab:

```yaml
apiVersion: "stable.example.com/v1"
kind: CronTab
spec:
cronSpec: "* * * * */5"
image: my-awesome-cron-image
```

This Crontab can be modified using the following Argo Workflow:

```yaml
apiVersion: argoproj.io/v1alpha1
kind: Workflow
metadata:
generateName: k8s-patch-
spec:
entrypoint: cront-tmpl
templates:
- name: cront-tmpl
resource:
action: patch
mergeStrategy: merge # Must be one of [strategic merge json]
manifest: |
apiVersion: "stable.example.com/v1"
kind: CronTab
spec:
cronSpec: "* * * * */10"
image: my-awesome-cron-image
```

## Docker-in-Docker Using Sidecars

An application of sidecars is to implement Docker-in-Docker (DinD). DinD is useful when you want to run Docker commands from inside a container. For example, you may want to build and push a container image from inside your build container. In the following example, we use the docker:dind container to run a Docker daemon in a sidecar and give the main container access to the daemon.
Expand Down
7 changes: 7 additions & 0 deletions pkg/apis/workflow/v1alpha1/openapi_generated.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions pkg/apis/workflow/v1alpha1/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -781,6 +781,10 @@ type ResourceTemplate struct {
// Must be one of: get, create, apply, delete, replace
Action string `json:"action"`

// MergeStrategy is the strategy used to merge a patch. It defaults to "strategic"
// Must be one of: strategic, merge, json
MergeStrategy string `json:"mergeStrategy,omitempty"`

// Manifest contains the kubernetes manifest
Manifest string `json:"manifest"`

Expand Down
7 changes: 7 additions & 0 deletions workflow/executor/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,13 @@ func (we *WorkflowExecutor) ExecResource(action string, manifestPath string, isD
}

if action == "patch" {
mergeStrategy := "strategic"
if we.Template.Resource.MergeStrategy != "" {
mergeStrategy = we.Template.Resource.MergeStrategy
}

args = append(args, "--type")
args = append(args, mergeStrategy)

args = append(args, "-p")
buff, err := ioutil.ReadFile(manifestPath)
Expand Down

0 comments on commit 73a37f2

Please sign in to comment.