| garciadeblas | cf603f5 | 2025-06-04 11:57:28 +0200 | [diff] [blame] | 1 | #!/bin/bash |
| 2 | # |
| 3 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | # you may not use this file except in compliance with the License. |
| 5 | # You may obtain a copy of the License at |
| 6 | # |
| 7 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | # |
| 9 | # Unless required by applicable law or agreed to in writing, software |
| 10 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | # See the License for the specific language governing permissions and |
| 13 | # limitations under the License. |
| 14 | # |
| 15 | |
| 16 | set -e -o pipefail |
| 17 | |
| 18 | HERE=$(dirname "$(readlink --canonicalize "$BASH_SOURCE")") |
| 19 | source "${HERE}/../library/functions.sh" |
| 20 | source "${HERE}/../library/trap.sh" |
| 21 | source "${HERE}/../library/logging" |
| 22 | source "${HERE}/../library/track" |
| 23 | |
| 24 | source "${HERE}/00-default-install-options.rc" |
| 25 | [ ! -f "${OSM_HOME_DIR}/user-install-options.rc" ] || source "${OSM_HOME_DIR}/user-install-options.rc" |
| 26 | source "${CREDENTIALS_DIR}/git_environment.rc" |
| 27 | |
| 28 | OSM_HELM_WORK_DIR="/etc/osm/helm" |
| garciadeblas | cf603f5 | 2025-06-04 11:57:28 +0200 | [diff] [blame] | 29 | KUBECONFIG_AUX_CLUSTER_FILE="${OSM_HOME_DIR}/clusters/kubeconfig-aux-svc.yaml" |
| 30 | KUBECONFIG_MGMT_CLUSTER_FILE="${OSM_HOME_DIR}/clusters/kubeconfig-mgmt.yaml" |
| 31 | [ "${HERE}" == "/usr/share/osm-devops/installers" ] || OSM_HELM_UPDATE_DEPENDENCIES="y" |
| 32 | OSM_GITOPS_ENABLED=${INSTALL_MGMT_CLUSTER:-"y"} |
| 33 | |
| 34 | # TODO: move this to a parent script that creates the VM |
| 35 | mkdir -p "${OSM_HOME_DIR}/clusters" |
| 36 | if [ -n "${KUBECONFIG_OSM_CLUSTER}" ]; then |
| 37 | cp "${KUBECONFIG_OSM_CLUSTER}" "${OSM_HOME_DIR}/clusters/kubeconfig-osm.yaml" |
| 38 | else |
| 39 | cp "${HOME}/.kube/config" "${OSM_HOME_DIR}/clusters/kubeconfig-osm.yaml" |
| 40 | fi |
| 41 | |
| 42 | export KUBECONFIG="${OSM_HOME_DIR}/clusters/kubeconfig-osm.yaml" |
| garciadeblas | 8a28f6d | 2025-06-11 11:11:56 +0200 | [diff] [blame] | 43 | if [ -z "${OSM_K8S_NGINX_IPADDRESS}" ]; then |
| 44 | echo "OSM_K8S_NGINX_IPADDRESS is not set, will try to get it from the cluster" |
| 45 | OSM_K8S_NGINX_IPADDRESS=$(kubectl get svc ingress-nginx-controller -n ingress-nginx -o jsonpath='{.status.loadBalancer.ingress[0].ip}' 2>/dev/null) |
| 46 | fi |
| 47 | echo "Using OSM_K8S_NGINX_IPADDRESS=${OSM_K8S_NGINX_IPADDRESS}" |
| garciadeblas | cf603f5 | 2025-06-04 11:57:28 +0200 | [diff] [blame] | 48 | |
| 49 | # Create folder to store helm values |
| 50 | sudo mkdir -p ${OSM_HELM_WORK_DIR} |
| 51 | |
| 52 | # Saving secrets |
| 53 | echo "Creating namespace ${OSM_NAMESPACE}" |
| garciadeblas | 8a28f6d | 2025-06-11 11:11:56 +0200 | [diff] [blame] | 54 | kubectl get ns "${OSM_NAMESPACE}" >/dev/null 2>&1 || kubectl create ns "${OSM_NAMESPACE}" |
| garciadeblas | cf603f5 | 2025-06-04 11:57:28 +0200 | [diff] [blame] | 55 | echo "Saving age keys in OSM cluster" |
| garciadeblas | 8a28f6d | 2025-06-11 11:11:56 +0200 | [diff] [blame] | 56 | kubectl -n ${OSM_NAMESPACE} get secret mgmt-cluster-age-keys >/dev/null 2>&1 || \ |
| garciadeblas | cf603f5 | 2025-06-04 11:57:28 +0200 | [diff] [blame] | 57 | kubectl -n ${OSM_NAMESPACE} create secret generic mgmt-cluster-age-keys --from-file=privkey="${CREDENTIALS_DIR}/age.mgmt.key" --from-file=pubkey="${CREDENTIALS_DIR}/age.mgmt.pub" |
| 58 | echo "Creating secrets with kubeconfig files" |
| garciadeblas | 8a28f6d | 2025-06-11 11:11:56 +0200 | [diff] [blame] | 59 | if [ -f "${KUBECONFIG_AUX_CLUSTER_FILE}" ]; then |
| 60 | kubectl -n ${OSM_NAMESPACE} get secret auxcluster-secret >/dev/null 2>&1 || \ |
| 61 | kubectl -n ${OSM_NAMESPACE} create secret generic auxcluster-secret --from-file=kubeconfig="${KUBECONFIG_AUX_CLUSTER_FILE}" |
| 62 | fi |
| 63 | if [ -f "${KUBECONFIG_MGMT_CLUSTER_FILE}" ]; then |
| 64 | kubectl -n ${OSM_NAMESPACE} get secret mgmtcluster-secret >/dev/null 2>&1 || \ |
| 65 | kubectl -n ${OSM_NAMESPACE} create secret generic mgmtcluster-secret --from-file=kubeconfig="${KUBECONFIG_MGMT_CLUSTER_FILE}" |
| 66 | fi |
| garciadeblas | cf603f5 | 2025-06-04 11:57:28 +0200 | [diff] [blame] | 67 | # Update helm dependencies |
| 68 | [ -n "${OSM_HELM_UPDATE_DEPENDENCIES}" ] && \ |
| 69 | echo "Updating helm dependencies" && \ |
| 70 | helm dependency update "${HERE}/helm/osm" |
| 71 | |
| 72 | # Generate helm values to be passed with --set |
| 73 | OSM_HELM_OPTS="" |
| 74 | # OSM_HELM_OPTS="${OSM_HELM_OPTS} --set nbi.useOsmSecret=false" |
| 75 | |
| 76 | # TODO: review if next line is really needed or should be conditional to DOCKER_REGISTRY_URL not empty |
| 77 | # [ -n "${DOCKER_REGISTRY_URL}" ] && OSM_HELM_OPTS="${OSM_HELM_OPTS} --set global.image.repository=${DOCKER_REGISTRY_URL}${DOCKER_USER}" |
| 78 | OSM_HELM_OPTS="${OSM_HELM_OPTS} --set global.image.repositoryBase=${DOCKER_REGISTRY_URL}${DOCKER_USER}" |
| 79 | [ ! "$OSM_DOCKER_TAG" == "testing-daily" ] && OSM_HELM_OPTS="${OSM_HELM_OPTS} --set-string global.image.tag=${OSM_DOCKER_TAG}" |
| 80 | [ ! "$OSM_DOCKER_TAG" == "testing-daily" ] && OSM_HELM_OPTS="${OSM_HELM_OPTS} --set prometheus.server.sidecarContainers.prometheus-config-sidecar.image=${DOCKER_REGISTRY_URL}${DOCKER_USER}/prometheus:${OSM_DOCKER_TAG}" |
| 81 | |
| garciadeblas | 8a28f6d | 2025-06-11 11:11:56 +0200 | [diff] [blame] | 82 | OSM_BASE_DOMAIN="${OSM_BASE_DOMAIN:-"${OSM_K8S_NGINX_IPADDRESS}.nip.io"}" |
| 83 | OSM_HELM_OPTS="${OSM_HELM_OPTS} --set global.hostname=${OSM_BASE_DOMAIN}" |
| 84 | OSM_HELM_OPTS="${OSM_HELM_OPTS} --set grafana.ingress.hosts={grafana.${OSM_BASE_DOMAIN}}" |
| 85 | OSM_HELM_OPTS="${OSM_HELM_OPTS} --set prometheus.server.ingress.hosts={prometheus.${OSM_BASE_DOMAIN}}" |
| garciadeblas | 9b3ae03 | 2025-08-06 12:09:29 +0200 | [diff] [blame^] | 86 | OSM_HELM_OPTS="${OSM_HELM_OPTS} --set airflow.ingress.web.hosts={airflow.${OSM_BASE_DOMAIN}}" |
| garciadeblas | 8a28f6d | 2025-06-11 11:11:56 +0200 | [diff] [blame] | 87 | # OSM_HELM_OPTS="${OSM_HELM_OPTS} --set prometheus.alertmanager.ingress.hosts={alertmanager.${OSM_BASE_DOMAIN}}" |
| garciadeblas | cf603f5 | 2025-06-04 11:57:28 +0200 | [diff] [blame] | 88 | if [ -z "${OSM_GITOPS_ENABLED}" ]; then |
| 89 | OSM_HELM_OPTS="${OSM_HELM_OPTS} --set global.gitops.enabled=false" |
| 90 | else |
| garciadeblas | 823d8a3 | 2025-07-08 10:41:53 +0200 | [diff] [blame] | 91 | OSM_HELM_OPTS="${OSM_HELM_OPTS} --set global.gitops.gitBaseUrl=${GIT_BASE_HTTP_URL}" |
| garciadeblas | cf603f5 | 2025-06-04 11:57:28 +0200 | [diff] [blame] | 92 | OSM_HELM_OPTS="${OSM_HELM_OPTS} --set global.gitops.fleetRepoUrl=${FLEET_REPO_HTTP_URL}" |
| 93 | OSM_HELM_OPTS="${OSM_HELM_OPTS} --set global.gitops.swcatalogsRepoUrl=${SW_CATALOGS_REPO_HTTP_URL}" |
| 94 | # TODO: evaluate if we need to set two git user names, one for fleet and one for sw-catalogs |
| garciadeblas | 823d8a3 | 2025-07-08 10:41:53 +0200 | [diff] [blame] | 95 | OSM_HELM_OPTS="${OSM_HELM_OPTS} --set global.gitops.gitUser=${GIT_BASE_USERNAME}" |
| garciadeblas | cf603f5 | 2025-06-04 11:57:28 +0200 | [diff] [blame] | 96 | AGE_MGMT_PUBKEY=$(tr -d '\n' < ${CREDENTIALS_DIR}/age.mgmt.pub) |
| 97 | OSM_HELM_OPTS="${OSM_HELM_OPTS} --set global.gitops.pubkey=${AGE_MGMT_PUBKEY}" |
| 98 | fi |
| 99 | |
| 100 | if [ -n "${OSM_BEHIND_PROXY}" ]; then |
| 101 | OSM_HELM_OPTS="${OSM_HELM_OPTS} --set global.behindHttpProxy=true" |
| 102 | [ -n "${HTTP_PROXY}" ] && OSM_HELM_OPTS="${OSM_HELM_OPTS} --set global.httpProxy.HTTP_PROXY=\"${HTTP_PROXY}\"" |
| 103 | [ -n "${HTTPS_PROXY}" ] && OSM_HELM_OPTS="${OSM_HELM_OPTS} --set global.httpProxy.HTTPS_PROXY=\"${HTTPS_PROXY}\"" |
| 104 | if [ -n "${NO_PROXY}" ]; then |
| 105 | if [[ ! "${NO_PROXY}" =~ .*".svc".* ]]; then |
| 106 | NO_PROXY="${NO_PROXY},.svc" |
| 107 | fi |
| 108 | if [[ ! "${NO_PROXY}" =~ .*".cluster.local".* ]]; then |
| 109 | NO_PROXY="${NO_PROXY},.cluster.local" |
| 110 | fi |
| 111 | OSM_HELM_OPTS="${OSM_HELM_OPTS} --set global.httpProxy.NO_PROXY=\"${NO_PROXY//,/\,}\"" |
| 112 | fi |
| 113 | fi |
| 114 | |
| 115 | echo "helm upgrade --install -n $OSM_NAMESPACE --create-namespace $OSM_HELM_RELEASE ${HERE}/helm/osm ${OSM_HELM_OPTS}" |
| 116 | helm upgrade --install -n $OSM_NAMESPACE --create-namespace $OSM_HELM_RELEASE ${HERE}/helm/osm ${OSM_HELM_OPTS} |
| 117 | # Override existing values.yaml with the final values.yaml used to install OSM |
| 118 | helm -n $OSM_NAMESPACE get values $OSM_HELM_RELEASE | sudo tee -a ${OSM_HELM_WORK_DIR}/osm-values.yaml |
| 119 | |
| 120 | # Check OSM health state |
| 121 | echo -e "Checking OSM health state..." |
| 122 | set +e |
| 123 | ${HERE}/45-osm-health.sh || \ |
| 124 | (echo -e "OSM is not healthy, but will probably converge to a healthy state soon." && \ |
| 125 | echo -e "Check OSM status with: kubectl -n ${OSM_NAMESPACE} get all" && \ |
| 126 | track healthchecks osm_unhealthy didnotconverge) |
| 127 | track healthchecks after_healthcheck_ok |
| 128 | set -e |
| 129 | |
| 130 | echo -e "Saving OSM enviroment to credentials folder..." |
| 131 | OSM_HOSTNAME=$(kubectl get --namespace osm -o jsonpath="{.spec.rules[0].host}" ingress nbi-ingress) |
| garciadeblas | 8a28f6d | 2025-06-11 11:11:56 +0200 | [diff] [blame] | 132 | # OSM_HOSTNAME="nbi.${OSM_BASE_DOMAIN}:443" |
| garciadeblas | cf603f5 | 2025-06-04 11:57:28 +0200 | [diff] [blame] | 133 | echo -e "OSM HOSTNAME: ${OSM_HOSTNAME}" |
| 134 | |
| 135 | cat << EOF > "${CREDENTIALS_DIR}/osm_environment.rc" |
| 136 | export OSM_HOSTNAME="${OSM_HOSTNAME}" |
| 137 | EOF |