istio
130 строк · 3.2 Кб
1// Copyright Istio Authors
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15package archive
16
17import (
18"archive/tar"
19"compress/gzip"
20"io"
21"os"
22"path/filepath"
23"strings"
24"sync"
25)
26
27const (
28bugReportSubdir = "bug-report"
29proxyLogsPathSubdir = "proxies"
30istioLogsPathSubdir = "istio"
31clusterInfoSubdir = "cluster"
32analyzeSubdir = "analyze"
33operatorLogsPathSubdir = "operator"
34cniLogsPathSubdir = "cni"
35)
36
37var (
38tmpDir string
39initDir sync.Once
40)
41
42// DirToArchive is the dir to archive.
43func DirToArchive(rootDir string) string {
44return filepath.Dir(getRootDir(rootDir))
45}
46
47// OutputRootDir is the root dir of output artifacts.
48func OutputRootDir(rootDir string) string {
49return getRootDir(rootDir)
50}
51
52func ProxyOutputPath(rootDir, namespace, pod string) string {
53return filepath.Join(getRootDir(rootDir), proxyLogsPathSubdir, namespace, pod)
54}
55
56func IstiodPath(rootDir, namespace, pod string) string {
57return filepath.Join(getRootDir(rootDir), istioLogsPathSubdir, namespace, pod)
58}
59
60func OperatorPath(rootDir, namespace, pod string) string {
61return filepath.Join(getRootDir(rootDir), operatorLogsPathSubdir, namespace, pod)
62}
63
64func AnalyzePath(rootDir, namespace string) string {
65return filepath.Join(getRootDir(rootDir), analyzeSubdir, namespace)
66}
67
68func ClusterInfoPath(rootDir string) string {
69return filepath.Join(getRootDir(rootDir), clusterInfoSubdir)
70}
71
72func CniPath(rootDir, pod string) string {
73return filepath.Join(getRootDir(rootDir), cniLogsPathSubdir, pod)
74}
75
76// Create creates a gzipped tar file from srcDir and writes it to outPath.
77func Create(srcDir, outPath string) error {
78mw, err := os.Create(outPath)
79if err != nil {
80return err
81}
82defer mw.Close()
83gzw := gzip.NewWriter(mw)
84defer gzw.Close()
85
86tw := tar.NewWriter(gzw)
87defer tw.Close()
88
89return filepath.Walk(srcDir, func(file string, fi os.FileInfo, err error) error {
90if err != nil {
91return err
92}
93if !fi.Mode().IsRegular() {
94return nil
95}
96header, err := tar.FileInfoHeader(fi, fi.Name())
97if err != nil {
98return err
99}
100header.Name = strings.TrimPrefix(strings.Replace(file, srcDir, "", -1), string(filepath.Separator))
101header.Size = fi.Size()
102header.Mode = int64(fi.Mode())
103header.ModTime = fi.ModTime()
104if err := tw.WriteHeader(header); err != nil {
105return err
106}
107
108f, err := os.Open(file)
109if err != nil {
110return err
111}
112defer f.Close()
113
114if _, err := io.Copy(tw, f); err != nil {
115return err
116}
117return nil
118})
119}
120
121func getRootDir(rootDir string) string {
122if rootDir != "" {
123return rootDir
124}
125initDir.Do(func() {
126// Extra subdir so archive extracts under new ./bug-report subdir.
127tmpDir = filepath.Join(os.TempDir(), bugReportSubdir, bugReportSubdir)
128})
129return tmpDir
130}
131