Repository navigation
Transformation to Templates
In Starting Point: A Basic App, you build your basic app in OpenShift. You did this using the oc new-build with Source-To-Image S2I. You may have also used the console to make some other changes. The broader objective here is we are building Cloud Natives Apps. Here I'll focus on "configuration as code"--the entire configuration is managed in code; Well, with declarative YAML. In OpenShift configuration is declared in YAML files. Every thing in your app can be described in a YAML file.
- Go trough the process of taking your app running in OpenShift and transform it's configuration to a YAML manifest that can be applied repeatedly.
- We will next have you convert your manifest into a template. You will use this template to recreate your entire application, everything, with a single command. With your template you will be able to easily pass variable when you
processyour template. This allows you to vary any part of your deployment.
A template describes a set of objects that can be parameterized and processed to produce a list of objects for creation by OpenShift Container Platform. A template can be processed to create anything you have permission to create within a project, for example services, build configurations, and deployment configurations. A template may also define a set of labels to apply to every object defined in the template.
This process exports the declarative configuration of your complete application.
We'll add your manifest to an openshift folder.
mkdir openshift
cd openshift
oc -n myproject \
get all \
-l app=demo-app \
--export \
-o yaml > demo-app.yamlNote: all isn't always all, strictly. There are times i've had to specify other objects like configmap,route,secrets
Now that you have this, you should be able to delete your application with
oc delete ... and recreate it with this manifest using oc apply -f ...
Delete your app
oc delete all -l app=demo-app
pod "demo-app-1-6hgrb" deleted
replicationcontroller "demo-app-1" deleted
service "demo-app" deleted
deploymentconfig.apps.openshift.io "demo-app" deleted
buildconfig.build.openshift.io "demo-app" deleted
build.build.openshift.io "demo-app-1" deleted
imagestream.image.openshift.io "demo-app" deleted
route.route.openshift.io "demo-app" deletedRecreate it
oc apply -f demo-app.yaml
service/demo-app created
deploymentconfig.apps.openshift.io/demo-app created
buildconfig.build.openshift.io/demo-app created
build.build.openshift.io/demo-app-1 created
imagestream.image.openshift.io/demo-app created
route.route.openshift.io/demo-app created
Error from server (Forbidden): pods "demo-app-1-6hgrb" is forbidden: cannot set blockOwnerDeletion if an ownerReference refers to a resource you can't set finalizers on: no RBAC policy matched, <nil>
Error from server (Forbidden): replicationcontrollers "demo-app-1" is forbidden: cannot set blockOwnerDeletion if an ownerReference refers to a resource you can't set finalizers on: no RBAC policy matched, <nil>As you see there will be errors.
There are some object that should be removed from the manifest.
- Delete the
PodandReplicationControllerandBuild(not theBuildConfig)
oc apply -f myApp.yaml
build.build.openshift.io/demo-app-1 created
Error from server (Invalid): Service "demo-app" is invalid: metadata.uid: Invalid value: "6e90d15f-9e7e-11e9-85ef-b652f53a8e6e": field is immutable
Error from server (Conflict): Operation cannot be fulfilled on deploymentconfigs.apps.openshift.io "demo-app": the object has been modified; please apply your changes to the latest version and try again
Error from server (Conflict): Operation cannot be fulfilled on buildconfigs.build.openshift.io "demo-app": the object has been modified; please apply your changes to the latest version and try again
Error from server (Conflict): Operation cannot be fulfilled on imagestreams.image.openshift.io "demo-app": the object has been modified; please apply your changes to the latest version and try again
Error from server (Conflict): Operation cannot be fulfilled on routes.route.openshift.io "demo-app": the object has been modified; please apply your changes to the latest version and try againThis caught me: Above I ran oc apply without deleting the previous state. Delete everything again before trying to apply again.
oc delete all -l app=demo-app
service "demo-app" deleted
deploymentconfig.apps.openshift.io "demo-app" deleted
buildconfig.build.openshift.io "demo-app" deleted
imagestream.image.openshift.io "demo-app" deleted
route.route.openshift.io "demo-app" deletedoc apply -f demo-app.yaml
service/demo-app created
deploymentconfig.apps.openshift.io/demo-app created
buildconfig.build.openshift.io/demo-app created
imagestream.image.openshift.io/demo-app created
route.route.openshift.io/demo-app createdoc apply -f demo-app.yaml
build.build.openshift.io/demo-app-1 created
Error from server (Invalid): Service "demo-app" is invalid: metadata.uid: Invalid value: "1bad74a5-9e83-11e9-85ef-b652f53a8e6e": field is immutable
Error from server (Conflict): Operation cannot be fulfilled on deploymentconfigs.apps.openshift.io "demo-app": the object has been modified; please apply your changes to the latest version and try again
Error from server (Conflict): Operation cannot be fulfilled on buildconfigs.build.openshift.io "demo-app": the object has been modified; please apply your changes to the latest version and try again
Error from server (Conflict): Operation cannot be fulfilled on imagestreams.image.openshift.io "demo-app": the object has been modified; please apply your changes to the latest version and try again
Error from server (Conflict): Operation cannot be fulfilled on routes.route.openshift.io "demo-app": the object has been modified; please apply your changes to the latest version and try againThe process here is to to run oc apply -f demo-app.yaml repeatedly and review the output removing things that generate issues;
You'll find there's still too much "specific" detail in the manifest.
Edit the manifest to make it more "generic"
- last applied configuration annotations (just to keep the file clean)
- creationTimestamp
- generation
- clusterIP
- namespace
- resourceVersion
- selflink
- uid
- status
Remove the objects again and apply the manifest more than once (this should succeed with unchanged showing up when no changes are made)
oc delete all -l app=demo-app
pod "demo-app-1-bw7kq" deleted
replicationcontroller "demo-app-1" deleted
service "demo-app" deleted
deploymentconfig.apps.openshift.io "demo-app" deleted
buildconfig.build.openshift.io "demo-app" deleted
build.build.openshift.io "demo-app-1" deleted
imagestream.image.openshift.io "demo-app" deleted
route.route.openshift.io "demo-app" deleted
oc apply -f demo-app.yaml
service/demo-app created
deploymentconfig.apps.openshift.io/demo-app created
buildconfig.build.openshift.io/demo-app created
imagestream.image.openshift.io/demo-app created
route.route.openshift.io/demo-app created
oc apply -f demo-app.yaml
service/demo-app created
deploymentconfig.apps.openshift.io/demo-app created
buildconfig.build.openshift.io/demo-app created
imagestream.image.openshift.io/demo-app created
route.route.openshift.io/demo-app created| Tag | Comment |
|---|---|
| v2.0.0 | Extracted a manifest of the App and made it generic |
Here's where the real work is: convert this manifest to a template.
The following code can be used as an example of a template: (it's taken from the OpenShift 201 training)
apiVersion: template.openshift.io/v1
kind: Template
labels:
template: grafana-template
message: |-
The following service(s) have been created in your project: ${GRAFANA_SERVICE_NAME}.
For more information about using this template, including OpenShift considerations, contact the rocketchat community.
metadata:
annotations:
description: |-
Grafana Template for use in OpenShift 201 lab without persistent storage.
WARNING: Any configuration stored will be lost upon pod destruction. ConfigMaps should be used for codified configuration.
iconClass: icon-other-unknown
openshift.io/display-name: grafana-openshift201
openshift.io/documentation-url: https://github.com/bcdevops/devops-platform-workshops
openshift.io/long-description: This template provides a sample Grafana configuration for lab purposes.
openshift.io/provider-display-name: BCDevOps
openshift.io/support-url: https://github.com/bcdevops/
tags: monitoring,grafana
name: grafana-template
parameters:
- description: Maximum amount of memory the container can use.
displayName: Memory Limit
name: MEMORY_LIMIT
required: true
value: 512Mi
- description: The namespace this templated is deployed into.
displayName: Namespace
name: NAMESPACE
value: openshift
- description: The name of the OpenShift Service exposed for the database.
displayName: Grafana Service Name
name: GRAFANA_SERVICE_NAME
required: true
value: grafana
- description: The name of the Loki service to connect to.
displayName: Loki Service Name
name: LOKI_SERVICE_NAME
required: true
value: loki
- description: The name of the Prometheus service to connect to.
displayName: Prometheus Service Name
name: PROMETHEUS_SERVICE_NAME
required: true
value: prometheus
- description: Default route subdomain
displayName: Route subdomain
name: ROUTE_SUBDOMAIN
required: true
value: pathfinder.gov.bc.ca
objects:
- apiVersion: v1
kind: Service
metadata:
annotations:
openshift.io/generated-by: OpenShiftNewApp
labels:
app: ${GRAFANA_SERVICE_NAME}
name: ${GRAFANA_SERVICE_NAME}
spec:
ports:
- name: 3000-tcp
port: 3000
protocol: TCP
targetPort: 3000
selector:
app: ${GRAFANA_SERVICE_NAME}
deploymentconfig: ${GRAFANA_SERVICE_NAME}
sessionAffinity: None
type: ClusterIP
- apiVersion: apps.openshift.io/v1
kind: DeploymentConfig
metadata:
annotations:
openshift.io/generated-by: OpenShiftNewApp
labels:
app: ${GRAFANA_SERVICE_NAME}
name: ${GRAFANA_SERVICE_NAME}
spec:
replicas: 1
revisionHistoryLimit: 10
selector:
app: ${GRAFANA_SERVICE_NAME}
deploymentconfig: ${GRAFANA_SERVICE_NAME}
strategy:
activeDeadlineSeconds: 21600
resources: {}
rollingParams:
intervalSeconds: 1
maxSurge: 25%
maxUnavailable: 25%
timeoutSeconds: 600
updatePeriodSeconds: 1
type: Rolling
template:
metadata:
annotations:
openshift.io/generated-by: OpenShiftNewApp
labels:
app: ${GRAFANA_SERVICE_NAME}
deploymentconfig: ${GRAFANA_SERVICE_NAME}
spec:
containers:
- image: grafana/grafana:v6.2.0
imagePullPolicy: IfNotPresent
name: ${GRAFANA_SERVICE_NAME}
ports:
- containerPort: 3000
protocol: TCP
resources: {}
terminationMessagePath: /dev/termination-log
terminationMessagePolicy: File
volumeMounts:
- mountPath: /etc/grafana/provisioning/dashboards/
name: volume-bfku7
- mountPath: /etc/grafana/provisioning/datasources/
name: volume-ttg2w
- mountPath: /var/lib/grafana/dashboards/
name: volume-y3co5
dnsPolicy: ClusterFirst
restartPolicy: Always
schedulerName: default-scheduler
securityContext: {}
terminationGracePeriodSeconds: 30
volumes:
- configMap:
defaultMode: 420
name: ${GRAFANA_SERVICE_NAME}-dashboard-providers
name: volume-bfku7
- configMap:
defaultMode: 420
name: ${GRAFANA_SERVICE_NAME}-datasources
name: volume-ttg2w
- configMap:
defaultMode: 420
name: ${GRAFANA_SERVICE_NAME}-dashboards
name: volume-y3co5
test: false
triggers:
- type: ConfigChange
- imageChangeParams:
automatic: true
containerNames:
- ${GRAFANA_SERVICE_NAME}
from:
kind: ImageStreamTag
name: ${GRAFANA_SERVICE_NAME}:6.2.0
namespace: ${NAMESPACE}
type: ImageChange
- apiVersion: image.openshift.io/v1
kind: ImageStream
metadata:
annotations:
openshift.io/generated-by: OpenShiftNewApp
openshift.io/image.dockerRepositoryCheck: 2019-05-26T17:34:32Z
generation: 2
labels:
app: ${GRAFANA_SERVICE_NAME}
name: ${GRAFANA_SERVICE_NAME}
spec:
lookupPolicy:
local: false
tags:
- annotations:
openshift.io/imported-from: grafana/grafana:6.2.0
from:
kind: DockerImage
name: grafana/grafana:6.2.0
generation: 2
importPolicy: {}
name: 6.2.0
referencePolicy:
type: Source
- apiVersion: route.openshift.io/v1
kind: Route
metadata:
annotations:
openshift.io/host.generated: "true"
labels:
app: ${GRAFANA_SERVICE_NAME}
name: ${GRAFANA_SERVICE_NAME}
spec:
host: ${GRAFANA_SERVICE_NAME}-${NAMESPACE}.${ROUTE_SUBDOMAIN}
port:
targetPort: 3000-tcp
to:
kind: Service
name: ${GRAFANA_SERVICE_NAME}
weight: 100
wildcardPolicy: None
- apiVersion: v1
data:
dashboards.yml: "apiVersion: 1\n\nproviders:\n # <string> provider name\n- name:
'default'\n # <int> org id. will default to orgId 1 if not specified\n orgId:
1\n # <string, required> name of the dashboard folder. Required\n folder:
''\n # <string> folder UID. will be automatically generated if not specified\n
\ folderUid: ''\n # <string, required> provider type. Required\n type: file\n
\ # <bool> disable dashboard deletion\n disableDeletion: false\n # <bool>
enable dashboard editing\n editable: true\n # <int> how often Grafana will
scan for changed dashboards\n updateIntervalSeconds: 10 \n options:\n #
<string, required> path to dashboard files on disk. Required\n path: /var/lib/grafana/dashboards"
kind: ConfigMap
metadata:
labels:
app: ${GRAFANA_SERVICE_NAME}
name: ${GRAFANA_SERVICE_NAME}-dashboard-providers
- apiVersion: v1
data:
simple_dashboard.json: |-
{
"annotations": {
"list": [
{
"builtIn": 1,
"datasource": "-- Grafana --",
"enable": true,
"hide": true,
"iconColor": "rgba(0, 211, 255, 1)",
"name": "Annotations & Alerts",
"type": "dashboard"
}
]
},
"editable": true,
"gnetId": null,
"graphTooltip": 0,
"id": 1,
"links": [],
"panels": [
{
"cacheTimeout": null,
"colorBackground": false,
"colorValue": false,
"colors": [
"#299c46",
"rgba(237, 129, 40, 0.89)",
"#d44a3a"
],
"format": "none",
"gauge": {
"maxValue": 100,
"minValue": 0,
"show": false,
"thresholdLabels": false,
"thresholdMarkers": true
},
"gridPos": {
"h": 9,
"w": 12,
"x": 0,
"y": 0
},
"id": 2,
"interval": null,
"links": [],
"mappingType": 1,
"mappingTypes": [
{
"name": "value to text",
"value": 1
},
{
"name": "range to text",
"value": 2
}
],
"maxDataPoints": 100,
"nullPointMode": "connected",
"nullText": null,
"options": {},
"postfix": "",
"postfixFontSize": "50%",
"prefix": "",
"prefixFontSize": "50%",
"rangeMaps": [
{
"from": "null",
"text": "N/A",
"to": "null"
}
],
"sparkline": {
"fillColor": "rgba(31, 118, 189, 0.18)",
"full": false,
"lineColor": "rgb(31, 120, 193)",
"show": false
},
"tableColumn": "",
"targets": [
{
"expr": "sum(up)\n",
"format": "time_series",
"intervalFactor": 1,
"refId": "A"
}
],
"thresholds": "",
"timeFrom": null,
"timeShift": null,
"title": "Simple Panel",
"type": "singlestat",
"valueFontSize": "80%",
"valueMaps": [
{
"op": "=",
"text": "N/A",
"value": "null"
}
],
"valueName": "current"
}
],
"schemaVersion": 18,
"style": "dark",
"tags": [],
"templating": {
"list": []
},
"time": {
"from": "now-6h",
"to": "now"
},
"timepicker": {
"refresh_intervals": [
"5s",
"10s",
"30s",
"1m",
"5m",
"15m",
"30m",
"1h",
"2h",
"1d"
],
"time_options": [
"5m",
"15m",
"1h",
"6h",
"12h",
"24h",
"2d",
"7d",
"30d"
]
},
"timezone": "",
"title": "Simple Dashboard",
"uid": "ybk9TjWZk",
"version": 1
}
kind: ConfigMap
metadata:
labels:
app: ${GRAFANA_SERVICE_NAME}
name: ${GRAFANA_SERVICE_NAME}-dashboards
- apiVersion: v1
data:
datasource.yml: "# config file version\napiVersion: 1\n\ndatasources:\n- name:
Prometheus\n type: prometheus\n access: proxy\n orgId: 1\n url: http://${PROMETHEUS_SERVICE_NAME}:80
\n isDefault: true\n version: 1\n editable: true\n- name: Loki\n type: loki\n
\ orgId: 1\n access: proxy\n url: http://${LOKI_SERVICE_NAME}:3100 \n jsonData:\n
\ maxLines: 1000"
kind: ConfigMap
metadata:
labels:
app: ${GRAFANA_SERVICE_NAME}
name: ${GRAFANA_SERVICE_NAME}-datasourcesWhen you have successfully created your template you will be able to process it, setting the variables as you choose. (Again the following is taken from the Openshift 201 Training) If you were to process and apply the above template example:
oc create -f your-template-file.yaml
oc process grafana-template \
-p GRAFANA_SERVICE_NAME=[grafana app name] \
-p LOKI_SERVICE_NAME=[loki-service] \
-p PROMETHEUS_SERVICE_NAME=[prometheus-service] \
-p ROUTE_SUBDOMAIN=pathfinder.gov.bc.ca \
-p NAMESPACE=[dev namespace] \
| oc apply -f -| Tag | Comment |
|---|---|
| v2.0.1 | Create an initial template from yaml manifest |
You now have transformed your running app, in OpenShift into a reusable template. You can now easily create versions of your app changing what you want.
Continue to the next part: Using the bcdk: A Jenkins Pipeline That is PR Triggerred