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

Merge inventory-generator with origin-ansible image #5043

Merged

Conversation

juanvallejo
Copy link
Contributor

@juanvallejo juanvallejo commented Aug 9, 2017

Begin adding https://github.com/juanvallejo/dynamic-inventory as a new sub-pkg under images/.
the inventory-generator is based on this proposal

Trello card for the creation of this image: https://trello.com/c/314Df06O
Trello card for integrating this image with openshift-ansible: https://trello.com/c/ARPorrvv

Goal

The goal behind having this new image is to:

  • Create a script that runs in a pod in a cluster.
  • Script collects information about cluster.
  • Script uses collected information to create inventory file for that cluster.
  • Our health checks playbook (inside the pod in that same cluster) uses that inventory file to run our checks against the cluster.

Building

From the root of the openshift/ansible directory

$ docker build --rm -t openshift/inventory-gen images/inventory-generator

Implementation

The reason why (currently) both master-config.yaml and admin.kubeconfig are needed is because we use master-config.yaml to extract most of the information about hosts that are part of the current deployment, and then use admin.kubeconfig to establish a connection with the oc binary, to retrieve any additional information about any remaining nodes through the output of $ oc get nodes -o yaml.

I am hoping to use this PR and its feedback to mature the contents of this script.

cc @rhcarvalho @brenton @sosiouxme

@juanvallejo juanvallejo force-pushed the jvallejo/add-inventory-generator-image branch 6 times, most recently from c3c7e45 to cae4164 Compare August 9, 2017 14:43
@juanvallejo juanvallejo requested review from sosiouxme, abhgupta and rhcarvalho and removed request for abhgupta August 9, 2017 18:19
@rhcarvalho
Copy link
Contributor


RUN INSTALL_PKGS="openssh-clients wget git" \
&& yum install -y --setopt=tsflags=nodocs $INSTALL_PKGS \
&& EPEL_PKGS="PyYAML ansible python2-boto" \
Copy link
Contributor

Choose a reason for hiding this comment

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

Why don't we enable EPEL first and then install all packages at once?

Copy link
Member

Choose a reason for hiding this comment

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

EPEL packages sometimes shadow official packages, which we would usually prefer. There are two basic ways to prevent this - this is one - the other is to define the EPEL repo but leave it inactive and only enable it for the yum transactions needed. Either way you end up separating out the EPEL dependencies.

The other reason to do this is because it identifies which packages are "gaps" in the official packages. In the downstream these tend to require some different treatment, either shipping packages in another channel, getting them via RHSCL, or making them unnecessary somehow. And if things change and the packages are no longer needed... it becomes obvious you can get rid of EPEL.

ENV APP_ROOT=/opt/app-root \
HOME=/opt/app-root/src \
WORK_DIR=/opt/app-root \
USER_UID=1001
Copy link
Contributor

Choose a reason for hiding this comment

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

Bad indentation / alignment.

chown ${USER_UID}:0 ${HOME}
chmod ug+rwx -R ${HOME}

# runtime user will need to be able to self-insert in /etc/passwd
Copy link
Contributor

Choose a reason for hiding this comment

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

Why does the user needs to edit /etc/passwd?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ah, will word that comment a bit better. This comment justifies the need to make /etc/passwd writable, in order to tie a username to the given uid. This is required for ssh to work from within the container

chmod g+rw /opt/app-root/src

# ensure that the ansible content is accessible
chmod -R g+r ${WORK_DIR} /bin
Copy link
Contributor

Choose a reason for hiding this comment

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

Why do we change the permissions of /bin too?

find ${WORK_DIR} -type d -exec chmod g+x {} +

# This will redirect us to the latest stable release.
redirect=$(curl -k https://github.com/openshift/origin/releases/latest)
Copy link
Contributor

Choose a reason for hiding this comment

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

Why do we use of -k / --insecure?

Copy link
Member

Choose a reason for hiding this comment

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

Agreed, not needed

/usr/local/bin/generate ${OUTPUT_INVENTORY}

if [[ -z "${PLAYBOOK}" ]]; then
echo
Copy link
Contributor

Choose a reason for hiding this comment

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

Why this?

fi

# run ansible playbook using generated inventory
ansible-playbook -vvv -i ${OUTPUT_INVENTORY} ${PLAYBOOK} --become --become-user root
Copy link
Contributor

Choose a reason for hiding this comment

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

Double space before --become; missing new line.

Why do we default to -vvv (and hard code it)?

Why do we set --become-user root? It is the default.

Why do we need --become?

@@ -0,0 +1,348 @@
#!/bin/env python
Copy link
Contributor

Choose a reason for hiding this comment

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

Note: the "traditional" path/shebang is #!/usr/bin/env python, though this should also work on Fedora/RHEL/CentOS.

@@ -0,0 +1,17 @@
#!/bin/bash -e
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggestion: set shell options in the script, not in the shebang -- if the script is executed like /path/to/shell path/to/script, the -e flag is suddenly gone.

Also, I recommend:

set -o errexit
set -o nounset
set -o pipefail

This is what we do most often (see shell scripts in Origin), each option for a good reason.

# This file serves as the main entrypoint to the inventory-generator image.
#
# For more information see the documentation:
# https://github.com/juanvallejo/dynamic-inventory/blob/master/README.md
Copy link
Contributor

Choose a reason for hiding this comment

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

Update URL?

@rhcarvalho
Copy link
Contributor

I did a first pass, but did not review everything.

Is it worth having yet another image, what about simply adding a new script to the existing openshift-ansible image?

@juanvallejo juanvallejo force-pushed the jvallejo/add-inventory-generator-image branch from cae4164 to 5cee9db Compare August 10, 2017 13:55
@juanvallejo
Copy link
Contributor Author

@rhcarvalho

Is it worth having yet another image, what about simply adding a new script to the existing openshift-ansible image?

Was not sure to what extent we wanted to couple this with our existing image. But if it makes more sense to have this functionality as part of it, I can go ahead and update the PR.

cc @sosiouxme @brenton

@sosiouxme
Copy link
Member

One image sounds good to me.

@juanvallejo juanvallejo force-pushed the jvallejo/add-inventory-generator-image branch from 00345ba to 009e570 Compare August 15, 2017 16:31
@juanvallejo
Copy link
Contributor Author

@rhcarvalho @sosiouxme still testing, but went ahead and merged inventory-generator with our image. Its readme file has been updated (README_INVENTORY_GENERATOR.md).

@juanvallejo juanvallejo force-pushed the jvallejo/add-inventory-generator-image branch 2 times, most recently from 45c37d0 to 70e2725 Compare August 15, 2017 17:13
@juanvallejo juanvallejo changed the title [WIP] add inventory-generator under new sub pkg [WIP] merge inventory-generator with origin-ansible image Aug 15, 2017
Copy link
Member

@sosiouxme sosiouxme left a comment

Choose a reason for hiding this comment

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

So I know this isn't supposed to be full-featured, but it needs a fair amount more before it will even handle my regular AWS test clusters passably...

(Sorry for the tardiness of my input here)

openshift_hosted_logging_deploy = True

m = Host("masters")
m.host_alias(common_host_alias)
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 there is no alias? My hosts don't have aliases.

continue

n = Host("nodes")
n.host_alias(common_host_alias)
Copy link
Member

Choose a reason for hiding this comment

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

Likewise; aliases should be optional and I don't think you're going to want to use the same one for all the nodes.

exit(1)

# set inventory values based on user configuration
common_host_alias = user_config.get('common_host_alias', 'openshiftdevel')
Copy link
Member

Choose a reason for hiding this comment

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

all hosts default to the same alias? that seems unlikely to work if there is more than one host.

m.host_alias(common_host_alias)
m.address(master_config["masterIP"])
m.public_host_name(master_public_url)
host_groups["masters"] = HostGroup([m])
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 there are multiple masters?


# connect to remote host using `oc login...` and extract all possible node information
oc = OpenShiftClient()
oc.login(master_public_url, openshift_cluster_user, openshift_cluster_pass)
Copy link
Member

Choose a reason for hiding this comment

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

I'd really like to be able to just use an existing kubeconfig to connect.

This only handles the use case of a user with a login and password. It's can be a pain to set one of those up and it's likely they want it to be a service account with a revocable auth token. Plus it's possible the master_public_url isn't what they want to use, nor insecure TLS.

Simplest would probably be to use a kubeconfig. It's easy for the user to create one with oc login. We don't need to recreate oc login in python.

try:
config_file_obj = open(USER_CONFIG, 'r')
raw_config_file = config_file_obj.read()
user_config = yaml.load(raw_config_file)
Copy link
Member

Choose a reason for hiding this comment

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

if the file is empty then this returns None and all the access below fails. Should check that the result is a dict or just make it an empty one.

-v /tmp/aws/master-config.yaml:/opt/app-root/src/master-config.yaml:Z \
-e OPTS="-v --become --become-user root" \
-e PLAYBOOK_FILE=playbooks/byo/config.yml \
-e GENERATE_INVENTORY=true \
Copy link
Member

Choose a reason for hiding this comment

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

need to mention where the inventory gets generated, and they need to specify where Ansible should look for it right?


INVY = 'generated_hosts'
if len(sys.argv) > 1:
INVY = sys.argv[1]
Copy link
Member

Choose a reason for hiding this comment

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

should be able to specify full path, not just the name.

Also would like to be able to print to stdout.


# open new inventory file for writing
try:
inv_file_obj = open(HOME + '/' + INVY, 'w+')
Copy link
Member

Choose a reason for hiding this comment

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

should be configurable

inv_file_obj.write("ansible_become={}\n".format(ansible_become))
inv_file_obj.write("openshift_uninstall_images={}\n".format(str(openshift_uninstall_images)))
inv_file_obj.write("openshift_deployment_type={}\n".format(openshift_deployment_type))
inv_file_obj.write("openshift_install_examples={}\n".format(str(openshift_install_examples)))
Copy link
Member

Choose a reason for hiding this comment

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

What about all the other stuff from my config file?

openshift_release: 3.6
openshift_image_tag: v3.6.0
openshift_hosted_logging_deploy: True
openshift_logging_image_version: v3.6.0
openshift_disable_check: disk_availability,docker_storage,memory_availability

etc.... i think we want to be able to generate as close to the original file as possible.

@juanvallejo juanvallejo force-pushed the jvallejo/add-inventory-generator-image branch from 11f10f3 to 81a7439 Compare August 16, 2017 20:34
@juanvallejo
Copy link
Contributor Author

@sosiouxme thanks for the feedback; review comments addressed.
Will only support one master in this iteration. In a future update it would
be nice to add support to multiple masters (and host aliases) through the
user config file.

@juanvallejo juanvallejo force-pushed the jvallejo/add-inventory-generator-image branch 3 times, most recently from 40ac162 to a821120 Compare August 18, 2017 14:00
@juanvallejo
Copy link
Contributor Author

[test]

# runtime user will need to be able to self-insert in /etc/passwd
chmod g+rw /etc/passwd

# make required config dirs writable
chown ${USER_UID}:0 ${HOME}/.kube/config
chmod a+rw ${HOME}/.kube/config
Copy link
Contributor

Choose a reason for hiding this comment

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

I suspect this file is bind-mounted, and thus this would have an impact on permissions outside of the container...

- specifies where to look for the bind-mounted `master-config.yaml` file in the container
- if omitted or a `null` value is given, its value is defaulted to `/opt/app-root/src/master-config.yaml`

- `admin_kubeconfig_path` (required):
Copy link
Contributor

Choose a reason for hiding this comment

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

How come it is both required and has a default value?!

- username of account capable of listing nodes in a cluster
- used for querying the cluster using `oc` to retrieve additional node information.

- `master_config_path` (required):
Copy link
Contributor

Choose a reason for hiding this comment

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

How come it is both required and has a default value?!

# runtime user will need to be able to self-insert in /etc/passwd
chmod g+rw /etc/passwd

# make required config dirs writable
chown ${USER_UID}:0 ${HOME}/.kube/config
Copy link
Contributor

Choose a reason for hiding this comment

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

These lines ignore admin_kubeconfig_path, and I think it will only work if admin_kubeconfig_path has its default value?


### Build

`docker build --rm -t openshift/origin-ansible -f images/installer/Dockerfile .`
Copy link
Contributor

Choose a reason for hiding this comment

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

I suspect this must be run from the repository root (to give the correct meaning for .) and thus we need to note that?

Copy link
Contributor

Choose a reason for hiding this comment

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

Use triple-backticks for making it a block. Single backtick is used for inline code snippets.

```
docker build --rm -t openshift/origin-ansible -f images/installer/Dockerfile .

```

Copy link
Contributor

Choose a reason for hiding this comment

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

Hmm, on a second thought, the build process is already described elsewhere, the inventory generation does not need to talk about building images?!

DEFAULT_MASTER_CONFIG_PATH = HOME + '/master-config.yaml'
DEFAULT_ADMIN_KUBECONFIG_PATH = HOME + '/.kube/config'

INVY_FULL_PATH = HOME + '/generated_hosts'
Copy link
Contributor

Choose a reason for hiding this comment

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

INVY? Sounds funny :-)

@@ -0,0 +1 @@
../installer/root/etc/inventory-generator-config.yaml
Copy link
Contributor

Choose a reason for hiding this comment

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

Why the symlink?

openshift_image_tag: v3.6.0
openshift_hosted_logging_deploy: null # defaults to "true" if loggingPublicURL is set in master-config.yaml
openshift_logging_image_version: v3.6.0
openshift_disable_check: disk_availability,docker_storage,memory_availability
Copy link
Contributor

Choose a reason for hiding this comment

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

Won't this suggest people to disable these checks unintentionally?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Hm, so I had this for debugging purposes and had meant to take them out :) will update

---
# meta config
master_config_path: null # defaults to /opt/app-root/src/master-config.yaml
admin_kubeconfig_path: null # defaults to /opt/app-root/src/.kube/config
Copy link
Contributor

Choose a reason for hiding this comment

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

How helpful is it to include these?

Feels like the default paths are repeated in many places.


openshift_release: 3.6
openshift_image_tag: v3.6.0
openshift_hosted_logging_deploy: null # defaults to "true" if loggingPublicURL is set in master-config.yaml
Copy link
Contributor

Choose a reason for hiding this comment

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

Is it helpful to include this?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'm thinking with this and admin_kubeconfig_path above, I could at least default them to their default values instead of null. I think having these present in the default config file gives a better glance of what can be tweaked

@juanvallejo juanvallejo force-pushed the jvallejo/add-inventory-generator-image branch from a821120 to b769288 Compare August 21, 2017 21:23
@juanvallejo juanvallejo force-pushed the jvallejo/add-inventory-generator-image branch from b411e30 to daf81fc Compare August 31, 2017 21:02
@juanvallejo
Copy link
Contributor Author

@sosiouxme thanks for the feedback, review comments addressed. Let's see how ci tests fare this time

@rhcarvalho
Copy link
Contributor

@juanvallejo please rebase and if there are no other changes we can start aos-ci-test and then tag for merge ;)

@juanvallejo juanvallejo force-pushed the jvallejo/add-inventory-generator-image branch from daf81fc to a705552 Compare September 5, 2017 13:27
@openshift-ci-robot openshift-ci-robot added the size/XL Denotes a PR that changes 500-999 lines, ignoring generated files. label Sep 5, 2017
@juanvallejo
Copy link
Contributor Author

@rhcarvalho thanks, done!

@juanvallejo juanvallejo force-pushed the jvallejo/add-inventory-generator-image branch from a705552 to 032858a Compare September 5, 2017 21:01
@juanvallejo
Copy link
Contributor Author

re[test]

@juanvallejo juanvallejo force-pushed the jvallejo/add-inventory-generator-image branch from 323c429 to 31dd1b4 Compare September 11, 2017 21:45
@juanvallejo juanvallejo force-pushed the jvallejo/add-inventory-generator-image branch from 31dd1b4 to c032e4c Compare September 12, 2017 13:41
@juanvallejo
Copy link
Contributor Author

flaked on openshift/origin#8571 re[test]

@sosiouxme
Copy link
Member

aos-ci-test

@openshift-bot
Copy link

Evaluated for openshift ansible test up to 19ea800

@sosiouxme
Copy link
Member

I found a few things that still needed addressing, so I did that... works for me now.

@openshift-bot
Copy link

continuous-integration/openshift-jenkins/test FAILURE (https://ci.openshift.redhat.com/jenkins/job/test_pull_request_openshift_ansible/659/) (Base Commit: 665c5c2) (PR Branch Commit: 19ea800)

@openshift-bot
Copy link

success: "aos-ci-jenkins/OS_3.6_NOT_containerized, aos-ci-jenkins/OS_3.6_NOT_containerized_e2e_tests" for c032e4c (logs)

@openshift-bot
Copy link

success: "aos-ci-jenkins/OS_3.6_containerized, aos-ci-jenkins/OS_3.6_containerized_e2e_tests" for c032e4c (logs)

@sosiouxme
Copy link
Member

... why are the test statuses still yellow?

@sdodson
Copy link
Member

sdodson commented Sep 14, 2017

Looks like they were run against an old commit, the commit is recorded at the time the aos-ci-test job is queued rather than at the start of the job. So it ran against c032e4c which has since been updated?

@sdodson
Copy link
Member

sdodson commented Sep 14, 2017

aos-ci-test

@sdodson
Copy link
Member

sdodson commented Sep 14, 2017

If that isn't complete tomorrow i'm fine to merge this manually based on the results of https://ci.openshift.redhat.com/jenkins/job/test_pull_request_openshift_ansible/659/

@sdodson sdodson merged commit 7df0b64 into openshift:master Sep 14, 2017
@sdodson
Copy link
Member

sdodson commented Sep 14, 2017

sigh I just noticed no one had added a merge tag, let me know if this should be reverted.

@sosiouxme
Copy link
Member

@sdodson only reason there was no merge tag was because waiting for aos-ci-test. This is quite low risk so I wouldn't worry about reverting.

@juanvallejo juanvallejo deleted the jvallejo/add-inventory-generator-image branch September 14, 2017 18:52
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
size/XL Denotes a PR that changes 500-999 lines, ignoring generated files.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants