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

$(HOSTNAME) not replaced in args #57726

Closed
prestonvanloon opened this issue Jan 2, 2018 · 23 comments
Closed

$(HOSTNAME) not replaced in args #57726

prestonvanloon opened this issue Jan 2, 2018 · 23 comments
Labels
kind/bug Categorizes issue or PR as related to a bug. sig/node Categorizes an issue or PR as relevant to SIG Node.

Comments

@prestonvanloon
Copy link

Is this a BUG REPORT or FEATURE REQUEST?:

/kind bug

What happened:

Given the following pod configuration:

apiVersion: v1
kind: Pod
metadata:
  name: command-demo
  labels:
    purpose: demonstrate-command
spec:
  containers:
  - name: command-demo-container
    image: debian
    command: ["/bin/echo"]
    args: ["$(HOSTNAME)"]
  restartPolicy: OnFailure

This pod echoed literately $(HOSTNAME).

What you expected to happen:

The pod to echo command-demo (the pod name).

How to reproduce it (as minimally and precisely as possible):

kubectl apply -f pod.yaml
kubectl logs command-demo

Anything else we need to know?:

This works for other environment variables such as KUBERNETES_SERVICE_HOST.

Environment:

  • Kubernetes version (use kubectl version):
Client Version: version.Info{Major:"1", Minor:"7", GitVersion:"v1.7.3", GitCommit:"2c2fe6e8278a5db2d15a013987b53968c743f2a1", GitTreeState:"clean", BuildDate:"2017-08-03T07:00:21Z", GoVersion:"go1.8.3", Compiler:"gc", Platform:"linux/amd64"}
Server Version: version.Info{Major:"1", Minor:"7", GitVersion:"v1.7.0", GitCommit:"d3ada0119e776222f11ec7945e6d860061339aad", GitTreeState:"clean", BuildDate:"2017-07-26T00:12:31Z", GoVersion:"go1.8.3", Compiler:"gc", Platform:"linux/amd64"}
  • Cloud provider or hardware configuration: minikube version: v0.21.0
  • OS (e.g. from /etc/os-release):
NAME="Ubuntu"
VERSION="17.10 (Artful Aardvark)"
ID=ubuntu
ID_LIKE=debian
PRETTY_NAME="Ubuntu 17.10"
VERSION_ID="17.10"
HOME_URL="https://www.ubuntu.com/"
SUPPORT_URL="https://help.ubuntu.com/"
BUG_REPORT_URL="https://bugs.launchpad.net/ubuntu/"
PRIVACY_POLICY_URL="https://www.ubuntu.com/legal/terms-and-policies/privacy-policy"
VERSION_CODENAME=artful
UBUNTU_CODENAME=artful
  • Kernel (e.g. uname -a):
    Linux naydra 4.13.0-21-generic #24-Ubuntu SMP Mon Dec 18 17:29:16 UTC 2017 x86_64 x86_64 x86_64 GNU/Linux
  • Install tools:
  • Others:
@k8s-ci-robot k8s-ci-robot added needs-sig Indicates an issue or PR lacks a `sig/foo` label and requires one. kind/bug Categorizes issue or PR as related to a bug. labels Jan 2, 2018
@prestonvanloon
Copy link
Author

I'm not 100% sure on the label...
@kubernetes/sig-scheduling-bug

@dims
Copy link
Member

dims commented Jan 4, 2018

/sig scheduling

@k8s-ci-robot k8s-ci-robot added sig/scheduling Categorizes an issue or PR as relevant to SIG Scheduling. and removed needs-sig Indicates an issue or PR lacks a `sig/foo` label and requires one. labels Jan 4, 2018
@ravisantoshgudimetla
Copy link
Contributor

/remove-sig scheduling
/sig node

@k8s-ci-robot k8s-ci-robot added sig/node Categorizes an issue or PR as relevant to SIG Node. and removed sig/scheduling Categorizes an issue or PR as relevant to SIG Scheduling. labels Jan 12, 2018
@cjcaj
Copy link

cjcaj commented Jan 19, 2018

You could use the downward API to expose the pod name:
https://kubernetes.io/docs/tasks/inject-data-application/environment-variable-expose-pod-information/

@prestonvanloon
Copy link
Author

@cjcaj Thanks for the tip. I think this is still a valid issue because you can access HOSTNAME as the environment variable (i.e. printenv HOSTNAME), but cannot pass it as an argument.

My current workaround is to execute the command from bash as such:

       command: 
           - "/bin/bash"
           - "-c"
           - "echo $HOSTNAME"

@cristifalcas
Copy link

cristifalcas commented Apr 5, 2018

I would like to use this also. HOSTNAME to be replaced in arguments.

In AWS for now the NODE name is different from hostname. NODE name is set as AWS internal hostname, the hostname is from our organization.

@fejta-bot
Copy link

Issues go stale after 90d of inactivity.
Mark the issue as fresh with /remove-lifecycle stale.
Stale issues rot after an additional 30d of inactivity and eventually close.

If this issue is safe to close now please do so with /close.

Send feedback to sig-testing, kubernetes/test-infra and/or fejta.
/lifecycle stale

@k8s-ci-robot k8s-ci-robot added the lifecycle/stale Denotes an issue or PR has remained open with no activity and has become stale. label Jul 4, 2018
@Ochir
Copy link

Ochir commented Jul 18, 2018

I also vote for this feature request

@discordianfish
Copy link
Contributor

What's wrong with using the downward api for this? (Beside I couldn't find the doc with google, which lead me here ;)).

@prestonvanloon The variables in command/args aren't really env variable. They can only refer to the variables defined in the manifest, which is why without setting HOSTNAME yourself (e.g via downward api) it won't be available.

@nikhita
Copy link
Member

nikhita commented Aug 10, 2018

/remove-lifecycle stale

@k8s-ci-robot k8s-ci-robot removed the lifecycle/stale Denotes an issue or PR has remained open with no activity and has become stale. label Aug 10, 2018
@prestonvanloon
Copy link
Author

@discordianfish my specific use case is to use the hostname of a pod created by some deployment.

Does the downward API provide this as a command line argument or simply making it available as an environment variable? If the case is the latter, then I'm in the same spot. I can already access the HOSTNAME via environment variable, but I can't intuitively provide it as a command argument.

@discordianfish
Copy link
Contributor

@prestonvanloon It makes it available as env variable and available for templating. Just try setting HOSTNAME via the downward api and you should be able to refer to it in your args like you do, e.g:

apiVersion: v1
kind: Pod
metadata:
  name: command-demo
  labels:
    purpose: demonstrate-command
spec:
  containers:
  - name: command-demo-container
    image: debian
    command: ["/bin/echo"]
    args: ["$(NODE_NAME)"]
    env:
    - name: NODE_NAME
      valueFrom:
        fieldRef:
          fieldPath: spec.nodeName

@discordianfish
Copy link
Contributor

Can this be closed?

If anything this is a documentation issue. I see how it's confusing that these arg variable references look like environment variables although they are not.

@cristifalcas
Copy link

This is not true for provider aws. In this case it will put the internal aws name of the instance, not the hostname

@discordianfish
Copy link
Contributor

@cristifalcas It will pull in the kubernetes node name. Is there a reason for having a different name there than your hostname? Not sure what exact setup you run on AWS, but I'd argue the node name should ideally be the same as the hostname.

@cristifalcas
Copy link

It's a bug in k8s: #54482

@prestonvanloon
Copy link
Author

@discordianfish Please look at my original example, I need the pod's name, not the node's name.

@discordianfish
Copy link
Contributor

@prestonvanloon
Copy link
Author

@discordianfish Thanks! That would work for me.

It is not as ideal as using the $HOSTNAME, but it would solve my problem.

I'll go ahead and close this as my issue has a workaround with the downward API.

@gertcuykens
Copy link

Just want to add this for completion because it took me some time to figure out

    env:
    - name: HOSTNAME
      valueFrom:
        fieldRef:
          fieldPath: metadata.name

@ritheshgm
Copy link

Was there any update on that, I was trying to use the env variable from my worker node, When I tried to use that env in POD yaml it just proving the string not the actual value as shown below.

apiVersion: v1
kind: Pod
metadata:
name: test-1
spec:
containers:

  • name: test-container
    image: k8s.gcr.io/busybox
    command: [ "sh", "-c"]
    args:
    • while true; do
      echo -en '\n';
      printenv DB_NAME;
      sleep 1000;
      done;
      env:
    • name: DB_NAME
      value: ${DB_NAME}
      restartPolicy: Never

on container I'm getting below output.

/ # echo $DB_NAME
${DB_NAME}
/ #

@frankruizhi
Copy link

Was there any update on that, I was trying to use the env variable from my worker node, When I tried to use that env in POD yaml it just proving the string not the actual value as shown below.

apiVersion: v1
kind: Pod
metadata:
name: test-1
spec:
containers:

  • name: test-container
    image: k8s.gcr.io/busybox
    command: [ "sh", "-c"]
    args:

    • while true; do
      echo -en '\n';
      printenv DB_NAME;
      sleep 1000;
      done;
      env:
    • name: DB_NAME
      value: ${DB_NAME}
      restartPolicy: Never

on container I'm getting below output.

/ # echo $DB_NAME
${DB_NAME}
/ #

Maybe you could use $(DB_NAME) instead.

HarikrishnanBalagopal added a commit to HarikrishnanBalagopal/move2kube-ui that referenced this issue Jul 31, 2022
There are certain nuances to working with environment
variables in Kubernetes YAMLs.
See kubernetes/kubernetes#57726

Also add a spinner to QA wizard while processing.

Signed-off-by: Harikrishnan Balagopal <harikrishmenon@gmail.com>
HarikrishnanBalagopal added a commit to HarikrishnanBalagopal/move2kube-ui that referenced this issue Jul 31, 2022
There are certain nuances to working with environment
variables in Kubernetes YAMLs.
See kubernetes/kubernetes#57726

Also add a spinner to QA wizard while processing.

Signed-off-by: Harikrishnan Balagopal <harikrishmenon@gmail.com>
ashokponkumar pushed a commit to konveyor/move2kube-ui that referenced this issue Jul 31, 2022
There are certain nuances to working with environment
variables in Kubernetes YAMLs.
See kubernetes/kubernetes#57726

Also add a spinner to QA wizard while processing.

Signed-off-by: Harikrishnan Balagopal <harikrishmenon@gmail.com>
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. sig/node Categorizes an issue or PR as relevant to SIG Node.
Projects
None yet
Development

No branches or pull requests