blob: 62d5d8dca3fb1e7fe200b6f3d144c74372741892 [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',
gstc79131a2025-01-13 11:19:12 +053092 vgcEnabled: false,
Joey Armstrongc33a0bf2023-09-08 14:41:23 -040093 withMacLearning: false,
94 withFttb: false,
Joey Armstrong6ee5c7b2023-09-27 15:23:50 -040095 extraHelmFlags: '',
Joey Armstrongc33a0bf2023-09-08 14:41:23 -040096 localCharts: false,
Joey Armstrong6ee5c7b2023-09-27 15:23:50 -040097 // location of the kubernetes config file,
98 // if null we assume it's stored in the $KUBECONFIG
99 kubeconfig: null,
Jan Klareb4789282024-11-15 13:19:10 +0100100 cluster: 'local'
Matteo Scandolo42f6e572021-01-25 15:11:34 -0800101 ]
102
Joey Armstrong6ee5c7b2023-09-27 15:23:50 -0400103 Map cfg = defaultConfig + config
Matteo Scandolo42f6e572021-01-25 15:11:34 -0800104
Joey Armstrong6ee5c7b2023-09-27 15:23:50 -0400105 def volthaInfraChart = 'onf/voltha-infra'
Matteo Scandolofcfc60d2021-02-24 09:06:48 -0800106
107 if (cfg.localCharts) {
Joey Armstrongc33a0bf2023-09-08 14:41:23 -0400108 volthaInfraChart = "$WORKSPACE/voltha-helm-charts/voltha-infra"
Matteo Scandolofcfc60d2021-02-24 09:06:48 -0800109
Joey Armstrongc33a0bf2023-09-08 14:41:23 -0400110 sh(label : 'HELM: Update deps',
111 script : """
112pushd $WORKSPACE/voltha-helm-charts/voltha-infra
113helm dep update
114popd
115""")
Matteo Scandolofcfc60d2021-02-24 09:06:48 -0800116 }
117
Matteo Scandolo42f6e572021-01-25 15:11:34 -0800118 println "Deploying VOLTHA Infra with the following parameters: ${cfg}."
119
Matteo Scandolod82d1de2021-04-06 14:55:58 -0700120 def kubeconfig = cfg.kubeconfig
121 if (kubeconfig == null) {
Joey Armstrongc33a0bf2023-09-08 14:41:23 -0400122 kubeconfig = env.KUBECONFIG
Matteo Scandolod82d1de2021-04-06 14:55:58 -0700123 }
124
Joey Armstrong7987c112022-12-05 12:42:43 -0500125 doKubeNamespaces() // WIP: joey
Joey Armstrong96158a92022-11-25 10:36:06 -0500126
Joey Armstrongc33a0bf2023-09-08 14:41:23 -0400127 sh(label : 'KUBE: Create namespace',
128 script : """
129kubectl create namespace ${cfg.infraNamespace} || true
130kubectl create configmap -n ${cfg.infraNamespace} kube-config "--from-file=kube_config=${kubeconfig}" || true
131""")
Matteo Scandolod82d1de2021-04-06 14:55:58 -0700132
Joey Armstrong6ee5c7b2023-09-27 15:23:50 -0400133 String serviceConfigFile = cfg.workflow
Hardik Windlass810b6cf2022-02-24 09:21:18 +0000134 if (cfg.withMacLearning && cfg.workflow == 'tt') {
Joey Armstrong6ee5c7b2023-09-27 15:23:50 -0400135 serviceConfigFile = 'tt-maclearner'
Hardik Windlassc97ceae2022-05-13 10:12:55 +0530136 } else if (cfg.withFttb && cfg.workflow == 'dt') {
Joey Armstrong6ee5c7b2023-09-27 15:23:50 -0400137 serviceConfigFile = 'dt-fttb'
Hardik Windlass810b6cf2022-02-24 09:21:18 +0000138 }
139
Matteo Scandoloa1b5d792021-06-01 12:19:31 -0700140 // bitnamic/etch has change the replica format between the currently used 5.4.2 and the latest 6.2.5
141 // for now put both values in the extra helm chart flags
gstc79131a2025-01-13 11:19:12 +0530142 if (cfg.vgcEnabled) {
143 sh(label : 'HELM: upgrade --install',
144 script : """
145 helm upgrade --install --create-namespace -n ${cfg.infraNamespace} voltha-infra ${volthaInfraChart} \
146 --set kafka.replicaCount=${cfg.kafkaReplica},kafka.zookeeper.replicaCount=${cfg.kafkaReplica} \
147 --set etcd.statefulset.replicaCount=${cfg.etcdReplica} \
148 --set etcd.replicaCount=${cfg.etcdReplica} \
149 --set etcd.ingress.enabled=true \
150 --set etcd.ingress.hosts[0].host=voltha-infra.${cfg.cluster} \
151 --set etcd.ingress.hosts[0].paths[0]='/etcdserverpb.KV/' \
152 -f $WORKSPACE/voltha-helm-charts/examples/${serviceConfigFile}-values.yaml ${cfg.extraHelmFlags}
153 """)
154 } else {
155 sh(label : 'HELM: upgrade --install',
156 script : """
157 helm upgrade --install --create-namespace -n ${cfg.infraNamespace} voltha-infra ${volthaInfraChart} \
158 --set onos-classic.replicas=${cfg.onosReplica},onos-classic.atomix.replicas=${cfg.atomixReplica} \
159 --set kafka.replicaCount=${cfg.kafkaReplica},kafka.zookeeper.replicaCount=${cfg.kafkaReplica} \
160 --set etcd.statefulset.replicaCount=${cfg.etcdReplica} \
161 --set etcd.replicaCount=${cfg.etcdReplica} \
162 --set etcd.ingress.enabled=true \
163 --set etcd.ingress.hosts[0].host=voltha-infra.${cfg.cluster} \
164 --set etcd.ingress.hosts[0].paths[0]='/etcdserverpb.KV/' \
165 -f $WORKSPACE/voltha-helm-charts/examples/${serviceConfigFile}-values.yaml ${cfg.extraHelmFlags}
166 """)
167 }
Joey Armstrong96158a92022-11-25 10:36:06 -0500168
Joey Armstrongc33a0bf2023-09-08 14:41:23 -0400169 leave('process')
Joey Armstrong7987c112022-12-05 12:42:43 -0500170 return
Matteo Scandolo42f6e572021-01-25 15:11:34 -0800171}
Joey Armstrong7987c112022-12-05 12:42:43 -0500172
173// -----------------------------------------------------------------------
174// -----------------------------------------------------------------------
Joey Armstrong6ee5c7b2023-09-27 15:23:50 -0400175def call(Map config=[:]) {
Joey Armstrongc33a0bf2023-09-08 14:41:23 -0400176 enter('main')
Joey Armstrong7987c112022-12-05 12:42:43 -0500177
Joey Armstrong6ee5c7b2023-09-27 15:23:50 -0400178 try {
179 process(config)
Joey Armstrong7987c112022-12-05 12:42:43 -0500180 }
Joey Armstrong6ee5c7b2023-09-27 15:23:50 -0400181 catch (Exception err) { // groovylint-disable-line CatchException
Joey Armstrongc33a0bf2023-09-08 14:41:23 -0400182 String iam = getIam(name)
Joey Armstrong6ee5c7b2023-09-27 15:23:50 -0400183 println("** ${iam}: EXCEPTION ${err}")
184 throw err
Joey Armstrong7987c112022-12-05 12:42:43 -0500185 }
Joey Armstrong6ee5c7b2023-09-27 15:23:50 -0400186 finally {
187 leave('main')
Joey Armstrong7987c112022-12-05 12:42:43 -0500188 }
Joey Armstrongc33a0bf2023-09-08 14:41:23 -0400189
Joey Armstrong7987c112022-12-05 12:42:43 -0500190 return
191}
192
193// [EOF]