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

[BUG] Sidecarset sourceContainerNameFrom 并不支持name,namespace #1735

Open
magicsong opened this issue Sep 10, 2024 · 12 comments · May be fixed by #1739
Open

[BUG] Sidecarset sourceContainerNameFrom 并不支持name,namespace #1735

magicsong opened this issue Sep 10, 2024 · 12 comments · May be fixed by #1739
Assignees
Labels
kind/bug Something isn't working

Comments

@magicsong
Copy link

What happened:
https://github.com/pigletfly/kruise/blob/6a62320848f8e10e037c747eeef6b99552af7fa9/pkg/control/sidecarcontrol/util.go#L434
What you expected to happen:
这里hardcode枚举了,仅仅支持labels和annoation,sidecar无法获取自己的name,namepsace
How to reproduce it (as minimally and precisely as possible):

Anything else we need to know?:

Environment:

  • Kruise version: 最新代码
  • Kubernetes version (use kubectl version):
  • Install details (e.g. helm install args):
  • Others:
@magicsong magicsong added the kind/bug Something isn't working label Sep 10, 2024
@magicsong
Copy link
Author

https://github.com/kubernetes/kubernetes/blob/master/pkg/apis/core/pods/helpers.go 更新一下代码是不是就可以了

@ABNER-1
Copy link
Member

ABNER-1 commented Sep 11, 2024

@magicsong 看起来是的,你愿意同步一下这段代码贡献一下吗?
如果可以的话可以将这几个case 加到对应的 ut 中。

@ABNER-1 ABNER-1 added the kind/good-first-issue Good for newcomers label Sep 11, 2024
@furykerry
Copy link
Member

it is strange to use pod name and namespace as part of the container name , are there any reason of this usage? @magicsong

@magicsong
Copy link
Author

it is strange to use pod name and namespace as part of the container name , are there any reason of this usage? @magicsong

"sourceContainerNameFrom should be a mechanism similar to the downward API, used to mount pod metadata to environment variables, rather than its literal name meaning."

@furykerry
Copy link
Member

furykerry commented Sep 11, 2024

"sourceContainerNameFrom should be a mechanism similar to the downward API, used to mount pod metadata to environment variables, rather than its literal name meaning."

so what field do you need the complete downward api ? it make no sense to make sourceContainerNameFrom refer to the pod name. Do you mean other fields? TransferEnv can refer to the environment name of another container, this environment can use downward api.

@magicsong
Copy link
Author

"sourceContainerNameFrom should be a mechanism similar to the downward API, used to mount pod metadata to environment variables, rather than its literal name meaning."

so what field do you need the complete downward api ? it make no sense to make sourceContainerNameFrom refer to the pod name. Do you mean other fields? TransferEnv can refer to the environment name of another container, this environment can use downward api.

The sidecar needs to have the ability to know its own name and namespace without interfering with the main container. Not all main containers have the environment variables PodName and PodNamespace, and the names are fixed.

Additionally, the OpenKruise documentation mentions support for [metadata.name], but it is not actually supported.

@magicsong magicsong linked a pull request Sep 12, 2024 that will close this issue
@furykerry
Copy link
Member

The sidecar needs to have the ability to know its own name and namespace without interfering with the main container. Not all main containers have the environment variables PodName and PodNamespace, and the names are fixed.

you do not need to use transferEnv for this purpose, you can just use downward api related environment in the container part of sidecarset (sidecarset.spec.containers[].env[].valueFrom.fieldRef.fieldPath )

@magicsong
Copy link
Author

The sidecar needs to have the ability to know its own name and namespace without interfering with the main container. Not all main containers have the environment variables PodName and PodNamespace, and the names are fixed.

you do not need to use transferEnv for this purpose, you can just use downward api related environment in the container part of sidecarset (sidecarset.spec.containers[].env[].valueFrom.fieldRef.fieldPath )

maybe you could have a try. it will be rejected

@furykerry
Copy link
Member

you can try this example

apiVersion: apps.kruise.io/v1alpha1
kind: SidecarSet
metadata:
  name: test-sidecarset
spec:
  selector:
    matchLabels:
      app: nginx
  # matchExpressions:
  # - {key: tier, operator: In, values: [frontend, middleware]}
  updateStrategy:
    type: RollingUpdate
    maxUnavailable: 1
  containers:
  - name: sidecar1
    image: centos:6.7
    command: ["sleep", "999d"] # do nothing at all
    env:
    - name: podName
      valueFrom:
        fieldRef:
          fieldPath: metadata.name
    volumeMounts:
    - name: log-volume
      mountPath: /var/log
  volumes: # this field will be merged into pod.spec.volumes
  - name: log-volume
    emptyDir: {}

@furykerry furykerry removed the kind/good-first-issue Good for newcomers label Sep 12, 2024
@magicsong
Copy link
Author

magicsong commented Sep 13, 2024

you can try this example

apiVersion: apps.kruise.io/v1alpha1
kind: SidecarSet
metadata:
  name: test-sidecarset
spec:
  selector:
    matchLabels:
      app: nginx
  # matchExpressions:
  # - {key: tier, operator: In, values: [frontend, middleware]}
  updateStrategy:
    type: RollingUpdate
    maxUnavailable: 1
  containers:
  - name: sidecar1
    image: centos:6.7
    command: ["sleep", "999d"] # do nothing at all
    env:
    - name: podName
      valueFrom:
        fieldRef:
          fieldPath: metadata.name
    volumeMounts:
    - name: log-volume
      mountPath: /var/log
  volumes: # this field will be merged into pod.spec.volumes
  - name: log-volume
    emptyDir: {}

considering the container name of the main container. if there is more than 2 sidecar, i need to know which is the main container according its name

@magicsong
Copy link
Author

magicsong commented Sep 13, 2024

After some experimentation, I found that:

  1. sourceContainerNameFrom cannot retrieve the container name.
  2. fieldRef is a limited subset of the official capabilities, and it's better to use DownwardApi directly.
  3. The documentation is misleading. https://openkruise.io/zh/docs/user-manuals/sidecarset/#sidecar-container%E6%B3%A8%E5%85%A5

@furykerry
Copy link
Member

since sidecar and app container shares the same pod, sidecar can retrieve the pod information using downward api with sidecar's own environment, it is not necessary to transfer environment from app container.

Anyway the document is indeed misleading, we will fix that.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
kind/bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants