Skip to content

Commit

Permalink
Cloudnative ci automation (elastic#837)
Browse files Browse the repository at this point in the history
This commit provides the relevant Jenkins CI automation to open Pull requests to kibana github repository in order to keep Cloud-Native teams manifests in sync with the manifests that are used into Fleet UI.

For full information check elastic#706

Updated .ci/Jenkins file that is triggered upon PR requests of /elastic-agent/deploy/kubernetes/* changes
Updated Makefile to add functionality needed to create the extra files for the new prs to kibana remote repository
  • Loading branch information
gizas authored Aug 17, 2022
1 parent 1ebffe9 commit d4f33d0
Show file tree
Hide file tree
Showing 8 changed files with 169 additions and 12 deletions.
38 changes: 38 additions & 0 deletions .ci/Jenkinsfile
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,44 @@ pipeline {
}
}
}
stage('Sync K8s') { //This stage opens a PR to kibana Repository in order to sync k8s manifests
when {
// Only on main branch
// Enable if k8s related changes.
allOf {
branch 'main' // Only runs for branch main
expression { return env.K8S_CHANGES == "true" } // If k8s changes
}
}
failFast false
agent {label 'ubuntu-20.04 && immutable'}
options { skipDefaultCheckout() }
stages {
stage('OpenKibanaPR') {
steps {
withGhEnv(version: '2.4.0') {
deleteDir()
unstashV2(name: 'source', bucket: "${JOB_GCS_BUCKET}", credentialsId: "${JOB_GCS_CREDENTIALS}")
dir("${BASE_DIR}/deploy/kubernetes"){
sh(label: '[File Creation] Create-Needed-Manifest', script: """
WITHOUTCONFIG=true make generate-k8s
./creator_k8s_manifest.sh . """)
sh(label: '[Clone] Kibana-Repository', script: """
make ci-clone-kibana-repository
cp Makefile ./kibana
cd kibana
make ci-create-kubernetes-templates-pull-request """)
}
}
}
post {
always {
junit(allowEmptyResults: true, keepLongStdio: true, testResults: "${BASE_DIR}/build/TEST-*.xml")
}
}
}
}
}
stage('e2e tests') {
when {
// Always when running builds on branches/tags
Expand Down
71 changes: 65 additions & 6 deletions deploy/kubernetes/Makefile
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
ALL=elastic-agent-standalone elastic-agent-managed
BEAT_VERSION=$(shell head -n 1 ../../version/docs/version.asciidoc | cut -c 17- )

.PHONY: generate-k8s $(ALL)
#variables needed for ci-create-kubernetes-templates-pull-request
ELASTIC_AGENT_REPO=kibana
ELASTIC_AGENT_REPO_PATH=x-pack/plugins/fleet/server/services/
FILE_REPO=elastic_agent_manifest.ts
ELASTIC_AGENT_BRANCH=update-k8s-templates-$(shell date "+%Y%m%d%H%M%S")

.PHONY: generate-k8s $(ALL)
generate-k8s: $(ALL)

test: generate-k8s
for FILE in $(shell ls *-kubernetes.yaml); do \
BEAT=$$(echo $$FILE | cut -d \- -f 1); \
Expand All @@ -14,10 +19,64 @@ test: generate-k8s
clean:
@for f in $(ALL); do rm -f "$$f-kubernetes.yaml"; done

$(ALL):
@echo "Generating $@-kubernetes.yaml"
@rm -f $@-kubernetes.yaml
$(ALL):
ifdef WITHOUTCONFIG
@echo "Generating [email protected]"
@rm -f [email protected]
@for f in $(shell ls $@/*.yaml | grep -v daemonset-configmap); do \
sed "s/%VERSION%/VERSION/g" $$f >> [email protected]; \
echo --- >> [email protected]; \
done
else
@echo "Generating [email protected]"
@rm -f [email protected]
@for f in $(shell ls $@/*.yaml); do \
sed "s/%VERSION%/${BEAT_VERSION}/g" $$f >> [email protected]; \
echo --- >> [email protected]; \
done
done
endif

CHDIR_SHELL := $(SHELL)
define chdir
$(eval _D=$(firstword $(1) $(@D)))
$(info $(MAKE): cd $(_D)) $(eval SHELL = cd $(_D); $(CHDIR_SHELL))
endef

## ci-clone-kibana-repository : Clone Kibana Repository and copy new files for the PR
.PHONY: ci-clone-kibana-repository
ci-clone-kibana-repository:
git clone [email protected]:elastic/kibana.git
cp $(FILE_REPO) $(ELASTIC_AGENT_REPO)/$(ELASTIC_AGENT_REPO_PATH)

## ci-create-kubernetes-templates-pull-request : Create the pull request for the kubernetes templates
.PHONY: ci-create-kubernetes-templates-pull-request
ci-create-kubernetes-templates-pull-request:
HASDIFF=$$(git status | grep $(FILE_REPO) | wc -l); \
if [ $${HASDIFF} -ne 1 ]; \
then \
echo "No differences found with kibana git repository" && \
exit 1; \
fi
echo "INFO: Create branch to update k8s templates"
git checkout -b $(ELASTIC_AGENT_BRANCH)
echo "INFO: add files if any"
git add $(ELASTIC_AGENT_REPO_PATH)$(FILE_REPO)
echo "INFO: commit changes if any"
git diff --staged --quiet || git commit -m "[Automated PR] Publish kubernetes templates for elastic-agent"
echo "INFO: show remote details"
git remote -v
ifeq ($(DRY_RUN),TRUE)
echo "INFO: skip pushing branch"
else
echo "INFO: push branch"
@git push --set-upstream origin $(ELASTIC_AGENT_BRANCH)
echo "INFO: create pull request"
@gh pr create \
--title "Update kubernetes templates for elastic-agent" \
--body "Automated by ${BUILD_URL}" \
--label automation \
--base main \
--head $(ELASTIC_AGENT_BRANCH) \
--reviewer elastic/obs-cloudnative-monitoring
endif

58 changes: 58 additions & 0 deletions deploy/kubernetes/creator_k8s_manifest.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
#!/bin/bash
####
# Bash Script that creates the needed https://github.com/elastic/kibana/blob/main/x-pack/plugins/fleet/server/services/elastic_agent_manifest.ts
# The script takes as an argument the path of elastic-agent manifests
# Eg. ./creator_k8s_manifest.sh deploy/kubernetes
####


STANDALONE=elastic-agent-standalone-kubernetes-without-configmap.yaml
MANAGED=elastic-agent-managed-kubernetes-without-configmap.yaml
OUTPUT_FILE=elastic_agent_manifest.ts

#Check if arguments provided
((!$#)) && echo "No arguments provided!Please provide path of elastic-agent files" && exit 1
MANIFEST_PATH=$1

#Check if file elastic-agent-standalone-kubernetes-without-configmap.yaml exists
if [ ! -f "$MANIFEST_PATH/$STANDALONE" ]; then
echo "$MANIFEST_PATH/$STANDALONE does not exists"
exit 1
fi

#Check if file elastic-agent-managed-kubernetes-without-configmap.yaml exists
if [ ! -f "$MANIFEST_PATH/$MANAGED" ]; then
echo "$MANIFEST_PATH/$MANAGED does not exists"
exit 1
fi

#Start creation of output file
cat << EOF > $OUTPUT_FILE
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0; you may not use this file except in compliance with the Elastic License
* 2.0.
*/
export const elasticAgentStandaloneManifest = \`---
EOF

cat $MANIFEST_PATH/$STANDALONE >> $OUTPUT_FILE
echo "\`;" >> $OUTPUT_FILE

cat << EOF >> $OUTPUT_FILE
export const elasticAgentManagedManifest = \`---
EOF

cat $MANIFEST_PATH/$MANAGED >> $OUTPUT_FILE
echo -n "\`;" >> $OUTPUT_FILE

#Replacing all occurencies of elastic-agent-standalone
sed -i -e 's/elastic-agent-standalone/elastic-agent/g' $OUTPUT_FILE

#Remove ES_HOST entry from file
sed -i -e '/# The Elasticsearch host to communicate with/d' $OUTPUT_FILE
sed -i -e '/ES_HOST/d' $OUTPUT_FILE
sed -i -e '/value: ""/d' $OUTPUT_FILE
2 changes: 1 addition & 1 deletion deploy/kubernetes/elastic-agent-managed-kubernetes.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ spec:
# Elasticsearch API key used to enroll Elastic Agents in Fleet (https://www.elastic.co/guide/en/fleet/current/fleet-enrollment-tokens.html#fleet-enrollment-tokens)
# If FLEET_ENROLLMENT_TOKEN is empty then KIBANA_HOST, KIBANA_FLEET_USERNAME, KIBANA_FLEET_PASSWORD are needed
- name: FLEET_ENROLLMENT_TOKEN
value: ""
value: "token-id"
- name: KIBANA_HOST
value: "http://kibana:5601"
# The basic authentication username used to connect to Kibana and retrieve a service_token to enable Fleet
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ spec:
# Elasticsearch API key used to enroll Elastic Agents in Fleet (https://www.elastic.co/guide/en/fleet/current/fleet-enrollment-tokens.html#fleet-enrollment-tokens)
# If FLEET_ENROLLMENT_TOKEN is empty then KIBANA_HOST, KIBANA_FLEET_USERNAME, KIBANA_FLEET_PASSWORD are needed
- name: FLEET_ENROLLMENT_TOKEN
value: ""
value: "token-id"
- name: KIBANA_HOST
value: "http://kibana:5601"
# The basic authentication username used to connect to Kibana and retrieve a service_token to enable Fleet
Expand Down
5 changes: 3 additions & 2 deletions deploy/kubernetes/elastic-agent-standalone-kubernetes.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# For more information refer to https://www.elastic.co/guide/en/fleet/current/running-on-kubernetes-standalone.html
# For more information refer https://www.elastic.co/guide/en/fleet/current/running-on-kubernetes-standalone.html
apiVersion: v1
kind: ConfigMap
metadata:
Expand Down Expand Up @@ -624,6 +624,7 @@ data:
# period: 10s
# condition: ${kubernetes.labels.app} == 'redis'
---
# For more information refer to https://www.elastic.co/guide/en/fleet/current/running-on-kubernetes-standalone.html
apiVersion: apps/v1
kind: DaemonSet
metadata:
Expand Down Expand Up @@ -664,7 +665,7 @@ spec:
value: "elastic"
# The basic authentication password used to connect to Elasticsearch
- name: ES_PASSWORD
value: ""
value: "changeme"
# The Elasticsearch host to communicate with
- name: ES_HOST
value: ""
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# For more information refer to https://www.elastic.co/guide/en/fleet/current/running-on-kubernetes-standalone.html
# For more information refer https://www.elastic.co/guide/en/fleet/current/running-on-kubernetes-standalone.html
apiVersion: v1
kind: ConfigMap
metadata:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# For more information refer to https://www.elastic.co/guide/en/fleet/current/running-on-kubernetes-standalone.html
apiVersion: apps/v1
kind: DaemonSet
metadata:
Expand Down Expand Up @@ -38,7 +39,7 @@ spec:
value: "elastic"
# The basic authentication password used to connect to Elasticsearch
- name: ES_PASSWORD
value: ""
value: "changeme"
# The Elasticsearch host to communicate with
- name: ES_HOST
value: ""
Expand Down

0 comments on commit d4f33d0

Please sign in to comment.