Skip to content

spec.postBuild.substitute variables not being applied to the content of spec.patches #1543

@alanrichman

Description

@alanrichman

We are using Flux 2.7.2 and are seeing a problem where variables declared in spec.postBuild.substitute of a Kustomization are not being applied to the content of spec.patches of that same Kustomization. We use this behavior to extend the usage of ImagePolicy resources since the control comments for them do not work when in a Yaml string literal. We also use it for some other things but this is the easiest example to illustrate.

This worked before the 2.7 release:

apiVersion: kustomize.toolkit.fluxcd.io/v1
kind: Kustomization
metadata:
  name: some-tool
  namespace: flux-system
spec:
  interval: 1m0s
  sourceRef:
    kind: GitRepository
    name: flux-system
    namespace: flux-system
  path: ./path
  postBuild:
    substitute:
      someToolImage: my-registry.com/some-tool # {"$imagepolicy": "flux-system:some-tool:name"}
      someToolTag: 1.2.3 # {"$imagepolicy": "flux-system:some-tool:tag"}
  patches:
    - target:
        kind: HelmRelease
        name: some-tool
      patch: |-
        - op: add
          path: /spec/values/controller
          value:
            image: ${someToolImage}
            tag: ${someToolTag}

Now it produces a manifest in-cluster like this:

apiVersion: kustomize.toolkit.fluxcd.io/v1
kind: Kustomization
metadata:
  name: some-tool
  namespace: flux-system
spec:
  patches:
  - patch: "- op: add\n  path: /spec/values/controller/image\n
      \ value:\n    repository: \n    tag: @"
    target:
      kind: HelmRelease
      name: some-tool
  path: ./path
  postBuild:
    substitute:
      someToolImage: my-registry.com/some-tool
      someToolTag: 1.2.3
  sourceRef:
    kind: GitRepository
    name: flux-system

When it reconciles it produces this error which I suspect is just due to the variable not being substituted (or being substituted as an empty string) causing invalid yaml.

unable to parse SM or JSON patch from [patch: "- op: remove\n  path: /spec/chart/spec/version\n- op: replace\n  path: /spec/chart/spec/sourceRef/name\n  value: luna-sts\n- op: add\n  path: /spec/values/imagePullSecrets\n  value:\n    - name: harbor-cluster-docker-secret\n- op: add\n  path: /spec/values/operator/image\n  value:\n    repository: \n    tag: @"]

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions