Skip to content

Commit

Permalink
Merge pull request redhat-cop#13 from pabrahamsson/openshift-applier
Browse files Browse the repository at this point in the history
Add support for OpenShift applier
  • Loading branch information
etsauer committed Dec 8, 2017
2 parents 7c0a549 + 3e4fef4 commit 796948b
Show file tree
Hide file tree
Showing 31 changed files with 220 additions and 203 deletions.
78 changes: 18 additions & 60 deletions basic-tomcat/README.md
Expand Up @@ -10,33 +10,36 @@ This example demonstrates how to implement a full end-to-end Jenkins Pipeline fo

## Quickstart

### Requirements
1. [OpenShift Applier](https://github.com/redhat-cop/casl-ansible)
`git clone git@github.com:redhat-cop/casl-ansible.git`
`git checkout v3.6.1`
2. [Ansible](https://www.ansible.com/)
`sudo dnf install ansible`

### Installation
Run the following commands to instantiate this example.

```
cd ./basic-tomcat
oc create -f projects/projects.yml
oc process openshift//jenkins-ephemeral | oc apply -f- -n basic-tomcat-dev
oc process -f deploy/basic-tomcat-template.yml --param-file=deploy/dev/params | oc apply -f-
oc process -f deploy/basic-tomcat-template.yml --param-file=deploy/stage/params | oc apply -f-
oc process -f deploy/basic-tomcat-template.yml --param-file=deploy/prod/params | oc apply -f-
oc process -f build/basic-java-template.yml --param-file build/dev/params | oc apply -f-
ansible-playbook -i inventory/hosts ../casl-ansible/playbooks/openshift-cluster-seed.yml --connection=local
```
The above command will create all the necessary projects and OpenShift objects as well as a Jenkins instance that will build, promote and deploy the application.
Run the following commands to instantiate this example.

## Architecture

### OpenShift Templates

The components of this pipeline are divided into two templates.

The first template, `build/basic-tomcat-template.yml` is what we are calling the "Build" template. It contains:
The first template, `files/builds/template.yml` is what we are calling the "Build" template. It contains:

* A `jenkinsPipelineStrategy` BuildConfig
* An `s2i` BuildConfig
* An ImageStream for the s2i build config to push to

The build template contains a default source code repo for a java application compatible with this pipelines architecture (https://github.com/etsauer/ticket-monster).

The second template, `deploy/basic-tomcat-template.yml` is the "Deploy" template. It contains:
The second template, `files/deployment/template.yml` is the "Deploy" template. It contains:

* A tomcat8 DeploymentConfig
* A Service definition
Expand Down Expand Up @@ -68,32 +71,16 @@ https://github.com/etsauer/ticket-monster

For the purposes of this demo, we are going to create three stages for our application to be promoted through.

- `basic-tomcat-build`
- `basic-tomcat-dev`
- `basic-tomcat-stage`
- `basic-tomcat-prod`

In the spirit of _Infrastructure as Code_ we have a YAML file that defines the `ProjectRequests` for us. This is as an alternative to running `oc new-project`, but will yeild the same result.

```
$ oc create -f projects/projects.yml
projectrequest "basic-tomcat-dev" created
projectrequest "basic-tomcat-stage" created
projectrequest "basic-tomcat-prod" created
```
### 2. Stand up Jenkins master in build

### 2. Stand up Jenkins master in dev

For this step, the OpenShift default template set provides exactly what we need to get jenkins up and running.

```
$ oc process openshift//jenkins-ephemeral | oc apply -f- -n basic-tomcat-dev
route "jenkins" created
deploymentconfig "jenkins" created
serviceaccount "jenkins" created
rolebinding "jenkins_edit" created
service "jenkins-jnlp" created
service "jenkins" created
```
For this step, the OpenShift default template set provides exactly what we need to get jenkins up and running. Jenkins will be running in the `build` project and promote and deploy to the `basic-tomcat-dev` project.

### 4. Instantiate Pipeline

Expand All @@ -107,46 +94,17 @@ A _deploy template_ is provided at `deploy/basic-tomcat-template.yml` that defin

This template should be instantiated once in each of the namespaces that our app will be deployed to. For this purpose, we have created a param file to be fed to `oc process` to customize the template for each environment.

Deploy the deployment template to all three projects.
```
$ oc process -f deploy/basic-tomcat-template.yml --param-file=deploy/dev/params | oc apply -f-
service "basic-tomcat" created
route "basic-tomcat" created
imagestream "basic-tomcat" created
deploymentconfig "basic-tomcat" created
rolebinding "jenkins_edit" configured
$ oc process -f deploy/basic-tomcat-template.yml --param-file=deploy/stage/params | oc apply -f-
service "basic-tomcat" created
route "basic-tomcat" created
imagestream "basic-tomcat" created
deploymentconfig "basic-tomcat" created
rolebinding "jenkins_edit" created
$ oc process -f deploy/basic-tomcat-template.yml --param-file=deploy/prod/params | oc apply -f-
service "basic-tomcat" created
route "basic-tomcat" created
imagestream "basic-tomcat" created
deploymentconfig "basic-tomcat" created
rolebinding "jenkins_edit" created
```

A _build template_ is provided at `build/basic-java-template.yml` that defines all the resources required to build our java app. It includes:
A _build template_ is provided at `files/builds/template.yml` that defines all the resources required to build our java app. It includes:

* A `BuildConfig` that defines a `JenkinsPipelineStrategy` build, which will be used to define out pipeline.
* A `BuildConfig` that defines a `Source` build with `Binary` input. This will build our image.

Deploy the pipeline template in dev only.
```
$ oc process -f build/basic-java-template.yml --param-file build/dev/params | oc apply -f-
buildconfig "basic-tomcat-pipeline" created
buildconfig "basic-tomcat" created
```

At this point you should be able to go to the Web Console and follow the pipeline by clicking in your `myapp-dev` project, and going to *Builds* -> *Pipelines*. At several points you will be prompted for input on the pipeline. You can interact with it by clicking on the _input required_ link, which takes you to Jenkins, where you can click the *Proceed* button. By the time you get through the end of the pipeline you should be able to visit the Route for your app deployed to the `myapp-prod` project to confirm that your image has been promoted through all stages.

## Cleanup

Cleaning up this example is as simple as deleting the projects we created at the beginning.

```
oc delete project basic-tomcat-dev basic-tomcat-prod basic-tomcat-stage
oc delete project basic-tomcat-build basic-tomcat-dev basic-tomcat-prod basic-tomcat-stage
```
3 changes: 0 additions & 3 deletions basic-tomcat/build/dev/params

This file was deleted.

3 changes: 0 additions & 3 deletions basic-tomcat/deploy/dev/params

This file was deleted.

3 changes: 0 additions & 3 deletions basic-tomcat/deploy/prod/params

This file was deleted.

4 changes: 0 additions & 4 deletions basic-tomcat/deploy/stage/params

This file was deleted.

3 changes: 3 additions & 0 deletions basic-tomcat/files/builds/params
@@ -0,0 +1,3 @@
NAMESPACE=basic-tomcat-build
SOURCE_REPOSITORY_URL=https://github.com/pabrahamsson/ticket-monster.git
SOURCE_REPOSITORY_REF=build
Expand Up @@ -44,6 +44,7 @@ objects:
name: ${APPLICATION_NAME}
namespace: "${NAMESPACE}"
spec:
failedBuildHistoryLimit: 3
output:
to:
kind: ImageStreamTag
Expand All @@ -58,6 +59,14 @@ objects:
name: ${IMAGE_STREAM_TAG_NAME}
namespace: ${IMAGE_STREAM_NAMESPACE}
type: Source
successfulBuildHistoryLimit: 3
- apiVersion: v1
kind: ImageStream
metadata:
labels:
application: ${APPLICATION_NAME}
name: ${APPLICATION_NAME}
namespace: ${NAMESPACE}
parameters:
- description: The name for the application.
name: APPLICATION_NAME
Expand Down Expand Up @@ -99,4 +108,4 @@ parameters:
- description: Image stream tag for the image you'd like to use to build the application
name: IMAGE_STREAM_TAG_NAME
required: true
value: jboss-webserver30-tomcat8-openshift:1.2
value: jboss-webserver31-tomcat8-openshift:1.0
1 change: 1 addition & 0 deletions basic-tomcat/files/deployment/build/params
@@ -0,0 +1 @@
MEMORY_LIMIT=512Mi
2 changes: 2 additions & 0 deletions basic-tomcat/files/deployment/dev/params
@@ -0,0 +1,2 @@
APPLICATION_NAME=basic-tomcat
ENV=-dev
2 changes: 2 additions & 0 deletions basic-tomcat/files/deployment/prod/params
@@ -0,0 +1,2 @@
APPLICATION_NAME=basic-tomcat
ENV=-prod
2 changes: 2 additions & 0 deletions basic-tomcat/files/deployment/stage/params
@@ -0,0 +1,2 @@
APPLICATION_NAME=basic-tomcat
ENV=-stage
@@ -1,58 +1,19 @@
apiVersion: v1
kind: Template
labels:
template: jws30-tomcat8-deployment
template: deployment-template
metadata:
annotations:
description: Application template for JWS applications built using a Jenkins Pipeline
iconClass: icon-tomcat
tags: tomcat,tomcat8,java,jboss,xpaas
version: 1.2.0
name: jws30-tomcat8-deployment
description: Application template for applications built using a Jenkins Pipeline
name: deployment-template
objects:
- apiVersion: v1
kind: Service
metadata:
annotations:
description: The web server's http port.
labels:
application: ${APPLICATION_NAME}
name: ${APPLICATION_NAME}
namespace: ${NAMESPACE}
spec:
ports:
- port: 8080
targetPort: 8080
selector:
deploymentConfig: ${APPLICATION_NAME}
- apiVersion: v1
id: ${APPLICATION_NAME}-http
kind: Route
metadata:
annotations:
description: Route for application's http service.
labels:
application: ${APPLICATION_NAME}
name: ${APPLICATION_NAME}
namespace: ${NAMESPACE}
spec:
host: ${HOSTNAME_HTTP}
to:
name: ${APPLICATION_NAME}
- apiVersion: v1
kind: ImageStream
metadata:
labels:
application: ${APPLICATION_NAME}
name: ${APPLICATION_NAME}
namespace: ${NAMESPACE}
- apiVersion: v1
kind: DeploymentConfig
metadata:
labels:
application: ${APPLICATION_NAME}
name: ${APPLICATION_NAME}
namespace: ${NAMESPACE}
namespace: ${APPLICATION_NAME}${ENV}
spec:
replicas: 1
selector:
Expand Down Expand Up @@ -100,6 +61,63 @@ objects:
name: ${APPLICATION_NAME}:latest
type: ImageChange
- type: ConfigChange
- apiVersion: v1
kind: ImageStream
metadata:
labels:
application: ${APPLICATION_NAME}
name: ${APPLICATION_NAME}
namespace: ${APPLICATION_NAME}${ENV}
- apiVersion: v1
id: ${APPLICATION_NAME}-http
kind: Route
metadata:
annotations:
description: Route for application's service.
labels:
application: ${APPLICATION_NAME}
name: ${APPLICATION_NAME}
namespace: ${APPLICATION_NAME}${ENV}
spec:
host: ${HOSTNAME_HTTP}
to:
name: ${APPLICATION_NAME}
- apiVersion: v1
kind: Service
metadata:
annotations:
description: Service for application.
labels:
application: ${APPLICATION_NAME}
name: ${APPLICATION_NAME}
namespace: ${APPLICATION_NAME}${ENV}
spec:
ports:
- name: 8080-tcp
port: 8080
protocol: TCP
targetPort: 8080
selector:
application: ${APPLICATION_NAME}
deploymentConfig: ${APPLICATION_NAME}
- apiVersion: v1
kind: Service
metadata:
annotations:
description: Service for application.
labels:
application: ${APPLICATION_NAME}
name: ${APPLICATION_NAME}
namespace: ${APPLICATION_NAME}${ENV}
spec:
ports:
- name: 8080-tcp
port: 8080
protocol: TCP
targetPort: 8080
selector:
application: ${APPLICATION_NAME}
deploymentConfig: ${APPLICATION_NAME}
- apiVersion: v1
groupNames: null
kind: RoleBinding
Expand All @@ -108,7 +126,7 @@ objects:
labels:
template: basic-tomcat-template
name: jenkins_edit
namespace: ${NAMESPACE}
namespace: ${APPLICATION_NAME}${ENV}
roleRef:
name: edit
subjects:
Expand All @@ -122,8 +140,8 @@ parameters:
name: APPLICATION_NAME
required: true
value: jws-app
- description: The namespace to deploy into
name: NAMESPACE
- description: The environment
name: ENV
required: true
- description: Name of a service account that can deploy to this project
name: SA_NAME
Expand All @@ -132,6 +150,7 @@ parameters:
- description: Namespace of service account that can deploy to this project
name: SA_NAMESPACE
required: true
value: basic-tomcat-build
- description: 'Custom hostname for http service route. Leave blank for default hostname,
e.g.: <application-name>-<project>.<default-domain-suffix>'
name: HOSTNAME_HTTP
Expand Down
Expand Up @@ -19,3 +19,9 @@ items:
name: basic-tomcat-prod
creationTimestam: null
displayName: Prod - Basic Tomcat App
- kind: ProjectRequest
apiVersion: v1
metadata:
name: basic-tomcat-build
creationTimestam: null
displayName: Build - Jenkins is here
27 changes: 27 additions & 0 deletions basic-tomcat/inventory/group_vars/all.yml
@@ -0,0 +1,27 @@
---
openshift_cluster_content:
- object: projectrequest
content:
- name: basic-tomcat-spaces
file: "{{ inventory_dir }}/../files/projects/projects.yml"
file_action: create
- object: deployments
content:
- name: jenkins
namespace: basic-tomcat-build
template: openshift//jenkins-ephemeral
params: "{{ inventory_dir }}/../files/deployment/build/params"
- name: basic-tomcat-dev
template: "{{ inventory_dir }}/../files/deployment/template.yml"
params: "{{ inventory_dir }}/../files/deployment/dev/params"
- name: basic-tomcat-stage
template: "{{ inventory_dir }}/../files/deployment/template.yml"
params: "{{ inventory_dir }}/../files/deployment/stage/params"
- name: basic-tomcat-prod
template: "{{ inventory_dir }}/../files/deployment/template.yml"
params: "{{ inventory_dir }}/../files/deployment/prod/params"
- object: builds
content:
- name: jenkins
template: "{{ inventory_dir }}/../files/builds/template.yml"
params: "{{ inventory_dir }}/../files/builds/params"
2 changes: 2 additions & 0 deletions basic-tomcat/inventory/hosts
@@ -0,0 +1,2 @@
[seed-hosts]
localhost

0 comments on commit 796948b

Please sign in to comment.