...

Text file src/github.com/GoogleCloudPlatform/k8s-config-connector/scripts/resource-autogen/generate-resources.sh

Documentation: github.com/GoogleCloudPlatform/k8s-config-connector/scripts/resource-autogen

     1#!/bin/bash
     2# Copyright 2023 Google LLC
     3#
     4# Licensed under the Apache License, Version 2.0 (the "License");
     5# you may not use this file except in compliance with the License.
     6# You may obtain a copy of the License at
     7#
     8#      http://www.apache.org/licenses/LICENSE-2.0
     9#
    10# Unless required by applicable law or agreed to in writing, software
    11# distributed under the License is distributed on an "AS IS" BASIS,
    12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13# See the License for the specific language governing permissions and
    14# limitations under the License.
    15
    16set -o errexit
    17set -o nounset
    18set -o pipefail
    19
    20while [[ $# -gt 0 ]]; do
    21  case "${1}" in
    22    --cnrm-repo)  LOCAL_CNRM_REPO="${2:-}"; shift ;;
    23    --mm-repo)    LOCAL_MM_REPO="${2:-}"; shift ;;
    24    *)            break
    25  esac
    26  shift
    27done
    28
    29echo "Ensure you have set up magic-modules locally following the guide: https://googlecloudplatform.github.io/magic-modules/docs/getting-started/setup/ !"
    30echo "This script usually takes a few minutes to finish."
    31LOCAL_CNRM_REPO="${LOCAL_CNRM_REPO:-}"
    32LOCAL_MM_REPO="${LOCAL_MM_REPO:-}"
    33
    34if [[ "${LOCAL_CNRM_REPO}" == "" ]] && [[ "${CNRM_REPO:-}" != "" ]]; then
    35    echo "Using environment variable CNRM_REPO for LOCAL_CNRM_REPO..."
    36    LOCAL_CNRM_REPO="${CNRM_REPO}"
    37fi
    38if [[ "${LOCAL_CNRM_REPO}" == "" ]]; then
    39    echo "Absolute path to local CNRM repository is required. You should either set it via --cnrm-repo flag, or using environment variable CNRM_REPO ('export CNRM_REPO=[path/to/cnrm/repo]')."
    40    exit 1
    41fi
    42if [[ "${LOCAL_MM_REPO}" == "" ]] && [[ "${MM_REPO:-}" != "" ]]; then
    43    echo "Using environment variable MM_REPO for LOCAL_MM_REPO..."
    44    LOCAL_MM_REPO="${MM_REPO}"
    45fi
    46if [[ "${LOCAL_MM_REPO}" == "" ]]; then
    47    echo "Absolute path to local magic-modules repository is required. You should either set it via --mm-repo flag, or using environment variable MM_REPO ('export MM_REPO=[path/to/magic-modules/repo]')."
    48    exit 1
    49fi
    50
    51echo "Variables..."
    52echo "LOCAL_CNRM_REPO=${LOCAL_CNRM_REPO}"
    53echo "LOCAL_MM_REPO=${LOCAL_MM_REPO}"
    54
    55echo "Cleaning up the existing auto-generated files..."
    56CD_GENERATED_COMMAND="cd ${LOCAL_CNRM_REPO}/scripts/resource-autogen/generated"
    57echo "${CD_GENERATED_COMMAND}"
    58eval "${CD_GENERATED_COMMAND}"
    59RM_COMMAND="rm -r ./samples ./servicemappings"
    60echo "${RM_COMMAND}"
    61eval "${RM_COMMAND}"
    62
    63echo "Generating service mappings using the KCC provider in magic-modules..."
    64CD_MM_COMMAND="cd ${LOCAL_MM_REPO}/mmv1"
    65echo "${CD_MM_COMMAND}"
    66eval "${CD_MM_COMMAND}"
    67
    68# If generating resources based on services/products, the patches won't be
    69# applied correctly.
    70EXEC_COMMAND="bundle exec compiler -e terraform -a -f kcc -o ${LOCAL_CNRM_REPO}/scripts/resource-autogen/generated -v beta"
    71echo "${EXEC_COMMAND}"
    72eval "${EXEC_COMMAND}"
    73
    74echo "Applying patches to the generated service mappings in cnrm..."
    75CD_AUTOGEN_COMMAND="cd ${LOCAL_CNRM_REPO}/scripts/resource-autogen"
    76echo "${CD_AUTOGEN_COMMAND}"
    77eval "${CD_AUTOGEN_COMMAND}"
    78
    79MAKE_APPLY_PATCHES_COMMAND="make apply-autogen-patches"
    80echo "${MAKE_APPLY_PATCHES_COMMAND}"
    81eval "${MAKE_APPLY_PATCHES_COMMAND}"
    82
    83echo "Converting TF samples to KCC testdata for allowlisted resources..."
    84CD_CNRM_COMMAND="cd ${LOCAL_CNRM_REPO}"
    85echo "${CD_CNRM_COMMAND}"
    86eval "${CD_CNRM_COMMAND}"
    87
    88MAKE_GENERATE_COMMAND="make generate"
    89echo "${MAKE_GENERATE_COMMAND}"
    90eval "${MAKE_GENERATE_COMMAND}"
    91
    92# It's possible to have errors when generating the testdata, but the errors
    93# shouldn't block this script.
    94CONVERT_SAMPLES_COMMAND="go run scripts/resource-autogen/main.go"
    95echo "${CONVERT_SAMPLES_COMMAND}"
    96eval "${CONVERT_SAMPLES_COMMAND} || true"
    97
    98echo "Generating CRDs for allowlisted resources..."
    99GENERATE_CRDS_COMMAND="make manifests"
   100echo "${GENERATE_CRDS_COMMAND}"
   101eval "${GENERATE_CRDS_COMMAND}"

View as plain text