|
| 1 | +#!/usr/bin/env bash |
| 2 | + |
| 3 | +# Licensed Materials - Property of IBM |
| 4 | +# Copyright IBM Corporation 2023. All Rights Reserved |
| 5 | +# US Government Users Restricted Rights - |
| 6 | +# Use, duplication or disclosure restricted by GSA ADP Schedule Contract with IBM Corp. |
| 7 | +# |
| 8 | +# This is an internal component, bundled with an official IBM product. |
| 9 | +# Please refer to that particular license for additional information. |
| 10 | + |
| 11 | +set -o errtrace |
| 12 | +set -o nounset |
| 13 | + |
| 14 | +# ---------- Command arguments ---------- |
| 15 | +OC=oc |
| 16 | +LSR_NAMESPACE="ibm-lsr" |
| 17 | + |
| 18 | +# Catalog sources and namespace |
| 19 | +ENABLE_PRIVATE_CATALOG=0 |
| 20 | +LSR_SOURCE="ibm-license-service-reporter-catalog" |
| 21 | +LSR_SOURCE_NS="openshift-marketplace" |
| 22 | + |
| 23 | +# ---------- Command variables ---------- |
| 24 | + |
| 25 | +# script base directory |
| 26 | +BASE_DIR=$(cd $(dirname "$0")/$(dirname "$(readlink $0)") && pwd -P) |
| 27 | + |
| 28 | +# ---------- Main functions ---------- |
| 29 | +function main() { |
| 30 | + parse_arguments "$@" |
| 31 | + pre_req |
| 32 | + label_catalogsource |
| 33 | + label_ns_and_related |
| 34 | + label_subscription |
| 35 | + label_lsr |
| 36 | + success "Successfully labeled all the resources" |
| 37 | +} |
| 38 | + |
| 39 | +function print_usage(){ #TODO update usage definition |
| 40 | + script_name=`basename ${0}` |
| 41 | + echo "Usage: ${script_name} [OPTIONS]" |
| 42 | + echo "" |
| 43 | + echo "Label License Service Reporter resources to prepare for Backup." |
| 44 | + echo "License Service Reporter namespace is always required." |
| 45 | + echo "" |
| 46 | + echo "Options:" |
| 47 | + echo " --oc string Optional. File path to oc CLI. Default uses oc in your PATH. Can also be set in env.properties." |
| 48 | + echo " --lsr-ns Optional. Specifying will enable labeling of the license service reporter operator. Permissions may need to be updated to include the namespace." |
| 49 | + echo " --enable-private-catalog Optional. Specifying will look for catalog sources in the operator namespace. If enabled, will look for license service reporter in its respective namespaces." |
| 50 | + echo " --lsr-catalog Optional. Specifying will look for the license service reporter catalog source name." |
| 51 | + echo " --lsr-catalog-ns Optional. Specifying will look for the license service reporter catalog source namespace." |
| 52 | + echo " -h, --help Print usage information" |
| 53 | + echo "" |
| 54 | + |
| 55 | +} |
| 56 | + |
| 57 | +function parse_arguments() { |
| 58 | + script_name=`basename ${0}` |
| 59 | + echo "All arguments passed into the ${script_name}: $@" |
| 60 | + echo "" |
| 61 | + |
| 62 | + # process options |
| 63 | + while [[ "$@" != "" ]]; do |
| 64 | + case "$1" in |
| 65 | + --oc) |
| 66 | + shift |
| 67 | + OC=$1 |
| 68 | + ;; |
| 69 | + --lsr-ns ) |
| 70 | + shift |
| 71 | + LSR_NAMESPACE=$1 |
| 72 | + ;; |
| 73 | + --enable-private-catalog) |
| 74 | + ENABLE_PRIVATE_CATALOG=1 |
| 75 | + ;; |
| 76 | + --lsr-catalog) |
| 77 | + shift |
| 78 | + LSR_SOURCE=$1 |
| 79 | + ;; |
| 80 | + --lsr-catalog-ns) |
| 81 | + shift |
| 82 | + LSR_SOURCE_NS=$1 |
| 83 | + ;; |
| 84 | + -h | --help) |
| 85 | + print_usage |
| 86 | + exit 1 |
| 87 | + ;; |
| 88 | + *) |
| 89 | + echo "Entered option $1 not supported. Run ./${script_name} -h for script usage info." |
| 90 | + ;; |
| 91 | + esac |
| 92 | + shift |
| 93 | + done |
| 94 | + echo "" |
| 95 | +} |
| 96 | + |
| 97 | +function pre_req(){ |
| 98 | + |
| 99 | + title "Start to validate the parameters passed into script... " |
| 100 | + # Checking oc command logged in |
| 101 | + user=$($OC whoami 2> /dev/null) |
| 102 | + if [ $? -ne 0 ]; then |
| 103 | + error "You must be logged into the OpenShift Cluster from the oc command line" |
| 104 | + else |
| 105 | + success "oc command logged in as ${user}" |
| 106 | + fi |
| 107 | +} |
| 108 | + |
| 109 | +function label_catalogsource() { |
| 110 | + |
| 111 | + title "Start to label the License Service Reporter catalog sources... " |
| 112 | + # Label the Private CatalogSources in provided namespaces |
| 113 | + if [ $ENABLE_PRIVATE_CATALOG -eq 1 ]; then |
| 114 | + LSR_SOURCE_NS=$LSR_NAMESPACE |
| 115 | + fi |
| 116 | + ${OC} label catalogsource "$LSR_SOURCE" foundationservices.cloudpak.ibm.com=lsr -n "$CSR_SOURCE_NS" --overwrite=true 2>/dev/null |
| 117 | + echo "" |
| 118 | +} |
| 119 | + |
| 120 | +function label_ns_and_related() { |
| 121 | + |
| 122 | + title "Start to label the namespaces, operatorgroups... " |
| 123 | + |
| 124 | + # Label the cert manager namespace |
| 125 | + ${OC} label namespace "$LSR_NAMESPACE" foundationservices.cloudpak.ibm.com=lsr --overwrite=true 2>/dev/null |
| 126 | + |
| 127 | + # Label the cert manager OperatorGroup |
| 128 | + operator_group=$(${OC} get operatorgroup -n "$LSR_NAMESPACE" -o jsonpath='{.items[*].metadata.name}') |
| 129 | + ${OC} label operatorgroup "$operator_group" foundationservices.cloudpak.ibm.com=lsr -n "$LSR_NAMESPACE" --overwrite=true 2>/dev/null |
| 130 | + |
| 131 | + echo "" |
| 132 | +} |
| 133 | + |
| 134 | + |
| 135 | +function label_subscription() { |
| 136 | + |
| 137 | + title "Start to label the Subscriptions... " |
| 138 | + local lsr_pm="ibm-license-service-reporter-operator" |
| 139 | + ${OC} label subscriptions.operators.coreos.com $lsr_pm foundationservices.cloudpak.ibm.com=lsr -n $LSR_NAMESPACE --overwrite=true 2>/dev/null |
| 140 | + echo "" |
| 141 | +} |
| 142 | + |
| 143 | +function label_lsr() { |
| 144 | + |
| 145 | + title "Start to label the License Service Reporter... " |
| 146 | + ${OC} label customresourcedefinition ibmlicenseservicereporters.operator.ibm.com foundationservices.cloudpak.ibm.com=lsr --overwrite=true 2>/dev/null |
| 147 | + |
| 148 | + info "Start to label the LSR instances" |
| 149 | + lsr_instances=$(${OC} get ibmlicenseservicereporters.operator.ibm.com -n $LSR_NAMESPACE -o jsonpath='{.items[*].metadata.name}') |
| 150 | + while IFS= read -r lsr_instance; do |
| 151 | + ${OC} label ibmlicenseservicereporters.operator.ibm.com $lsr_instance foundationservices.cloudpak.ibm.com=lsr -n $LSR_NAMESPACE --overwrite=true 2>/dev/null |
| 152 | + |
| 153 | + # Label the secrets with OIDC configured |
| 154 | + client_secret_name=$(${OC} get ibmlicenseservicereporters.operator.ibm.com $lsr_instance -n $LSR_NAMESPACE -o yaml | awk -F '--client-secret-name=' '{print $2}' | tr -d '"' | tr -d '\n') |
| 155 | + ${OC} label secret $client_secret_name foundationservices.cloudpak.ibm.com=lsr -n $LSR_NAMESPACE --overwrite=true 2>/dev/null |
| 156 | + |
| 157 | + provider_ca_secret_name=$(${OC} get ibmlicenseservicereporters.operator.ibm.com $lsr_instance -n $LSR_NAMESPACE -o yaml | awk -F '--provider-ca-secret-name=' '{print $2}' | tr -d '"' | tr -d '\n') |
| 158 | + ${OC} label secret $provider_ca_secret_name foundationservices.cloudpak.ibm.com=lsr -n $LSR_NAMESPACE --overwrite=true 2>/dev/null |
| 159 | + done <<< "$lsr_instances" |
| 160 | + |
| 161 | + info "Start to label the necessary secrets" |
| 162 | + secrets=$(${OC} get secrets -n $LSR_NAMESPACE | grep ibm-license-service-reporter-token | cut -d ' ' -f1) |
| 163 | + for secret in ${secrets[@]}; do |
| 164 | + ${OC} label secret $secret foundationservices.cloudpak.ibm.com=lsr -n $LSR_NAMESPACE --overwrite=true 2>/dev/null |
| 165 | + done |
| 166 | + secrets=$(${OC} get secrets -n $LSR_NAMESPACE | grep ibm-license-service-reporter-credential | cut -d ' ' -f1) |
| 167 | + for secret in ${secrets[@]}; do |
| 168 | + ${OC} label secret $secret foundationservices.cloudpak.ibm.com=lsr -n $LSR_NAMESPACE --overwrite=true 2>/dev/null |
| 169 | + done |
| 170 | + |
| 171 | + echo "" |
| 172 | +} |
| 173 | + |
| 174 | +# ---------- Info functions ----------# |
| 175 | + |
| 176 | +function msg() { |
| 177 | + printf '%b\n' "$1" |
| 178 | +} |
| 179 | + |
| 180 | +function success() { |
| 181 | + msg "\33[32m[✔] ${1}\33[0m" |
| 182 | +} |
| 183 | + |
| 184 | +function error() { |
| 185 | + msg "\33[31m[✘] ${1}\33[0m" |
| 186 | + exit 1 |
| 187 | +} |
| 188 | + |
| 189 | +function title() { |
| 190 | + msg "\33[34m# ${1}\33[0m" |
| 191 | +} |
| 192 | + |
| 193 | +function info() { |
| 194 | + msg "[INFO] ${1}" |
| 195 | +} |
| 196 | + |
| 197 | +function warning() { |
| 198 | + msg "\33[33m[✗] ${1}\33[0m" |
| 199 | +} |
| 200 | + |
| 201 | +main $* |
| 202 | + |
| 203 | +# ---------------- finish ---------------- |
0 commit comments