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