blob: db17b42b9172af20937f59489618b0d161f8e68f [file] [log] [blame]
Joey Armstrong96158a92022-11-25 10:36:06 -05001#!/usr/bin/env groovy
Joey Armstrong7987c112022-12-05 12:42:43 -05002// -----------------------------------------------------------------------
Joey Armstrongaf679da2023-01-31 14:22:41 -05003// Copyright 2021-2023 Open Networking Foundation (ONF) and the ONF Contributors
4//
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 {
21// volthaDeploy([
22// onosReplica: 3
23// ])
24// }
25// }
Joey Armstrong7987c112022-12-05 12:42:43 -050026// -----------------------------------------------------------------------
Matteo Scandolo42f6e572021-01-25 15:11:34 -080027
Joey Armstrong7987c112022-12-05 12:42:43 -050028// -----------------------------------------------------------------------
29// -----------------------------------------------------------------------
30def getIam(String func)
31{
32 // Cannot rely on a stack trace due to jenkins manipulation
33 String src = 'vars/volthaInfraDeploy.groovy'
34 String iam = [src, func].join('::')
35 return iam
36}
Joey Armstrong96158a92022-11-25 10:36:06 -050037
Joey Armstrongc33a0bf2023-09-08 14:41:23 -040038
39// -----------------------------------------------------------------------
40// Intent: Log progress message
41// -----------------------------------------------------------------------
42void enter(String name) {
43 // Announce ourselves for log usability
44 String iam = getIam(name)
45 println("${iam}: ENTER")
46 return
47}
48
49// -----------------------------------------------------------------------
50// Intent: Log progress message
51// -----------------------------------------------------------------------
52void leave(String name) {
53 // Announce ourselves for log usability
54 String iam = getIam(name)
55 println("${iam}: LEAVE")
56 return
57}
58
Joey Armstrong7987c112022-12-05 12:42:43 -050059// -----------------------------------------------------------------------
60// Intent: Display and interact with kubernetes namespaces.
61// -----------------------------------------------------------------------
62def doKubeNamespaces()
63{
Joey Armstrongc33a0bf2023-09-08 14:41:23 -040064 enter('doKubeNamespaces')
Joey Armstrong7987c112022-12-05 12:42:43 -050065
66 /*
67 [joey] - should pre-existing hint the env is tainted (?)
68 05:24:57 + kubectl create namespace infra
69 05:24:57 Error from server (AlreadyExists): namespaces "infra" already exists
70 05:24:57 error: failed to create configmap: configmaps "kube-config" already exists
71
72 [joey] Thinking we should:
Joey Armstrongc33a0bf2023-09-08 14:41:23 -040073 o A special case exists (create namespace)
74 o helm upgrade --install (inital update)
Joey Armstrong7987c112022-12-05 12:42:43 -050075 */
76
Joey Armstrong1cf25302022-12-06 09:51:03 -050077 sh('kubectl get namespaces || true')
Joey Armstrong7987c112022-12-05 12:42:43 -050078
Joey Armstrongc33a0bf2023-09-08 14:41:23 -040079 leave('doKubeNamespaces')
Joey Armstrong7987c112022-12-05 12:42:43 -050080 return
81}
82
83// -----------------------------------------------------------------------
84// -----------------------------------------------------------------------
85def process(Map config)
86{
Joey Armstrongc33a0bf2023-09-08 14:41:23 -040087 enter('process')
Joey Armstrong7987c112022-12-05 12:42:43 -050088
Matteo Scandolo42f6e572021-01-25 15:11:34 -080089 // NOTE use params or directule extraHelmFlags??
90 def defaultConfig = [
Joey Armstrongc33a0bf2023-09-08 14:41:23 -040091 onosReplica: 1,
92 atomixReplica: 1,
93 kafkaReplica: 1,
94 etcdReplica: 1,
95 infraNamespace: "infra",
96 workflow: "att",
97 withMacLearning: false,
98 withFttb: false,
99 extraHelmFlags: "",
100 localCharts: false,
101 kubeconfig: null, // location of the kubernetes config file, if null we assume it's stored in the $KUBECONFIG environment variable
Matteo Scandolo42f6e572021-01-25 15:11:34 -0800102 ]
103
Matteo Scandolo42f6e572021-01-25 15:11:34 -0800104 def cfg = defaultConfig + config
105
Matteo Scandolofcfc60d2021-02-24 09:06:48 -0800106 def volthaInfraChart = "onf/voltha-infra"
107
108 if (cfg.localCharts) {
Joey Armstrongc33a0bf2023-09-08 14:41:23 -0400109 volthaInfraChart = "$WORKSPACE/voltha-helm-charts/voltha-infra"
Matteo Scandolofcfc60d2021-02-24 09:06:48 -0800110
Joey Armstrongc33a0bf2023-09-08 14:41:23 -0400111 sh(label : 'HELM: Update deps',
112 script : """
113pushd $WORKSPACE/voltha-helm-charts/voltha-infra
114helm dep update
115popd
116""")
Matteo Scandolofcfc60d2021-02-24 09:06:48 -0800117 }
118
Matteo Scandolo42f6e572021-01-25 15:11:34 -0800119 println "Deploying VOLTHA Infra with the following parameters: ${cfg}."
120
Matteo Scandolod82d1de2021-04-06 14:55:58 -0700121 def kubeconfig = cfg.kubeconfig
122 if (kubeconfig == null) {
Joey Armstrongc33a0bf2023-09-08 14:41:23 -0400123 kubeconfig = env.KUBECONFIG
Matteo Scandolod82d1de2021-04-06 14:55:58 -0700124 }
125
Joey Armstrong7987c112022-12-05 12:42:43 -0500126 doKubeNamespaces() // WIP: joey
Joey Armstrong96158a92022-11-25 10:36:06 -0500127
Joey Armstrongc33a0bf2023-09-08 14:41:23 -0400128 sh(label : 'KUBE: Create namespace',
129 script : """
130kubectl create namespace ${cfg.infraNamespace} || true
131kubectl create configmap -n ${cfg.infraNamespace} kube-config "--from-file=kube_config=${kubeconfig}" || true
132""")
Matteo Scandolod82d1de2021-04-06 14:55:58 -0700133
Hardik Windlass810b6cf2022-02-24 09:21:18 +0000134 def serviceConfigFile = cfg.workflow
135 if (cfg.withMacLearning && cfg.workflow == 'tt') {
Joey Armstrongc33a0bf2023-09-08 14:41:23 -0400136 serviceConfigFile = "tt-maclearner"
Hardik Windlassc97ceae2022-05-13 10:12:55 +0530137 } else if (cfg.withFttb && cfg.workflow == 'dt') {
Joey Armstrongc33a0bf2023-09-08 14:41:23 -0400138 serviceConfigFile = "dt-fttb"
Hardik Windlass810b6cf2022-02-24 09:21:18 +0000139 }
140
Matteo Scandoloa1b5d792021-06-01 12:19:31 -0700141 // bitnamic/etch has change the replica format between the currently used 5.4.2 and the latest 6.2.5
142 // for now put both values in the extra helm chart flags
Joey Armstrongc33a0bf2023-09-08 14:41:23 -0400143 sh(label : 'HELM: upgrade --install',
144 script : """
Matteo Scandoloed1afdd2021-04-02 16:25:45 -0700145 helm upgrade --install --create-namespace -n ${cfg.infraNamespace} voltha-infra ${volthaInfraChart} \
Matteo Scandolo660e4af2021-04-02 11:16:09 -0700146 --set onos-classic.replicas=${cfg.onosReplica},onos-classic.atomix.replicas=${cfg.atomixReplica} \
Matteo Scandolod43bb302021-04-20 10:19:29 -0700147 --set kafka.replicaCount=${cfg.kafkaReplica},kafka.zookeeper.replicaCount=${cfg.kafkaReplica} \
148 --set etcd.statefulset.replicaCount=${cfg.etcdReplica} \
Matteo Scandoloa1b5d792021-06-01 12:19:31 -0700149 --set etcd.replicaCount=${cfg.etcdReplica} \
Hardik Windlass810b6cf2022-02-24 09:21:18 +0000150 -f $WORKSPACE/voltha-helm-charts/examples/${serviceConfigFile}-values.yaml ${cfg.extraHelmFlags}
Joey Armstrongc33a0bf2023-09-08 14:41:23 -0400151""")
Joey Armstrong96158a92022-11-25 10:36:06 -0500152
Joey Armstrongc33a0bf2023-09-08 14:41:23 -0400153 leave('process')
Joey Armstrong7987c112022-12-05 12:42:43 -0500154 return
Matteo Scandolo42f6e572021-01-25 15:11:34 -0800155}
Joey Armstrong7987c112022-12-05 12:42:43 -0500156
157// -----------------------------------------------------------------------
158// -----------------------------------------------------------------------
159def call(Map config)
160{
Joey Armstrongc33a0bf2023-09-08 14:41:23 -0400161 enter('main')
Joey Armstrong7987c112022-12-05 12:42:43 -0500162
163 if (!config) {
164 config = [:]
165 }
166
167 try
168 {
Joey Armstrongc33a0bf2023-09-08 14:41:23 -0400169 process(config)
Joey Armstrong7987c112022-12-05 12:42:43 -0500170 }
171 catch (Exception err)
172 {
Joey Armstrongc33a0bf2023-09-08 14:41:23 -0400173 String iam = getIam(name)
174 println("** ${iam}: EXCEPTION ${err}")
175 throw err
Joey Armstrong7987c112022-12-05 12:42:43 -0500176 }
177 finally
178 {
Joey Armstrongc33a0bf2023-09-08 14:41:23 -0400179 leave('main')
Joey Armstrong7987c112022-12-05 12:42:43 -0500180 }
Joey Armstrongc33a0bf2023-09-08 14:41:23 -0400181
Joey Armstrong7987c112022-12-05 12:42:43 -0500182 return
183}
184
185// [EOF]