alarmadapter helm renamed to alarmmanager
[ric-plt/ric-dep.git] / bin / install
1 #!/bin/bash
2 ################################################################################
3 #   Copyright (c) 2019 AT&T Intellectual Property.                             #
4 #   Copyright (c) 2019 Nokia.                                                  #
5 #                                                                              #
6 #   Licensed under the Apache License, Version 2.0 (the "License");            #
7 #   you may not use this file except in compliance with the License.           #
8 #   You may obtain a copy of the License at                                    #
9 #                                                                              #
10 #       http://www.apache.org/licenses/LICENSE-2.0                             #
11 #                                                                              #
12 #   Unless required by applicable law or agreed to in writing, software        #
13 #   distributed under the License is distributed on an "AS IS" BASIS,          #
14 #   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.   #
15 #   See the License for the specific language governing permissions and        #
16 #   limitations under the License.                                             #
17 ################################################################################
18
19 function wait_for_pods() {
20   echo -n "waiting for $1 pods to run"
21
22   STILL_WAITING=true
23   while $STILL_WAITING; do
24       STILL_WAITING=false
25       PODS=$(kubectl get pods -n $2 2>/dev/null | grep $1 | awk '{print $1}')
26       if [ -z $PODS ]; then
27         STILL_WAITING=true
28         sleep 1
29         echo -n "."
30       fi
31       for POD in ${PODS}; do
32         if [[ $(kubectl get pod ${POD} -n $2 -o go-template --template "{{.status.phase}}") != "Running" ]]; then
33             STILL_WAITING=true
34             sleep 1
35             echo -n "."
36             break
37         fi
38       done 
39   done
40
41   echo
42 }
43
44 function wait_for_cats() {
45   echo -n "waiting for $1 daemonset to complete"
46
47   STILL_WAITING=true
48   while $STILL_WAITING; do
49       STILL_WAITING=false
50       PODS=$(kubectl get pods -n $2 | grep $1 | awk '{print $1}')
51       for POD in ${PODS}; do
52         if [[ $(kubectl logs ${POD} -n $2 --tail 1) != "done" ]]; then
53             STILL_WAITING=true
54             sleep 1
55             echo -n "."
56             break
57         fi
58       done 
59   done
60
61   echo
62 }
63
64 KERNEL_OPTIMIZATION=false
65 IS_HELM3=$(helm version --short|grep -e "^v3")
66
67 while [ -n "$1" ]; do # while loop starts
68
69     case "$1" in
70
71     -f) OVERRIDEYAML=$2
72         shift
73         ;;
74     -c) LIST_OF_COMPONENTS=$2
75         shift
76         ;;
77     -o) KERNEL_OPTIMIZATION=true
78         ;;
79     *) echo "Option $1 not recognized" ;; # In case you typed a different option other than a,b,c
80
81     esac
82
83     shift
84
85 done
86
87 if [ -z "$OVERRIDEYAML" ];then
88     echo "****************************************************************************************************************"
89     echo "                                                     ERROR                                                      "
90     echo "****************************************************************************************************************"
91     echo "RIC deployment without deployment recipe is currently disabled. Please specify an recipe with the -f option."
92     echo "****************************************************************************************************************"
93     exit 1
94 fi
95
96 if [ -z $IS_HELM3 ]
97 then
98   HAS_COMMON_PACKAGE=$(helm search local/ric-common | grep ric-common)
99 else 
100   HAS_COMMON_PACKAGE=$(helm search repo local/ric-common | grep ric-common)
101 fi
102
103 if [ -z "$HAS_COMMON_PACKAGE" ];then
104     echo "****************************************************************************************************************"
105     echo "                                                     ERROR                                                      "
106     echo "****************************************************************************************************************"
107     echo "Can't locate the ric-common helm package in the local repo. Please make sure that it is properly installed."
108     echo "****************************************************************************************************************"
109     exit 1
110 fi
111
112 DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null && pwd )"
113 COMMON_BLOCK=$(cat $OVERRIDEYAML | awk '/^common:/{getline; while ($0 ~ /^ +.*|^ *|^ *#.*$/) {print $0; if (getline == 0) {break}}}')
114 NAMESPACE_BLOCK=$(cat $OVERRIDEYAML | awk '/^  namespace:/{getline; while ($0 ~ /^ +.*|^ *|^ *#.*$/) {print $0; if (getline == 0) {break}}}')
115 PLTNAMESPACE=$(echo "$NAMESPACE_BLOCK" | awk '/^ *platform:/{print $2}')
116 INFRANAMESPACE=$(echo "$NAMESPACE_BLOCK" | awk '/^ *infra:/{print $2}')
117 XAPPNAMESPACE=$(echo "$NAMESPACE_BLOCK" | awk '/^ *xapp:/{print $2}')
118 RELEASE_PREFIX=$(echo "$COMMON_BLOCK" | awk '/^ *releasePrefix:/{print $2}')
119 LOCAL_REPOSITORY=$(echo "$COMMON_BLOCK" | awk '/^ *localregistry:/{print $2}')
120
121 # replace the dbaasha with dbaas1 if deploying non HA DBaaS
122 COMPONENTS=${LIST_OF_COMPONENTS:-"infrastructure dbaas xapp-onboarder appmgr rtmgr e2mgr e2term a1mediator submgr vespamgr jaegeradapter o1mediator alarmmanager"}
123 echo "Deploying RIC infra components [$COMPONENTS]"
124
125
126 if ! kubectl get ns ${PLTNAMESPACE:-ricplt}> /dev/null 2>&1; then
127     kubectl create ns ${PLTNAMESPACE:-ricplt}
128 fi
129 if ! kubectl get ns ${INFRANAMESPACE:-ricinfra}> /dev/null 2>&1; then
130     kubectl create ns ${INFRANAMESPACE:-ricinfra}
131 fi
132 if ! kubectl get ns ${XAPPNAMESPACE:-ricxapp}> /dev/null 2>&1; then
133     kubectl create ns ${XAPPNAMESPACE:-ricxapp}
134 fi
135 FOUND_RECIPE=$(kubectl get configmap -n ${PLTNAMESPACE:-ricplt} ricplt-recipe 2>/dev/null )
136 if [ ! -z "$FOUND_RECIPE" ]; then
137     kubectl delete configmap -n ${PLTNAMESPACE:-ricplt} ricplt-recipe
138 fi
139 kubectl create configmap -n ${PLTNAMESPACE:-ricplt} ricplt-recipe --from-file=recipe=$OVERRIDEYAML
140
141 if [ ! -z "$LOCAL_REPOSITORY" ]; then
142     LOCAL_REPOSITORY="$LOCAL_REPOSITORY/"
143 fi
144
145
146 echo Add cluster roles
147     cat >ricplt-role.yaml <<EOF
148 ---
149 apiVersion: rbac.authorization.k8s.io/v1
150 kind: ClusterRole
151 metadata:
152   name: ricplt-system-tiller
153 rules:
154   - apiGroups: [""]
155     resources: ["deployments"]
156     verbs: ["get", "list", "create", "delete"]
157   - apiGroups: ["apiextensions.k8s.io"]
158     resources: ["customresourcedefinitions"]
159     verbs: ["get", "list", "create", "delete"]
160   - apiGroups: ["rbac.authorization.k8s.io"]
161     resources: ["clusterroles", "clusterrolebindings"]
162     verbs: ["get", "list", "create", "delete"]
163   - apiGroups: [""]
164     resources: ["events"]
165     verbs: ["create", "patch"]
166   - apiGroups: [""]
167     resources: ["nodes"]
168     verbs: ["list", "watch", "get"]
169   - apiGroups: [""]
170     resources: ["nodes/metrics"]
171     verbs: ["list", "watch", "get"]
172   - apiGroups: [""]
173     resources: ["nodes/proxy"]
174     verbs: ["list", "watch", "get"]
175   - apiGroups: ["configuration.konghq.com"]
176     resources: ["kongconsumers"]
177     verbs: ["get", "list", "watch"]
178   - apiGroups: ["configuration.konghq.com"]
179     resources: ["kongcredentials"]
180     verbs: ["get", "list", "watch"]
181   - apiGroups: ["configuration.konghq.com"]
182     resources: ["kongingresses"]
183     verbs: ["get", "list", "watch"]
184   - apiGroups: ["configuration.konghq.com"]
185     resources: ["kongplugins"]
186     verbs: ["get", "list", "watch"]
187   - apiGroups: ["networking.k8s.io"]
188     resources: ["ingresses"]
189     verbs: ["watch", "list", "get", "create", "delete", "update"]
190   - apiGroups: [""]
191     resources: ["ingresses"]
192     verbs: ["watch", "list", "get", "create", "delete", "update"]
193   - apiGroups: [""]
194     resources: ["persistentvolumes"]
195     verbs: ["watch", "list", "get", "create", "delete"]
196   - apiGroups: ["danm.k8s.io"]
197     resources: ["clusternetworks"]
198     verbs: ["watch", "list", "get", "create", "delete"]
199   - apiGroups: ["extensions"]
200     resources: ["ingresses/status"]
201     verbs: ["update", "get", "list", "watch"]
202   - apiGroups: ["networking.k8s.io"]
203     resources: ["ingresses/status"]
204     verbs: ["update", "get", "list", "watch"]
205   - apiGroups: ["certificates.k8s.io"]
206     resources: ["certificatesigningrequests"]
207     verbs: ["list", "watch"]
208   - apiGroups: ["storage.k8s.io"]
209     resources: ["storageclasses"]
210     verbs: ["list", "watch"]
211   - nonResourceURLs: ["/metrics"]
212     verbs: ["get"]
213 ---
214 apiVersion: rbac.authorization.k8s.io/v1
215 kind: ClusterRoleBinding
216 metadata:
217   name: ricplt-system-tiller
218 roleRef:
219   apiGroup: rbac.authorization.k8s.io
220   kind: ClusterRole
221   name: ricplt-system-tiller
222 subjects:
223   - kind: ServiceAccount
224     name: tiller
225     namespace: kube-system
226 EOF
227
228 if [ -z $IS_HELM3 ]
229 then
230    kubectl apply -f ricplt-role.yaml
231    rm ricplt-role.yaml
232 fi
233
234
235 # Add kernel optimization for radis services
236 if $KERNEL_OPTIMIZATION; then
237     cat >kernel_optimizer.yaml <<EOF
238 apiVersion: apps/v1
239 kind: DaemonSet
240 metadata:
241   namespace: ${INFRANAMESPACE:-ricinfra}
242   name: redis-kernel-optimizer
243 spec:
244   selector:
245     matchLabels:
246       app: redis-kernel-optimizer
247   template:
248     metadata:
249       labels:
250         app: redis-kernel-optimizer
251     spec:
252       volumes:
253       - name: sys
254         hostPath:
255           path: /sys
256       containers:
257       - name: disable-thp
258         image: ${LOCAL_REPOSITORY}busybox
259         securityContext:
260           runAsNonRoot: false
261           privileged: true
262           runAsUser: 0
263         command: ["sh", "-c"]
264         args:
265         - |-
266           set -e
267           set -o pipefail
268           trap 'exit' TERM
269           echo never > /rootfs/sys/kernel/mm/transparent_hugepage/enabled
270           echo never > /rootfs/sys/kernel/mm/transparent_hugepage/defrag
271           sysctl -w net.core.somaxconn=511
272           grep -q -F [never] /sys/kernel/mm/transparent_hugepage/enabled
273           grep -q -F [never] /sys/kernel/mm/transparent_hugepage/defrag
274           sysctl -n net.core.somaxconn | grep 511 -q
275           echo "done"
276           while true; do sleep 1; done
277         volumeMounts:
278         - name: sys
279           mountPath: /rootfs/sys
280 EOF
281 kubectl apply -f kernel_optimizer.yaml
282 wait_for_pods redis-kernel-optimizer ${INFRANAMESPACE:-ricinfra}
283 wait_for_cats redis-kernel-optimizer ${INFRANAMESPACE:-ricinfra}
284 kubectl delete -f kernel_optimizer.yaml
285 rm kernel_optimizer.yaml
286 fi
287
288
289 for component in $COMPONENTS; do
290     helm dep up $DIR/../helm/$component
291     COMPONENT="${RELEASE_PREFIX}-$component"
292     if [ -z $IS_HELM3 ]
293     then
294       COMPONENT=" --name $COMPONENT"
295     fi
296     helm install -f $OVERRIDEYAML --namespace "${PLTNAMESPACE:-ricplt}" $COMPONENT $DIR/../helm/$component
297     sleep 8
298 done
299
300
301
302