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

Port build_ds_container.sh to python #7987

Merged

Conversation

rhmdnd
Copy link
Collaborator

@rhmdnd rhmdnd commented Dec 9, 2021

The build_ds_container.sh script is useful for building content and
integrating it into your kubernetes clusters. Overtime, it's grown more
and more functionality, like building content in the cluster, locally,
or pushing built container images to repositories.

This commit attempts to reorganize the script and ports it to Python,
based on recommendations from the initial authors.

The primary changes include:

  • Using python kubernetes clients for interacting with clusters
  • Quieter logging by default, while maintaining verbosity using --debug
  • Auto-generated container image tags when publishing content to
    repositories (useful for development workflows)

This patch doesn't include support for OCP deployments as it was
originally developed to build content for EKS benchmarks. That
functionality will come in another revision of this patch and should
remain backwards compatible with build_ds_container.sh.

Signed-off-by: Lance Bragstad lbragsta@redhat.com

Description:

  • Description here. Replace this text. Don't use the italics format!

Rationale:

  • Rationale here. Replace this text. Don't use the italics format!

  • Fixes # Issue number here (e.g. Updating sysctl XCCDF naming #26) or remove this line if no issue exists.

@openshift-ci openshift-ci bot added the do-not-merge/work-in-progress Used by openshift-ci bot. label Dec 9, 2021
api.create_namespace(body)
log.info('Created namespace %s', namespace)
except exceptions.ApiException as err:
if err.status == 409:
Copy link
Contributor

Choose a reason for hiding this comment

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

Just curious, does 409 always only refer to when a namespace already exists.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Yeah, that's AlreadyExists. Might be good to use a constant or comment this number.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

It should only be emitted when the namespace already exists. I added a comment to the code to make that more clear.


def build_content_locally(products, debug=False):
build_binary_path = os.path.join(REPO_PATH, 'build_product')
command = [build_binary_path] + products
Copy link
Contributor

Choose a reason for hiding this comment

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

I haven't tried the code, but I am wondering do we need space between [build_binary_path] and products

Copy link
Contributor

Choose a reason for hiding this comment

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

Ok, I see, this is an array

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Good question - it can be done either way in python. The python community issued guidance via a PEP that recommends using whitespace around operators.

https://www.python.org/dev/peps/pep-0008/#other-recommendations

@Vincent056
Copy link
Contributor

I think the codes looks good!

return True
except exceptions.ApiException as err:
return False
if err.status == 404:
Copy link
Collaborator

Choose a reason for hiding this comment

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

Hmm, can we reach this if the previous line is return False ?

Copy link
Collaborator Author

@rhmdnd rhmdnd Dec 10, 2021

Choose a reason for hiding this comment

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

Yep - good catch. I didn't finish smoothing out the wrinkles here before pushing this version.

I removed the if statement since I don't think it matters. Instead, I just logged something for the user and returned false.

This will be fixed in the next revision.


def create_image_stream():
# FIXME(lbragstad) Fill this out using an openshift client, since we're
# dealing with an openshift-specific resource.
Copy link
Collaborator

Choose a reason for hiding this comment

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

Just wondering what we might gain by adding a second client lib for this. I think as long as the below works by specifying the api_version we'd be good.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

ack

if result.returncode == 0:
log.info(f'Pushed image to {repository}:{tag}')
elif result.returncode == 125:
log.error(
Copy link
Collaborator

Choose a reason for hiding this comment

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

Responded in the slack thread with some thoughts on the repo push.. It's unclear to me how the old script inherits repo and image tags to push this to the cluster.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

@JAORMX added that functionality recently as command line arguments. In that implementation, the script relied on podman login.

I initially implemented this method with the ability to pass in a username/password and invoking the command with --creds, but afterwards I removed it to keep things simpler.

Instead, @JAORMX and I agreed that we can leave authentication to the user, prior to them running this script. In that case, I think the best we can do is just detect if authentication failed for some reason and give them guidance on how to fix it.

@rhmdnd rhmdnd force-pushed the port-build-ds-container-to-python branch 2 times, most recently from a0c378d to 806c914 Compare December 10, 2021 19:11
JAORMX
JAORMX previously requested changes Dec 14, 2021
utils/build_ds_container.py Outdated Show resolved Hide resolved
@rhmdnd rhmdnd force-pushed the port-build-ds-container-to-python branch from 806c914 to 757476f Compare January 17, 2022 22:56
@rhmdnd rhmdnd changed the title WIP: Port build_ds_container.sh to python Port build_ds_container.sh to python Jan 17, 2022
@openshift-ci openshift-ci bot removed the do-not-merge/work-in-progress Used by openshift-ci bot. label Jan 17, 2022
The build_ds_container.sh script is useful for building content and
integrating it into your kubernetes clusters. Overtime, it's grown more
and more functionality, like building content in the cluster, locally,
or pushing built container images to repositories.

This commit attempts to reorganize the script and ports it to Python,
based on recommendations from the initial authors.

The primary changes include:

 - Quieter logging by default, while maintaining verbosity using --debug
 - Auto-generated container image tags when publishing content to
   repositories (useful for development workflows)

Signed-off-by: Lance Bragstad <lbragsta@redhat.com>
Since we ported the build_ds_container.sh script to a python equivalent,
we don't need to maintain both versions and introduce potential drift.

This commit updates the bash script to print a message for users to
adopt the new script.
@rhmdnd rhmdnd force-pushed the port-build-ds-container-to-python branch from 757476f to 0186161 Compare January 18, 2022 18:12
Copy link
Contributor

@Vincent056 Vincent056 left a comment

Choose a reason for hiding this comment

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

Just tested out the python code, I am wondering if we could have a more verbose output for example when Building content for ocp4, rhcos4, in case of errors?

@rhmdnd
Copy link
Collaborator Author

rhmdnd commented Jan 18, 2022

@Vincent056 I suppressed the output by default, but it should emit the same output as the bash script if you pass --debug, but let me know if this isn't working how you expect it to.

@Vincent056
Copy link
Contributor

@Vincent056 I suppressed the output by default, but it should emit the same output as the bash script if you pass --debug, but let me know if this isn't working how you expect it to.

Sorry didn't see that earlier, it looks good!

@rhmdnd
Copy link
Collaborator Author

rhmdnd commented Jan 18, 2022

@Vincent056 I can take the opposite approach too and emit verbose logging by default, and supply an option to suppress logging using something like --quiet if you prefer.

Copy link
Collaborator

@jhrozek jhrozek left a comment

Choose a reason for hiding this comment

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

The quiet mode was a bit of a surprise to me at first (I happened to have a broken profile in tree..) but I think it's fine and we can revert the debug option to quiet later on.
/lgtm

@jhrozek jhrozek dismissed JAORMX’s stale review January 27, 2022 14:37

The requested changes were addressed

@jhrozek jhrozek merged commit 0a818b7 into ComplianceAsCode:master Jan 27, 2022
@yuumasato yuumasato added this to the 0.1.61 milestone Jan 27, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants