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

Add template function to syslog adapter to traverse .Container.Config.Env #182

Open
wants to merge 4 commits into
base: master
Choose a base branch
from

Conversation

Carles-Figuerola
Copy link

This was created as a way to pass a specific environment variable that's inside of the docker container. The variable .Container.Config.Env is formed of an array of key=value so it's not sortable or findable from the templating engine. Calling the container with something like this:

sudo docker run -d -v /var/run/docker.sock:/var/run/docker.sock -e SYSLOG_TAG="{{getEnvVar .Container.Config.Env \"MARATHON_APP_ID\"}}"  logspout:dev

will create a line as such:

Apr 27 15:50:56 87d5094fd8f8 nginx-marathon[19644]: 10.213.48.218 - - [27/Apr/2016:15:50:56 +0000] "GET / HTTP/1.1" 304 0 "-" "asdfsadf" "-"

where nginx-marathon is part of the .Container.Config.Env:

$ docker inspect 87d5094fd8f8 
<sic>
           "Env": [
                "MARATHON_APP_ID=nginx-marathon",
                "TEST=test",
                "SOMETHING_ELSE=false",
                "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin",
                "NGINX_VERSION=1.9.14-1~jessie"
            ],
<sic>

This would also help solve #163 and #137

PS. several envs can be also concatenated:

sudo docker run -d -v /var/run/docker.sock:/var/run/docker.sock -e SYSLOG_TAG="{{getEnvVar .Container.Config.Env \"MARATHON_APP_ID\"}}, {{getEnvVar .Container.Config.Env \"TEST\"}}"  logspout:dev

@@ -29,6 +30,15 @@ func getopt(name, dfault string) string {
return value
}

func tplGetEnvVar(env []string, key string) string {
for _, value := range env {
if strings.HasPrefix(value, fmt.Sprintf("%s%s", key, "=")) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe do the fmt.Sprintf("%s%s", key, "=") once in the beginning of the function? Saves a few allocs.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, changed.

@JeanMertz
Copy link

@josegonzalez any chance to get this merged? This would help us a lot in identifying logs from specific services.

@timbunce
Copy link

Ping. Another hopeful user.

@@ -29,6 +30,16 @@ func getopt(name, dfault string) string {
return value
}

func tplGetEnvVar(env []string, key string) string {
key_equals := fmt.Sprintf("%s%s", key, "=")
Copy link
Contributor

@gaul gaul Sep 20, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

key_equals := key + "="

key_equals := fmt.Sprintf("%s%s", key, "=")
for _, value := range env {
if strings.HasPrefix(value, key_equals) {
return strings.Split(value, "=")[1]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

return value[strings.Index(value, "=")+1:]

key_equals := key + "="
for _, value := range env {
if strings.HasPrefix(value, key_equals) {
return value[strings.Index(value, "=")+1:]
Copy link
Contributor

@gaul gaul Sep 20, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can simplify this to:

return value[len(key_equals)+1:]

@billryan
Copy link

Hope this PR could be merged soon.

@daniilyar
Copy link

+1, we really need this. Envvars are the only way to pass additional info about the container in case if your container orchestrator doesn't allow setting a container labels, but allows setting container envvars. The good example is Kubernetes, which does not allow (kubernetes/kubernetes#3764) setting container labels, only pod labels

@jorisw
Copy link

jorisw commented Oct 12, 2017

Did somebody try to merge this and the CI somehow failed? If this PR has been approved many of us would like to use it.

@mashayev
Copy link

@jorisw

  1. If I'm not wrong this feature was released. you can use:

    - name: SYSLOG_TAG
      value: '{{ index .Container.Config.Env "PROGRAM_NAME" }}'
    

    When PROGRAM_NAME is your environment variable.

  2. In my environment the use case was different I just needed to know in which region I'm running, so I used configMap instead, I create configMap per environment and include the data for this specific environment.
    For example:
    Create configMap:

    ---
    apiVersion: v1
    kind: ConfigMap
    metadata:
      name: cluster-info
      namespace: kube-system
    data:
      name: amazon-web-services-us-east-1
    
    

    Use this configMap data in logspout yaml:

    env:
      - name: SYSLOG_HOSTNAME
        valueFrom:
          configMapKeyRef:
            name: cluster-info
            key: name
    

Hope this info was helpful for you :)

@mmoore0011
Copy link

@mashayev

When I try your solution I get

error calling index: cannot index slice/array with type string

This would be a great improvement for me. I'm not sure if this commit dropped off the radar because no one is available to fix the test or because people have found other ways to include individual env vars in the log messages. Does anyone following this have a working solution?

@ksmithut
Copy link

ksmithut commented Jun 7, 2019

@mmoore0011 Just ran into this from another issue here. I'm really close to having to do something like was suggested here, but it's just essentially checking each character to see if it matches the environment variable name up until the =, the uses that. I've been working on one for the better part of an hour, but Split wasn't available...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet