...

Text file src/github.com/GoogleCloudPlatform/k8s-config-connector/scripts/cluster-troubleshoot.sh

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

     1#!/usr/bin/env bash
     2# Copyright 2022 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
    16# This script is intended as a diagnostic tool to:
    17#  - catch common issues with Config Connector
    18#  - extract diagnostic data that helps with debugging
    19#
    20# It assumes the following:
    21# - kubectl is configured to operate on the cluster running Config Connector
    22#
    23# Conventions:
    24# - section headers ARE ALL CAPS
    25#
    26# Example output for successful run:
    27#
    28#    KUBERNETES VERSION:
    29#    Client Version: v1.16.4
    30#    Server Version: v1.17.14-gke.1600
    31#
    32#    CONFIG CONNECTOR VERSION:
    33#    1.32.0
    34#
    35#    CONFIG CONNECTOR MODE:
    36#    namespaced
    37#
    38#    installation verified.
    39
    40CONTACT_SUPPORT="
    41If you have followed troubleshooting instructions and are still unable to
    42resolve the issue, contact GCP Support (https://cloud.google.com/support-hub) or
    43file an issue on GitHub (https://github.com/GoogleCloudPlatform/k8s-config-connector/issues)
    44including the output of this script for further assistance."
    45
    46function main {
    47    print_kubernetes_version
    48    echo
    49    print_config_connector_version
    50    echo
    51    print_config_connector_mode
    52    echo
    53    echo
    54
    55    if ! installation_is_correct; then
    56        echo "$CONTACT_SUPPORT"
    57        return 1
    58    else
    59        echo "installation verified."
    60    fi
    61}
    62
    63function print_kubernetes_version {
    64    echo "KUBERNETES VERSION:"
    65    kubectl version --short
    66}
    67
    68function print_config_connector_version {
    69    echo "CONFIG CONNECTOR VERSION:"
    70    kubectl get ns cnrm-system -o jsonpath='{.metadata.annotations.cnrm\.cloud\.google\.com/version}'
    71    echo
    72}
    73
    74function print_config_connector_mode {
    75    echo "CONFIG CONNECTOR MODE:"
    76    kubectl get ConfigConnector "configconnector.core.cnrm.cloud.google.com" -o=jsonpath="{@.spec.mode}"
    77}
    78
    79function installation_is_correct {
    80    verify_cnrm_system_namespace_exists && \
    81    verify_config_connector_operator_installed && \
    82    verify_config_connector_kind_exists && \
    83    verify_cnrm_system_status
    84}
    85
    86function verify_cnrm_system_namespace_exists {
    87    local error_message="
    88\"cnrm-system\" NAMESPACE NOT FOUND.
    89
    90The cnrm-system namespace should be created as part of the installation process.
    91This namespace is where Config Connector controllers run.
    92
    93to troubleshoot: run through the installation instructions again."
    94
    95    if ! kubectl get namespace cnrm-system > /dev/null; then
    96        echo "$error_message"
    97        return 1
    98    fi
    99}
   100
   101function verify_config_connector_kind_exists {
   102    local error_message="
   103ConfigConnector KIND NOT FOUND.
   104
   105A resource of kind ConfigConnector must be applied in order for the Config Controller
   106operator to spawn it's resources, and start to reconcile resources.
   107
   108remediation:
   109
   110See \"Configuring Config Connector\":
   111  https://cloud.google.com/config-connector/docs/how-to/install-upgrade-uninstall#addon-configuring".
   112
   113    if [ "$(kubectl get ConfigConnector)" = "" ]; then
   114        echo "$error_message"
   115        return 1
   116    fi
   117}
   118
   119function verify_config_connector_operator_installed {
   120    local error_message="
   121CONFIG CONNECTOR OPERATOR NOT FOUND
   122
   123As of 1.33, the operator (or the GKE add-on) is the only supported
   124method to install Config Conector.
   125
   126remediation:
   127
   128If you have previously installed and verified your Config Connector installation, upgrade to the config connector
   129operator as stated in https://cloud.google.com/config-connector/docs/how-to/advanced-install#upgrading_from_non-operator_installations.
   130
   131Otherwise, you may not have installed Config Connector to the cluster.
   132See https://cloud.google.com/config-connector/docs/how-to/install-upgrade-uninstall."
   133    if [[ "$(kubectl get pod -n configconnector-operator-system | wc -l)" == 0 ]]; then
   134        echo "$error_message"
   135        return 1
   136    fi
   137}
   138
   139function verify_cnrm_system_status {
   140    local error_message="
   141NON-RUNNING PODS DETECTED IN \"cnrm-system\" NAMESPACE
   142
   143The cnrm-system namespace is where Config Connector resources
   144required to properly operate are installed.
   145
   146If there are resources that are unhealthy in this namespace,
   147Config Connector will not operate properly.
   148
   149to troubleshoot: examine the output below."
   150    local output
   151    output="$(kubectl get pods -n cnrm-system --field-selector status.phase!=Running 2>/dev/null)"
   152    if [ "$output" != "" ]; then
   153        echo \
   154"$error_message
   155
   156non-running pods:
   157$output"
   158        return 1
   159    fi
   160}
   161
   162main

View as plain text