blob: 8ec30083602cf49c57e7011512798a735d4eafcf [file] [log] [blame]
Joey Armstrong96158a92022-11-25 10:36:06 -05001#!/usr/bin/env groovy
Joey Armstrong7987c112022-12-05 12:42:43 -05002// -----------------------------------------------------------------------
Joey Armstrong518f3572024-02-11 07:56:25 -05003// Copyright 2021-2024 Open Networking Foundation (ONF) and the ONF Contributors
Joey Armstrongaf679da2023-01-31 14:22:41 -05004//
5// Licensed under the Apache License, Version 2.0 (the "License");
6// you may not use this file except in compliance with the License.
7// You may obtain a copy of the License at
8//
9// http://www.apache.org/licenses/LICENSE-2.0
10//
11// Unless required by applicable law or agreed to in writing, software
12// distributed under the License is distributed on an "AS IS" BASIS,
13// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14// See the License for the specific language governing permissions and
15// limitations under the License.
16// -----------------------------------------------------------------------
Matteo Scandolo42f6e572021-01-25 15:11:34 -080017// usage
18//
19// stage('test stage') {
20// steps {
Joey Armstrong6ee5c7b2023-09-27 15:23:50 -040021// volthaDeploy([onosReplica: 3])
Matteo Scandolo42f6e572021-01-25 15:11:34 -080022// }
23// }
Joey Armstrong7987c112022-12-05 12:42:43 -050024// -----------------------------------------------------------------------
Matteo Scandolo42f6e572021-01-25 15:11:34 -080025
Joey Armstrong7987c112022-12-05 12:42:43 -050026// -----------------------------------------------------------------------
27// -----------------------------------------------------------------------
Joey Armstrong6ee5c7b2023-09-27 15:23:50 -040028String getIam(String func) {
Joey Armstrong7987c112022-12-05 12:42:43 -050029 // Cannot rely on a stack trace due to jenkins manipulation
30 String src = 'vars/volthaInfraDeploy.groovy'
31 String iam = [src, func].join('::')
32 return iam
33}
Joey Armstrong96158a92022-11-25 10:36:06 -050034
Joey Armstrongc33a0bf2023-09-08 14:41:23 -040035// -----------------------------------------------------------------------
36// Intent: Log progress message
37// -----------------------------------------------------------------------
38void enter(String name) {
39 // Announce ourselves for log usability
40 String iam = getIam(name)
41 println("${iam}: ENTER")
42 return
43}
44
45// -----------------------------------------------------------------------
46// Intent: Log progress message
47// -----------------------------------------------------------------------
48void leave(String name) {
49 // Announce ourselves for log usability
50 String iam = getIam(name)
51 println("${iam}: LEAVE")
52 return
53}
54
Joey Armstrong7987c112022-12-05 12:42:43 -050055// -----------------------------------------------------------------------
56// Intent: Display and interact with kubernetes namespaces.
57// -----------------------------------------------------------------------
Joey Armstrong6ee5c7b2023-09-27 15:23:50 -040058void doKubeNamespaces() {
59 String iam = 'doKubeNamespaces'
60 enter(iam)
Joey Armstrong7987c112022-12-05 12:42:43 -050061
62 /*
63 [joey] - should pre-existing hint the env is tainted (?)
64 05:24:57 + kubectl create namespace infra
65 05:24:57 Error from server (AlreadyExists): namespaces "infra" already exists
66 05:24:57 error: failed to create configmap: configmaps "kube-config" already exists
67
68 [joey] Thinking we should:
Joey Armstrongc33a0bf2023-09-08 14:41:23 -040069 o A special case exists (create namespace)
70 o helm upgrade --install (inital update)
Joey Armstrong7987c112022-12-05 12:42:43 -050071 */
72
Joey Armstrong1cf25302022-12-06 09:51:03 -050073 sh('kubectl get namespaces || true')
Joey Armstrong7987c112022-12-05 12:42:43 -050074
Joey Armstrong6ee5c7b2023-09-27 15:23:50 -040075 leave(iam)
Joey Armstrong7987c112022-12-05 12:42:43 -050076 return
77}
78
79// -----------------------------------------------------------------------
80// -----------------------------------------------------------------------
Joey Armstrong6ee5c7b2023-09-27 15:23:50 -040081void process(Map config) {
Joey Armstrongc33a0bf2023-09-08 14:41:23 -040082 enter('process')
Joey Armstrong7987c112022-12-05 12:42:43 -050083
Matteo Scandolo42f6e572021-01-25 15:11:34 -080084 // NOTE use params or directule extraHelmFlags??
Joey Armstrong6ee5c7b2023-09-27 15:23:50 -040085 Map defaultConfig = [
Joey Armstrongc33a0bf2023-09-08 14:41:23 -040086 onosReplica: 1,
87 atomixReplica: 1,
88 kafkaReplica: 1,
89 etcdReplica: 1,
Joey Armstrong6ee5c7b2023-09-27 15:23:50 -040090 infraNamespace: 'infra',
91 workflow: 'att',
Joey Armstrongc33a0bf2023-09-08 14:41:23 -040092 withMacLearning: false,
93 withFttb: false,
Joey Armstrong6ee5c7b2023-09-27 15:23:50 -040094 extraHelmFlags: '',
Joey Armstrongc33a0bf2023-09-08 14:41:23 -040095 localCharts: false,
Joey Armstrong6ee5c7b2023-09-27 15:23:50 -040096 // location of the kubernetes config file,
97 // if null we assume it's stored in the $KUBECONFIG
98 kubeconfig: null,
Matteo Scandolo42f6e572021-01-25 15:11:34 -080099 ]
100
Joey Armstrong6ee5c7b2023-09-27 15:23:50 -0400101 Map cfg = defaultConfig + config
Matteo Scandolo42f6e572021-01-25 15:11:34 -0800102
Joey Armstrong6ee5c7b2023-09-27 15:23:50 -0400103 def volthaInfraChart = 'onf/voltha-infra'
Matteo Scandolofcfc60d2021-02-24 09:06:48 -0800104
105 if (cfg.localCharts) {
Joey Armstrongc33a0bf2023-09-08 14:41:23 -0400106 volthaInfraChart = "$WORKSPACE/voltha-helm-charts/voltha-infra"
Matteo Scandolofcfc60d2021-02-24 09:06:48 -0800107
Joey Armstrongc33a0bf2023-09-08 14:41:23 -0400108 sh(label : 'HELM: Update deps',
109 script : """
110pushd $WORKSPACE/voltha-helm-charts/voltha-infra
111helm dep update
112popd
113""")
Matteo Scandolofcfc60d2021-02-24 09:06:48 -0800114 }
115
Matteo Scandolo42f6e572021-01-25 15:11:34 -0800116 println "Deploying VOLTHA Infra with the following parameters: ${cfg}."
117
Matteo Scandolod82d1de2021-04-06 14:55:58 -0700118 def kubeconfig = cfg.kubeconfig
119 if (kubeconfig == null) {
Joey Armstrongc33a0bf2023-09-08 14:41:23 -0400120 kubeconfig = env.KUBECONFIG
Matteo Scandolod82d1de2021-04-06 14:55:58 -0700121 }
122
Joey Armstrong7987c112022-12-05 12:42:43 -0500123 doKubeNamespaces() // WIP: joey
Joey Armstrong96158a92022-11-25 10:36:06 -0500124
Joey Armstrongc33a0bf2023-09-08 14:41:23 -0400125 sh(label : 'KUBE: Create namespace',
126 script : """
127kubectl create namespace ${cfg.infraNamespace} || true
128kubectl create configmap -n ${cfg.infraNamespace} kube-config "--from-file=kube_config=${kubeconfig}" || true
129""")
Matteo Scandolod82d1de2021-04-06 14:55:58 -0700130
Joey Armstrong6ee5c7b2023-09-27 15:23:50 -0400131 String serviceConfigFile = cfg.workflow
Hardik Windlass810b6cf2022-02-24 09:21:18 +0000132 if (cfg.withMacLearning && cfg.workflow == 'tt') {
Joey Armstrong6ee5c7b2023-09-27 15:23:50 -0400133 serviceConfigFile = 'tt-maclearner'
Hardik Windlassc97ceae2022-05-13 10:12:55 +0530134 } else if (cfg.withFttb && cfg.workflow == 'dt') {
Joey Armstrong6ee5c7b2023-09-27 15:23:50 -0400135 serviceConfigFile = 'dt-fttb'
Hardik Windlass810b6cf2022-02-24 09:21:18 +0000136 }
137
Matteo Scandoloa1b5d792021-06-01 12:19:31 -0700138 // bitnamic/etch has change the replica format between the currently used 5.4.2 and the latest 6.2.5
139 // for now put both values in the extra helm chart flags
Joey Armstrongc33a0bf2023-09-08 14:41:23 -0400140 sh(label : 'HELM: upgrade --install',
141 script : """
Matteo Scandoloed1afdd2021-04-02 16:25:45 -0700142 helm upgrade --install --create-namespace -n ${cfg.infraNamespace} voltha-infra ${volthaInfraChart} \
Matteo Scandolo660e4af2021-04-02 11:16:09 -0700143 --set onos-classic.replicas=${cfg.onosReplica},onos-classic.atomix.replicas=${cfg.atomixReplica} \
Matteo Scandolod43bb302021-04-20 10:19:29 -0700144 --set kafka.replicaCount=${cfg.kafkaReplica},kafka.zookeeper.replicaCount=${cfg.kafkaReplica} \
145 --set etcd.statefulset.replicaCount=${cfg.etcdReplica} \
Matteo Scandoloa1b5d792021-06-01 12:19:31 -0700146 --set etcd.replicaCount=${cfg.etcdReplica} \
Roger Luethi2403ef02023-09-27 06:27:22 +0200147 --set etcd.ingress.enabled=true \
148 --set etcd.ingress.enableVirtualHosts=true \
Hardik Windlass810b6cf2022-02-24 09:21:18 +0000149 -f $WORKSPACE/voltha-helm-charts/examples/${serviceConfigFile}-values.yaml ${cfg.extraHelmFlags}
Joey Armstrongc33a0bf2023-09-08 14:41:23 -0400150""")
Joey Armstrong96158a92022-11-25 10:36:06 -0500151
Joey Armstrongc33a0bf2023-09-08 14:41:23 -0400152 leave('process')
Joey Armstrong7987c112022-12-05 12:42:43 -0500153 return
Matteo Scandolo42f6e572021-01-25 15:11:34 -0800154}
Joey Armstrong7987c112022-12-05 12:42:43 -0500155
156// -----------------------------------------------------------------------
157// -----------------------------------------------------------------------
Joey Armstrong6ee5c7b2023-09-27 15:23:50 -0400158def call(Map config=[:]) {
Joey Armstrongc33a0bf2023-09-08 14:41:23 -0400159 enter('main')
Joey Armstrong7987c112022-12-05 12:42:43 -0500160
Joey Armstrong6ee5c7b2023-09-27 15:23:50 -0400161 try {
162 process(config)
Joey Armstrong7987c112022-12-05 12:42:43 -0500163 }
Joey Armstrong6ee5c7b2023-09-27 15:23:50 -0400164 catch (Exception err) { // groovylint-disable-line CatchException
Joey Armstrongc33a0bf2023-09-08 14:41:23 -0400165 String iam = getIam(name)
Joey Armstrong6ee5c7b2023-09-27 15:23:50 -0400166 println("** ${iam}: EXCEPTION ${err}")
167 throw err
Joey Armstrong7987c112022-12-05 12:42:43 -0500168 }
Joey Armstrong6ee5c7b2023-09-27 15:23:50 -0400169 finally {
170 leave('main')
Joey Armstrong7987c112022-12-05 12:42:43 -0500171 }
Joey Armstrongc33a0bf2023-09-08 14:41:23 -0400172
Joey Armstrong7987c112022-12-05 12:42:43 -0500173 return
174}
175
176// [EOF]