[stable/gocd] Add a post start lifecycle hook for GoCD server to preconfigure the server (#4945)

* Preconfigure elastic agent profile and plugin settings.

* Add the preconfigure GoCD server section to the README.
* Introduce a changelog.
* Update the Chart version.

* Add the API call to create and unpause a simple GoCD pipeline in the preconfigure script.

* Increment chart version.

* Change to label to be consistent with the rest of the templates.
This commit is contained in:
Varsha Varadarajan
2018-04-18 14:47:59 -07:00
committed by k8s-ci-robot
parent ef222f07fe
commit f7745ea5cb
6 changed files with 214 additions and 2 deletions
+21
View File
@@ -0,0 +1,21 @@
### 1.0.5
* [8b205e0](https://github.com/kubernetes/charts/commit/8b205e0): Add a script to preconfigure the GoCD server using the `postStart` lifecycle hook.
### 1.0.4
* [7fa1860](https://github.com/kubernetes/charts/commit/7fa1860): Typo fix
### 1.0.3
* [c1eaaa1](https://github.com/kubernetes/charts/commit/c1eaaa1): README changes
### 1.0.2
* [ca90ee6](https://github.com/kubernetes/charts/commit/ca90ee6): fix typo: change recalim -> reclaim in README.
### 1.0.1
* [d9e850b](https://github.com/kubernetes/charts/commit/d9e850b): Changed kubernetes plugin version to v1.0.0
### 1.0.0
* [9323233](https://github.com/kubernetes/charts/commit/9325233): Moved the GoCD Helm chart to stable
+1 -1
View File
@@ -1,6 +1,6 @@
name: gocd
home: https://www.gocd.org/
version: 1.0.4
version: 1.0.5
appVersion: 18.2.0
description: GoCD is an open-source continuous delivery server to model and visualize complex workflows with ease.
icon: https://gocd.github.io/assets/images/go-icon-black-192x192.png
+41
View File
@@ -68,6 +68,7 @@ The following tables list the configurable parameters of the GoCD chart and thei
| Parameter | Description | Default |
| ------------------------------------------ | ------------------------------------------------------------------------------------------------------------- | ------------------- |
| `server.enabled` | Enable GoCD Server. Supported values are `true`, `false`. When enabled, the GoCD server deployment is done on helm install. | `true` |
| `server.shouldPreconfigure` | Preconfigure GoCD Server to have a default elastic agent profile and Kubernetes elastic agent plugin settings. Supported values are `true`, `false`. | `true` |
| `server.image.repository` | GoCD server image | `gocd/gocd-server` |
| `server.image.tag` | GoCD server image tag | `.Chart.appVersion` |
| `server.image.pullPolicy` | Image pull policy | `IfNotPresent` |
@@ -86,6 +87,46 @@ The following tables list the configurable parameters of the GoCD chart and thei
| `server.healthCheck.periodSeconds` | GoCD server heath check interval period. | `15` |
| `server.healthCheck.failureThreshold` | Number of unsuccessful attempts made to the GoCD server health check endpoint before restarting. | `10` |
#### Preconfiguring the GoCD Server
Based on the information available about the Kubernetes cluster, the [Kubernetes elastic agent](https://github.com/gocd/kubernetes-elastic-agents) plugin settings can be configured. A default elastic agent profile too is created so that users can concentrate on building their CD pipeline.
A simple first pipeline is created in order to bootstrap the getting started experience for users.
If you are comfortable with GoCD and feel that there is no need to preconfigure the server, then you can override `server.shouldPreconfigure` to be false.
**Note: If the GoCD server is started with an existing config from a persistent volume, set the value of `server.shouldPreconfigure` to `false`.**
```bash
$ helm install --namespace gocd --name gocd-app --set server.shouldPreconfigure=false stable/gocd
```
We are using the `postStart` container lifecycle hook to configure the plugin settings and the elastic agent profile. On starting the container, an attempt is made to configure the GoCD server.
```bash
$ kubectl get pods --namespace gocd
```
The above command will show the pod state. This will be in `ContainerCreating` till the preconfigure script exits.
```bash
$ kubectl describe pods --namespace gocd
```
The above command will show the events that occurred in detail. This can be used to determine if there is any problem at the time of creating the GoCD server pod. If the preconfigure script fails for some reason, the event `FailedPostStartHook` is published.
The output of the preconfigure script is provided at `/godata/logs/preconfigure.log`.
```bash
$ helm status gocd-app
```
This command provides the information on how to access the GoCD server.
The cases when the attempt to preconfigure the GoCD server fails:
1. The service account token mounted as a secret for the GoCD server pod does not have sufficient permissions. The API call to configure the plugin settings will fail.
2. If the GoCD server is started with an existing configuration with security configured, then the API calls in the preconfigure script will fail.
### GoCD Agent
| Parameter | Description | Default |
+133
View File
@@ -0,0 +1,133 @@
apiVersion: v1
kind: ConfigMap
metadata:
name: {{ template "gocd.fullname" . }}
labels:
app: {{ template "gocd.name" . }}
chart: "{{ .Chart.Name }}-{{ .Chart.Version }}"
release: "{{ .Release.Name }}"
heritage: "{{ .Release.Service }}"
data:
preconfigure_server.sh: |-
#!/bin/bash
SERVICE_ACCOUNT_PATH=/var/run/secrets/kubernetes.io/serviceaccount
KUBE_TOKEN=$(<${SERVICE_ACCOUNT_PATH}/token)
while true
do
status_code=$(curl 'http://localhost:8153/go/api/v1/health' -o /dev/null -w "%{http_code}")
if [ $status_code == 200 ]; then
break
fi
sleep 10
done
set -e
echo "Trying to create an elastic profile now." >> /godata/logs/preconfigure.log
(curl --fail -i 'http://localhost:8153/go/api/elastic/profiles' \
-H 'Accept: application/vnd.go.cd.v1+json' \
-H 'Content-Type: application/json' \
-X POST -d '{
"id": "demo-app",
"plugin_id": "cd.go.contrib.elasticagent.kubernetes",
"properties": [
{
"key": "Image",
"value": "gocd/gocd-agent-docker-dind:v18.2.0"
},
{
"key": "PodConfiguration",
"value": "apiVersion: v1\nkind: Pod\nmetadata:\n name: pod-name-prefix-{\{ POD_POSTFIX }\}\n labels:\n app: web\nspec:\n containers:\n - name: gocd-agent-container-{\{ CONTAINER_POSTFIX }\}\n image: {\{ GOCD_AGENT_IMAGE }\}:{\{ LATEST_VERSION }\}\n securityContext:\n privileged: true"
},
{
"key": "SpecifiedUsingPodConfiguration",
"value": "false"
},
{
"key": "Privileged",
"value": "true"
}
]
}' >> /godata/logs/preconfigure.log)
echo "Trying to configure plugin settings." >> /godata/logs/preconfigure.log
(curl --fail -i 'http://localhost:8153/go/api/admin/plugin_settings' \
-H 'Accept: application/vnd.go.cd.v1+json' \
-H 'Content-Type: application/json' \
-X POST -d '{
"plugin_id": "cd.go.contrib.elasticagent.kubernetes",
"configuration": [
{
"key": "go_server_url",
"value": "https://{{ template "gocd.fullname" . }}-server:{{ .Values.server.service.httpsPort }}/go"
},
{
"key": "kubernetes_cluster_url",
"value": "https://'$KUBERNETES_SERVICE_HOST':'$KUBERNETES_SERVICE_PORT_HTTPS'"
},
{
"key": "namespace",
"value": "{{ .Release.Namespace }}"
},
{
"key": "security_token",
"value": "'$KUBE_TOKEN'"
}
]
}' >> /godata/logs/preconfigure.log)
echo "Trying to creating a hello world pipeline." >> /godata/logs/preconfigure.log
(curl --fail -i 'http://localhost:8153/go/api/admin/pipelines' \
-H 'Accept: application/vnd.go.cd.v5+json' \
-H 'Content-Type: application/json' \
-X POST -d '{ "group": "sample",
"pipeline": {
"label_template": "${COUNT}",
"name": "hello_world",
"materials": [
{
"type": "git",
"attributes": {
"url": "https://github.com/gocd-contrib/getting-started-repo",
"shallow_clone": true
}
}
],
"stages": [
{
"name": "default_stage",
"jobs": [
{
"name": "default_job",
"elastic_profile_id": "demo-app",
"tasks": [
{
"type": "exec",
"attributes": {
"command": "echo",
"arguments": [
"Hello World"
]
}
}
]
}
]
}
]
}
}' >> /godata/logs/preconfigure.log )
echo "Unpausing the pipeline." >> /godata/logs/preconfigure.log
(curl --fail -i 'http://localhost:8153/go/api/pipelines/hello_world/unpause' \
-H 'Accept: application/vnd.go.cd.v1+json' \
-H 'X-GoCD-Confirm: true' \
-X POST >> /godata/logs/preconfigure.log)
echo "Done preconfiguring the GoCD server" >> /godata/logs/preconfigure.log
@@ -25,11 +25,15 @@ spec:
release: {{ .Release.Name | quote }}
component: server
spec:
serviceAccountName: {{ template "gocd.serviceAccountName" . }}
{{- if .Values.server.persistence.enabled }}
volumes:
- name: goserver-vol
persistentVolumeClaim:
claimName: {{ .Values.server.persistence.existingClaim | default (printf "%s-%s" (include "gocd.fullname" .) "server") }}
- name: config-vol
configMap:
name: {{ template "gocd.fullname" . }}
{{- end }}
containers:
- name: {{ template "gocd.name" . }}-server
@@ -75,6 +79,15 @@ spec:
- name: goserver-vol
mountPath: /docker-entrypoint.d
subPath: {{ .Values.server.persistence.subpath.dockerEntryPoint }}
- name: config-vol
mountPath: /preconfigure_server.sh
subPath: preconfigure_server.sh
{{- end }}
{{- if .Values.server.shouldPreconfigure }}
lifecycle:
postStart:
exec:
command: ["/bin/bash","/preconfigure_server.sh"]
{{- end }}
resources:
{{ toYaml .Values.server.resources | indent 12 }}
+5 -1
View File
@@ -15,12 +15,16 @@ serviceAccount:
create: true
# The name of the ServiceAccount to use.
# If not set and create is true, a name is generated using the fullname template
# If create if false and a name is not specified, the default service account is used for the cluster role binding.
# If create is false and a name is not specified, the default service account is used for the cluster role binding.
name:
server:
# server.enabled is the toggle to run GoCD Server. Change to false for Agent Only Deployment.
enabled: true
# server.shouldPreconfigure is used to invoke a script to pre configure the elastic agent profile and the plugin settings in the GoCD server.
# Note: If this value is set to true, then, the serviceAccount.name is configured for the GoCD server pod. The service account token is mounted as a secret and is used in the lifecycle hook.
# Note: An attempt to preconfigure the GoCD server is made. There are cases where the pre-configuration can fail and the GoCD server starts with an empty config.
shouldPreconfigure: true
image:
# server.image.repository is the GoCD Server image name
repository: "gocd/gocd-server"