Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save jdufresne-ebsco/a14f60d0a3cb3bf0ebe17709f7b2d601 to your computer and use it in GitHub Desktop.
Save jdufresne-ebsco/a14f60d0a3cb3bf0ebe17709f7b2d601 to your computer and use it in GitHub Desktop.
k8s-mesh-component-rollback-example.groovy
@Library('medusa@master') _
node('docker') {
deleteDir()
def applicationDefinition = [
Pipeline: [
AssemblyLine: 'sandbox'
]
]
def assemblyLine = getAssemblyLine applicationDefinition: applicationDefinition
def environmentDefinition = getEnvironmentDefinition environmentId: assemblyLine[0]
def kubeApplyFile = 'grafana-temp.yaml'
_writeApplyFile(kubeApplyFile)
// Return a list of k8s object types that are being applied
def types = _getApplyTypes(kubeApplyFile)
withHydra environmentDefinition: environmentDefinition, block: {
def rollbackFile = 'grafana-temp-rollback.yaml'
// Write each k8s object type to a file with yaml document separator for potential roll back
types.each { type ->
sh "echo '---' >> ${rollbackFile}"
sh "kubectl apply view-last-applied ${type} grafana-temp -n monitoring -o yaml >> ${rollbackFile}"
}
echo "Deployment"
sh "kubectl apply -n monitoring -f ${kubeApplyFile}"
sh 'sleep 10s' //sleep in order to show before and after in cluster
echo "ROLLBACK"
sh "kubectl apply -n monitoring -f ${rollbackFile}"
}
}
def _getApplyTypes(file) {
def types = []
def kubeApplyYaml = readYaml file: file
kubeApplyYaml.each { kubeObj ->
types.add(kubeObj.kind)
}
return types
}
def _writeApplyFile(file) {
writeFile file: file, text: """---
apiVersion: v1
kind: Service
metadata:
annotations:
prometheus.io/scrape: 'true'
service.beta.kubernetes.io/aws-load-balancer-internal: 0.0.0.0/0
external-dns.alpha.kubernetes.io/hostname: _COMPONENT_DNS_NAME_
name: grafana-temp
namespace: monitoring
labels:
app: grafana-temp
spec:
type: LoadBalancer
ports:
- port: 3000
protocol: TCP
name: http
selector:
app: grafana-temp
---
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
annotations:
configmap.reloader.stakater.com/reload: "kubernetes-cluster-monitoring-dashboard"
name: grafana-temp
namespace: monitoring
spec:
replicas: 2
selector:
matchLabels:
app: grafana-temp
template:
metadata:
name: grafana-temp
labels:
app: grafana-temp
version: v6.0.0
annotations:
sidecar.istio.io/inject: "false"
spec:
serviceAccountName: grafana
containers:
- name: grafana
image: 201777367430.dkr.ecr.us-east-1.amazonaws.com/platform/istio/components/grafana:6.0.0
resources:
limits:
cpu: 500m
memory: 512Mi
requests:
cpu: 100m
memory: 512Mi
imagePullPolicy: IfNotPresent
livenessProbe:
failureThreshold: 3
httpGet:
path: /login
port: 3000
scheme: HTTP
initialDelaySeconds: 10
periodSeconds: 10
successThreshold: 1
timeoutSeconds: 1
ports:
- containerPort: 3000
env:
# Only put environment related config here. Generic Istio config
# should go in addons/grafana/grafana.ini.
- name: GF_PATHS_DATA
value: /data/grafana
volumeMounts:
- mountPath: /data/grafana
name: grafana-data
- name: config-volume-kubernetes-cluster-monitoring-dashboard
mountPath: /var/lib/grafana/dashboards/
priorityClassName: high-priority
volumes:
- name: grafana-data
emptyDir: {}
- name: config-volume-kubernetes-cluster-monitoring-dashboard
configMap:
name: kubernetes-cluster-monitoring-dashboard
---
apiVersion: v1
kind: ServiceAccount
metadata:
name: grafana-temp
namespace: monitoring
---
apiVersion: autoscaling/v2beta1
kind: HorizontalPodAutoscaler
metadata:
name: grafana-temp
namespace: monitoring
spec:
maxReplicas: 5
minReplicas: 2
scaleTargetRef:
apiVersion: apps/v1beta1
kind: Deployment
name: grafana-temp
metrics:
- type: Resource
resource:
name: cpu
targetAverageUtilization: 75
"""
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment