kubernetes-sigs / kustomize

Customization of kubernetes YAML configurations
Apache License 2.0
10.7k stars 2.22k forks source link

element spec.rules.http.paths.backend.service.port of ingress resource is not inherited #5713

Open heroes1412 opened 3 weeks ago

heroes1412 commented 3 weeks ago

What happened?

i try to create a ingress-patch.yaml file, to patch some value, i found spec.rules.http.paths.backend.service.port and spec.rules.http.paths.backend.service.port.number is not inherited from the base

What did you expect to happen?

spec.rules.http.paths.backend.service.port and spec.rules.http.paths.backend.service.port.number is inherited from the base

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

# kustomization.yaml
apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
resources:
- ../../base
patches:
- path: ingress-patch.yaml
# ingress.yaml
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: app2.rke2.local
  labels:
    app: app2
  annotations:
    nginx.ingress.kubernetes.io/rewrite-target: /
    nginx.ingress.kubernetes.io/use-forwarded-headers: "true"
spec:
  ingressClassName: nginx
  rules:
  - host: "app2.hoang92bn.local"
    http:
      paths:
      - pathType: Prefix
        path: "/"
        backend:
          service:
            name: svc-app2
            port:
              number: 30080
# ingress-patch.yaml
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: app2.rke2.local
  labels:
    app: app2
  annotations:
    nginx.ingress.kubernetes.io/rewrite-target: /
    nginx.ingress.kubernetes.io/use-forwarded-headers: "true"
spec:
  ingressClassName: nginx
  rules:
  - host: "app2.hoang92bn.local"
    http:
      paths:
      - pathType: Prefix
        path: "/"
        backend:
          service:
            name: svc-app2

Expected output

service.port of ingress resource will be inherited from the base.

Actual output

apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  annotations:
    ....
  labels:
    app: app2
    env: prod
  name: prod-app2.rke2.local
spec:
  ingressClassName: nginx
  rules:
  - host: prod.app2.rke2.local
    http:
      paths:
      - backend:
          service:
            name: prod-svc-app2
        path: /
        pathType: Prefix

Kustomize version

v5.4.2

Operating system

Linux

koba1t commented 4 days ago

Hi @heroes1412

The patches.path in kustomize is using Patch Strategic Merge method that is used in kubect. When we want to merge a list in K8s resources, kustomize check "x-kubernetes-patch-strategy" anchor in the API definition in the kubernetes. (like here) But ingress looks like no anchors that referred from kustomize and kustomize can't merge lists and try to replace all items.

So, Could you consider using a JSON6902-style patch? You can patch for resources what you want to do.

/triage out-of-scope /kind needs-information

k8s-ci-robot commented 4 days ago

@koba1t: The label(s) kind/needs-information cannot be applied, because the repository doesn't have them.

In response to [this](https://github.com/kubernetes-sigs/kustomize/issues/5713#issuecomment-2187208373): >Hi @heroes1412 > >The `patches.path` in kustomize is using [Patch Strategic Merge method](https://kubectl.docs.kubernetes.io/references/kustomize/kustomization/patchesstrategicmerge/) that is used in [kubect]( >https://kubernetes.io/docs/tasks/manage-kubernetes-objects/update-api-object-kubectl-patch/). >When we want to merge a list in K8s resources, kustomize check ["x-kubernetes-patch-strategy"](https://kubernetes.io/docs/tasks/manage-kubernetes-objects/update-api-object-kubectl-patch/#notes-on-the-strategic-merge-patch) anchor in the API definition in the [kubernetes](https://github.com/kubernetes/kubernetes). (like [here](https://github.com/kubernetes/kubernetes/blob/b510f785e6f65cf10ed80b0eb032e867676c49a7/api/openapi-spec/v3/api__v1_openapi.json#L2078)) >But [ingress](https://github.com/kubernetes/kubernetes/blob/b510f785e6f65cf10ed80b0eb032e867676c49a7/api/openapi-spec/v3/apis__networking.k8s.io__v1_openapi.json) looks like no anchors that referred from kustomize and kustomize can't merge lists and try to replace all items. > >So, Could you consider using a [JSON6902-style patch](https://kubectl.docs.kubernetes.io/references/kustomize/kustomization/patches/#patch-using-inline-json6902)? >You can patch for resources what you want to do. > >/triage out-of-scope >/kind needs-information Instructions for interacting with me using PR comments are available [here](https://git.k8s.io/community/contributors/guide/pull-requests.md). If you have questions or suggestions related to my behavior, please file an issue against the [kubernetes-sigs/prow](https://github.com/kubernetes-sigs/prow/issues/new?title=Prow%20issue:) repository.