NONRTRIC-946: Servicemanager - add Kong data plane and control plane
[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     if [ ! -f ../../.env ]; then
24         cp -v ../../.env.example ../../.env
25         sed -i 's/KONG_DOMAIN=<string>/KONG_DOMAIN=kong/' ../../.env
26         sed -i 's/KONG_PROTOCOL=<http or https protocol scheme>/KONG_PROTOCOL=http/' ../../.env
27         sed -i 's/KONG_CONTROL_PLANE_IPV4=<host string>/KONG_CONTROL_PLANE_IPV4=kong-kong-admin.kong.svc.cluster.local/' ../../.env
28         sed -i 's/KONG_CONTROL_PLANE_PORT=<port number>/KONG_CONTROL_PLANE_PORT=8001/' ../../.env
29         sed -i 's/KONG_DATA_PLANE_IPV4=<host string>/KONG_DATA_PLANE_IPV4=kong-kong-proxy.kong.svc.cluster.local/' ../../.env
30         sed -i 's/KONG_DATA_PLANE_PORT=<port number>/KONG_DATA_PLANE_PORT=80/' ../../.env
31         sed -i 's/CAPIF_PROTOCOL=<http or https protocol scheme>/CAPIF_PROTOCOL=http/' ../../.env
32         sed -i 's/CAPIF_IPV4=<host>/CAPIF_IPV4=capifcore.servicemanager.svc.cluster.local/' ../../.env
33         sed -i 's/CAPIF_PORT=<port number>/CAPIF_PORT=8090/' ../../.env
34         sed -i 's/LOG_LEVEL=<Trace, Debug, Info, Warning, Error, Fatal or Panic>/LOG_LEVEL=Info/' ../../.env
35         sed -i 's/SERVICE_MANAGER_PORT=<port number>/SERVICE_MANAGER_PORT=8095/' ../../.env
36         sed -i 's/TEST_SERVICE_IPV4=<host string>/TEST_SERVICE_IPV4=10.101.1.101/' ../../.env
37         sed -i 's/TEST_SERVICE_PORT=<port number>/TEST_SERVICE_PORT=30951/' ../../.env
38         echo "created .env"
39     else
40         echo "found .env"
41     fi
42 }
43
44 substitute_repos(){
45     echo "substitute_repos"
46     docker_repo=$1
47
48     # Use our own Capificore and ServiceManager images
49     sed -i "s*nexus3.o-ran-sc.org:10004/o-ran-sc/nonrtric-plt-capifcore:CAPIF_VERSION*$docker_repo/capifcore:latest*" ../manifests/capifcore.yaml
50     sed -i "s*nexus3.o-ran-sc.org:10004/o-ran-sc/nonrtric-plt-servicemanager:SERVICEMANAGER_VERSION*$docker_repo/servicemanager:latest*" ../manifests/servicemanager.yaml
51
52     sed -i 's*imagePullPolicy: IfNotPresent*imagePullPolicy: Always*' ../manifests/capifcore.yaml
53     sed -i 's*imagePullPolicy: IfNotPresent*imagePullPolicy: Always*' ../manifests/servicemanager.yaml
54 }
55
56 substitute_repo_versions(){
57     echo "substitute_repo_versions"
58     servicemanager_version=$(awk '/tag:/{print $2}' ../../container-tag.yaml)
59     capif_version=$(awk '/tag:/{print $2}' ../../../capifcore/container-tag.yaml)
60     # Set the Capificore and ServiceManager image versions
61     sed -i "s*nexus3.o-ran-sc.org:10004/o-ran-sc/nonrtric-plt-capifcore:CAPIF_VERSION*nexus3.o-ran-sc.org:10004/o-ran-sc/nonrtric-plt-capifcore:$capif_version*" ../manifests/capifcore.yaml
62     sed -i "s*nexus3.o-ran-sc.org:10004/o-ran-sc/nonrtric-plt-servicemanager:SERVICEMANAGER_VERSION*nexus3.o-ran-sc.org:10004/o-ran-sc/nonrtric-plt-servicemanager:$servicemanager_version*" ../manifests/servicemanager.yaml
63 }
64
65 add_env(){
66     echo "add_env"
67     # Our additional .env has to exist in the project root folder
68     additional_env="../../$1"
69
70     # Add our own .env file
71     if [ -f $additional_env ]; then
72         echo "found additional env $1"
73         kubectl create configmap env-configmap --from-file=../../.env --from-file=$additional_env -n servicemanager
74
75         # Add additional env file to volume mounting
76         env_filename=$(basename "$additional_env")
77         echo "env_filename $env_filename"
78
79         mount_path_wc=$(grep "mountPath: /app/servicemanager/$env_filename" ../manifests/servicemanager.yaml | wc -l)
80         env_path_count=$((mount_path_wc))
81         if [ $env_path_count -eq 0 ]; then
82             echo "Adding mount path"
83             sed -i -e '/subPath: .env/a\' \
84                 -e "        - name: config-volume\n          mountPath: /app/servicemanager/$env_filename\n          subPath: $env_filename" ../manifests/servicemanager.yaml
85         fi
86
87         # Update SERVICE_MANAGER_ENV to point to additional env
88         env_extension=$(basename "$additional_env" | awk -F. '{print $NF}')
89         echo "SERVICE_MANAGER_ENV=$env_extension"
90         sed -i "/- name: SERVICE_MANAGER_ENV/{n;s/              value: \"\"/              value: \"$env_extension\"/}" ../manifests/servicemanager.yaml
91         return 0  # Return zero for success
92     else
93         echo "additional env $additional_env NOT found"
94         return 1  # Return non-zero for failure
95     fi
96 }
97
98 echo $(date -u) "deploy-to-k8s started"
99
100 # Check if the development switch is provided as a command-line argument
101 USE_OWN_REPO=false
102 ADD_ENV=false
103
104 while [[ "$#" -gt 0 ]]; do
105     case "$1" in
106         -r|--repo)
107             USE_OWN_REPO=true
108             shift  # consume the switch
109             if [ -n "$1" ]; then
110                 DOCKER_REPO="$1"
111                 shift  # consume the value
112             else
113                 echo "Error: Argument for $1 is missing." >&2
114                 exit 1
115             fi
116             ;;
117         -e|--env)
118             ADD_ENV=true
119             shift  # consume the switch
120             if [ -n "$1" ]; then
121                 ENV_PATH="$1"
122                 shift  # consume the value
123             else
124                 echo "Error: Argument for $1 is missing." >&2
125                 exit 1
126             fi
127             ;;
128         *)
129             echo "Unknown argument: $1"
130             exit 1
131             ;;
132     esac
133 done
134
135 kubectl create ns kong
136
137 # Set up storage for Postgres, used by Kong
138 # Minikube uses dynamic provisioning
139 CURRENT_CONTEXT=$(kubectl config current-context)
140 if [ "$CURRENT_CONTEXT" != "minikube" ]; then
141     kubectl create -f ../manifests/kong-postgres-pv.yaml
142 fi
143 kubectl create -f ../manifests/kong-postgres-pvc.yaml
144
145 # Deploy Kong
146 helm repo add kong https://charts.konghq.com
147 helm repo update
148 helm install kong kong/kong -n kong -f ../manifests/values.yaml
149
150 create_env_from_template
151
152 # Check if the development switch is enabled
153 if [ "$USE_OWN_REPO" = true ]; then
154     substitute_repos $DOCKER_REPO
155 else
156     substitute_repo_versions
157 fi
158
159 kubectl create ns servicemanager
160
161 if [ "$ADD_ENV" = true ]; then
162     add_env $ENV_PATH
163     # Check if the function failed
164     if [ $? -ne 0 ]; then
165         kubectl create configmap env-configmap --from-file=../../.env -n servicemanager
166     fi
167 else
168     kubectl create configmap env-configmap --from-file=../../.env -n servicemanager
169 fi
170
171 # Create the Kubernetes resources
172 kubectl create -f ../manifests/capifcore.yaml
173 kubectl create -f ../manifests/servicemanager.yaml
174
175 kubectl rollout status deployment capifcore -n servicemanager --timeout=90s
176 kubectl rollout status deployment servicemanager -n servicemanager --timeout=90s
177 kubectl rollout status deployment kong-kong -n kong --timeout=90s
178
179 echo $(date -u) "deploy-to-k8s completed"