9e81d61fe9a6cce98c0a197056f06f97d2ceaa42
[nonrtric/plt/sme.git] / servicemanager / deploy / src / deploy-to-k8s.sh
1 #!/bin/bash
2 ##############################################################################
3 #
4 #   Copyright (C) 2024: OpenInfra Foundation Europe
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
20 create_env_from_template(){
21     # Set up .env file for Kubernetes Config Map
22     echo "create_env_from_template"
23     cp -v ../../.env.example ./.env
24     sed -i 's/KONG_DOMAIN=<string>/KONG_DOMAIN=kong/' .env
25     sed -i 's/KONG_PROTOCOL=<http or https protocol scheme>/KONG_PROTOCOL=http/' .env
26     sed -i 's/KONG_IPV4=<host string>/KONG_IPV4=10.101.1.101/' .env
27     sed -i 's/KONG_DATA_PLANE_PORT=<port number>/KONG_DATA_PLANE_PORT=32080/' .env
28     sed -i 's/KONG_CONTROL_PLANE_PORT=<port number>/KONG_CONTROL_PLANE_PORT=32081/' .env
29     sed -i 's/CAPIF_PROTOCOL=<http or https protocol scheme>/CAPIF_PROTOCOL=http/' .env
30     sed -i 's/CAPIF_IPV4=<host>/CAPIF_IPV4=10.101.1.101/' .env
31     sed -i 's/CAPIF_PORT=<port number>/CAPIF_PORT=31570/' .env
32     sed -i 's/LOG_LEVEL=<Trace, Debug, Info, Warning, Error, Fatal or Panic>/LOG_LEVEL=Info/' .env
33     sed -i 's/SERVICE_MANAGER_PORT=<port number>/SERVICE_MANAGER_PORT=8095/' .env
34     sed -i 's/TEST_SERVICE_IPV4=<host string>/TEST_SERVICE_IPV4=10.101.1.101/' .env
35     sed -i 's/TEST_SERVICE_PORT=<port number>/TEST_SERVICE_PORT=30951/' .env
36 }
37
38 substitute_repo(){
39     echo "substitute_repo"
40     docker_repo=$1
41
42     # Use our own Capificore and ServiceManager images
43     sed -i "s/image: o-ran-sc.org\/nonrtric\/plt\/capifcore/image: $docker_repo\/capifcore:latest/" ../manifests/capifcore.yaml
44     sed -i 's/imagePullPolicy: IfNotPresent/imagePullPolicy: Always/' ../manifests/capifcore.yaml
45     sed -i "s/image: o-ran-sc.org\/nonrtric\/plt\/servicemanager/image: $docker_repo\/servicemanager:latest/" ../manifests/servicemanager.yaml
46     sed -i 's/imagePullPolicy: IfNotPresent/imagePullPolicy: Always/' ../manifests/servicemanager.yaml
47 }
48
49 add_env(){
50     echo "add_env"
51     additional_env=$1
52     # Add our own .env file
53     if [ -f $additional_env ]; then
54         echo "found additional env"
55         kubectl create configmap env-configmap --from-file=.env --from-file=$additional_env -n servicemanager
56
57         # Add additional env file to volume mounting
58         env_filename=$(basename "$additional_env")
59         echo "env_filename $env_filename"
60
61         mount_path_wc=$(grep "mountPath: /app/$env_filename" ../manifests/servicemanager.yaml | wc -l)
62         env_path_count=$((mount_path_wc))
63         if [ $env_path_count -eq 0 ]; then
64             echo "Adding mount path"
65             sed -i -e '/subPath: .env/a\' -e "        - name: config-volume\n          mountPath: /app/$env_filename\n          subPath: $env_filename" ../manifests/servicemanager.yaml
66         fi
67
68         # Update SERVICE_MANAGER_ENV to point to additional env
69         env_extension=$(basename "$additional_env" | awk -F. '{print $NF}')
70         echo "env_extension $env_extension"
71
72         sed -i "/- name: SERVICE_MANAGER_ENV/{n;s/              value: \"\"/              value: \"$env_extension\"/}" ../manifests/servicemanager.yaml
73     fi
74 }
75
76 echo $(date -u) "deploy-to-k8s started"
77
78 # Check if the development switch is provided as a command-line argument
79 USE_OWN_REPO=false
80 ADD_ENV=false
81
82 while [[ "$#" -gt 0 ]]; do
83     case "$1" in
84         -r|--repo)
85             USE_OWN_REPO=true
86             shift  # consume the switch
87             if [ -n "$1" ]; then
88                 DOCKER_REPO="$1"
89                 shift  # consume the value
90             else
91                 echo "Error: Argument for $1 is missing." >&2
92                 exit 1
93             fi
94             ;;
95         -e|--env)
96             ADD_ENV=true
97             shift  # consume the switch
98             if [ -n "$1" ]; then
99                 ENV_PATH="$1"
100                 shift  # consume the value
101             else
102                 echo "Error: Argument for $1 is missing." >&2
103                 exit 1
104             fi
105             ;;
106         *)
107             echo "Unknown argument: $1"
108             exit 1
109             ;;
110     esac
111 done
112
113 kubectl create ns kong
114
115 # Set up storage for Postgres, used by Kong
116 # Minikube uses dynamic provisioning
117 CURRENT_CONTEXT=$(kubectl config current-context)
118 if [ "$CURRENT_CONTEXT" != "minikube" ]; then
119     kubectl create -f ../manifests/kong-postgres-pv.yaml
120 fi
121 kubectl create -f ../manifests/kong-postgres-pvc.yaml
122
123 # Deploy Kong
124 helm repo add kong https://charts.konghq.com
125 helm repo update
126 helm install kong kong/kong -n kong -f ../manifests/values.yaml
127
128 create_env_from_template
129
130 # Check if the development switch is enabled
131 if [ "$USE_OWN_REPO" = true ]; then
132     substitute_repo $DOCKER_REPO
133 fi
134
135 kubectl create ns servicemanager
136
137 if [ "$ADD_ENV" = true ]; then
138     add_env $ENV_PATH
139 else
140     kubectl create configmap env-configmap --from-file=.env -n servicemanager
141 fi
142
143 # Create the Kubernetes resources
144 kubectl create -f ../manifests/capifcore.yaml
145 kubectl create -f ../manifests/servicemanager.yaml
146
147 kubectl rollout status deployment capifcore -n servicemanager --timeout=90s
148 kubectl rollout status deployment servicemanager -n servicemanager --timeout=90s
149 kubectl rollout status deployment kong-kong -n kong --timeout=90s
150
151 echo $(date -u) "deploy-to-k8s completed"