Joey Armstrong | 96158a9 | 2022-11-25 10:36:06 -0500 | [diff] [blame] | 1 | #!/usr/bin/env groovy |
Joey Armstrong | 7987c11 | 2022-12-05 12:42:43 -0500 | [diff] [blame] | 2 | // ----------------------------------------------------------------------- |
Matteo Scandolo | 42f6e57 | 2021-01-25 15:11:34 -0800 | [diff] [blame] | 3 | // usage |
| 4 | // |
| 5 | // stage('test stage') { |
| 6 | // steps { |
| 7 | // volthaDeploy([ |
| 8 | // onosReplica: 3 |
| 9 | // ]) |
| 10 | // } |
| 11 | // } |
Joey Armstrong | 7987c11 | 2022-12-05 12:42:43 -0500 | [diff] [blame] | 12 | // ----------------------------------------------------------------------- |
Matteo Scandolo | 42f6e57 | 2021-01-25 15:11:34 -0800 | [diff] [blame] | 13 | |
Joey Armstrong | 7987c11 | 2022-12-05 12:42:43 -0500 | [diff] [blame] | 14 | // ----------------------------------------------------------------------- |
| 15 | // ----------------------------------------------------------------------- |
| 16 | def 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 Armstrong | 96158a9 | 2022-11-25 10:36:06 -0500 | [diff] [blame] | 23 | |
Joey Armstrong | 7987c11 | 2022-12-05 12:42:43 -0500 | [diff] [blame] | 24 | // ----------------------------------------------------------------------- |
| 25 | // Intent: Display and interact with kubernetes namespaces. |
| 26 | // ----------------------------------------------------------------------- |
| 27 | def doKubeNamespaces() |
| 28 | { |
| 29 | String iam = getIam('doKubeNamespaces') |
Joey Armstrong | 96158a9 | 2022-11-25 10:36:06 -0500 | [diff] [blame] | 30 | println("** ${iam}: ENTER") |
Joey Armstrong | 7987c11 | 2022-12-05 12:42:43 -0500 | [diff] [blame] | 31 | |
| 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 Armstrong | c4b5fd2 | 2022-12-05 14:17:34 -0500 | [diff] [blame] | 44 | script: 'kubectl get namespaces || true', |
Joey Armstrong | 7987c11 | 2022-12-05 12:42:43 -0500 | [diff] [blame] | 45 | returnStdout: true |
| 46 | ).trim() |
| 47 | print(namespaces) |
| 48 | |
| 49 | // Document prior to removal |
| 50 | namespaces.each{namespace -> |
Joey Armstrong | c4b5fd2 | 2022-12-05 14:17:34 -0500 | [diff] [blame] | 51 | namespaces = sh("kubectl describe namespaces ${namespace} || true") |
Joey Armstrong | 7987c11 | 2022-12-05 12:42:43 -0500 | [diff] [blame] | 52 | } |
| 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 | // ----------------------------------------------------------------------- |
| 67 | def process(Map config) |
| 68 | { |
| 69 | String iam = getIam('process') |
| 70 | println("** ${iam}: ENTER") |
| 71 | |
Matteo Scandolo | 42f6e57 | 2021-01-25 15:11:34 -0800 | [diff] [blame] | 72 | // 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 Windlass | 810b6cf | 2022-02-24 09:21:18 +0000 | [diff] [blame] | 80 | withMacLearning: false, |
Hardik Windlass | c97ceae | 2022-05-13 10:12:55 +0530 | [diff] [blame] | 81 | withFttb: false, |
Matteo Scandolo | 42f6e57 | 2021-01-25 15:11:34 -0800 | [diff] [blame] | 82 | extraHelmFlags: "", |
Matteo Scandolo | fcfc60d | 2021-02-24 09:06:48 -0800 | [diff] [blame] | 83 | localCharts: false, |
Matteo Scandolo | d82d1de | 2021-04-06 14:55:58 -0700 | [diff] [blame] | 84 | kubeconfig: null, // location of the kubernetes config file, if null we assume it's stored in the $KUBECONFIG environment variable |
Matteo Scandolo | 42f6e57 | 2021-01-25 15:11:34 -0800 | [diff] [blame] | 85 | ] |
| 86 | |
Matteo Scandolo | 42f6e57 | 2021-01-25 15:11:34 -0800 | [diff] [blame] | 87 | def cfg = defaultConfig + config |
| 88 | |
Matteo Scandolo | fcfc60d | 2021-02-24 09:06:48 -0800 | [diff] [blame] | 89 | 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 Scandolo | 42f6e57 | 2021-01-25 15:11:34 -0800 | [diff] [blame] | 101 | println "Deploying VOLTHA Infra with the following parameters: ${cfg}." |
| 102 | |
Matteo Scandolo | d82d1de | 2021-04-06 14:55:58 -0700 | [diff] [blame] | 103 | def kubeconfig = cfg.kubeconfig |
| 104 | if (kubeconfig == null) { |
| 105 | kubeconfig = env.KUBECONFIG |
| 106 | } |
| 107 | |
Joey Armstrong | 7987c11 | 2022-12-05 12:42:43 -0500 | [diff] [blame] | 108 | doKubeNamespaces() // WIP: joey |
Joey Armstrong | 96158a9 | 2022-11-25 10:36:06 -0500 | [diff] [blame] | 109 | |
Matteo Scandolo | 42f6e57 | 2021-01-25 15:11:34 -0800 | [diff] [blame] | 110 | sh """ |
| 111 | kubectl create namespace ${cfg.infraNamespace} || true |
Matteo Scandolo | d82d1de | 2021-04-06 14:55:58 -0700 | [diff] [blame] | 112 | kubectl create configmap -n ${cfg.infraNamespace} kube-config "--from-file=kube_config=${kubeconfig}" || true |
Matteo Scandolo | 42f6e57 | 2021-01-25 15:11:34 -0800 | [diff] [blame] | 113 | """ |
Matteo Scandolo | d82d1de | 2021-04-06 14:55:58 -0700 | [diff] [blame] | 114 | |
Hardik Windlass | 810b6cf | 2022-02-24 09:21:18 +0000 | [diff] [blame] | 115 | def serviceConfigFile = cfg.workflow |
| 116 | if (cfg.withMacLearning && cfg.workflow == 'tt') { |
| 117 | serviceConfigFile = "tt-maclearner" |
Hardik Windlass | c97ceae | 2022-05-13 10:12:55 +0530 | [diff] [blame] | 118 | } else if (cfg.withFttb && cfg.workflow == 'dt') { |
| 119 | serviceConfigFile = "dt-fttb" |
Hardik Windlass | 810b6cf | 2022-02-24 09:21:18 +0000 | [diff] [blame] | 120 | } |
| 121 | |
Matteo Scandolo | a1b5d79 | 2021-06-01 12:19:31 -0700 | [diff] [blame] | 122 | // 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 Scandolo | 42f6e57 | 2021-01-25 15:11:34 -0800 | [diff] [blame] | 124 | sh """ |
Matteo Scandolo | ed1afdd | 2021-04-02 16:25:45 -0700 | [diff] [blame] | 125 | helm upgrade --install --create-namespace -n ${cfg.infraNamespace} voltha-infra ${volthaInfraChart} \ |
Matteo Scandolo | 660e4af | 2021-04-02 11:16:09 -0700 | [diff] [blame] | 126 | --set onos-classic.replicas=${cfg.onosReplica},onos-classic.atomix.replicas=${cfg.atomixReplica} \ |
Matteo Scandolo | d43bb30 | 2021-04-20 10:19:29 -0700 | [diff] [blame] | 127 | --set kafka.replicaCount=${cfg.kafkaReplica},kafka.zookeeper.replicaCount=${cfg.kafkaReplica} \ |
| 128 | --set etcd.statefulset.replicaCount=${cfg.etcdReplica} \ |
Matteo Scandolo | a1b5d79 | 2021-06-01 12:19:31 -0700 | [diff] [blame] | 129 | --set etcd.replicaCount=${cfg.etcdReplica} \ |
Hardik Windlass | 810b6cf | 2022-02-24 09:21:18 +0000 | [diff] [blame] | 130 | -f $WORKSPACE/voltha-helm-charts/examples/${serviceConfigFile}-values.yaml ${cfg.extraHelmFlags} |
Matteo Scandolo | 42f6e57 | 2021-01-25 15:11:34 -0800 | [diff] [blame] | 131 | """ |
Joey Armstrong | 96158a9 | 2022-11-25 10:36:06 -0500 | [diff] [blame] | 132 | |
Joey Armstrong | 7987c11 | 2022-12-05 12:42:43 -0500 | [diff] [blame] | 133 | return |
Matteo Scandolo | 42f6e57 | 2021-01-25 15:11:34 -0800 | [diff] [blame] | 134 | } |
Joey Armstrong | 7987c11 | 2022-12-05 12:42:43 -0500 | [diff] [blame] | 135 | |
| 136 | // ----------------------------------------------------------------------- |
| 137 | // ----------------------------------------------------------------------- |
| 138 | def 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 Armstrong | 8a3c13b | 2022-12-06 09:27:44 -0500 | [diff] [blame^] | 154 | // Cannot re-throw ATM: too many potential shell errors. |
| 155 | // throw err |
Joey Armstrong | 7987c11 | 2022-12-05 12:42:43 -0500 | [diff] [blame] | 156 | } |
| 157 | finally |
| 158 | { |
| 159 | println("** ${iam}: LEAVE") |
| 160 | } |
| 161 | return |
| 162 | } |
| 163 | |
| 164 | // [EOF] |