-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Closes #2262: Allowing users to specify arbitrary CLI arguments for Ansible-based operators #3374
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
Changes from all commits
71814cc
6cdddd7
af1eab2
e5f1205
0da45dd
c61ddf7
6b10eac
05e3a44
8303ef9
e35d950
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| entries: | ||
| - description: > | ||
| Added the "--ansible-args" command-line flag that allows users to specify arbitrary | ||
| CLI arguments for ansible-based operators that are passed through ansible-runner. | ||
| For example, passing --ansible-vault as an arbitrary argument allows the user to store | ||
| sensitive data in encrypted files. | ||
|
|
||
| kind: "addition" | ||
|
|
||
| breaking: false | ||
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -36,6 +36,7 @@ type InputDir struct { | |||||||||||||||||||||
| Parameters map[string]interface{} | ||||||||||||||||||||||
| EnvVars map[string]string | ||||||||||||||||||||||
| Settings map[string]string | ||||||||||||||||||||||
| CmdLine string | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| // makeDirs creates the required directory structure. | ||||||||||||||||||||||
|
|
@@ -131,6 +132,19 @@ func (i *InputDir) Write() error { | |||||||||||||||||||||
| return err | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| // Trimming off the first and last characters if the command is wrapped by single quotations | ||||||||||||||||||||||
| if strings.HasPrefix(i.CmdLine, string("'")) && i.CmdLine[0] == i.CmdLine[len(i.CmdLine)-1] { | ||||||||||||||||||||||
| i.CmdLine = i.CmdLine[1 : len(i.CmdLine)-1] | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
|
camilamacedo86 marked this conversation as resolved.
|
||||||||||||||||||||||
| cmdLineBytes := []byte(i.CmdLine) | ||||||||||||||||||||||
| if len(cmdLineBytes) > 0 { | ||||||||||||||||||||||
| err = i.addFile("env/cmdline", cmdLineBytes) | ||||||||||||||||||||||
| if err != nil { | ||||||||||||||||||||||
| return err | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Regards the
err := ioutil.WriteFile(filepath.Join(i.Path, "path"), content, 0644)
if err != nil {
log.Error(err, "Unable to write file", "Path", fullPath)
}
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The reason here is because the addFile just do the above code so has no need for an extra func and then, it makes easier understand that it is about to write a file in the disk.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. All the parameters above (Parameters, EnvVars, Settings) all use the addFile while having their bytes written to disk, so I thought we could use that function instead of repeating the code.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hi @VenkatRamaraju, Thank you for your reply. Let's see the addFile: operator-sdk/internal/ansible/runner/internal/inputdir/inputdir.go Lines 55 to 63 in 8303ef9
Is not it the same of call the err := ioutil.WriteFile(filepath.Join(i.Path, "path"), content, 0644)
if err != nil {
log.Errorf("Unable to write file (%s) : (%s)", fullPath, err)
}So my suggestion is to replace the addFiles calls to use And then, see that is better we use the FileMode const;
Feel free to ping me as well for we spoke about it.
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @camilamacedo86 since this is just reusing the function the rest of the code is already using I think dropping the
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yep .. I just saw that. |
||||||||||||||||||||||
|
|
||||||||||||||||||||||
| // ANSIBLE_INVENTORY takes precedence over our generated hosts file | ||||||||||||||||||||||
| // so if the envvar is set we don't bother making it, we just copy | ||||||||||||||||||||||
| // the inventory into our runner directory | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| apiVersion: apiextensions.k8s.io/v1 | ||
| kind: CustomResourceDefinition | ||
| metadata: | ||
| name: argstests.test.example.com | ||
| spec: | ||
| group: test.example.com | ||
| names: | ||
| kind: ArgsTest | ||
| listKind: ArgsTestList | ||
| plural: argstests | ||
| singular: argstest | ||
| scope: Namespaced | ||
| versions: | ||
| - name: v1alpha1 | ||
| schema: | ||
| openAPIV3Schema: | ||
| type: object | ||
| x-kubernetes-preserve-unknown-fields: true | ||
| served: true | ||
| storage: true | ||
| subresources: | ||
| status: {} |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| --- | ||
| - name: Create the test.example.com/v1alpha1.ArgsTest | ||
| k8s: | ||
| state: present | ||
| definition: | ||
| apiVersion: test.example.com/v1alpha1 | ||
| kind: ArgsTest | ||
| metadata: | ||
| name: args-test | ||
| namespace: '{{ namespace }}' | ||
| spec: | ||
| field: value | ||
| wait: yes | ||
| wait_timeout: 300 | ||
| wait_condition: | ||
| type: Running | ||
| reason: Successful | ||
| status: "True" | ||
| register: args_test | ||
|
|
||
| - name: Assert sentinel ConfigMap has been created for Molecule Test | ||
| assert: | ||
| that: cm.data.msg == "The decrypted value is thisisatest" | ||
| vars: | ||
| cm: "{{ q('k8s', api_version='v1', kind='ConfigMap', namespace=namespace, | ||
| resource_name='args-test').0 }}" |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| --- | ||
| - hosts: localhost | ||
| gather_facts: no | ||
| collections: | ||
| - community.kubernetes | ||
| tasks: | ||
| - name: Get the decrypted message variable | ||
| include_vars: | ||
| file: /opt/ansible/vars.yml | ||
| name: the_secret | ||
| - name: Create configmap | ||
| k8s: | ||
| definition: | ||
| apiVersion: v1 | ||
| kind: ConfigMap | ||
| metadata: | ||
| name: '{{ meta.name }}' | ||
| namespace: '{{ meta.namespace }}' | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This configmap needs a |
||
| data: | ||
| msg: The decrypted value is {{the_secret.the_secret}} | ||
Uh oh!
There was an error while loading. Please reload this page.