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

get pod namespace/name from environment #311

Merged
merged 1 commit into from
Jul 25, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 23 additions & 11 deletions pkg/exporter/nettop/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,24 +64,36 @@ func podNameFromServiceAccountToken() (string, error) {
}

func currentPodInfo() (string, string, error) {
namespace, err := os.ReadFile("/var/run/secrets/kubernetes.io/serviceaccount/namespace")
if err != nil {
return "", "", fmt.Errorf("failed get namespace in sidecar mode, err: %v", err)
}

name, err := podNameFromServiceAccountToken()
if err != nil {
log.Warnf("failed get pod name from /var/run/secrets/kubernetes.io/serviceaccount/token, fallback to hostname")
var err error
namespace := os.Getenv("KUBESKOOP_POD_NAMESPACE")
name := os.Getenv("KUBESKOOP_POD_NAME")

name, err := os.ReadFile("/etc/hostname")
if namespace == "" {
log.Infof("failed get pod namespace for sidecar mode from env KUBESKOOP_POD_NAMESPACE, try from k8s serviceaccount")
namespaceBytes, err := os.ReadFile("/var/run/secrets/kubernetes.io/serviceaccount/namespace")
if err != nil {
return "", "", fmt.Errorf("failed get namespace in sidecar mode, err: %v", err)
}

return string(namespace), string(name), nil
namespace = string(namespaceBytes)
}

if name == "" {
log.Infof("failed get pod namespace for sidecar mode from env KUBESKOOP_POD_NAME, try from k8s serviceaccount")
name, err = podNameFromServiceAccountToken()
if err != nil {
log.Warnf("failed get pod name from /var/run/secrets/kubernetes.io/serviceaccount/token, fallback to hostname")

nameBytes, err := os.ReadFile("/etc/hostname")
if err != nil {
return "", "", fmt.Errorf("failed get namespace in sidecar mode, err: %v", err)
}

return namespace, string(nameBytes), nil
}
}

return string(namespace), name, nil
return namespace, name, nil
}

func initDefaultEntity(sidecarMode bool) error {
Expand Down
Loading