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

Closes #2262: Allowing users to specify arbitrary CLI arguments for Ansible-based operators #3374

Merged
merged 10 commits into from Jul 31, 2020
Merged

Conversation

VenkatRamaraju
Copy link
Contributor

Description of the change:
Added a command-line flag for ansible operators to allow users to specify arbitrary CLI arguments that are passed through ansible-runner.

Motivation for the change:
Allows users to specify arbitrary CLI arguments for ansible-based operators.
Fixes issue #2262

Checklist

If the pull request includes user-facing changes, extra documentation is required:

@openshift-ci-robot openshift-ci-robot added needs-rebase Indicates a PR cannot be merged because it has merge conflicts with HEAD. and removed needs-rebase Indicates a PR cannot be merged because it has merge conflicts with HEAD. labels Jul 9, 2020
Copy link
Member

@jmrodri jmrodri left a comment

Choose a reason for hiding this comment

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

Very good. One minor nit on the changelog. And I have a questions about the affect an empty env/cmdline will have on things, if any.

Comment on lines 3 to 4
Added a command-line flag that allows users to specify arbitrary CLI arguments for ansible-based operators
that are passed through ansible-runner.
Copy link
Member

Choose a reason for hiding this comment

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

I would specify the actual flag you added. Something along the lines of "Added the --ansible-args command-line flag that ...."

Comment on lines 92 to 98
fmt.Sprintf("%v", maxArtifacts), "--role", roleName, "--roles-path", rolePath,
"--hosts", "localhost", "-i", ident, "run", inputDirPath)
}
return exec.Command("ansible-runner", "--rotate-artifacts",
fmt.Sprintf("%v", maxArtifacts), "--role", roleName, "--roles-path", rolePath, "--hosts",
"localhost", "-i", ident, "run", inputDirPath)
fmt.Sprintf("%v", maxArtifacts), "--role", roleName, "--roles-path", rolePath,
"--hosts", "localhost", "-i", ident, "run", inputDirPath)
}
Copy link
Member

Choose a reason for hiding this comment

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

Thank you for wrapping this better.

Comment on lines 137 to 147
err = i.addFile("env/cmdline", cmdLineBytes)
if err != nil {
return err
}

Copy link
Member

Choose a reason for hiding this comment

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

What if the i.CmdLine is empty? will having an empty env/cmdline affect things?

Copy link
Contributor

@camilamacedo86 camilamacedo86 left a comment

Choose a reason for hiding this comment

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

Just a few nits, otherwise show great 👍
Good work.

@@ -212,12 +212,9 @@ spec:

## Passing Arbitrary Arguments to Ansible

Users can specify an arbitrary command-line argument to the ```operator-sdk exec-entrypoint``` and ```run --local``` subcommands for Ansible-based Operators via the ```ansible-args``` flag.
You are able to use the flag `--ansible-args` to pass an arbitrary argument to the Ansible-based Operator. With this option we can, for example, allow a playbook to run a specific part of the configuration without running the whole playbook:
Copy link
Contributor

Choose a reason for hiding this comment

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

A second thought on this ... The so called --ansible-args are actually the arguments to be passed to ansible-runner rather than ansible or ansible-playbook. This means we can pass anything below using this new flag:

usage: ansible-runner [-h] [--version] [-p PLAYBOOK | -m MODULE | -r ROLE]
                      [--debug] [--logfile LOGFILE] [-b BINARY] [-i IDENT]
                      [--rotate-artifacts ROTATE_ARTIFACTS]
                      [--artifact-dir ARTIFACT_DIR]
                      [--project-dir PROJECT_DIR] [--inventory INVENTORY] [-j]
                      [--omit-event-data] [--only-failed-event-data] [-q] [-v]
                      [--limit LIMIT] [--cmdline CMDLINE] [--hosts HOSTS]
                      [--forks FORKS] [--roles-path ROLES_PATH]
                      [--role-vars ROLE_VARS] [--role-skip-facts]
                      [-a MODULE_ARGS] [--process-isolation]
                      [--process-isolation-executable PROCESS_ISOLATION_EXECUTABLE]
                      [--process-isolation-path PROCESS_ISOLATION_PATH]
                      [--process-isolation-hide-paths [PROCESS_ISOLATION_HIDE_PATHS [PROCESS_ISOLATION_HIDE_PATHS ...]]]
                      [--process-isolation-show-paths [PROCESS_ISOLATION_SHOW_PATHS [PROCESS_ISOLATION_SHOW_PATHS ...]]]
                      [--process-isolation-ro-paths [PROCESS_ISOLATION_RO_PATHS [PROCESS_ISOLATION_RO_PATHS ...]]]
                      [--directory-isolation-base-path DIRECTORY_ISOLATION_BASE_PATH]
                      [--resource-profiling]
                      [--resource-profiling-base-cgroup RESOURCE_PROFILING_BASE_CGROUP]
                      [--resource-profiling-cpu-poll-interval RESOURCE_PROFILING_CPU_POLL_INTERVAL]
                      [--resource-profiling-memory-poll-interval RESOURCE_PROFILING_MEMORY_POLL_INTERVAL]
                      [--resource-profiling-pid-poll-interval RESOURCE_PROFILING_PID_POLL_INTERVAL]
                      [--resource-profiling-results-dir RESOURCE_PROFILING_RESULTS_DIR]
                      COMMAND private_data_dir

It would be awkward if we later want to pass more arguments to ansible if we call this flag --ansible-args. Not quite sure will be exposing the --cmdline CMDLINE someday or even we do have such use cases. However, just for avoiding confusion, would it be better to call this as --runner-args?

Copy link
Member

Choose a reason for hiding this comment

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

These are actually arguments to ansible, not runner

Copy link
Member

@fabianvf fabianvf Jul 13, 2020

Choose a reason for hiding this comment

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

They are written to the env/cmdline file and then ansible-runner passes them to the ansible command that it runs

Copy link
Contributor

Choose a reason for hiding this comment

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

Ah, didn't read the code carefully. That solves my concern. Thank you, @fabianvf .

changelog/fragments/arbitraryArgs.yaml Show resolved Hide resolved
"ansible-args",
"",
strings.Join(append(helpTextPrefix,
"Ansible args."),
Copy link
Member

Choose a reason for hiding this comment

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

Add a more complete help text here

kind: ConfigMap
metadata:
name: '{{ meta.name }}'
namespace: '{{ meta.namespace }}'
Copy link
Member

Choose a reason for hiding this comment

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

This configmap needs a data field or it will just be empty

metadata:
name: '{{ meta.name }}'
namespace: '{{ meta.namespace }}'
- name: Get the decrypted message variable
Copy link
Member

Choose a reason for hiding this comment

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

This should happen before the configmap is created

include_vars:
file: /opt/ansible/vars.yml
name: the_secret
- data:
Copy link
Member

Choose a reason for hiding this comment

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

This belongs on the configmap, it's not a valid task by itself

@openshift-ci-robot openshift-ci-robot added needs-rebase Indicates a PR cannot be merged because it has merge conflicts with HEAD. and removed needs-rebase Indicates a PR cannot be merged because it has merge conflicts with HEAD. labels Jul 14, 2020
@openshift-ci-robot openshift-ci-robot added needs-rebase Indicates a PR cannot be merged because it has merge conflicts with HEAD. and removed needs-rebase Indicates a PR cannot be merged because it has merge conflicts with HEAD. labels Jul 15, 2020
@openshift-ci-robot openshift-ci-robot added needs-rebase Indicates a PR cannot be merged because it has merge conflicts with HEAD. and removed needs-rebase Indicates a PR cannot be merged because it has merge conflicts with HEAD. labels Jul 21, 2020
Copy link
Member

@fabianvf fabianvf left a comment

Choose a reason for hiding this comment

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

/lgtm

@openshift-ci-robot openshift-ci-robot added the lgtm Indicates that a PR is ready to be merged. label Jul 28, 2020
if err != nil {
return err
}
}
Copy link
Contributor

Choose a reason for hiding this comment

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

Regards the addFile:

  • IMO I do not think that we need to add this func. See that we can replace that by:
	err := ioutil.WriteFile(filepath.Join(i.Path, "path"), content, 0644)
	if err != nil {
		log.Error(err, "Unable to write file", "Path", fullPath)
	}
  • Also, the log.Error(err, "Unable to write file", "Path", fullPath) we can replace by:
log.Errorf("Unable to write file (%s) : (%s)", fullPath, err)

Copy link
Contributor

Choose a reason for hiding this comment

The 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.

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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.

Copy link
Contributor

Choose a reason for hiding this comment

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

Hi @VenkatRamaraju,

Thank you for your reply. Let's see the addFile:

// addFile adds a file to the given relative path within the input directory.
func (i *InputDir) addFile(path string, content []byte) error {
fullPath := filepath.Join(i.Path, path)
err := ioutil.WriteFile(fullPath, content, 0644)
if err != nil {
log.Error(err, "Unable to write file", "Path", fullPath)
}
return err
}

Is not it the same of call the WriteFile?. So, why we need that?

        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 ioutil.WriteFile directly.

And then, see that is better we use the FileMode const;

Feel free to ping me as well for we spoke about it.

Copy link
Member

Choose a reason for hiding this comment

The 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 addFile function would make sense to do separately, I don't think it's worth blocking the PR to fix it here

Copy link
Contributor

Choose a reason for hiding this comment

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

Yep .. I just saw that.

Copy link
Contributor

@camilamacedo86 camilamacedo86 left a comment

Choose a reason for hiding this comment

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

a few nits for a better maintenance ability.

Changed runner args to just args

Testing

Changelog file

Changes based on review

Error checking

Changes

docs: arbitrary args + ansible-vault support

Changes from review

Changes

Changes

Changes
@openshift-ci-robot openshift-ci-robot removed the lgtm Indicates that a PR is ready to be merged. label Jul 30, 2020
Comment on lines 27 to 30
<<<<<<< HEAD
root := cobra.Command{
Use: "ansible-operator",
=======
Copy link
Contributor

Choose a reason for hiding this comment

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

rebase issue :)


kind: "addition"

breaking: false
Copy link
Contributor

Choose a reason for hiding this comment

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

nit space at the end.

Copy link
Member

@fabianvf fabianvf left a comment

Choose a reason for hiding this comment

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

/lgtm

@openshift-ci-robot openshift-ci-robot added the lgtm Indicates that a PR is ready to be merged. label Jul 31, 2020
Copy link
Contributor

@camilamacedo86 camilamacedo86 left a comment

Choose a reason for hiding this comment

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

/lgtm
Shows fine 👍

@fabianvf fabianvf merged commit bd9057c into operator-framework:master Jul 31, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
lgtm Indicates that a PR is ready to be merged.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

6 participants