Skip to content

Commit

Permalink
Add custom GitHub gateway (#150)
Browse files Browse the repository at this point in the history
* Refactoring gateway

* Updated state handling of event sources

* updating gateways

* refactor resource gateway

* updating gateways

* updating gateways

* updating gateways

* updating gateways

* updating gateways

* refactoring sensor

* refactoring controllers

* refactoring controllers

* Updating hack

* Updating documentation

* updated examples

* Adding recover in gateways

* Gateway is now deployed as a pod instead of deployment

* refactoring state machine of sensor and gateway

* Updating sensor and gateway state machine

* Updating sensor and gateway state machine

* Updating examples

* Add custom GitHub gateway

Issue #95
  • Loading branch information
pjediny authored and VaibhavPage committed Jan 10, 2019
1 parent 0bb0273 commit a671891
Show file tree
Hide file tree
Showing 13 changed files with 520 additions and 0 deletions.
9 changes: 9 additions & 0 deletions Gopkg.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions Gopkg.toml
Original file line number Diff line number Diff line change
Expand Up @@ -87,3 +87,7 @@ required = [
[[prune.project]]
name = "k8s.io/code-generator"
unused-packages = false

[[constraint]]
name = "github.com/google/go-github"
version = "21.0.0"
10 changes: 10 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,16 @@ gitlab-linux:
gitlab-image: gitlab-linux
docker build -t $(IMAGE_PREFIX)gitlab-gateway:$(IMAGE_TAG) -f ./gateways/custom/gitlab/Dockerfile .
@if [ "$(DOCKER_PUSH)" = "true" ] ; then docker push $(IMAGE_PREFIX)gitlab-gateway:$(IMAGE_TAG) ; fi

github:
CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -v -ldflags '${LDFLAGS}' -o ${DIST_DIR}/github-gateway ./gateways/custom/github/cmd

github-linux:
CGO_ENABLED=0 GOOS=linux GOARCH=amd64 make github

github-image: github-linux
docker build -t $(IMAGE_PREFIX)github-gateway:$(IMAGE_TAG) -f ./gateways/custom/github/Dockerfile .
@if [ "$(DOCKER_PUSH)" = "true" ] ; then docker push $(IMAGE_PREFIX)github-gateway:$(IMAGE_TAG) ; fi

test:
go test $(shell go list ./... | grep -v /vendor/) -race -short -v
Expand Down
20 changes: 20 additions & 0 deletions examples/gateways/github-configmap.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
apiVersion: v1
kind: ConfigMap
metadata:
name: github-gateway-configmap
data:
project_1: |-
owner: "owner-example"
repository: "repo-example"
url: "http://webhook-gateway-gateway-svc/push"
events:
- "*"
apiToken:
name: github-access
key: token
webHookSecret:
name: github-access
key: secret
insecure: false
active: true
contentType: "json"
32 changes: 32 additions & 0 deletions examples/gateways/github.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
apiVersion: argoproj.io/v1alpha1
kind: Gateway
metadata:
name: github-gateway
labels:
gateways.argoproj.io/gateway-controller-instanceid: argo-events
gateway-name: "github-gateway"
spec:
deploySpec:
metadata:
labels:
gateway-type: github
dispatch-mechanism: http
spec:
containers:
- name: "gateway-client"
image: "metalgearsolid/gateway-client:v0.6.1"
imagePullPolicy: "Always"
command: ["/bin/gateway-client"]
- name: "file-events"
image: "metalgearsolid/github-gateway:v0.6.1"
imagePullPolicy: "Always"
command: ["/bin/github-gateway"]
serviceAccountName: "argo-events-sa"
configMap: "github-gateway-configmap"
type: "github"
dispatchProtocol: "HTTP"
eventVersion: "1.0"
processorPort: "9330"
watchers:
sensors:
- name: "github-sensor"
36 changes: 36 additions & 0 deletions examples/sensors/github.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
apiVersion: argoproj.io/v1alpha1
kind: Sensor
metadata:
name: github-sensor
labels:
sensors.argoproj.io/sensor-controller-instanceid: argo-events
spec:
repeat: true
serviceAccountName: argo-events-sa
imageVersion: "latest"
imagePullPolicy: Always
signals:
- name: webhook-gateway/index
triggers:
- name: github-workflow-trigger
resource:
namespace: argo-events
group: argoproj.io
version: v1alpha1
kind: Workflow
source:
inline: |
apiVersion: argoproj.io/v1alpha1
kind: Workflow
metadata:
generateName: hello-world-
spec:
entrypoint: whalesay
templates:
- name: whalesay
container:
args:
- "hello world"
command:
- cowsay
image: "docker/whalesay:latest"
7 changes: 7 additions & 0 deletions gateways/custom/github/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
FROM alpine:latest as certs
RUN apk --update add ca-certificates

FROM scratch
COPY --from=certs /etc/ssl/certs/ca-certificates.crt /etc/ssl/certs/ca-certificates.crt
COPY dist/github-gateway /bin/
ENTRYPOINT [ "/bin/github-gateway" ]
43 changes: 43 additions & 0 deletions gateways/custom/github/cmd/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
Copyright 2018 KompiTech GmbH
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package main

import (
"github.com/argoproj/argo-events/common"
"github.com/argoproj/argo-events/gateways"
"github.com/argoproj/argo-events/gateways/custom/github"
"k8s.io/client-go/kubernetes"
"os"
)

func main() {
kubeConfig, _ := os.LookupEnv(common.EnvVarKubeConfig)
restConfig, err := common.GetClientConfig(kubeConfig)
if err != nil {
panic(err)
}
clientset := kubernetes.NewForConfigOrDie(restConfig)
namespace, ok := os.LookupEnv(common.EnvVarGatewayNamespace)
if !ok {
panic("namespace is not provided")
}
gateways.StartGateway(&github.GithubEventSourceExecutor{
Log: common.GetLoggerContext(common.LoggerConf()).Logger(),
Namespace: namespace,
Clientset: clientset,
})
}
90 changes: 90 additions & 0 deletions gateways/custom/github/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
/*
Copyright 2018 KompiTech GmbH
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package github

import (
"github.com/ghodss/yaml"
"github.com/rs/zerolog"
"k8s.io/client-go/kubernetes"
"github.com/google/go-github/github"
)

// GithubEventSourceExecutor implements ConfigExecutor
type GithubEventSourceExecutor struct {
Log zerolog.Logger
// GitlabClient is client for gitlab api
GithubClient *github.Client
// Clientset is kubernetes client
Clientset kubernetes.Interface
// Namespace where gateway is deployed
Namespace string
}

// GithubConfig contains information to setup a github project integration
// +k8s:openapi-gen=true
type GithubConfig struct {
// GitHub owner name i.e. argoproj
Owner string `json:"owner"`

// GitHub repo name i.e. argo-events
Repository string `json:"repository"`

// Github events to subscribe to which the gateway will subscribe
Events []string `json:"events"`

// External URL for hooks
URL string `json:"url"`

// K8s secret containing github api token
APIToken *GithubSecret `json:"apiToken"`

// K8s secret containing WebHook Secret
WebHookSecret *GithubSecret `json:"webHookSecret"`

// Insecure tls verification
Insecure bool `json:"insecure"`

// Active
Active bool `json:"active"`

// ContentType json or form
ContentType string `json:"contentType"`
}

// GithubSecret contains information of k8 secret which holds the github api access token key
// +k8s:openapi-gen=true
type GithubSecret struct {
// Name of k8 secret containing api token or webhook secret
Name string
// Key for api token/webhook secret
Key string
}

// cred stores the api access token or webhook secret
type cred struct {
secret string
}

// parseEventSource parses a configuration of gateway
func parseEventSource(config *string) (*GithubConfig, error) {
var g *GithubConfig
err := yaml.Unmarshal([]byte(*config), &g)
if err != nil {
return nil, err
}
return g, err
}
Loading

0 comments on commit a671891

Please sign in to comment.