Skip to content

Support using files as values for template parameters #10687

Closed

Description

@elyscape

It would be nice if oc process had some way to specify that a parameter should be filled in using the contents of file. As an example, curl provides this functionality. More details on that at the end of the post.

At present, in order to include the contents of a file as a parameter when processing a template, you need to use the shell to load the file into the command. For example, if we have a directory with the files in this gist, we can substitute values in like so:

scapeless:template elyscape$ oc process -f template.yml -o yaml "VALUE=$(cat value.txt)" "BLOCKVALUE=$(cat value.txt)"
apiVersion: v1
items:
- apiVersion: v1
  data:
    blockvalue: |
      value
    value: value
  kind: ConfigMap
  metadata:
    creationTimestamp: null
    name: amap
kind: List
metadata: {}

And also like so:

scapeless:template elyscape$ oc process -f template.yml -o yaml -v "VALUE=$(cat value.txt),BLOCKVALUE=$(cat value.txt)"
apiVersion: v1
items:
- apiVersion: v1
  data:
    blockvalue: |
      value
    value: value
  kind: ConfigMap
  metadata:
    creationTimestamp: null
    name: amap
kind: List
metadata: {}

This also works if we have multiple lines:

scapeless:template elyscape$ oc process -f template.yml -o yaml "VALUE=$(cat multiline.txt)" "BLOCKVALUE=$(cat multiline.txt)"
apiVersion: v1
items:
- apiVersion: v1
  data:
    blockvalue: |
      line 1
      line 2
      line 3
    value: |-
      line 1
      line 2
      line 3
  kind: ConfigMap
  metadata:
    creationTimestamp: null
    name: amap
kind: List
metadata: {}

Unless you use the -v flag, in which case the first parameter gets the first line and anything after it gets ignored:

scapeless:template elyscape$ oc process -f template.yml -o yaml -v "VALUE=$(cat multiline.txt),BLOCKVALUE=$(cat multiline.txt)"
apiVersion: v1
items:
- apiVersion: v1
  data:
    blockvalue: |2+

    value: line 1
  kind: ConfigMap
  metadata:
    creationTimestamp: null
    name: amap
kind: List
metadata: {}
scapeless:template elyscape$ oc process -f template.yml -o yaml -v "BLOCKVALUE=$(cat multiline.txt),VALUE=$(cat multiline.txt)"
apiVersion: v1
items:
- apiVersion: v1
  data:
    blockvalue: |
      line 1
    value: ""
  kind: ConfigMap
  metadata:
    creationTimestamp: null
    name: amap
kind: List
metadata: {}

This is probably a bug in the -v/--value= processing code, as using multiple -v flags gets it to at least load the first line for each parameter:

scapeless:template elyscape$ oc process -f template.yml -o yaml -v "VALUE=$(cat multiline.txt)" -v "BLOCKVALUE=$(cat multiline.txt)"
apiVersion: v1
items:
- apiVersion: v1
  data:
    blockvalue: |
      line 1
    value: line 1
  kind: ConfigMap
  metadata:
    creationTimestamp: null
    name: amap
kind: List
metadata: {}

In any event, it would be nice to be able to do something like with curl (see below), and issue a command like this:

oc process -f template.yml -o yaml --value-from-file [email protected] --value-from-file [email protected]

And have it read in the files specified. It might also be nice to be able to specify a YAML/JSON file on the command line that contained all the parameters. Example mockup:

scapeless:template elyscape$ cat values.yml
VALUE: value
BLOCKVALUE: |
  line 1
  line 2
  line 3
scapeless:template elyscape$ oc process -f template.yml -o yaml --values-from-file values.yml
apiVersion: v1
items:
- apiVersion: v1
  data:
    blockvalue: |
      line 1
      line 2
      line 3
    value: value
  kind: ConfigMap
  metadata:
    creationTimestamp: null
    name: amap
kind: List
metadata: {}
Version
oc v1.2.1
kubernetes v1.2.0-36-g4a3f9c5
Additional Information

Background: I set up an nginx instance running behind ngrok for the following examples. The data files are in the gist I linked above.

The following command:

curl -XPOST http://50afc31f.ngrok.io --data-urlencode [email protected]

Results in a request that looks like this:

POST / HTTP/1.1
Host: 50afc31f.ngrok.io
User-Agent: curl/7.43.0
Accept: */*
Content-Length: 11
Content-Type: application/x-www-form-urlencoded

param=value

Using multiline-curl.txt, the following command:

curl -XPOST http://50afc31f.ngrok.io --data-urlencode [email protected]

Causes this request:

POST / HTTP/1.1
Host: 50afc31f.ngrok.io
User-Agent: curl/7.43.0
Accept: */*
Content-Length: 42
Content-Type: application/x-www-form-urlencoded

param=line%201%0D%0Aline%202%0D%0Aline%203

Using args.txt, this command:

curl -XPOST http://50afc31f.ngrok.io --data @args.txt

Produces this request:

POST / HTTP/1.1
Host: 50afc31f.ngrok.io
User-Agent: curl/7.43.0
Accept: */*
Content-Length: 62
Content-Type: application/x-www-form-urlencoded
X-Forwarded-For: 12.2.178.130

field1=value1&field2=value2&field3=value3%0D%0Awith a new line

Activity

  1. self-assigned this
    on Aug 26, 2016
  2. assigned and unassigned on Sep 7, 2016
  3. mmilata commented on Sep 15, 2016

    @mmilata
    Contributor

    oc new-app performs template instantiation as well so it should also support this feature.

    Is it reasonable to not add new command-line flags but instead extend the syntax to support VARIABLE@file/with/value.txt in addition to VARIABLE=value? There shouldn't be compatibility issues because the former currently results in an error.

  4. bparees commented on Sep 15, 2016

    @bparees
    Contributor

    everywhere else in openshift we use explicit flags to indicate files, i'd rather not introduce the @ syntax specifically here.

  5. bparees commented on Sep 15, 2016

    @bparees
    Contributor

    @mmilata
    sorry, i misread what this feature is, i thought it was supplying a list of parameter key/values from a file.

    the syntax you propose for loading the value of a parameter from a file seems ok to me.

  6. mmilata commented on Sep 15, 2016

    @mmilata
    Contributor

    @bparees Supplying list of keys/values from a file is also mentioned in @elyscape's issue description although probably not a primary issue here. I can implement both if we want both, changes will be needed in similar places anyway.

  7. mmilata commented on Sep 16, 2016

    @mmilata
    Contributor

    Unless you use the -v flag, in which case the first parameter gets the first line and anything after it gets ignored:
    This is probably a bug in the -v/--value= processing code, as using multiple -v flags gets it to at least load the first line for each parameter:

    Correct, this is property (bug?) of the pflag library.

  8. mmilata commented on Nov 3, 2016

    @mmilata
    Contributor

    Closing this, as:

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

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions