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

Kustomize patches overwritten by namespace #5626

Open
AchimGrolimund opened this issue Mar 27, 2024 · 2 comments
Open

Kustomize patches overwritten by namespace #5626

AchimGrolimund opened this issue Mar 27, 2024 · 2 comments
Labels
kind/bug Categorizes issue or PR as related to a bug. needs-triage Indicates an issue or PR lacks a `triage/foo` label and requires one.

Comments

@AchimGrolimund
Copy link

What happened?

We would like to patch a rolebinding.yaml with kustomize. The namespace must be set in the meta information and the namespace must be patched in the subjcts area, as the service account can be in a different namespace.

With the old patchesJson6902 it works.

What did you expect to happen?

cI expect the namespace to be in the meta information and the namespace we want to batch in the subject.

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

patches fails

# kustomization.yaml
apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
namespace: ch-future-world-test

resources:
  - rolebinding_image-puller.yaml

patches:
  - target:
      kind: RoleBinding
      name: image-puller
      version: v1
    patch: |-
      - op: replace
        path: /subjects/0/namespace
        value: ch-future-world-prod
# rolebinding_image-puller.yaml
kind: RoleBinding
apiVersion: rbac.authorization.k8s.io/v1

metadata:
  name: image-puller
  namespace: ch-future-world-123
subjects:
  - kind: ServiceAccount
    name: default
    namespace: ch-future-world-intg
roleRef:
  apiGroup: rbac.authorization.k8s.io
  kind: ClusterRole
  name: system:image-puller

patchesJson6902 works

# kustomization.yaml
apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
namespace: ch-future-world-test

resources:
  - rolebinding_image-puller.yaml

patchesJson6902:
  - patch: |-
      - op: replace
        path: /subjects/0/namespace
        value: ch-future-world-prod
    target:
      kind: RoleBinding
      name: image-puller
      version: v1
# rolebinding_image-puller.yaml
kind: RoleBinding
apiVersion: rbac.authorization.k8s.io/v1

metadata:
  name: image-puller
  namespace: ch-future-world-123
subjects:
  - kind: ServiceAccount
    name: default
    namespace: ch-future-world-intg
roleRef:
  apiGroup: rbac.authorization.k8s.io
  kind: ClusterRole
  name: system:image-puller

Expected output

apiVersion: rbac.authorization.k8s.io/v1
kind: RoleBinding
metadata:
  name: image-puller
  namespace: ch-future-world-test
roleRef:
  apiGroup: rbac.authorization.k8s.io
  kind: ClusterRole
  name: system:image-puller
subjects:
- kind: ServiceAccount
  name: default
  namespace: ch-future-world-prod # <<== Here is the PROD Namespace from the patches part

Actual output

patches fails

apiVersion: rbac.authorization.k8s.io/v1
kind: RoleBinding
metadata:
  name: image-puller
  namespace: ch-future-world-test
roleRef:
  apiGroup: rbac.authorization.k8s.io
  kind: ClusterRole
  name: system:image-puller
subjects:
- kind: ServiceAccount
  name: default
  namespace: ch-future-world-test # <<== Here is the wrong namespace coming from `namespace`

patchesJson6902 works

apiVersion: rbac.authorization.k8s.io/v1
kind: RoleBinding
metadata:
  name: image-puller
  namespace: ch-future-world-test
roleRef:
  apiGroup: rbac.authorization.k8s.io
  kind: ClusterRole
  name: system:image-puller
subjects:
- kind: ServiceAccount
  name: default
  namespace: ch-future-world-prod # <<== Here is the right namespace coming from `patchesJson6902`

Kustomize version

4.5.4 & 5.3.0

Operating system

Linux

@AchimGrolimund AchimGrolimund added the kind/bug Categorizes issue or PR as related to a bug. label Mar 27, 2024
@k8s-ci-robot k8s-ci-robot added the needs-triage Indicates an issue or PR lacks a `triage/foo` label and requires one. label Mar 27, 2024
@k8s-ci-robot
Copy link
Contributor

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.

@matthewhughes934
Copy link
Contributor

matthewhughes934 commented Apr 3, 2024

Replacing the namespace on the subject of a RoleBinding where the subject name is "default" appears to be the expected behaviour (from here):

// In DefaultSubjectsOnly mode (default mode), RoleBinding and ClusterRoleBinding have namespace set on
// elements of the "subjects" field if and only if the subject elements
// "name" is "default". Otherwise the namespace is not set.

You may be able to configure a namespace transformer to use some other behaviour here (some details/discussion in this issue: #629)

As for why it works with a patchesJson6902 I believe that's because of the ordering of the slice here: builtinhelpers.PatchTransformer is before builtinhelpers.NamespaceTransformer so the patch is applied, then the namespace transformer updates the namespace, but builtinhelpers.PatchJson6902Transformer is after the namespace transformer so it applies the patch after the namespace transformer does its update (I am not sure if this is the desired/expected behaviour)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
kind/bug Categorizes issue or PR as related to a bug. needs-triage Indicates an issue or PR lacks a `triage/foo` label and requires one.
Projects
None yet
Development

No branches or pull requests

3 participants