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

Run user-provided command as part of build flow #6715

Merged

Conversation

rhcarvalho
Copy link
Contributor

This is meant to be used for running project unit tests as part of a build.

Trello card: https://trello.com/c/I1v1YeOf
Closes #6758

This PR/card won't introduce changes to oc new-build nor oc new-app nor the Web Console.
For now, the feature can be used via oc edit bc.


Pending work:

@rhcarvalho
Copy link
Contributor Author

@bparees @PI-Victor PTAL

cpuShares := r.read("/sys/fs/cgroup/cpu,cpuacct/cpu.shares")
cpuPeriod := r.read("/sys/fs/cgroup/cpu,cpuacct/cpu.cfs_period_us")
cpuQuota := r.read("/sys/fs/cgroup/cpu,cpuacct/cpu.cfs_quota_us")
memory := r.read("/sys/fs/cgroup/memory/memory.limit_in_bytes")
Copy link
Contributor Author

Choose a reason for hiding this comment

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

@bparees this is the part you will need in the card Enforce CGroup constraints on build containers.

Worth noting that we need better error handling / implementation here. E.g.:

  • the memory limit read from /sys/fs/cgroup might overflow int64. The Docker client uses int64, so we can't really send anything > 2^63-1.
  • these values might be incorrectly set or not available with older versions of the Docker daemon. edit: we require Docker 1.8.2, which will place the files as expected.

@rhcarvalho rhcarvalho force-pushed the build-post-commit-pre-push-usr-cmd branch 2 times, most recently from 199a073 to 027404c Compare January 19, 2016 16:07
glog.Flush()
}
return nil
}

func (s *S2IBuilder) runPostCommitHook(imageID string) error {
glog.Infof("Running post commit hook with image %s ...", imageID)
Copy link
Contributor

Choose a reason for hiding this comment

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

also log the command being run.

Copy link
Contributor

Choose a reason for hiding this comment

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

also not sure the point of this function. whoever is calling it should just call runScriptInContainer directly. (and then it should do the glog.Info)

@@ -204,19 +215,42 @@ func (s *S2IBuilder) Build() error {
return err
}

// TODO: why in docker builds we defer removing the image, while here we don't?!
Copy link
Contributor Author

Choose a reason for hiding this comment

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

@bparees @mfojtik may I ask you why we don't defer removing the built image in Docker builds, while we do it in S2I?

Feature or bug?

Copy link
Contributor

Choose a reason for hiding this comment

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

probably a bug, I don't see any reason to keep the image cached on the node locally when the image is pushed to remote registry (other than a performance issue when running all-in-one ;-) @csrwng any ideas?

Copy link
Contributor

Choose a reason for hiding this comment

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

See my comment on the pull to remove. We stopped removing the image on purpose.

@rhcarvalho rhcarvalho force-pushed the build-post-commit-pre-push-usr-cmd branch from a8b0fc9 to 4d0798c Compare January 22, 2016 11:10
@rhcarvalho
Copy link
Contributor Author

I'm working with multiple commits now, as it makes my life easier.
We can squash it down to one when we're done with reviewing.

@rhcarvalho rhcarvalho force-pushed the build-post-commit-pre-push-usr-cmd branch from 4d0798c to 151ad0f Compare January 22, 2016 11:38
@@ -59,6 +59,10 @@ type BuildSpec struct {
// Compute resource requirements to execute the build
Resources kapi.ResourceRequirements

// PostCommit is a build hook executed after the build output image is
// committed, before it is pushed to a registry.
PostCommit BuildPostCommitSpec
Copy link
Contributor

Choose a reason for hiding this comment

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

Why is this not just post like deployments? Will we ever add postPush? What other possible posts are there.

Copy link
Contributor

Choose a reason for hiding this comment

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

strictly naming wise as i remember it's trying to outline more the fact that it takes place between the commit and before the push.

Copy link
Contributor

Choose a reason for hiding this comment

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

I would recommend we just use Post, and only have one of them ever. The
alignment with deployments matters to me.

On Fri, Jan 22, 2016 at 11:14 AM, Ionut Palade notifications@github.com
wrote:

In pkg/build/api/types.go
#6715 (comment):

@@ -59,6 +59,10 @@ type BuildSpec struct {
// Compute resource requirements to execute the build
Resources kapi.ResourceRequirements

  • // PostCommit is a build hook executed after the build output image is
  • // committed, before it is pushed to a registry.
  • PostCommit BuildPostCommitSpec

strictly naming wise as i remember it's trying to outline more the fact
that it takes place between the commit and before the push.


Reply to this email directly or view it on GitHub
https://github.com/openshift/origin/pull/6715/files#r50555320.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It is postCommit because not long ago we we were about to add post, which meant postPush and would trigger downstream builds. And longer ago, we were about to add "post build hooks" which were also semantically after push. Since the "push" part of the build can fail, I see value in being able to have post push actions at some point.

Copy link
Contributor

Choose a reason for hiding this comment

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

bump for interest.

Copy link
Contributor

Choose a reason for hiding this comment

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

I don't really see that being likely (adding more hooks), and if they are, they wouldn't be the 80% case anyway. However, this is fine for now.

@openshift-bot openshift-bot added the needs-rebase Indicates a PR cannot be merged because it has merge conflicts with HEAD. label Jan 22, 2016
@rhcarvalho rhcarvalho force-pushed the build-post-commit-pre-push-usr-cmd branch 2 times, most recently from 6f98eb4 to ac08605 Compare January 25, 2016 13:25
@openshift-bot openshift-bot removed the needs-rebase Indicates a PR cannot be merged because it has merge conflicts with HEAD. label Jan 25, 2016
func runPostCommitHook(dockerClient DockerClient, build *api.Build, strategyName, imageID string) error {
glog.Infof("Running post commit hook with image %s ...", imageID)
containerName := fmt.Sprintf("openshift_%s-build_%s_%s_post-commit_%08x", strategyName, build.Name, build.Namespace, rand.Uint32())
return runScriptInContainer(dockerClient, build.Spec.PostCommit.RunBash, imageID, containerName)
Copy link
Contributor Author

Choose a reason for hiding this comment

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

@bparees @mfojtik @smarterclayton please help me sanity checking here.

Is the only reason not to run the post commit script as a POD the fact that we need to reference a specific image that only exists in the node where it has been built?

This is one more difference between Build Post Commit and Deployment Lifecycle Hooks.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

According to a conversation with @bparees, there's also the problem that starting a pod would consume more of the users quota.

@rhcarvalho rhcarvalho force-pushed the build-post-commit-pre-push-usr-cmd branch from ac08605 to 3460048 Compare January 25, 2016 15:12
@smarterclayton
Copy link
Contributor

EventHistory is cleared when 1000 events have been written to etcd since
the last time you did a list - you can't ask for a change older than 1000
etcd indices.

On Thu, Feb 11, 2016 at 3:08 PM, Rodolfo Carvalho notifications@github.com
wrote:

@smarterclayton https://github.com/smarterclayton @bparees
https://github.com/bparees even after adding the retry logic (
rhcarvalho/origin@dbdb6a7
rhcarvalho@dbdb6a7),
the test still fails in Jenkins because of a timeout: we wait at most 10s
for a build to start.

https://ci.openshift.redhat.com/jenkins/job/test_pull_requests_origin/10671/console

I0211 12:25:59.513781 11193 rest.go:57] ---> event: watch.Event{Type:"ERROR", Object:(*unversioned.Status)(0xc20f87f480)}
I0211 12:25:59.513799 11193 rest.go:75] ---> the watcher expired, trying again with a new one
I0211 12:25:59.513805 11193 rest.go:35] ---> watcherLoop #31158
I0211 12:25:59.513824 11193 rest.go:46] ---> eventLoop #1
I0211 12:25:59.513835 11193 rest.go:84] ---> timed out waiting for build "busybox-1"

I cannot reproduce this locally, and would like to get some ideas on how
to proceed.
Simply increasing the timeout might remedy the situation for now, but it
is not a definitive solution.

I 'd like to know what causes the EventHistory to be cleared / watcher to
be expired so that I could reproduce it locally and propose a better fix...


Reply to this email directly or view it on GitHub
#6715 (comment).

@rhcarvalho
Copy link
Contributor Author

@smarterclayton is the etcd instance in Jenkins shared? Why would it get 1000 events in a "short" time frame?

@rhcarvalho rhcarvalho force-pushed the build-post-commit-pre-push-usr-cmd branch from 9efce2d to 0fefe49 Compare February 11, 2016 20:18
@rhcarvalho
Copy link
Contributor Author

I'm now forcing a timeout of 5 min waiting for the build, let's see what happens in Jenkins...

@smarterclayton
Copy link
Contributor

Events, controllers out of control, bad loop somewhere, etc.

On Thu, Feb 11, 2016 at 3:16 PM, Rodolfo Carvalho notifications@github.com
wrote:

@smarterclayton https://github.com/smarterclayton is the etcd instance
in Jenkins shared? Why would it get 1000 events in a "short" time frame?


Reply to this email directly or view it on GitHub
#6715 (comment).

@rhcarvalho rhcarvalho force-pushed the build-post-commit-pre-push-usr-cmd branch from 0fefe49 to 10c7245 Compare February 12, 2016 17:32
@rhcarvalho
Copy link
Contributor Author

@bparees PTAL

os::cmd::expect_success 'oc new-build -D "FROM busybox:1"'
os::cmd::try_until_text 'oc get istag busybox:1' 'busybox@sha256:'
os::cmd::expect_success 'oc patch bc/busybox -p '\''{"spec":{"postCommit":{"script":"echo hello $1","args":["world"],"command":null}}}'\'
os::cmd::expect_success_and_text 'oc get bc/busybox -o=jsonpath="{.spec.postCommit['\''script'\'','\''args'\'','\''command'\'']}"' '^echo hello \$1 \[world\] \[\]$'
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This test makes sure we persist the post commit hook correctly.

The line below is a test that ensures the hook is actually executed as part of the build, pending a fix to #7174.

@bparees
Copy link
Contributor

bparees commented Feb 12, 2016

@rhcarvalho squash and lgtm

@rhcarvalho
Copy link
Contributor Author

[test]

This is meant to be used for running project unit tests as part of a
build.

Changes:
- Add utility to run containers and stream logs
- Add build spec API field for post commit actions

  Includes a common-case API for running a shell script and an
  kapi.Container-like API for more sophisticated use cases.

A good deal of this was done pair programming with @PI-Victor.
@rhcarvalho rhcarvalho force-pushed the build-post-commit-pre-push-usr-cmd branch from 10c7245 to 3e5a859 Compare February 15, 2016 15:59
@openshift-bot
Copy link
Contributor

Evaluated for origin test up to 3e5a859

@rhcarvalho
Copy link
Contributor Author

@bparees squashed.

@bparees
Copy link
Contributor

bparees commented Feb 15, 2016

[merge]

@openshift-bot
Copy link
Contributor

continuous-integration/openshift-jenkins/merge SUCCESS (https://ci.openshift.redhat.com/jenkins/job/merge_pull_requests_origin/4962/) (Image: devenv-rhel7_3430)

@openshift-bot
Copy link
Contributor

Evaluated for origin merge up to 3e5a859

@openshift-bot
Copy link
Contributor

continuous-integration/openshift-jenkins/test SUCCESS (https://ci.openshift.redhat.com/jenkins/job/test_pr_origin/1146/)

openshift-bot pushed a commit that referenced this pull request Feb 15, 2016
@openshift-bot openshift-bot merged commit a722cb8 into openshift:master Feb 15, 2016
@smarterclayton
Copy link
Contributor

Also, can you guys make sure you have a blurb for me for the release notes
that describes how this works in a few sentences (if we have a CLI command,
even better).

On Mon, Feb 15, 2016 at 2:31 PM, OpenShift Bot notifications@github.com
wrote:

Merged #6715 #6715.


Reply to this email directly or view it on GitHub
#6715 (comment).

@rhcarvalho rhcarvalho deleted the build-post-commit-pre-push-usr-cmd branch February 16, 2016 12:54
@rhcarvalho
Copy link
Contributor Author

@smarterclayton as @bparees noted in the issue, the CLI for this is unlike to land soon.

The easiest way to use this right now is with oc patch as in the test/cmd tests, or using oc edit / edit YAML in Web Console.

$ oc patch bc/my-build-config -p '{"spec":{"postCommit":{"script":"rake test"}}}'

Full docs is coming at openshift/openshift-docs#1448

For release notes:

Build hooks can be used to execute commands in a temporary container running a freshly built image before it is pushed to a registry. This is useful to validate images by, e.g., running unit tests.

@smarterclayton
Copy link
Contributor

smarterclayton commented Feb 16, 2016 via email

@rhcarvalho
Copy link
Contributor Author

noted, thanks!

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.

9 participants