From 5bea08f5ddb850cf939b22f36571e4117dd26e13 Mon Sep 17 00:00:00 2001 From: Divyansh Manchanda Date: Tue, 16 Jul 2019 15:55:16 +0530 Subject: [PATCH] v2.01 move agent spec to separate file --- Dockerfile | 2 + agentpods/agent-dind.yaml | 33 +++++++++++++++ kubernetes.go | 85 +++++++++++++++------------------------ kubernetes.yaml | 2 +- scripts/build-release | 51 ----------------------- 5 files changed, 69 insertions(+), 104 deletions(-) create mode 100644 agentpods/agent-dind.yaml delete mode 100644 scripts/build-release diff --git a/Dockerfile b/Dockerfile index 1ff1636..fb209fc 100644 --- a/Dockerfile +++ b/Dockerfile @@ -15,5 +15,7 @@ ADD . /app/ WORKDIR /app RUN go build -o main . +COPY agentpods/* agentpods/ + EXPOSE 8082 CMD ["/app/main"] diff --git a/agentpods/agent-dind.yaml b/agentpods/agent-dind.yaml new file mode 100644 index 0000000..6c89d1a --- /dev/null +++ b/agentpods/agent-dind.yaml @@ -0,0 +1,33 @@ +apiVersion: v1 +kind: Pod +metadata: + name: vsts-agent-dind +spec: + containers: + - name: vsts-agent + image: microsoft/vsts-agent:ubuntu-16.04-docker-18.06.1-ce-standard + env: + - name: VSTS_ACCOUNT + valueFrom: + secretKeyRef: + name: vsts + key: VSTS_ACCOUNT + - name: VSTS_TOKEN + valueFrom: + secretKeyRef: + name: vsts + key: VSTS_TOKEN + - name: VSTS_POOL + value: divman + - name: DOCKER_HOST + value: tcp://localhost:2375 + - name: dind-daemon + image: docker:18.09.6-dind + securityContext: + privileged: true + volumeMounts: + - name: agent-pv-storage + mountPath: /var/lib/docker + volumes: + - name: agent-pv-storage + emptyDir: {} \ No newline at end of file diff --git a/kubernetes.go b/kubernetes.go index df2458e..825601a 100644 --- a/kubernetes.go +++ b/kubernetes.go @@ -1,6 +1,7 @@ package main import ( + "io/ioutil" "github.com/ghodss/yaml" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" @@ -10,62 +11,19 @@ import ( ) func CreatePod() string { - config, err := rest.InClusterConfig() + cs, err := getInClusterClientSet() if err != nil { return err.Error() } - clientset, err := kubernetes.NewForConfig(config) - if err != nil { - return err.Error() - } - - // If pod is to be created in a different namespace - // then secrets need to be created in the same namespace, i.e. VSTS_TOKEN and VSTS_ACCOUNT - // kubectl create secret generic vsts --from-literal=VSTS_TOKEN= --from-literal=VSTS_ACCOUNT=mseng - var podYaml = ` - apiVersion: v1 - kind: Pod - metadata: - name: vsts-agent-dind - spec: - containers: - - name: vsts-agent - image: microsoft/vsts-agent:ubuntu-16.04-docker-18.06.1-ce-standard - env: - - name: VSTS_ACCOUNT - valueFrom: - secretKeyRef: - name: vsts - key: VSTS_ACCOUNT - - name: VSTS_TOKEN - valueFrom: - secretKeyRef: - name: vsts - key: VSTS_TOKEN - - name: VSTS_POOL - value: divman - - name: DOCKER_HOST - value: tcp://localhost:2375 - - name: dind-daemon - image: docker:18.09.6-dind - securityContext: - privileged: true - volumeMounts: - - name: agent-pv-storage - mountPath: /var/lib/docker - volumes: - - name: agent-pv-storage - emptyDir: {} - ` - + var podYaml = getDefaultAgentSpecification() var p1 v1.Pod err1 := yaml.Unmarshal([]byte(podYaml), &p1) if err1 != nil { return "unmarshal error: " + err1.Error() } - podClient := clientset.CoreV1().Pods("azuredevops") + podClient := cs.CoreV1().Pods("azuredevops") pod, err2 := podClient.Create(&p1) if err2 != nil { return "podclient create error: " + err2.Error() @@ -75,21 +33,44 @@ func CreatePod() string { } func DeletePod(podname string) string { - config, err := rest.InClusterConfig() + cs, err := getInClusterClientSet() if err != nil { return err.Error() } - clientset, err := kubernetes.NewForConfig(config) - if err != nil { - return err.Error() - } + podClient := cs.CoreV1().Pods("azuredevops") - podClient := clientset.CoreV1().Pods("azuredevops") err2 := podClient.Delete(podname, &metav1.DeleteOptions{}) if err2 != nil { return "podclient delete error: " + err2.Error() } return "Deleted " + podname +} + +func getInClusterClientSet() (*kubernetes.Clientset, error) { + config, err := rest.InClusterConfig() + if err != nil { + return nil, err + } + + clientset, err := kubernetes.NewForConfig(config) + if err != nil { + return nil, err + } + + return clientset, nil +} + +func getDefaultAgentSpecification() string { + // If pod is to be created in a different namespace + // then secrets need to be created in the same namespace, i.e. VSTS_TOKEN and VSTS_ACCOUNT + // kubectl create secret generic vsts --from-literal=VSTS_TOKEN= --from-literal=VSTS_ACCOUNT=mseng + dat, err := ioutil.ReadFile("agentpods/agent-dind.yaml") + if err != nil { + return err.Error() + } + + var podYaml = string(dat) + return podYaml } \ No newline at end of file diff --git a/kubernetes.yaml b/kubernetes.yaml index a7abefc..991857d 100644 --- a/kubernetes.yaml +++ b/kubernetes.yaml @@ -54,7 +54,7 @@ spec: spec: serviceAccountName: pipelineprovider containers: - - image: divyanshm/goserver:v2.0 + - image: divyanshm/goserver:v2.01 name: goserver command: ["/app/main"] ports: diff --git a/scripts/build-release b/scripts/build-release deleted file mode 100644 index ba5ff62..0000000 --- a/scripts/build-release +++ /dev/null @@ -1,51 +0,0 @@ -#!/bin/sh -e - -# -# This script is inspired by coreos/etcd. -# The credits goes to coreos. -# This version is only a (slightly) modified version. -# -# Thanks to open source and thanks to CoreOS / etcd. -# -# @link https://github.com/coreos/etcd/blob/master/scripts/build-release -# - -VER=$1 -PROJ="simple-webserver" - -if [ -z "$1" ]; then - echo "Usage: ${0} VERSION" >> /dev/stderr - exit 255 -fi - -set -u - -function main { - mkdir release - - for os in darwin windows linux freebsd; do - export GOOS=${os} - export GOARCH="amd64" - - TARGET="simple-webserver-${VER}-${GOOS}-${GOARCH}" - mkdir ${TARGET} - cp README.md ${TARGET}/README.md - - local ext="" - if [ ${GOOS} == "windows" ]; then - ext=".exe" - fi - - go build -o ${TARGET}/simple-webserver${ext} - - if [ ${GOOS} == "linux" ]; then - tar cfz release/${TARGET}.tar.gz ${TARGET} - echo "Wrote release/${TARGET}.tar.gz" - else - zip -qr release/${TARGET}.zip ${TARGET} - echo "Wrote release/${TARGET}.zip" - fi - done -} - -main