Repository: jenkins-x/lighthouse
Pull request #947 (2020-08-20 14:07:31)

refactor: Get rid of cobra stuff in webhooks
Signed-off-by: Andrew Bayer <andrew.bayer@gmail.com>

abayer: /assign @hferentschik 
jenkins-x-bot: [APPROVALNOTIFIER] This PR is **NOT APPROVED**

This pull-request has been approved by:
To complete the [pull request process](https://git.k8s.io/community/contributors/guide/owners.md#the-code-review-process), please assign **abayer**
You can assign the PR to them by writing `/assign @abayer` in a comment when ready.

The full list of commands accepted by this bot can be found [here](https://go.k8s.io/bot-commands?repo=jenkins-x%2Flighthouse).

<details open>
Needs approval from an approver in each of these files:

- **[OWNERS](https://github.com/jenkins-x/lighthouse/blob/master/OWNERS)**

Approvers can indicate their approval by writing `/approve` in a comment
Approvers can cancel approval by writing `/approve cancel` in a comment
</details>
<!-- META={"approvers":["abayer"]} -->
jenkins-x-bot: @abayer: The following test **failed**, say `/retest` to rerun them all:

Test name | Commit | Details | Rerun command
--- | --- | --- | ---
pr-build | 611c89c99e8d94638377d849cce7a38d5045beef | [link](https://dashboard-jx.jenkins-x.live/teams/jx/projects/jenkins-x/lighthouse/PR-947/1) | `/test pr-build`

[View all Builds for this Pull Request](https://dashboard-jx.jenkins-x.live/teams/jx/projects/jenkins-x/lighthouse/PR-947) 

<details>

Instructions for interacting with me using PR comments are available [here](https://git.k8s.io/community/contributors/guide/pull-requests.md).  If you have questions or suggestions related to my behavior, please file an issue against the [jenkins-x/lighthouse](https://github.com/jenkins-x/lighthouse/issues/new?title=Command%20issue:) repository. I understand the commands that are listed [here](https://go.k8s.io/bot-commands).
</details>
<!-- test report -->
jenkins-x-bot: @abayer: The following tests **failed**, say `/retest` to rerun them all:

Test name | Commit | Details | Rerun command
--- | --- | --- | ---
pr-build | 611c89c99e8d94638377d849cce7a38d5045beef | [link](https://dashboard-jx.jenkins-x.live/teams/jx/projects/jenkins-x/lighthouse/PR-947/1) | `/test pr-build`
bbs | 611c89c99e8d94638377d849cce7a38d5045beef | [link](https://dashboard-jx.jenkins-x.live/teams/jx/projects/jenkins-x/lighthouse/PR-947/4) | `/test bbs`

[View all Builds for this Pull Request](https://dashboard-jx.jenkins-x.live/teams/jx/projects/jenkins-x/lighthouse/PR-947) 

<details>

Instructions for interacting with me using PR comments are available [here](https://git.k8s.io/community/contributors/guide/pull-requests.md).  If you have questions or suggestions related to my behavior, please file an issue against the [jenkins-x/lighthouse](https://github.com/jenkins-x/lighthouse/issues/new?title=Command%20issue:) repository. I understand the commands that are listed [here](https://go.k8s.io/bot-commands).
</details>
<!-- test report -->
jenkins-x-bot: @abayer: The following tests **failed**, say `/retest` to rerun them all:

Test name | Commit | Details | Rerun command
--- | --- | --- | ---
pr-build | 611c89c99e8d94638377d849cce7a38d5045beef | [link](https://dashboard-jx.jenkins-x.live/teams/jx/projects/jenkins-x/lighthouse/PR-947/1) | `/test pr-build`
bbs | 611c89c99e8d94638377d849cce7a38d5045beef | [link](https://dashboard-jx.jenkins-x.live/teams/jx/projects/jenkins-x/lighthouse/PR-947/4) | `/test bbs`
gitlab | 611c89c99e8d94638377d849cce7a38d5045beef | [link](https://dashboard-jx.jenkins-x.live/teams/jx/projects/jenkins-x/lighthouse/PR-947/5) | `/test gitlab`

[View all Builds for this Pull Request](https://dashboard-jx.jenkins-x.live/teams/jx/projects/jenkins-x/lighthouse/PR-947) 

<details>

Instructions for interacting with me using PR comments are available [here](https://git.k8s.io/community/contributors/guide/pull-requests.md).  If you have questions or suggestions related to my behavior, please file an issue against the [jenkins-x/lighthouse](https://github.com/jenkins-x/lighthouse/issues/new?title=Command%20issue:) repository. I understand the commands that are listed [here](https://go.k8s.io/bot-commands).
</details>
<!-- test report -->
jenkins-x-bot: @abayer: The following tests **failed**, say `/retest` to rerun them all:

Test name | Commit | Details | Rerun command
--- | --- | --- | ---
pr-build | 611c89c99e8d94638377d849cce7a38d5045beef | [link](https://dashboard-jx.jenkins-x.live/teams/jx/projects/jenkins-x/lighthouse/PR-947/1) | `/test pr-build`
bbs | 611c89c99e8d94638377d849cce7a38d5045beef | [link](https://dashboard-jx.jenkins-x.live/teams/jx/projects/jenkins-x/lighthouse/PR-947/4) | `/test bbs`
gitlab | 611c89c99e8d94638377d849cce7a38d5045beef | [link](https://dashboard-jx.jenkins-x.live/teams/jx/projects/jenkins-x/lighthouse/PR-947/5) | `/test gitlab`
ghe | 611c89c99e8d94638377d849cce7a38d5045beef | [link](https://dashboard-jx.jenkins-x.live/teams/jx/projects/jenkins-x/lighthouse/PR-947/2) | `/test ghe`

[View all Builds for this Pull Request](https://dashboard-jx.jenkins-x.live/teams/jx/projects/jenkins-x/lighthouse/PR-947) 

<details>

Instructions for interacting with me using PR comments are available [here](https://git.k8s.io/community/contributors/guide/pull-requests.md).  If you have questions or suggestions related to my behavior, please file an issue against the [jenkins-x/lighthouse](https://github.com/jenkins-x/lighthouse/issues/new?title=Command%20issue:) repository. I understand the commands that are listed [here](https://go.k8s.io/bot-commands).
</details>
<!-- test report -->
jenkins-x-bot: @abayer: The following tests **failed**, say `/retest` to rerun them all:

Test name | Commit | Details | Rerun command
--- | --- | --- | ---
pr-build | 611c89c99e8d94638377d849cce7a38d5045beef | [link](https://dashboard-jx.jenkins-x.live/teams/jx/projects/jenkins-x/lighthouse/PR-947/1) | `/test pr-build`
bbs | 611c89c99e8d94638377d849cce7a38d5045beef | [link](https://dashboard-jx.jenkins-x.live/teams/jx/projects/jenkins-x/lighthouse/PR-947/4) | `/test bbs`
gitlab | 611c89c99e8d94638377d849cce7a38d5045beef | [link](https://dashboard-jx.jenkins-x.live/teams/jx/projects/jenkins-x/lighthouse/PR-947/5) | `/test gitlab`
ghe | 611c89c99e8d94638377d849cce7a38d5045beef | [link](https://dashboard-jx.jenkins-x.live/teams/jx/projects/jenkins-x/lighthouse/PR-947/2) | `/test ghe`
github | 611c89c99e8d94638377d849cce7a38d5045beef | [link](https://dashboard-jx.jenkins-x.live/teams/jx/projects/jenkins-x/lighthouse/PR-947/3) | `/test github`

[View all Builds for this Pull Request](https://dashboard-jx.jenkins-x.live/teams/jx/projects/jenkins-x/lighthouse/PR-947) 

<details>

Instructions for interacting with me using PR comments are available [here](https://git.k8s.io/community/contributors/guide/pull-requests.md).  If you have questions or suggestions related to my behavior, please file an issue against the [jenkins-x/lighthouse](https://github.com/jenkins-x/lighthouse/issues/new?title=Command%20issue:) repository. I understand the commands that are listed [here](https://go.k8s.io/bot-commands).
</details>
<!-- test report -->
jenkins-x-bot: @abayer: The following tests **failed**, say `/retest` to rerun them all:

Test name | Commit | Details | Rerun command
--- | --- | --- | ---
bbs | 611c89c99e8d94638377d849cce7a38d5045beef | [link](https://dashboard-jx.jenkins-x.live/teams/jx/projects/jenkins-x/lighthouse/PR-947/4) | `/test bbs`
gitlab | 611c89c99e8d94638377d849cce7a38d5045beef | [link](https://dashboard-jx.jenkins-x.live/teams/jx/projects/jenkins-x/lighthouse/PR-947/5) | `/test gitlab`
ghe | 611c89c99e8d94638377d849cce7a38d5045beef | [link](https://dashboard-jx.jenkins-x.live/teams/jx/projects/jenkins-x/lighthouse/PR-947/2) | `/test ghe`
github | 611c89c99e8d94638377d849cce7a38d5045beef | [link](https://dashboard-jx.jenkins-x.live/teams/jx/projects/jenkins-x/lighthouse/PR-947/3) | `/test github`
tekton | 611c89c99e8d94638377d849cce7a38d5045beef | [link](https://dashboard-jx.jenkins-x.live/teams/jx/projects/jenkins-x/lighthouse/PR-947/6) | `/test tekton`

[View all Builds for this Pull Request](https://dashboard-jx.jenkins-x.live/teams/jx/projects/jenkins-x/lighthouse/PR-947) 

<details>

Instructions for interacting with me using PR comments are available [here](https://git.k8s.io/community/contributors/guide/pull-requests.md).  If you have questions or suggestions related to my behavior, please file an issue against the [jenkins-x/lighthouse](https://github.com/jenkins-x/lighthouse/issues/new?title=Command%20issue:) repository. I understand the commands that are listed [here](https://go.k8s.io/bot-commands).
</details>
<!-- test report -->
hferentschik: /lgtm
jenkins-x-bot: [APPROVALNOTIFIER] This PR is **APPROVED**

This pull-request has been approved by: *[hferentschik](https://github.com/jenkins-x/lighthouse/pull/947#issuecomment-677701967 "LGTM")*

The full list of commands accepted by this bot can be found [here](https://go.k8s.io/bot-commands?repo=jenkins-x%2Flighthouse).

The pull request process is described [here](https://git.k8s.io/community/contributors/guide/owners.md#the-code-review-process)

<details >
Needs approval from an approver in each of these files:

- ~~[OWNERS](https://github.com#issuecomment-677701967/jenkins-x/lighthouse/blob/master/OWNERS)~~ [hferentschik]

Approvers can indicate their approval by writing `/approve` in a comment
Approvers can cancel approval by writing `/approve cancel` in a comment
</details>
<!-- META={"approvers":[]} -->
jenkins-x-bot: @abayer: The following tests **failed**, say `/retest` to rerun them all:

Test name | Commit | Details | Rerun command
--- | --- | --- | ---
bbs | 611c89c99e8d94638377d849cce7a38d5045beef | [link](https://dashboard-jx.jenkins-x.live/teams/jx/projects/jenkins-x/lighthouse/PR-947/4) | `/test bbs`
gitlab | 611c89c99e8d94638377d849cce7a38d5045beef | [link](https://dashboard-jx.jenkins-x.live/teams/jx/projects/jenkins-x/lighthouse/PR-947/5) | `/test gitlab`
ghe | 611c89c99e8d94638377d849cce7a38d5045beef | [link](https://dashboard-jx.jenkins-x.live/teams/jx/projects/jenkins-x/lighthouse/PR-947/2) | `/test ghe`
github | 611c89c99e8d94638377d849cce7a38d5045beef | [link](https://dashboard-jx.jenkins-x.live/teams/jx/projects/jenkins-x/lighthouse/PR-947/3) | `/test github`
tekton | e90a0c92c2a99c8d5b69114963b47aad72a45c3d | [link](https://dashboard-jx.jenkins-x.live/teams/jx/projects/jenkins-x/lighthouse/PR-947/12) | `/test tekton`

[View all Builds for this Pull Request](https://dashboard-jx.jenkins-x.live/teams/jx/projects/jenkins-x/lighthouse/PR-947) 

<details>

Instructions for interacting with me using PR comments are available [here](https://git.k8s.io/community/contributors/guide/pull-requests.md).  If you have questions or suggestions related to my behavior, please file an issue against the [jenkins-x/lighthouse](https://github.com/jenkins-x/lighthouse/issues/new?title=Command%20issue:) repository. I understand the commands that are listed [here](https://go.k8s.io/bot-commands).
</details>
<!-- test report -->
abayer: /retest
jenkins-x-bot: @abayer: The following tests **failed**, say `/retest` to rerun them all:

Test name | Commit | Details | Rerun command
--- | --- | --- | ---
bbs | 611c89c99e8d94638377d849cce7a38d5045beef | [link](https://dashboard-jx.jenkins-x.live/teams/jx/projects/jenkins-x/lighthouse/PR-947/4) | `/test bbs`
gitlab | 611c89c99e8d94638377d849cce7a38d5045beef | [link](https://dashboard-jx.jenkins-x.live/teams/jx/projects/jenkins-x/lighthouse/PR-947/5) | `/test gitlab`
ghe | 611c89c99e8d94638377d849cce7a38d5045beef | [link](https://dashboard-jx.jenkins-x.live/teams/jx/projects/jenkins-x/lighthouse/PR-947/2) | `/test ghe`
tekton | e90a0c92c2a99c8d5b69114963b47aad72a45c3d | [link](https://dashboard-jx.jenkins-x.live/teams/jx/projects/jenkins-x/lighthouse/PR-947/12) | `/test tekton`
github | e90a0c92c2a99c8d5b69114963b47aad72a45c3d | [link](https://dashboard-jx.jenkins-x.live/teams/jx/projects/jenkins-x/lighthouse/PR-947/9) | `/test github`

[View all Builds for this Pull Request](https://dashboard-jx.jenkins-x.live/teams/jx/projects/jenkins-x/lighthouse/PR-947) 

<details>

Instructions for interacting with me using PR comments are available [here](https://git.k8s.io/community/contributors/guide/pull-requests.md).  If you have questions or suggestions related to my behavior, please file an issue against the [jenkins-x/lighthouse](https://github.com/jenkins-x/lighthouse/issues/new?title=Command%20issue:) repository. I understand the commands that are listed [here](https://go.k8s.io/bot-commands).
</details>
<!-- test report -->
jenkins-x-bot: @abayer: The following tests **failed**, say `/retest` to rerun them all:

Test name | Commit | Details | Rerun command
--- | --- | --- | ---
bbs | 611c89c99e8d94638377d849cce7a38d5045beef | [link](https://dashboard-jx.jenkins-x.live/teams/jx/projects/jenkins-x/lighthouse/PR-947/4) | `/test bbs`
gitlab | 611c89c99e8d94638377d849cce7a38d5045beef | [link](https://dashboard-jx.jenkins-x.live/teams/jx/projects/jenkins-x/lighthouse/PR-947/5) | `/test gitlab`
tekton | e90a0c92c2a99c8d5b69114963b47aad72a45c3d | [link](https://dashboard-jx.jenkins-x.live/teams/jx/projects/jenkins-x/lighthouse/PR-947/12) | `/test tekton`
github | e90a0c92c2a99c8d5b69114963b47aad72a45c3d | [link](https://dashboard-jx.jenkins-x.live/teams/jx/projects/jenkins-x/lighthouse/PR-947/9) | `/test github`
ghe | e90a0c92c2a99c8d5b69114963b47aad72a45c3d | [link](https://dashboard-jx.jenkins-x.live/teams/jx/projects/jenkins-x/lighthouse/PR-947/8) | `/test ghe`

[View all Builds for this Pull Request](https://dashboard-jx.jenkins-x.live/teams/jx/projects/jenkins-x/lighthouse/PR-947) 

<details>

Instructions for interacting with me using PR comments are available [here](https://git.k8s.io/community/contributors/guide/pull-requests.md).  If you have questions or suggestions related to my behavior, please file an issue against the [jenkins-x/lighthouse](https://github.com/jenkins-x/lighthouse/issues/new?title=Command%20issue:) repository. I understand the commands that are listed [here](https://go.k8s.io/bot-commands).
</details>
<!-- test report -->
jenkins-x-bot: @abayer: The following tests **failed**, say `/retest` to rerun them all:

Test name | Commit | Details | Rerun command
--- | --- | --- | ---
bbs | 611c89c99e8d94638377d849cce7a38d5045beef | [link](https://dashboard-jx.jenkins-x.live/teams/jx/projects/jenkins-x/lighthouse/PR-947/4) | `/test bbs`
tekton | e90a0c92c2a99c8d5b69114963b47aad72a45c3d | [link](https://dashboard-jx.jenkins-x.live/teams/jx/projects/jenkins-x/lighthouse/PR-947/12) | `/test tekton`
github | e90a0c92c2a99c8d5b69114963b47aad72a45c3d | [link](https://dashboard-jx.jenkins-x.live/teams/jx/projects/jenkins-x/lighthouse/PR-947/9) | `/test github`
ghe | e90a0c92c2a99c8d5b69114963b47aad72a45c3d | [link](https://dashboard-jx.jenkins-x.live/teams/jx/projects/jenkins-x/lighthouse/PR-947/8) | `/test ghe`
gitlab | e90a0c92c2a99c8d5b69114963b47aad72a45c3d | [link](https://dashboard-jx.jenkins-x.live/teams/jx/projects/jenkins-x/lighthouse/PR-947/11) | `/test gitlab`

[View all Builds for this Pull Request](https://dashboard-jx.jenkins-x.live/teams/jx/projects/jenkins-x/lighthouse/PR-947) 

<details>

Instructions for interacting with me using PR comments are available [here](https://git.k8s.io/community/contributors/guide/pull-requests.md).  If you have questions or suggestions related to my behavior, please file an issue against the [jenkins-x/lighthouse](https://github.com/jenkins-x/lighthouse/issues/new?title=Command%20issue:) repository. I understand the commands that are listed [here](https://go.k8s.io/bot-commands).
</details>
<!-- test report -->
jenkins-x-bot: @abayer: The following tests **failed**, say `/retest` to rerun them all:

Test name | Commit | Details | Rerun command
--- | --- | --- | ---
tekton | e90a0c92c2a99c8d5b69114963b47aad72a45c3d | [link](https://dashboard-jx.jenkins-x.live/teams/jx/projects/jenkins-x/lighthouse/PR-947/12) | `/test tekton`
github | e90a0c92c2a99c8d5b69114963b47aad72a45c3d | [link](https://dashboard-jx.jenkins-x.live/teams/jx/projects/jenkins-x/lighthouse/PR-947/9) | `/test github`
ghe | e90a0c92c2a99c8d5b69114963b47aad72a45c3d | [link](https://dashboard-jx.jenkins-x.live/teams/jx/projects/jenkins-x/lighthouse/PR-947/8) | `/test ghe`
gitlab | e90a0c92c2a99c8d5b69114963b47aad72a45c3d | [link](https://dashboard-jx.jenkins-x.live/teams/jx/projects/jenkins-x/lighthouse/PR-947/11) | `/test gitlab`
bbs | e90a0c92c2a99c8d5b69114963b47aad72a45c3d | [link](https://dashboard-jx.jenkins-x.live/teams/jx/projects/jenkins-x/lighthouse/PR-947/10) | `/test bbs`

[View all Builds for this Pull Request](https://dashboard-jx.jenkins-x.live/teams/jx/projects/jenkins-x/lighthouse/PR-947) 

<details>

Instructions for interacting with me using PR comments are available [here](https://git.k8s.io/community/contributors/guide/pull-requests.md).  If you have questions or suggestions related to my behavior, please file an issue against the [jenkins-x/lighthouse](https://github.com/jenkins-x/lighthouse/issues/new?title=Command%20issue:) repository. I understand the commands that are listed [here](https://go.k8s.io/bot-commands).
</details>
<!-- test report -->
jenkins-x-bot: New changes are detected. LGTM label has been removed.
hferentschik: /lgtm
jenkins-x-bot: [APPROVALNOTIFIER] This PR is **APPROVED**

This pull-request has been approved by: *[hferentschik](https://github.com/jenkins-x/lighthouse/pull/947#issuecomment-677712008 "LGTM")*

The full list of commands accepted by this bot can be found [here](https://go.k8s.io/bot-commands?repo=jenkins-x%2Flighthouse).

The pull request process is described [here](https://git.k8s.io/community/contributors/guide/owners.md#the-code-review-process)

<details >
Needs approval from an approver in each of these files:

- ~~[OWNERS](https://github.com#issuecomment-677712008/jenkins-x/lighthouse/blob/master/OWNERS)~~ [hferentschik]

Approvers can indicate their approval by writing `/approve` in a comment
Approvers can cancel approval by writing `/approve cancel` in a comment
</details>
<!-- META={"approvers":[]} -->
jenkins-x-bot: @abayer: The following tests **failed**, say `/retest` to rerun them all:

Test name | Commit | Details | Rerun command
--- | --- | --- | ---
github | e90a0c92c2a99c8d5b69114963b47aad72a45c3d | [link](https://dashboard-jx.jenkins-x.live/teams/jx/projects/jenkins-x/lighthouse/PR-947/9) | `/test github`
ghe | e90a0c92c2a99c8d5b69114963b47aad72a45c3d | [link](https://dashboard-jx.jenkins-x.live/teams/jx/projects/jenkins-x/lighthouse/PR-947/8) | `/test ghe`
gitlab | e90a0c92c2a99c8d5b69114963b47aad72a45c3d | [link](https://dashboard-jx.jenkins-x.live/teams/jx/projects/jenkins-x/lighthouse/PR-947/11) | `/test gitlab`
bbs | e90a0c92c2a99c8d5b69114963b47aad72a45c3d | [link](https://dashboard-jx.jenkins-x.live/teams/jx/projects/jenkins-x/lighthouse/PR-947/10) | `/test bbs`
tekton | e90a0c92c2a99c8d5b69114963b47aad72a45c3d | [link](https://dashboard-jx.jenkins-x.live/teams/jx/projects/jenkins-x/lighthouse/PR-947/13) | `/test tekton`

[View all Builds for this Pull Request](https://dashboard-jx.jenkins-x.live/teams/jx/projects/jenkins-x/lighthouse/PR-947) 

<details>

Instructions for interacting with me using PR comments are available [here](https://git.k8s.io/community/contributors/guide/pull-requests.md).  If you have questions or suggestions related to my behavior, please file an issue against the [jenkins-x/lighthouse](https://github.com/jenkins-x/lighthouse/issues/new?title=Command%20issue:) repository. I understand the commands that are listed [here](https://go.k8s.io/bot-commands).
</details>
<!-- test report -->

Patch:
diff --git a/Dockerfile.webhooks b/Dockerfile.webhooks
index 3c5df040..a0152a34 100644
--- a/Dockerfile.webhooks
+++ b/Dockerfile.webhooks
@@ -1,6 +1,6 @@
 FROM alpine:3.10
 RUN apk add --update --no-cache ca-certificates git
-COPY ./bin/lighthouse  /lighthouse
+COPY ./bin/webhooks  /webhooks
 RUN mkdir /jxhome
 ENV JX_HOME /jxhome
-ENTRYPOINT ["/lighthouse"]
+ENTRYPOINT ["/webhooks"]
diff --git a/Makefile b/Makefile
index 14c519e2..302482cd 100644
--- a/Makefile
+++ b/Makefile
@@ -3,7 +3,7 @@ rwildcard=$(wildcard $1$2) $(foreach d,$(wildcard $1*),$(call rwildcard,$d/,$2))
 
 PROJECT := github.com/jenkins-x/lighthouse
 
-WEBHOOKS_EXECUTABLE := lighthouse
+WEBHOOKS_EXECUTABLE := webhooks
 KEEPER_EXECUTABLE := keeper
 FOGHORN_EXECUTABLE := foghorn
 GCJOBS_EXECUTABLE := gc-jobs
diff --git a/cmd/webhooks/main.go b/cmd/webhooks/main.go
index a64eb500..2b7d95b2 100644
--- a/cmd/webhooks/main.go
+++ b/cmd/webhooks/main.go
@@ -1,23 +1,91 @@
 package main
 
 import (
-	"fmt"
+	"flag"
+	"net/http"
 	"os"
+	"strconv"
 
-	"github.com/jenkins-x/lighthouse/pkg/version"
+	"github.com/jenkins-x/lighthouse/pkg/interrupts"
+	"github.com/jenkins-x/lighthouse/pkg/logrusutil"
 	"github.com/jenkins-x/lighthouse/pkg/webhook"
+	"github.com/sirupsen/logrus"
 )
 
+const (
+	// HealthPath is the URL path for the HTTP endpoint that returns Health status.
+	HealthPath = "/Health"
+	// ReadyPath URL path for the HTTP endpoint that returns Ready status.
+	ReadyPath = "/Ready"
+)
+
+type options struct {
+	bindAddress string
+	path        string
+	port        int
+	jsonLog     bool
+
+	namespace      string
+	pluginFilename string
+	configFilename string
+	botName        string
+}
+
+func (o *options) Validate() error {
+	return nil
+}
+
+func gatherOptions(fs *flag.FlagSet, args ...string) options {
+	var o options
+	fs.BoolVar(&o.jsonLog, "json", true, "Enable JSON logging")
+	fs.IntVar(&o.port, "port", 8080, "The TCP port to listen on.")
+	fs.StringVar(&o.bindAddress, "bind", "",
+		"The interface address to bind to (by default, will listen on all interfaces/addresses).")
+	fs.StringVar(&o.path, "path", "/hook",
+		"The path to listen on for requests to trigger a pipeline run.")
+	fs.StringVar(&o.pluginFilename, "plugin-file", "", "Path to the plugins.yaml file. If not specified it is loaded from the 'plugins' ConfigMap")
+	fs.StringVar(&o.configFilename, "config-file", "", "7Path to the config.yaml file. If not specified it is loaded from the 'config' ConfigMap")
+	fs.StringVar(&o.botName, "bot-name", "", "The name of the bot user to run as. Defaults to $GIT_USER if not specified.")
+	fs.StringVar(&o.namespace, "namespace", "", "The namespace to listen in")
+
+	err := fs.Parse(args)
+	if err != nil {
+		logrus.WithError(err).Fatal("Invalid options")
+	}
+
+	return o
+}
+
 // Entrypoint for the command
 func main() {
-	cmds := webhook.NewCmdWebhook()
-	cmds.Version = version.GetVersion()
-	cmds.SetVersionTemplate("{{printf .Version}}\n")
+	defer interrupts.WaitForGracefulShutdown()
+
+	o := gatherOptions(flag.NewFlagSet(os.Args[0], flag.ExitOnError), os.Args[1:]...)
+	if err := o.Validate(); err != nil {
+		logrus.WithError(err).Fatal("Invalid options")
+	}
 
-	err := cmds.Execute()
+	if o.jsonLog {
+		logrus.SetFormatter(logrusutil.CreateDefaultFormatter())
+	}
+
+	controller, err := webhook.NewWebhooksController(o.path, o.namespace, o.botName, o.pluginFilename, o.configFilename)
 	if err != nil {
-		fmt.Printf("ERROR: %s\n", err.Error())
-		os.Exit(1)
+		logrus.WithError(err).Fatal("failed to set up controller")
 	}
-	os.Exit(0)
+	defer func() {
+		controller.CleanupGitClientDir()
+		controller.ConfigMapWatcher.Stop()
+	}()
+
+	mux := http.NewServeMux()
+	mux.Handle(HealthPath, http.HandlerFunc(controller.Health))
+	mux.Handle(ReadyPath, http.HandlerFunc(controller.Ready))
+
+	mux.Handle("/", http.HandlerFunc(controller.DefaultHandler))
+	mux.Handle(o.path, http.HandlerFunc(controller.HandleWebhookRequests))
+
+	logrus.Infof("Lighthouse is now listening on path %s and port %d for WebHooks", o.path, o.port)
+	err = http.ListenAndServe(":"+strconv.Itoa(o.port), mux)
+	logrus.WithError(err).Errorf("failed to server HTTP")
 }
diff --git a/pkg/webhook/webhook.go b/pkg/webhook/webhook.go
index b2ffeecb..2abb21a5 100644
--- a/pkg/webhook/webhook.go
+++ b/pkg/webhook/webhook.go
@@ -6,16 +6,13 @@ import (
 	"io/ioutil"
 	"net/http"
 	"net/url"
-	"strconv"
 	"strings"
 
 	"github.com/jenkins-x/go-scm/scm"
 	"github.com/jenkins-x/lighthouse/pkg/clients"
-	"github.com/jenkins-x/lighthouse/pkg/cmd/helper"
 	"github.com/jenkins-x/lighthouse/pkg/config"
 	"github.com/jenkins-x/lighthouse/pkg/git"
 	"github.com/jenkins-x/lighthouse/pkg/launcher"
-	"github.com/jenkins-x/lighthouse/pkg/logrusutil"
 	"github.com/jenkins-x/lighthouse/pkg/metrics"
 	"github.com/jenkins-x/lighthouse/pkg/plugins"
 	"github.com/jenkins-x/lighthouse/pkg/util"
@@ -23,73 +20,37 @@ import (
 	"github.com/jenkins-x/lighthouse/pkg/watcher"
 	"github.com/pkg/errors"
 	"github.com/sirupsen/logrus"
-
-	"github.com/spf13/cobra"
-)
-
-const (
-	// HealthPath is the URL path for the HTTP endpoint that returns health status.
-	HealthPath = "/health"
-	// ReadyPath URL path for the HTTP endpoint that returns ready status.
-	ReadyPath = "/ready"
 )
 
-// Options holds the command line arguments
-type Options struct {
-	BindAddress string
-	Path        string
-	Port        int
-	JSONLog     bool
-
-	namespace        string
-	pluginFilename   string
-	configFilename   string
-	server           *Server
-	botName          string
-	gitServerURL     string
-	configMapWatcher *watcher.ConfigMapWatcher
-	gitClient        git.Client
-	launcher         launcher.PipelineLauncher
+// WebhooksController holds the command line arguments
+type WebhooksController struct {
+	ConfigMapWatcher *watcher.ConfigMapWatcher
+
+	path           string
+	namespace      string
+	pluginFilename string
+	configFilename string
+	server         *Server
+	botName        string
+	gitServerURL   string
+	gitClient      git.Client
+	launcher       launcher.PipelineLauncher
 }
 
-// NewCmdWebhook creates the command
-func NewCmdWebhook() *cobra.Command {
-	options := Options{}
-
-	cmd := &cobra.Command{
-		Use:   "lighthouse",
-		Short: "Runs the lighthouse webhook handler",
-		Run: func(cmd *cobra.Command, args []string) {
-			err := options.Run()
-			helper.CheckErr(err)
-		},
-	}
-
-	cmd.Flags().BoolVarP(&options.JSONLog, "json", "", true, "Enable JSON logging")
-	cmd.Flags().IntVarP(&options.Port, "port", "", 8080, "The TCP port to listen on.")
-	cmd.Flags().StringVarP(&options.BindAddress, "bind", "", "",
-		"The interface address to bind to (by default, will listen on all interfaces/addresses).")
-	cmd.Flags().StringVarP(&options.Path, "path", "", "/hook",
-		"The path to listen on for requests to trigger a pipeline run.")
-	cmd.Flags().StringVar(&options.pluginFilename, "plugin-file", "", "Path to the plugins.yaml file. If not specified it is loaded from the 'plugins' ConfigMap")
-	cmd.Flags().StringVar(&options.configFilename, "config-file", "", "Path to the config.yaml file. If not specified it is loaded from the 'config' ConfigMap")
-	cmd.Flags().StringVar(&options.botName, "bot-name", "", "The name of the bot user to run as. Defaults to $GIT_USER if not specified.")
-	cmd.Flags().StringVar(&options.namespace, "namespace", "", "The namespace to listen in")
-	return cmd
-}
-
-// Run will implement this command
-func (o *Options) Run() error {
-	if o.JSONLog {
-		logrus.SetFormatter(logrusutil.CreateDefaultFormatter())
+// NewWebhooksController creates and configures the controller
+func NewWebhooksController(path, namespace, botName, pluginFilename, configFilename string) (*WebhooksController, error) {
+	o := &WebhooksController{
+		path:           path,
+		namespace:      namespace,
+		pluginFilename: pluginFilename,
+		configFilename: configFilename,
+		botName:        botName,
 	}
-
 	var err error
 	o.server, err = o.createHookServer()
 	if err != nil {
-		return errors.Wrapf(err, "failed to create Hook Server")
+		return nil, errors.Wrapf(err, "failed to create Hook Server")
 	}
-	defer o.configMapWatcher.Stop()
 
 	cfg := o.server.ConfigAgent.Config
 	o.gitServerURL = util.GetGitServer(cfg)
@@ -97,39 +58,33 @@ func (o *Options) Run() error {
 	if err != nil {
 		logrus.WithError(err).Fatal("Error getting git client.")
 	}
-	defer func() {
-		err := gitClient.Clean()
-		if err != nil {
-			logrus.WithError(err).Fatal("Error cleaning the git client.")
-		}
-	}()
-
 	o.gitClient = gitClient
 
 	_, _, lhClient, _, err := clients.GetAPIClients()
 	if err != nil {
-		return errors.Wrap(err, "Error creating kubernetes resource clients.")
+		return nil, errors.Wrap(err, "Error creating kubernetes resource clients.")
 	}
 	o.launcher = launcher.NewLauncher(lhClient, o.namespace)
-	mux := http.NewServeMux()
-	mux.Handle(HealthPath, http.HandlerFunc(o.health))
-	mux.Handle(ReadyPath, http.HandlerFunc(o.ready))
 
-	mux.Handle("/", http.HandlerFunc(o.defaultHandler))
-	mux.Handle(o.Path, http.HandlerFunc(o.handleWebHookRequests))
+	return o, nil
+}
 
-	logrus.Infof("Lighthouse is now listening on path %s and port %d for WebHooks", o.Path, o.Port)
-	return http.ListenAndServe(":"+strconv.Itoa(o.Port), mux)
+// CleanupGitClientDir cleans up the git client's working directory
+func (o *WebhooksController) CleanupGitClientDir() {
+	err := o.gitClient.Clean()
+	if err != nil {
+		logrus.WithError(err).Fatal("Error cleaning the git client.")
+	}
 }
 
-// health returns either HTTP 204 if the service is healthy, otherwise nothing ('cos it's dead).
-func (o *Options) health(w http.ResponseWriter, r *http.Request) {
+// Health returns either HTTP 204 if the service is healthy, otherwise nothing ('cos it's dead).
+func (o *WebhooksController) Health(w http.ResponseWriter, r *http.Request) {
 	logrus.Debug("Health check")
 	w.WriteHeader(http.StatusNoContent)
 }
 
-// ready returns either HTTP 204 if the service is ready to serve requests, otherwise HTTP 503.
-func (o *Options) ready(w http.ResponseWriter, r *http.Request) {
+// Ready returns either HTTP 204 if the service is Ready to serve requests, otherwise HTTP 503.
+func (o *WebhooksController) Ready(w http.ResponseWriter, r *http.Request) {
 	logrus.Debug("Ready check")
 	if o.isReady() {
 		w.WriteHeader(http.StatusNoContent)
@@ -138,10 +93,11 @@ func (o *Options) ready(w http.ResponseWriter, r *http.Request) {
 	}
 }
 
-func (o *Options) defaultHandler(w http.ResponseWriter, r *http.Request) {
+// DefaultHandler responds to requests without a specific handler
+func (o *WebhooksController) DefaultHandler(w http.ResponseWriter, r *http.Request) {
 	path := r.URL.Path
-	if path == o.Path || strings.HasPrefix(path, o.Path+"/") {
-		o.handleWebHookRequests(w, r)
+	if path == o.path || strings.HasPrefix(path, o.path+"/") {
+		o.HandleWebhookRequests(w, r)
 		return
 	}
 	path = strings.TrimPrefix(path, "/")
@@ -151,13 +107,13 @@ func (o *Options) defaultHandler(w http.ResponseWriter, r *http.Request) {
 	http.Error(w, fmt.Sprintf("unknown path %s", path), 404)
 }
 
-func (o *Options) isReady() bool {
+func (o *WebhooksController) isReady() bool {
 	// TODO a better readiness check
 	return true
 }
 
-// handle request for pipeline runs
-func (o *Options) handleWebHookRequests(w http.ResponseWriter, r *http.Request) {
+// HandleWebhookRequests handles incoming events
+func (o *WebhooksController) HandleWebhookRequests(w http.ResponseWriter, r *http.Request) {
 	if r.Method != http.MethodPost {
 		// liveness probe etc
 		logrus.WithField("method", r.Method).Debug("invalid http method so returning 200")
@@ -259,7 +215,7 @@ func (o *Options) handleWebHookRequests(w http.ResponseWriter, r *http.Request)
 }
 
 // ProcessWebHook process a webhook
-func (o *Options) ProcessWebHook(l *logrus.Entry, webhook scm.Webhook) (*logrus.Entry, string, error) {
+func (o *WebhooksController) ProcessWebHook(l *logrus.Entry, webhook scm.Webhook) (*logrus.Entry, string, error) {
 	repository := webhook.Repository()
 	fields := map[string]interface{}{
 		"Namespace": repository.Namespace,
@@ -400,16 +356,16 @@ func (o *Options) ProcessWebHook(l *logrus.Entry, webhook scm.Webhook) (*logrus.
 	return l, fmt.Sprintf("unknown hook %s", webhook.Kind()), nil
 }
 
-func (o *Options) secretFn(webhook scm.Webhook) (string, error) {
+func (o *WebhooksController) secretFn(webhook scm.Webhook) (string, error) {
 	return util.HMACToken(), nil
 }
 
-func (o *Options) createHookServer() (*Server, error) {
+func (o *WebhooksController) createHookServer() (*Server, error) {
 	configAgent := &config.Agent{}
 	pluginAgent := &plugins.ConfigAgent{}
 
 	var err error
-	o.configMapWatcher, err = watcher.SetupConfigMapWatchers(o.namespace, configAgent, pluginAgent)
+	o.ConfigMapWatcher, err = watcher.SetupConfigMapWatchers(o.namespace, configAgent, pluginAgent)
 	if err != nil {
 		return nil, errors.Wrapf(err, "failed to create ConfigMap watcher")
 	}


Test patch:
diff --git a/pkg/webhook/webhook_test.go b/pkg/webhook/webhook_test.go
index 15f32366..1dd0916a 100644
--- a/pkg/webhook/webhook_test.go
+++ b/pkg/webhook/webhook_test.go
@@ -26,7 +26,7 @@ type WebhookTestSuite struct {
 	KubeClient     kubernetes.Interface
 	SCMCLient      scm.Client
 	GitClient      git.Client
-	WebhookOptions *Options
+	WebhookOptions *WebhooksController
 	TestRepo       scm.Repository
 }
 
@@ -151,7 +151,7 @@ func (suite *WebhookTestSuite) SetupSuite() {
 		return []byte(token)
 	})
 	util.AddAuthToSCMClient(scmClient, token, false)
-	suite.WebhookOptions = &Options{
+	suite.WebhookOptions = &WebhooksController{
 		server: &Server{
 			ConfigAgent: configAgent,
 			Plugins:     pluginAgent,