|
| 1 | +/* |
| 2 | + * Copyright 2018- The Pixie Authors. |
| 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 | + * SPDX-License-Identifier: Apache-2.0 |
| 17 | + */ |
| 18 | + |
| 19 | +package vizier |
| 20 | + |
| 21 | +import ( |
| 22 | + "archive/zip" |
| 23 | + "context" |
| 24 | + "errors" |
| 25 | + "os" |
| 26 | + "strings" |
| 27 | + |
| 28 | + log "github.com/sirupsen/logrus" |
| 29 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 30 | + "k8s.io/client-go/kubernetes" |
| 31 | + "k8s.io/client-go/rest" |
| 32 | + |
| 33 | + "px.dev/pixie/src/utils/script" |
| 34 | + "px.dev/pixie/src/utils/shared/k8s" |
| 35 | +) |
| 36 | + |
| 37 | +// LogCollector collect logs for Pixie and cluster setup information. |
| 38 | +type LogCollector struct { |
| 39 | + k8sConfig *rest.Config |
| 40 | + k8sClientSet *kubernetes.Clientset |
| 41 | + cloudAddr string |
| 42 | + br *script.BundleManager |
| 43 | + k8s.LogCollector |
| 44 | +} |
| 45 | + |
| 46 | +// NewLogCollector creates a new log collector. |
| 47 | +func NewLogCollector(br *script.BundleManager, cloudAddr string) *LogCollector { |
| 48 | + cfg := k8s.GetConfig() |
| 49 | + cs := k8s.GetClientset(cfg) |
| 50 | + return &LogCollector{ |
| 51 | + cfg, |
| 52 | + cs, |
| 53 | + cloudAddr, |
| 54 | + br, |
| 55 | + *k8s.NewLogCollector(), |
| 56 | + } |
| 57 | +} |
| 58 | + |
| 59 | +// CollectPixieLogs collects logs for all Pixie pods and write them to the zip file fName. |
| 60 | +func (c *LogCollector) CollectPixieLogs(fName string) error { |
| 61 | + if !strings.HasSuffix(fName, ".zip") { |
| 62 | + return errors.New("fname must have .zip suffix") |
| 63 | + } |
| 64 | + f, err := os.Create(fName) |
| 65 | + if err != nil { |
| 66 | + return err |
| 67 | + } |
| 68 | + defer f.Close() |
| 69 | + |
| 70 | + zf := zip.NewWriter(f) |
| 71 | + defer zf.Close() |
| 72 | + |
| 73 | + vls := k8s.VizierLabelSelector() |
| 74 | + vizierLabelSelector := metav1.FormatLabelSelector(&vls) |
| 75 | + |
| 76 | + // We check across all namespaces for the matching pixie pods. |
| 77 | + vizierPodList, err := c.k8sClientSet.CoreV1().Pods("").List(context.Background(), metav1.ListOptions{LabelSelector: vizierLabelSelector}) |
| 78 | + if err != nil { |
| 79 | + return err |
| 80 | + } |
| 81 | + |
| 82 | + // We also need to get the logs the operator logs. |
| 83 | + // As the LabelSelectors are ANDed, we need to make a new query and merge |
| 84 | + // the results. |
| 85 | + ols := k8s.OperatorLabelSelector() |
| 86 | + operatorLabelSelector := metav1.FormatLabelSelector(&ols) |
| 87 | + |
| 88 | + operatorPodList, err := c.k8sClientSet.CoreV1().Pods("").List(context.Background(), metav1.ListOptions{LabelSelector: operatorLabelSelector}) |
| 89 | + if err != nil { |
| 90 | + return err |
| 91 | + } |
| 92 | + |
| 93 | + // Merge the two pod lists |
| 94 | + pods := append(vizierPodList.Items, operatorPodList.Items...) |
| 95 | + |
| 96 | + for _, pod := range pods { |
| 97 | + for _, containerStatus := range pod.Status.ContainerStatuses { |
| 98 | + // Ignore prev logs, they might not exist. |
| 99 | + _ = c.LogPodInfoToZipFile(zf, pod, containerStatus.Name, true) |
| 100 | + |
| 101 | + err := c.LogPodInfoToZipFile(zf, pod, containerStatus.Name, false) |
| 102 | + if err != nil { |
| 103 | + log.WithError(err).Warnf("Failed to log pod: %s", pod.Name) |
| 104 | + } |
| 105 | + } |
| 106 | + err = c.WritePodDescription(zf, pod) |
| 107 | + if err != nil { |
| 108 | + log.WithError(err).Warnf("failed to write pod description") |
| 109 | + } |
| 110 | + } |
| 111 | + |
| 112 | + err = c.LogKubeCmd(zf, "nodes.log", "describe", "node") |
| 113 | + if err != nil { |
| 114 | + log.WithError(err).Warn("failed to log node info") |
| 115 | + } |
| 116 | + |
| 117 | + err = c.LogKubeCmd(zf, "services.log", "describe", "services", "--all-namespaces", "-l", vizierLabelSelector) |
| 118 | + if err != nil { |
| 119 | + log.WithError(err).Warnf("failed to log services") |
| 120 | + } |
| 121 | + |
| 122 | + // Describe vizier and write it to vizier.log |
| 123 | + err = c.LogKubeCmd(zf, "vizier.log", "describe", "vizier", "--all-namespaces") |
| 124 | + if err != nil { |
| 125 | + log.WithError(err).Warnf("failed to log vizier crd") |
| 126 | + } |
| 127 | + |
| 128 | + clusterID, err := GetCurrentVizier(c.cloudAddr) |
| 129 | + if err != nil { |
| 130 | + log.WithError(err).Warnf("failed to get cluster ID") |
| 131 | + } |
| 132 | + outputCh, err := RunSimpleHealthCheckScript(c.br, c.cloudAddr, clusterID) |
| 133 | + |
| 134 | + if err != nil { |
| 135 | + entry := log.WithError(err) |
| 136 | + if _, ok := err.(*HealthCheckWarning); ok { |
| 137 | + entry.Warn("healthcheck script detected the following warnings:") |
| 138 | + } else { |
| 139 | + entry.Warn("failed to run healthcheck script") |
| 140 | + } |
| 141 | + } |
| 142 | + |
| 143 | + return c.LogOutputToZipFile(zf, "px_agent_diagnostics.txt", <-outputCh) |
| 144 | +} |
0 commit comments