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

This was working file with v3.0.0, I’ve updated to v3.0.3 and I’m getting the error:

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

This is the kustomization file:

# Adds namespace to all resources.
namespace: marquis-system

# Value of this field is prepended to the
# names of all resources, e.g. a deployment named
# "wordpress" becomes "alices-wordpress".
# Note that it should also match with the prefix (text before '-') of the namespace
# field above.
namePrefix: marquis-

# Labels to add to all resources and selectors.
#commonLabels:
#  someName: someValue

# Each entry in this list must resolve to an existing
# resource definition in YAML.  These are the resource
# files that kustomize reads, modifies and emits as a
# YAML string, with resources separated by document
# markers ("---").
resources:
- ./rbac/rbac_role.yaml
- ./rbac/rbac_role_binding.yaml
- ./manager/manager.yaml
- ./rbac/auth_proxy_service.yaml
- ./rbac/auth_proxy_role.yaml
- ./rbac/auth_proxy_role_binding.yaml

patches:
- manager_image_patch.yaml
  # Protect the /metrics endpoint by putting it behind auth.
  # Only one of manager_auth_proxy_patch.yaml and
  # manager_prometheus_metrics_patch.yaml should be enabled.
- manager_auth_proxy_patch.yaml
  # If you want your controller-manager to expose the /metrics
  # endpoint w/o any authn/z, uncomment the following line and
  # comment manager_auth_proxy_patch.yaml.
  # Only one of manager_auth_proxy_patch.yaml and
  # manager_prometheus_metrics_patch.yaml should be enabled.
#- manager_prometheus_metrics_patch.yaml

About this issue

  • Original URL
  • State: closed
  • Created 5 years ago
  • Comments: 17 (4 by maintainers)

Commits related to this issue

Most upvoted comments

I think the problem is kubectl uses a much older version of kustomize. It appears the “multiple patching” support was added in v3.1.0 of kustomize (based on when examples/patchMultipleObjects.md was added in this commit).

Works in kustomize 3.5.4

❯ kustomize version
{Version:3.5.4 GitCommit:3af514fa9f85430f0c1557c4a0291e62112ab026 BuildDate:2020-01-17T14:23:25+00:00 GoOs:darwin GoArch:amd64}

overlays/my-overlay/kustomization.yaml

patches:
  - patch: |
      apiVersion: apps/v1
      kind: Deployment
      metadata:
        name: does-not-matter
      spec:
        template:
          spec:
            priorityClassName: my-priority-class
    target:
      kind: Deployment

This works:

kustomize build overlays/my-overlay

But not in kubectl (1.18) with kustomize (2.0.3)

❯ kubectl version
Client Version: version.Info{Major:"1", Minor:"18", GitVersion:"v1.18.1", GitCommit:"7879fc12a63337efff607952a323df90cdc7a335", GitTreeState:"clean", BuildDate:"2020-04-10T21:53:51Z", GoVersion:"go1.14.2", Compiler:"gc", Platform:"darwin/amd64"}

Currently the only way to know which version of kustomize is used in kubectl is to consult the README, which currently states its using v2.0.3.

So this does not work:

❯ kubectl kustomize overlays/my-overlay
Error: AccumulateTarget: couldn't make target for {redacted}: cannot unmarshal object into Go struct field Kustomization.patchesStrategicMerge of type patch.StrategicMerge

As opposed to pointed out in other comments, patches should be used only, as patchesStrategicMerge field was deprecated in v5.0.0.

The change that worked for me while using patches: only, was:

 patches:
- ./patch-deployment.yaml
- ./patch-hpa.yaml
 patches:
- path: ./patch-deployment.yaml
- path: ./patch-hpa.yaml

Adding path: in patches.

Hi since patches is obsolete, this example should be removed or updated.

Yep that’s right

As opposed to pointed out in other comments, patches should be used only, as patchesStrategicMerge field was deprecated in v5.0.0.

The change that worked for me while using patches: only, was:

 patches:
- ./patch-deployment.yaml
- ./patch-hpa.yaml
 patches:
- path: ./patch-deployment.yaml
- path: ./patch-hpa.yaml

Adding path: in patches.

This can be automatically done by running kustomize edit fix.

Interesting…Was looking at the log. patches has been deprecated last year v1.0.9.

I guess we will have to check if kubebuilder needs to be updated. Meanwhile the easiest workaround is replaced patches: by patchesStrategicMerge:

“patches:” was obsolete. You are supposed to use “patchesStrategicMerge:” instead.

Since “patches:” is used for more extended patching.