Skip to content
This repository has been archived by the owner on Nov 15, 2022. It is now read-only.

Add support for environment variable substitution #300

Merged
merged 2 commits into from
Oct 5, 2017

Conversation

kadel
Copy link
Member

@kadel kadel commented Oct 3, 2017

You can use environment variables in you Kedge files. Expression is [[ variable_name ]].

fixes #224

TODO:

  • move variable substitution logic from pkg/cmd/util.go to its own package
  • documentation

move variable substitution logic from pkg/cmd/util.go to its own package

I'm keeping this as it is, for now. We need clean up whole pkg/cmd package. Created #301

@kadel kadel changed the title Add support for environment variable substitution [WIP] Add support for environment variable substitution Oct 3, 2017
@kadel kadel force-pushed the variables branch 2 times, most recently from 9633160 to 7d9d267 Compare October 3, 2017 13:18
@kadel kadel changed the title [WIP] Add support for environment variable substitution Add support for environment variable substitution Oct 4, 2017
Copy link
Collaborator

@concaf concaf left a comment

Choose a reason for hiding this comment

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

This also works for YAML fields and not only values, is that the expected behavior?
e.g.

name: httpd
containers:
- [[ KFIELD ]]: centos/httpd
services:
- name: httpd
  type: NodePort
  ports:
  - port: 8080
    targetPort: 80

and this works - KFIELD=image kedge generate -f docs/examples/simplest/httpd.yaml

Copy link
Collaborator

@concaf concaf left a comment

Choose a reason for hiding this comment

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

Works perfectly for me, code LGTM, feel free to merge 🎉

pkg/cmd/util.go Outdated
}

// replaceWithEnv is used with regexp.ReplaceAllFunc to replace variable
// with value from envrironment
Copy link
Collaborator

Choose a reason for hiding this comment

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

nit: environment

Copy link
Member Author

Choose a reason for hiding this comment

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

typo fixed, thx

@kadel
Copy link
Member Author

kadel commented Oct 4, 2017

This also works for YAML fields and not only values, is that the expected behavior?

Yep, that is a side effect of the way how it is implemented.

I wouldn't suggest doing it anywhere but it is possible.

kadel added 2 commits October 4, 2017 14:24
You can use environment variables in you Kedge files. Expression is [[ variable_name ]].
@surajssd
Copy link
Member

surajssd commented Oct 4, 2017

@kadel @containscafeine

Am I doing anything wrong?

$ kedge version
0.2.0 (f9c6b37)

$ cat file.yaml 
name: httpd
containers:
- image: [[ imagename ]]

$ imagename=foo
$ echo $imagename
foo

$ kedge generate -f .
failed to replace variables: undefined variable(s): imagename

@surajssd
Copy link
Member

surajssd commented Oct 4, 2017

This worked alright:

$ export imagename=foo
$ echo $imagename
foo

$ kedge generate -f .
---
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  creationTimestamp: null
  labels:
    app: httpd
  name: httpd
spec:
  strategy: {}
  template:
    metadata:
      creationTimestamp: null
      labels:
        app: httpd
      name: httpd
    spec:
      containers:
      - image: foo
        name: httpd
        resources: {}
status: {}

@surajssd
Copy link
Member

surajssd commented Oct 4, 2017

$ imagename=foo kedge generate -f .
---
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  creationTimestamp: null
  labels:
    app: httpd
  name: httpd
spec:
...

This also worked fine!

@kadel
Copy link
Member Author

kadel commented Oct 4, 2017

@kadel @containscafeine

Am I doing anything wrong?

$ kedge version
0.2.0 (f9c6b37)

$ cat file.yaml
name: httpd
containers:

  • image: [[ imagename ]]

$ imagename=foo
$ echo $imagename
foo

$ kedge generate -f .
failed to replace variables: undefined variable(s): imagename

This is OK it shouldn't work. This is how env variables work in bash.

If you don't export it the variable is not passed to any child processes of given shell.

imagename=foo ./kedge

Above works because In this case variable is directly passed to kedge process.

export imagename=foo
 ./kedge

Above is also working because export is used and that means that any child process of given shell will also see this variable

But following doesn't work as kedge process is a new child process of given shell, and it doesn't receive unexported variables.

imagename=foo 
./kedge

it works with echo, because echo is build in bash function.

Copy link
Member

@surajssd surajssd left a comment

Choose a reason for hiding this comment

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

@kadel thanks for that explanation!

This LGTM!

@surajssd surajssd merged commit b926376 into kedgeproject:master Oct 5, 2017
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

version numbers / image tags in kedge YAML
3 participants