blob: 5fcd4f9dd9d469e5d97652c2f916b643750bea0a [file] [log] [blame]
Joey Armstrong96158a92022-11-25 10:36:06 -05001#!/usr/bin/env groovy
Joey Armstrong7987c112022-12-05 12:42:43 -05002// -----------------------------------------------------------------------
Matteo Scandolo42f6e572021-01-25 15:11:34 -08003// usage
4//
5// stage('test stage') {
6// steps {
7// volthaDeploy([
8// onosReplica: 3
9// ])
10// }
11// }
Joey Armstrong7987c112022-12-05 12:42:43 -050012// -----------------------------------------------------------------------
Matteo Scandolo42f6e572021-01-25 15:11:34 -080013
Joey Armstrong7987c112022-12-05 12:42:43 -050014// -----------------------------------------------------------------------
15// -----------------------------------------------------------------------
16def getIam(String func)
17{
18 // Cannot rely on a stack trace due to jenkins manipulation
19 String src = 'vars/volthaInfraDeploy.groovy'
20 String iam = [src, func].join('::')
21 return iam
22}
Joey Armstrong96158a92022-11-25 10:36:06 -050023
Joey Armstrong7987c112022-12-05 12:42:43 -050024// -----------------------------------------------------------------------
25// Intent: Display and interact with kubernetes namespaces.
26// -----------------------------------------------------------------------
27def doKubeNamespaces()
28{
29 String iam = getIam('doKubeNamespaces')
Joey Armstrong96158a92022-11-25 10:36:06 -050030 println("** ${iam}: ENTER")
Joey Armstrong7987c112022-12-05 12:42:43 -050031
32 /*
33 [joey] - should pre-existing hint the env is tainted (?)
34 05:24:57 + kubectl create namespace infra
35 05:24:57 Error from server (AlreadyExists): namespaces "infra" already exists
36 05:24:57 error: failed to create configmap: configmaps "kube-config" already exists
37
38 [joey] Thinking we should:
39 o A special case exists (create namespace)
40 o helm upgrade --install (inital update)
41 */
42
43 namespaces = sh(
Joey Armstrongc4b5fd22022-12-05 14:17:34 -050044 script: 'kubectl get namespaces || true',
Joey Armstrong7987c112022-12-05 12:42:43 -050045 returnStdout: true
46 ).trim()
47 print(namespaces)
48
49 // Document prior to removal
50 namespaces.each{namespace ->
Joey Armstrongc4b5fd22022-12-05 14:17:34 -050051 namespaces = sh("kubectl describe namespaces ${namespace} || true")
Joey Armstrong7987c112022-12-05 12:42:43 -050052 }
53
54 /*
55 // [TODO] Remove if safe op: clean state and avoids a special case.
56 namespaces.each{namespace ->
57 namespaces = sh("kubectl delete namespaces ${namespace}")
58 }
59 */
60
61 println("** ${iam}: LEAVE")
62 return
63}
64
65// -----------------------------------------------------------------------
66// -----------------------------------------------------------------------
67def process(Map config)
68{
69 String iam = getIam('process')
70 println("** ${iam}: ENTER")
71
Matteo Scandolo42f6e572021-01-25 15:11:34 -080072 // NOTE use params or directule extraHelmFlags??
73 def defaultConfig = [
74 onosReplica: 1,
75 atomixReplica: 1,
76 kafkaReplica: 1,
77 etcdReplica: 1,
78 infraNamespace: "infra",
79 workflow: "att",
Hardik Windlass810b6cf2022-02-24 09:21:18 +000080 withMacLearning: false,
Hardik Windlassc97ceae2022-05-13 10:12:55 +053081 withFttb: false,
Matteo Scandolo42f6e572021-01-25 15:11:34 -080082 extraHelmFlags: "",
Matteo Scandolofcfc60d2021-02-24 09:06:48 -080083 localCharts: false,
Matteo Scandolod82d1de2021-04-06 14:55:58 -070084 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 -080085 ]
86
Matteo Scandolo42f6e572021-01-25 15:11:34 -080087 def cfg = defaultConfig + config
88
Matteo Scandolofcfc60d2021-02-24 09:06:48 -080089 def volthaInfraChart = "onf/voltha-infra"
90
91 if (cfg.localCharts) {
92 volthaInfraChart = "$WORKSPACE/voltha-helm-charts/voltha-infra"
93
94 sh """
95 pushd $WORKSPACE/voltha-helm-charts/voltha-infra
96 helm dep update
97 popd
98 """
99 }
100
Matteo Scandolo42f6e572021-01-25 15:11:34 -0800101 println "Deploying VOLTHA Infra with the following parameters: ${cfg}."
102
Matteo Scandolod82d1de2021-04-06 14:55:58 -0700103 def kubeconfig = cfg.kubeconfig
104 if (kubeconfig == null) {
105 kubeconfig = env.KUBECONFIG
106 }
107
Joey Armstrong7987c112022-12-05 12:42:43 -0500108 doKubeNamespaces() // WIP: joey
Joey Armstrong96158a92022-11-25 10:36:06 -0500109
Matteo Scandolo42f6e572021-01-25 15:11:34 -0800110 sh """
111 kubectl create namespace ${cfg.infraNamespace} || true
Matteo Scandolod82d1de2021-04-06 14:55:58 -0700112 kubectl create configmap -n ${cfg.infraNamespace} kube-config "--from-file=kube_config=${kubeconfig}" || true
Matteo Scandolo42f6e572021-01-25 15:11:34 -0800113 """
Matteo Scandolod82d1de2021-04-06 14:55:58 -0700114
Hardik Windlass810b6cf2022-02-24 09:21:18 +0000115 def serviceConfigFile = cfg.workflow
116 if (cfg.withMacLearning && cfg.workflow == 'tt') {
117 serviceConfigFile = "tt-maclearner"
Hardik Windlassc97ceae2022-05-13 10:12:55 +0530118 } else if (cfg.withFttb && cfg.workflow == 'dt') {
119 serviceConfigFile = "dt-fttb"
Hardik Windlass810b6cf2022-02-24 09:21:18 +0000120 }
121
Matteo Scandoloa1b5d792021-06-01 12:19:31 -0700122 // bitnamic/etch has change the replica format between the currently used 5.4.2 and the latest 6.2.5
123 // for now put both values in the extra helm chart flags
Matteo Scandolo42f6e572021-01-25 15:11:34 -0800124 sh """
Matteo Scandoloed1afdd2021-04-02 16:25:45 -0700125 helm upgrade --install --create-namespace -n ${cfg.infraNamespace} voltha-infra ${volthaInfraChart} \
Matteo Scandolo660e4af2021-04-02 11:16:09 -0700126 --set onos-classic.replicas=${cfg.onosReplica},onos-classic.atomix.replicas=${cfg.atomixReplica} \
Matteo Scandolod43bb302021-04-20 10:19:29 -0700127 --set kafka.replicaCount=${cfg.kafkaReplica},kafka.zookeeper.replicaCount=${cfg.kafkaReplica} \
128 --set etcd.statefulset.replicaCount=${cfg.etcdReplica} \
Matteo Scandoloa1b5d792021-06-01 12:19:31 -0700129 --set etcd.replicaCount=${cfg.etcdReplica} \
Hardik Windlass810b6cf2022-02-24 09:21:18 +0000130 -f $WORKSPACE/voltha-helm-charts/examples/${serviceConfigFile}-values.yaml ${cfg.extraHelmFlags}
Matteo Scandolo42f6e572021-01-25 15:11:34 -0800131 """
Joey Armstrong96158a92022-11-25 10:36:06 -0500132
Joey Armstrong7987c112022-12-05 12:42:43 -0500133 return
Matteo Scandolo42f6e572021-01-25 15:11:34 -0800134}
Joey Armstrong7987c112022-12-05 12:42:43 -0500135
136// -----------------------------------------------------------------------
137// -----------------------------------------------------------------------
138def call(Map config)
139{
140 String iam = getIam('main')
141 println("** ${iam}: ENTER")
142
143 if (!config) {
144 config = [:]
145 }
146
147 try
148 {
149 process(config)
150 }
151 catch (Exception err)
152 {
153 println("** ${iam}: EXCEPTION ${err}")
Joey Armstrong8a3c13b2022-12-06 09:27:44 -0500154 // Cannot re-throw ATM: too many potential shell errors.
155 // throw err
Joey Armstrong7987c112022-12-05 12:42:43 -0500156 }
157 finally
158 {
159 println("** ${iam}: LEAVE")
160 }
161 return
162}
163
164// [EOF]