Skip to content

Commit

Permalink
Merge pull request #10 from cloudogu/feature/multiple_stages
Browse files Browse the repository at this point in the history
implemented option for multiple stages
  • Loading branch information
pmarkiewka authored Dec 7, 2020
2 parents 463a427 + 513d12a commit fcbee15
Show file tree
Hide file tree
Showing 10 changed files with 119 additions and 28 deletions.
44 changes: 37 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,9 @@ Reproducible infrastructure to showcase GitOps workflows. Derived from our [cons
- [3rd Party app (NGINX) via Flux V1](#3rd-party-app-nginx-via-flux-v1)
- [PetClinic via Flux V2](#petclinic-via-flux-v2)
- [PetClinic via ArgoCD](#petclinic-via-argocd)

- [Remove apps from cluster](#remove-apps-from-cluster)
- [Options](#options)
- [Multiple stages](#multiple-stages)

<!-- END doctoc generated TOC please keep comment here to allow auto update -->

Expand Down Expand Up @@ -94,19 +95,20 @@ Login with `admin/admin`

##### PetClinic via Flux V1

* [Jenkinsfile](applications/petclinic/fluxv1/plain-k8s/Jenkinsfile)
* [Jenkinsfile](applications/petclinic/fluxv1/plain-k8s/Jenkinsfile) for plain `k8s` deployment
* [localhost:9000](http://localhost:9000) (Staging)
* [localhost:9001](http://localhost:9001) (Production)
* [localhost:9002](http://localhost:9002) (qa)

* [Jenkinsfile](applications/petclinic/fluxv1/helm/Jenkinsfile)
* [localhost:9002](http://localhost:9002) (Staging)
* [localhost:9003](http://localhost:9003) (Production)
* [Jenkinsfile](applications/petclinic/fluxv1/helm/Jenkinsfile) for `helm` deployment
* [localhost:9003](http://localhost:9003) (Staging)
* [localhost:9004](http://localhost:9004) (Production)

##### 3rd Party app (NGINX) via Flux V1

* [Jenkinsfile](applications/nginx/fluxv1/Jenkinsfile)
* [localhost:9002](http://localhost:9004) (Staging)
* [localhost:9003](http://localhost:9005) (Production)
* [localhost:9005](http://localhost:9005) (Staging)
* [localhost:9006](http://localhost:9006) (Production)

##### PetClinic via Flux V2

Expand All @@ -123,3 +125,31 @@ Login with `admin/admin`
## Remove apps from cluster

[`scripts/destroy.sh`](scripts/destroy.sh)

# Options

## Multiple stages
##### This feature is currently only useable for the plain petclinic with fluxv1

You can add additional stages in this [Jenkinsfile](applications/petclinic/fluxv1/plain-k8s/Jenkinsfile) for
the plain-k8s petclinic version with fluxv1.

Look for the `gitopsConfig` map and edit the following entry:

```
stages: [
staging: [ deployDirectly: true ],
production: [ deployDirectly: false ],
qa: [ ]
]
```

Just add another stage and define its deploy behaviour by setting `deployDirectly` to `true` or `false`.
The default is `false` so you can leave it empty like `qa: [ ]`.

If set to `true` the changes will deploy automatically when pushed to the gitops repository.
If set to `false` a pull request is created.

After adding a new stage you need to also create k8s-files in the corresponding folder.
So for the stage `qa` there have to be k8s-files in the following folder [`applications/petclinic/fluxv1/plain-k8s/k8s/qa`](applications/petclinic/fluxv1/plain-k8s/k8s/qa)

2 changes: 1 addition & 1 deletion applications/nginx/fluxv1/k8s/values-production.yaml
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
namespace: fluxv1-production
service:
port: 9005
port: 9006
2 changes: 1 addition & 1 deletion applications/nginx/fluxv1/k8s/values-staging.yaml
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
namespace: fluxv1-staging
service:
port: 9004
port: 9005
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
service:
port: 9003
port: 9004
2 changes: 1 addition & 1 deletion applications/petclinic/fluxv1/helm/k8s/values-staging.yaml
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
service:
port: 9002
port: 9003
44 changes: 31 additions & 13 deletions applications/petclinic/fluxv1/plain-k8s/Jenkinsfile
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,16 @@ node {
[ deploymentFilename: "deployment.yaml",
containerName: "spring-petclinic-plain",
imageName: imageName ]
],
// stages consists of [ $branchName: [ $deployConfig ]]
// where $deployConfig is an array which consists of $deployDirectly
//
// deployDirectly: true -> deploys directly
// deployDirectly: false -> creates a PR (default)
stages: [
staging: [ deployDirectly: true ],
production: [ deployDirectly: false ],
qa: [ ]
]
]

Expand Down Expand Up @@ -131,17 +141,25 @@ String pushToConfigRepo(Map gitopsConfig) {
git url: gitopsConfig.scmmConfigRepoUrl, branch: mainBranch, changelog: false, poll: false
git.fetch()

def repoChanges = new HashSet<String>()
repoChanges += createApplicationForStageAndPushToBranch 'staging', mainBranch, applicationRepo, git, gitopsConfig

git.checkoutOrCreate(application)
repoChanges += createApplicationForStageAndPushToBranch 'production', application, applicationRepo, git, gitopsConfig

changesOnGitOpsRepo = aggregateChangesOnGitOpsRepo(repoChanges)

if (changesOnGitOpsRepo) {
createPullRequest(gitopsConfig)
def allRepoChanges = new HashSet<String>()

gitopsConfig.stages.each{ stage, config ->
//checkout the main_branch before creating a new stage_branch. so it won't be branched off of an already checked out stage_branch
git.checkoutOrCreate(mainBranch)

if(config.deployDirectly) {
allRepoChanges += createApplicationForStageAndPushToBranch stage as String, mainBranch, applicationRepo, git, gitopsConfig
} else {
String stageBranch = "${stage}_${application}"
git.checkoutOrCreate(stageBranch)
String repoChanges = createApplicationForStageAndPushToBranch stage as String, stageBranch, applicationRepo, git, gitopsConfig
if(repoChanges) {
createPullRequest(gitopsConfig, stage as String, stageBranch)
allRepoChanges += repoChanges
}
}
}
changesOnGitOpsRepo = aggregateChangesOnGitOpsRepo(allRepoChanges)
}
} finally {
sh "rm -rf ${configRepoTempDir}"
Expand All @@ -152,7 +170,7 @@ String pushToConfigRepo(Map gitopsConfig) {

String createApplicationForStageAndPushToBranch(String stage, String branch, GitRepo applicationRepo, def git, Map gitopsConfig) {

String commitPrefix = stage == 'staging' ? '[S] ' : ''
String commitPrefix = "[${stage}] "

sh "mkdir -p ${stage}/${application}/"
sh "mkdir -p ${configDir}/"
Expand Down Expand Up @@ -200,15 +218,15 @@ String createApplicationCommitMessage(def git, def applicationRepo) {
return message
}

void createPullRequest(Map gitopsConfig) {
void createPullRequest(Map gitopsConfig, String stage, String sourceBranch) {

withCredentials([usernamePassword(credentialsId: gitopsConfig.scmmCredentialsId, passwordVariable: 'GIT_PASSWORD', usernameVariable: 'GIT_USER')]) {

String script =
'curl -s -o /dev/null -w "%{http_code}" ' +
"-u ${GIT_USER}:${GIT_PASSWORD} " +
'-H "Content-Type: application/vnd.scmm-pullRequest+json;v=2" ' +
'--data \'{"title": "created by service ' + application + '", "source": "' + application + '", "target": "' + mainBranch + '"}\' ' +
'--data \'{"title": "created by service ' + application + ' for stage ' + stage + '", "source": "' + sourceBranch + '", "target": "' + mainBranch + '"}\' ' +
gitopsConfig.scmmPullRequestUrl

// For debugging the quotation of the shell script, just do: echo script
Expand Down
21 changes: 21 additions & 0 deletions applications/petclinic/fluxv1/plain-k8s/k8s/qa/deployment.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
apiVersion: apps/v1
kind: Deployment
metadata:
name: spring-petclinic-plain
namespace: fluxv1-qa
spec:
replicas: 1
selector:
matchLabels:
app: spring-petclinic-plain
template:
metadata:
labels:
app: spring-petclinic-plain
spec:
containers:
- name: spring-petclinic-plain
image: localhost:8999/petclinic-plain:1
ports:
- containerPort: 8080
name: http
15 changes: 15 additions & 0 deletions applications/petclinic/fluxv1/plain-k8s/k8s/qa/service.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
apiVersion: v1
kind: Service
metadata:
namespace: fluxv1-qa
name: spring-petclinic-plain
labels:
app: spring-petclinic-plain
spec:
type: LoadBalancer
ports:
- name: http
port: 9002
targetPort: http
selector:
app: spring-petclinic-plain
4 changes: 4 additions & 0 deletions k8s-namespaces/fluxv1-qa.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
apiVersion: v1
kind: Namespace
metadata:
name: fluxv1-qa
11 changes: 7 additions & 4 deletions scripts/apply.sh
Original file line number Diff line number Diff line change
Expand Up @@ -296,10 +296,13 @@ function printWelcomeScreen() {
echo "| This may take a minute for the GitOps operator to sync. |"
echo "| |"
echo "| Flux V1 applications: |"
echo -e "| \e[32mhttp://localhost:9000/\e[0m for Flux V1 petclinic for staging |"
echo -e "| \e[32mhttp://localhost:9001/\e[0m for Flux V1 petclinic for production |"
echo -e "| \e[32mhttp://localhost:9002/\e[0m for Flux V1 nginx for staging |"
echo -e "| \e[32mhttp://localhost:9003/\e[0m for Flux V1 nginx for production |"
echo -e "| \e[32mhttp://localhost:9000/\e[0m for Flux V1 petclinic plain for staging |"
echo -e "| \e[32mhttp://localhost:9001/\e[0m for Flux V1 petclinic plain for production |"
echo -e "| \e[32mhttp://localhost:9002/\e[0m for Flux V1 petclinic plain for qa |"
echo -e "| \e[32mhttp://localhost:9003/\e[0m for Flux V1 petclinic helm for staging |"
echo -e "| \e[32mhttp://localhost:9004/\e[0m for Flux V1 petclinic helm for production |"
echo -e "| \e[32mhttp://localhost:9005/\e[0m for Flux V1 nginx for staging |"
echo -e "| \e[32mhttp://localhost:9006/\e[0m for Flux V1 nginx for production |"
echo "| |"
echo "| Flux V2 applications: |"
echo -e "| \e[32mhttp://localhost:9010/\e[0m for Flux V2 petclinic for staging |"
Expand Down

0 comments on commit fcbee15

Please sign in to comment.