Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support creating strategic merge patch #1375

Closed
timebertt opened this issue Feb 8, 2021 · 1 comment · Fixed by #1406
Closed

Support creating strategic merge patch #1375

timebertt opened this issue Feb 8, 2021 · 1 comment · Fixed by #1406
Assignees

Comments

@timebertt
Copy link
Contributor

Currently, strategic merge patches can be sent using client.RawPatch like this:

var deploy *appsv1.Deployment
deployCopy := deploy.DeepCopy()

// do changes
i, container := findContainerByName(deployCopy.Spec.Template.Spec.Containers, "nginx")
container.Image = "nginx:updated"
deployCopy.Spec.Template.Spec.Containers[i] = container

// calculate patch
oldDeployJSON, _ := json.Marshal(deployCopy)
deployJSON, _ := json.Marshal(deploy)

patchBytes, _ := strategicpatch.CreateTwoWayMergePatch(oldDeployJSON, deployJSON, &appsv1.Deployment{})

// send patch
return c.Patch(ctx, deploy, client.RawPatch(types.StrategicMergePatchType, patchBytes))

This will sent a patch looking like this, which will leverage the API's mergeKey:

{"spec":{"template":{"spec":{"$setElementOrder/containers":[{"name":"nginx"},{"name":"sidecar"}],"containers":[{"image":"nginx:some-version","name":"nginx"}]}}}}

It would be nice to support creating such strategic merge patch similar to client.MergeFrom (which will create a json merge patch), like this:

// do changes
i, container := findContainerByName(deployCopy.Spec.Template.Spec.Containers, "nginx")
container.Image = "nginx:updated"
deployCopy.Spec.Template.Spec.Containers[i] = container

// send patch
return c.Patch(ctx, deploy, client.StrategicMergeFrom(deployCopy))
@timebertt
Copy link
Contributor Author

/assign

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant