From 5918c210780db1faf2cf8cdcd85a1a18ad506a14 Mon Sep 17 00:00:00 2001
From: Dan Gillespie <dan@redspread.com>
Date: Sun, 20 Mar 2016 23:23:15 -0700
Subject: [PATCH 1/4] gitlab plugin base

---
 images/gitlabci/Dockerfile    | 12 ++++++++++
 images/gitlabci/README.md     | 53 +++++++++++++++++++++++++++++++++++++++++++
 images/gitlabci/entrypoint.sh | 34 +++++++++++++++++++++++++++
 3 files changed, 99 insertions(+)
 create mode 100644 images/gitlabci/Dockerfile
 create mode 100644 images/gitlabci/README.md
 create mode 100755 images/gitlabci/entrypoint.sh

diff --git a/images/gitlabci/Dockerfile b/images/gitlabci/Dockerfile
new file mode 100644
index 0000000..864bb92
--- /dev/null
+++ b/images/gitlabci/Dockerfile
@@ -0,0 +1,12 @@
+FROM gliderlabs/alpine:3.3
+
+RUN apk-install bash
+
+WORKDIR /opt/spread-gitlab
+
+ADD spread-linux /usr/local/bin/spread
+ADD entrypoint.sh ./entrypoint.sh
+
+ENV KUBECFG_INSECURE_SKIP_TLS_VERIFY="false"
+
+ENTRYPOINT ["entrypoint.sh"]
\ No newline at end of file
diff --git a/images/gitlabci/README.md b/images/gitlabci/README.md
new file mode 100644
index 0000000..cab0657
--- /dev/null
+++ b/images/gitlabci/README.md
@@ -0,0 +1,53 @@
+# Gitlab Spread Plugin
+
+This plugin allows you to add `spread` Kubernetes deployment to your Gitlab CI build pipeline.
+
+## Environment Variables
+
+These environment variables can be used to configure the deployment.
+
+### spread
+
+`DEPLOY_DIR`
+
+The directory spread should be deployed from
+
+
+### Cluster
+`KUBECFG_SERVER`
+
+Address of the Kubernetes API Server (https://hostname:port)
+
+`KUBECFG_API_VERSION`
+
+Preferred api version for communicating with the kubernetes cluster (v1, v2, etc)
+
+`KUBECFG_INSECURE_SKIP_TLS_VERIFY`
+
+Disable requirement that connections must pass TLS verification
+
+`KUBECFG_CERTIFICATE_AUTHORITY`
+
+Path to a cert file for the certificate authority.
+
+### User
+
+`KUBECFG_CLIENT_CERTIFICATE`
+
+Path to a client cert file for TLS
+
+`KUBECFG_CLIENT_KEY`
+
+Path to a client key file for TLS.
+
+`KUBECFG_TOKEN`
+
+Bearer token for cluster auth
+
+`KUBECFG_USERNAME`
+
+Username for basic auth
+
+`KUBECFG_PASSWORD`
+
+Password for basic auth
\ No newline at end of file
diff --git a/images/gitlabci/entrypoint.sh b/images/gitlabci/entrypoint.sh
new file mode 100755
index 0000000..3433bdd
--- /dev/null
+++ b/images/gitlabci/entrypoint.sh
@@ -0,0 +1,34 @@
+#!/bin/bash
+
+# setup kubectl config
+echo "setting up kubectl config"
+mkdir -p ~/.kube
+>~/.kube/config cat <<EOF
+kind: Config
+apiVersion: v1
+current-context: gitlab
+contexts:
+- name: gitlab
+  context:
+    cluster: default
+    user: default
+clusters:
+- name: default
+  cluster:
+    server: $KUBECFG_SERVER
+    api-version: $KUBECFG_API_VERSION
+    insecure-skip-tls-verify: $KUBECFG_INSECURE_SKIP_TLS_VERIFY
+    certificate-authority: $KUBECFG_CERTIFICATE_AUTHORITY
+users:
+- name: default
+  user:
+    client-certificate: $KUBECFG_CLIENT_CERTIFICATE
+    client-certificate-data: $KUBECFG_CLIENT_CERTIFICATE_DATA
+    client-key: $KUBECFG_CLIENT_KEY
+    token: $KUBECFG_TOKEN
+    username: $KUBECFG_USERNAME
+    password: $KUBECFG_PASSWORD
+EOF
+
+spread deploy $DEPLOY_DIR
+

From cbb57654948810c5476a2ddcebf648d517daf9bb Mon Sep 17 00:00:00 2001
From: Dan Gillespie <dan@redspread.com>
Date: Mon, 21 Mar 2016 00:11:44 -0700
Subject: [PATCH 2/4] compiles statically linked spread and puts it into the
 gitlab image

---
 .gitignore                 |  2 ++
 Makefile                   | 25 ++++++++++++++++++++++++-
 images/gitlabci/Dockerfile |  5 +++--
 3 files changed, 29 insertions(+), 3 deletions(-)

diff --git a/.gitignore b/.gitignore
index 0c9e329..11749ff 100644
--- a/.gitignore
+++ b/.gitignore
@@ -23,3 +23,5 @@ _testmain.go
 *.exe
 *.test
 *.prof
+
+build/
\ No newline at end of file
diff --git a/Makefile b/Makefile
index 1356c52..f25255d 100644
--- a/Makefile
+++ b/Makefile
@@ -11,14 +11,26 @@ GO ?= go
 GOX ?= gox
 GOFMT ?= gofmt # eventually should add "-s"
 GOLINT ?= golint
+DOCKER ?= docker
 
 GOFILES := find . -name '*.go' -not -path "./vendor/*"
 
 GOBUILD_LDFLAGS ?=
-GOBUILD_FLAGS ?=
+GOBUILD_FLAGS ?= -i -v
 GOTEST_FLAGS ?= -v
 GOX_FLAGS ?= -output="build/{{.Dir}}_{{.OS}}_{{.Arch}}" -os="${GOX_OS}" -arch="${GOX_ARCH}"
 
+STATIC_LDFLAGS ?=  --ldflags '-extldflags "-static" --s -w'
+
+GITLAB_CONTEXT ?= ./build/gitlab
+
+# image data
+ORG ?= redspreadapps
+NAME ?= spread-gitlab
+TAG ?= latest
+
+GITLAB_IMAGE_NAME = "$(ORG)/$(NAME):$(TAG)"
+
 GOX_OS ?= linux darwin windows
 GOX_ARCH ?= amd64
 
@@ -39,10 +51,21 @@ validate: lint checkgofmt vet
 build:
 	$(GO) install $(GOBUILD_FLAGS) $(GOBUILD_LDFLAGS) $(EXEC_PKG)
 
+build/spread-linux-static:
+	GOOS=linux $(GO) build -o $@ $(GOBUILD_FLAGS) $(STATIC_LDFLAGS) $(EXEC_PKG)
+	chmod +x $@
+
 .PHONY: crossbuild
 crossbuild: deps gox-setup
 	$(GOX) $(GOX_FLAGS) -gcflags="$(GOBUILD_FLAGS)" -ldflags="$(GOBUILD_LDFLAGS)" $(EXEC_PKG)
 
+.PHONY: build-gitlab
+build-gitlab: build/spread-linux-static
+	rm -rf $(GITLAB_CONTEXT)
+	cp -r ./images/gitlabci $(GITLAB_CONTEXT)
+	cp ./build/spread-linux-static $(GITLAB_CONTEXT)
+	$(DOCKER) build $(DOCKER_OPTS) -t $(GITLAB_IMAGE_NAME) $(GITLAB_CONTEXT)
+
 .PHONY: vet
 vet:
 	$(GO) vet $(PKGS) 
diff --git a/images/gitlabci/Dockerfile b/images/gitlabci/Dockerfile
index 864bb92..d4e5bdc 100644
--- a/images/gitlabci/Dockerfile
+++ b/images/gitlabci/Dockerfile
@@ -4,9 +4,10 @@ RUN apk-install bash
 
 WORKDIR /opt/spread-gitlab
 
-ADD spread-linux /usr/local/bin/spread
+ADD spread-linux-static /usr/local/bin/spread
 ADD entrypoint.sh ./entrypoint.sh
 
 ENV KUBECFG_INSECURE_SKIP_TLS_VERIFY="false"
+ENV DEPLOY_DIR="."
 
-ENTRYPOINT ["entrypoint.sh"]
\ No newline at end of file
+ENTRYPOINT ["./entrypoint.sh"]
\ No newline at end of file

From 659e04acea0c78571f9a4f01eb0b2622f113ce5c Mon Sep 17 00:00:00 2001
From: Dan Gillespie <dan@redspread.com>
Date: Mon, 21 Mar 2016 04:27:47 -0700
Subject: [PATCH 3/4] more opts

---
 Makefile                      |  4 ++--
 images/gitlabci/Dockerfile    | 11 +++--------
 images/gitlabci/README.md     | 12 ++++++++++++
 images/gitlabci/entrypoint.sh | 24 ++++++++++++++++++------
 4 files changed, 35 insertions(+), 16 deletions(-)

diff --git a/Makefile b/Makefile
index f25255d..8360586 100644
--- a/Makefile
+++ b/Makefile
@@ -26,7 +26,7 @@ GITLAB_CONTEXT ?= ./build/gitlab
 
 # image data
 ORG ?= redspreadapps
-NAME ?= spread-gitlab
+NAME ?= gitlabci
 TAG ?= latest
 
 GITLAB_IMAGE_NAME = "$(ORG)/$(NAME):$(TAG)"
@@ -109,4 +109,4 @@ godep:
 	@echo "Recalculating godeps, removing Godeps and vendor if not canceled in 5 seconds"
 	@sleep 5
 	rm -rf Godeps vendor
-	GO15VENDOREXPERIMENT="1" godep save -v ./pkg/... ./cli/... ./cmd/...
\ No newline at end of file
+	GO15VENDOREXPERIMENT="1" godep save -v ./pkg/... ./cli/... ./cmd/...
diff --git a/images/gitlabci/Dockerfile b/images/gitlabci/Dockerfile
index d4e5bdc..168feea 100644
--- a/images/gitlabci/Dockerfile
+++ b/images/gitlabci/Dockerfile
@@ -1,13 +1,8 @@
-FROM gliderlabs/alpine:3.3
-
-RUN apk-install bash
-
-WORKDIR /opt/spread-gitlab
+FROM docker:git
 
 ADD spread-linux-static /usr/local/bin/spread
-ADD entrypoint.sh ./entrypoint.sh
+ADD entrypoint.sh /opt/spread-gitlab/entrypoint.sh
 
 ENV KUBECFG_INSECURE_SKIP_TLS_VERIFY="false"
-ENV DEPLOY_DIR="."
 
-ENTRYPOINT ["./entrypoint.sh"]
\ No newline at end of file
+ENTRYPOINT ["/opt/spread-gitlab/entrypoint.sh"]
\ No newline at end of file
diff --git a/images/gitlabci/README.md b/images/gitlabci/README.md
index cab0657..3bbf610 100644
--- a/images/gitlabci/README.md
+++ b/images/gitlabci/README.md
@@ -30,16 +30,28 @@ Disable requirement that connections must pass TLS verification
 
 Path to a cert file for the certificate authority.
 
+`KUBECFG_CERTIFICATE_AUTHORITY_DATA`
+
+Certificate data
+
 ### User
 
 `KUBECFG_CLIENT_CERTIFICATE`
 
 Path to a client cert file for TLS
 
+`KUBECFG_CLIENT_CERTIFICATE_DATA`
+
+TLS client cert data
+
 `KUBECFG_CLIENT_KEY`
 
 Path to a client key file for TLS.
 
+`KUBECFG_CLIENT_KEY_DATA`
+
+Key data
+
 `KUBECFG_TOKEN`
 
 Bearer token for cluster auth
diff --git a/images/gitlabci/entrypoint.sh b/images/gitlabci/entrypoint.sh
index 3433bdd..ffb5a94 100755
--- a/images/gitlabci/entrypoint.sh
+++ b/images/gitlabci/entrypoint.sh
@@ -1,9 +1,11 @@
-#!/bin/bash
+#!/bin/sh
 
-# setup kubectl config
-echo "setting up kubectl config"
-mkdir -p ~/.kube
->~/.kube/config cat <<EOF
+# if config file does not exist produce one with env vars
+if [ ! -f ~/.kube/config ]; then
+    echo "setting up kubectl config"
+    mkdir -p ~/.kube
+
+    >~/.kube/config cat <<EOF
 kind: Config
 apiVersion: v1
 current-context: gitlab
@@ -19,16 +21,26 @@ clusters:
     api-version: $KUBECFG_API_VERSION
     insecure-skip-tls-verify: $KUBECFG_INSECURE_SKIP_TLS_VERIFY
     certificate-authority: $KUBECFG_CERTIFICATE_AUTHORITY
+    certificate-authority-data: $KUBECFG_CERTIFICATE_AUTHORITY_DATA
+
 users:
 - name: default
   user:
     client-certificate: $KUBECFG_CLIENT_CERTIFICATE
     client-certificate-data: $KUBECFG_CLIENT_CERTIFICATE_DATA
     client-key: $KUBECFG_CLIENT_KEY
+    client-key-data: $KUBECFG_CLIENT_KEY_DATA
     token: $KUBECFG_TOKEN
     username: $KUBECFG_USERNAME
     password: $KUBECFG_PASSWORD
 EOF
+fi
 
-spread deploy $DEPLOY_DIR
+if [ ! -z "$DEPLOY_DIR" ]; then
+    echo "using DEPLOY_DIR..."
+    spread deploy $DEPLOY_DIR
+    exit
+fi
 
+echo "Using project dir..."
+spread deploy $CI_PROJECT_DIR
\ No newline at end of file

From ee9345b9bafe370e85ac1ce35c97d761c717ae54 Mon Sep 17 00:00:00 2001
From: Dan Gillespie <dan@redspread.com>
Date: Mon, 21 Mar 2016 04:36:27 -0700
Subject: [PATCH 4/4] changed install to build

---
 Makefile | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/Makefile b/Makefile
index 8360586..ee88378 100644
--- a/Makefile
+++ b/Makefile
@@ -49,7 +49,7 @@ validate: lint checkgofmt vet
 
 .PHONY: build
 build:
-	$(GO) install $(GOBUILD_FLAGS) $(GOBUILD_LDFLAGS) $(EXEC_PKG)
+	$(GO) build $(GOBUILD_FLAGS) $(GOBUILD_LDFLAGS) $(EXEC_PKG)
 
 build/spread-linux-static:
 	GOOS=linux $(GO) build -o $@ $(GOBUILD_FLAGS) $(STATIC_LDFLAGS) $(EXEC_PKG)
