|
| 1 | +#!/usr/bin/env bash |
| 2 | + |
| 3 | +# Copyright 2020 The Kubernetes Authors. |
| 4 | +# |
| 5 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | +# you may not use this file except in compliance with the License. |
| 7 | +# You may obtain a copy of the License at |
| 8 | +# |
| 9 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +# |
| 11 | +# Unless required by applicable law or agreed to in writing, software |
| 12 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | +# See the License for the specific language governing permissions and |
| 15 | +# limitations under the License. |
| 16 | + |
| 17 | +set -o errexit |
| 18 | +set -o nounset |
| 19 | +set -o pipefail |
| 20 | + |
| 21 | +REPO_ROOT=$(dirname "${BASH_SOURCE[0]}")/../.. |
| 22 | +cd "${REPO_ROOT}" || exit 1 |
| 23 | + |
| 24 | +# shellcheck source=../hack/ensure-kind.sh |
| 25 | +source "${REPO_ROOT}/hack/ensure-kind.sh" |
| 26 | +# shellcheck source=../hack/ensure-kubectl.sh |
| 27 | +source "${REPO_ROOT}/hack/ensure-kubectl.sh" |
| 28 | + |
| 29 | +export ARTIFACTS="${ARTIFACTS:-${PWD}/_artifacts}" |
| 30 | +mkdir -p "${ARTIFACTS}/management-cluster" "${ARTIFACTS}/workload-cluster" |
| 31 | + |
| 32 | +export KUBECONFIG="${KUBECONFIG:-${PWD}/kubeconfig}" |
| 33 | + |
| 34 | +get_node_name() { |
| 35 | + local -r pod_name="${1}" |
| 36 | + echo "$(kubectl get pod "${pod_name}" -ojsonpath={.spec.nodeName})" |
| 37 | +} |
| 38 | + |
| 39 | +dump_mgmt_cluster_logs() { |
| 40 | + # Assume the first kind cluster is the management cluster |
| 41 | + local -r mgmt_cluster_name="$(kind get clusters | head -n 1)" |
| 42 | + if [[ -z "${mgmt_cluster_name}" ]]; then |
| 43 | + echo "No kind cluster is found" |
| 44 | + return |
| 45 | + fi |
| 46 | + |
| 47 | + kind get kubeconfig --name "${mgmt_cluster_name}" > "${PWD}/kind.kubeconfig" |
| 48 | + local -r kubectl_kind="kubectl --kubeconfig=${PWD}/kind.kubeconfig" |
| 49 | + |
| 50 | + local -r resources=( |
| 51 | + "clusters" |
| 52 | + "azureclusters" |
| 53 | + "machines" |
| 54 | + "azuremachines" |
| 55 | + "kubeadmconfigs" |
| 56 | + "machinedeployments" |
| 57 | + "azuremachinetemplates" |
| 58 | + "kubeadmconfigtemplates" |
| 59 | + "machinesets" |
| 60 | + "kubeadmcontrolplanes" |
| 61 | + "machinepools" |
| 62 | + "azuremachinepools" |
| 63 | + ) |
| 64 | + mkdir -p "${ARTIFACTS}/management-cluster/resources" |
| 65 | + for resource in "${resources[@]}"; do |
| 66 | + ${kubectl_kind} get --all-namespaces "${resource}" -oyaml > "${ARTIFACTS}/management-cluster/resources/${resource}.log" || true |
| 67 | + done |
| 68 | + |
| 69 | + { |
| 70 | + echo "images in docker" |
| 71 | + docker images |
| 72 | + echo "images in bootstrap cluster using kubectl CLI" |
| 73 | + (${kubectl_kind} get pods --all-namespaces -ojson \ |
| 74 | + | jq --raw-output '.items[].spec.containers[].image' | sort) |
| 75 | + echo "images in deployed cluster using kubectl CLI" |
| 76 | + (${kubectl_kind} get pods --all-namespaces -ojson \ |
| 77 | + | jq --raw-output '.items[].spec.containers[].image' | sort) |
| 78 | + } > "${ARTIFACTS}/management-cluster/images.info" |
| 79 | + |
| 80 | + { |
| 81 | + echo "kind cluster-info" |
| 82 | + ${kubectl_kind} cluster-info dump |
| 83 | + } > "${ARTIFACTS}/management-cluster/kind-cluster.info" |
| 84 | + |
| 85 | + kind export logs --name="${mgmt_cluster_name}" "${ARTIFACTS}/management-cluster" |
| 86 | +} |
| 87 | + |
| 88 | +dump_workload_cluster_logs() { |
| 89 | + echo "Deploying log-dump-daemonset" |
| 90 | + kubectl apply -f "${REPO_ROOT}/hack/log/log-dump-daemonset.yaml" |
| 91 | + kubectl wait pod -l app=log-dump-node --for=condition=Ready --timeout=5m |
| 92 | + |
| 93 | + local -r log_dump_pods=( $(kubectl get pod -l app=log-dump-node -ojsonpath={.items[*].metadata.name}) ) |
| 94 | + local log_dump_commands=( |
| 95 | + "journalctl --output=short-precise -u kubelet > kubelet.log" |
| 96 | + "journalctl --output=short-precise -u containerd > containerd.log" |
| 97 | + "journalctl --output=short-precise -k > kern.log" |
| 98 | + "journalctl --output=short-precise > journal.log" |
| 99 | + "cat /var/log/cloud-init.log > cloud-init.log" |
| 100 | + "cat /var/log/cloud-init-output.log > cloud-init-output.log" |
| 101 | + ) |
| 102 | + |
| 103 | + if [[ "$(uname)" == "Darwin" ]]; then |
| 104 | + # tar on Mac OS does not support --wildcards flag |
| 105 | + log_dump_commands+=( "tar -cf - var/log/pods --ignore-failed-read | tar xf - --strip-components=2 -C . '*kube-system*'" ) |
| 106 | + else |
| 107 | + log_dump_commands+=( "tar -cf - var/log/pods --ignore-failed-read | tar xf - --strip-components=2 -C . --wildcards '*kube-system*'" ) |
| 108 | + fi |
| 109 | + |
| 110 | + for log_dump_pod in "${log_dump_pods[@]}"; do |
| 111 | + local node_name="$(get_node_name "${log_dump_pod}")" |
| 112 | + |
| 113 | + local log_dump_dir="${ARTIFACTS}/workload-cluster/${node_name}" |
| 114 | + mkdir -p "${log_dump_dir}" |
| 115 | + pushd "${log_dump_dir}" > /dev/null |
| 116 | + for cmd in "${log_dump_commands[@]}"; do |
| 117 | + bash -c "kubectl exec ${log_dump_pod} -- ${cmd}" & |
| 118 | + done |
| 119 | + |
| 120 | + popd > /dev/null |
| 121 | + echo "Exported logs for node \"${node_name}\"" |
| 122 | + done |
| 123 | + |
| 124 | + # Wait for log-dumping commands running in the background to complete |
| 125 | + wait |
| 126 | +} |
| 127 | + |
| 128 | +cleanup() { |
| 129 | + kubectl delete -f "${REPO_ROOT}/hack/log/log-dump-daemonset.yaml" |
| 130 | + source "${REPO_ROOT}/hack/log/redact.sh" |
| 131 | +} |
| 132 | + |
| 133 | +trap cleanup EXIT |
| 134 | + |
| 135 | +echo "================ DUMPING LOGS FOR MANAGEMENT CLUSTER ================" |
| 136 | +dump_mgmt_cluster_logs |
| 137 | + |
| 138 | +echo "================ DUMPING LOGS FOR WORKLOAD CLUSTER ================" |
| 139 | +dump_workload_cluster_logs |
0 commit comments