kustomize icon indicating copy to clipboard operation
kustomize copied to clipboard

patches not working like patchesStrategicMerge when deleting manifests

Open mtrin opened this issue 1 year ago • 5 comments

What happened?

patches:
  - |-
    kind: ConfigMap
    apiVersion: v1
    metadata:
      name: foo
      namespace: bar
    $patch: delete

Gives

Error: invalid Kustomization: json: cannot unmarshal string into Go struct field Kustomization.patches of type types.Patch

patchesStrategicMerge:
  - |-
    kind: ConfigMap
    apiVersion: v1
    metadata:
      name: foo
      namespace: bar
    $patch: delete

it works with the warning

Warning: 'patchesStrategicMerge' is deprecated. Please use 'patches' instead. Run 'kustomize edit fix' to update your Kustomization automatically

What did you expect to happen?

patches to behave like patchesStrategicMerge

How can we reproduce it (as minimally and precisely as possible)?

as above

Expected output

No response

Actual output

No response

Kustomize version

v5.1.1

Operating system

Linux

mtrin avatar Oct 10 '23 22:10 mtrin

This issue is currently awaiting triage.

SIG CLI takes a lead on issue triage for this repo, but any Kubernetes member can accept issues by applying the triage/accepted label.

The triage/accepted label can be added by org members by writing /triage accepted in a comment.

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes/test-infra repository.

k8s-ci-robot avatar Oct 10 '23 22:10 k8s-ci-robot

also have this issue where patches: does not action $delete the same way patchesStrategicMerge did, meaning even if I split everytihng into different yaml, it's rather busted.

craiggunson avatar Jan 05 '24 01:01 craiggunson

Well the issue is as followed.

given the following structure:

$ cat resources.yaml
apiVersion: v1
kind: ConfigMap
metadata:
  name: example-config-1
data:
  # example of a simple property defined using --from-literal
  example.property.1: hello
---
apiVersion: v1
kind: ConfigMap
metadata:
  name: example-config-2
data:
  # example of a simple property defined using --from-literal
  example.property.2: world
$ cat kustomization.yaml
apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization

resources:
- resources.yaml

patches:
- |-
  $patch: delete
  kind: ConfigMap
  apiVersion: v1
  metadata:
    name: example-config-2

We get the following error:

$ ../kustomize build .
Error: invalid Kustomization: json: cannot unmarshal string into Go struct field Kustomization.patches of type types.Patch

Even with the latest kustomize version (5.4.0).

If we changes this to a patchesStrategicMerge, then it works, but we get a warning:

 ../kustomize build .
# Warning: 'patchesStrategicMerge' is deprecated. Please use 'patches' instead. Run 'kustomize edit fix' to update your Kustomization automatically.
apiVersion: v1
data:
  example.property.1: hello
kind: ConfigMap
metadata:
  name: example-config-1

If we do what the warning recommends, we get an actually working version:

$ cp kustomization.yaml kustomization.yaml.old
$ ../kustomize edit fix
# Warning: 'patchesStrategicMerge' is deprecated. Please use 'patches' instead. Run 'kustomize edit fix' to update your Kustomization automatically.

Fixed fields:
  patchesJson6902 -> patches
  patchesStrategicMerge -> patches
  commonLabels -> labels

To convert vars -> replacements, run the command `kustomize edit fix --vars`

WARNING: Converting vars to replacements will potentially overwrite many resource files
and the resulting files may not produce the same output when `kustomize build` is run.
We recommend doing this in a clean git repository where the change is easy to undo.

$ ../kustomize build .
apiVersion: v1
data:
  example.property.1: hello
kind: ConfigMap
metadata:
  name: example-config-1


$ diff -Naur kustomization.yaml.old kustomization.yaml
--- kustomization.yaml.old	2024-02-27 21:46:14.268610306 +0100
+++ kustomization.yaml	2024-02-27 21:46:24.702542924 +0100
@@ -4,10 +4,10 @@
 resources:
 - resources.yaml

-patchesStrategicMerge:
-- |-
-  $patch: delete
-  kind: ConfigMap
-  apiVersion: v1
-  metadata:
-    name: example-config-2
+patches:
+- patch: |-
+    $patch: delete
+    kind: ConfigMap
+    apiVersion: v1
+    metadata:
+      name: example-config-2

Now what is the trick? patches: needs each patch to start with patch: and not directly the patch as a structure.

So I think this works as intended.

duritong avatar Feb 27 '24 20:02 duritong