-
Notifications
You must be signed in to change notification settings - Fork 28
/
cluster.go
212 lines (190 loc) · 6.13 KB
/
cluster.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
package manager
import (
"context"
"fmt"
"io"
"os"
"path/filepath"
"strings"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
"gopkg.in/yaml.v2"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"
k8sjson "k8s.io/apimachinery/pkg/runtime/serializer/json"
"k8s.io/client-go/rest"
"github.com/rancher/support-bundle-kit/pkg/manager/collectors"
"github.com/rancher/support-bundle-kit/pkg/utils"
)
type Cluster struct {
sbm *SupportBundleManager
}
func NewCluster(ctx context.Context, sbm *SupportBundleManager) *Cluster {
return &Cluster{
sbm: sbm,
}
}
func (c *Cluster) GenerateClusterBundle(bundleDir string) (string, error) {
logrus.Debug("Generating cluster bundle...")
namespace, err := c.sbm.k8s.GetNamespace(c.sbm.PodNamespace)
if err != nil {
return "", errors.Wrap(err, "cannot get deployed namespace")
}
kubeVersion, err := c.sbm.k8s.GetKubernetesVersion()
if err != nil {
return "", errors.Wrap(err, "cannot get kubernetes version")
}
sb, err := c.sbm.state.GetSupportBundle(c.sbm.PodNamespace, c.sbm.BundleName)
if err != nil {
return "", errors.Wrap(err, "cannot get support bundle")
}
bundleMeta := &BundleMeta{
BundleName: sb.Name,
BundleVersion: BundleVersion,
KubernetesVersion: kubeVersion.GitVersion,
ProjectNamespaceUUID: string(namespace.UID),
BundleCreatedAt: utils.Now(),
IssueURL: sb.Spec.IssueURL,
IssueDescription: sb.Spec.Description,
}
bundleName := fmt.Sprintf("supportbundle_%s_%s.zip",
bundleMeta.ProjectNamespaceUUID,
strings.Replace(bundleMeta.BundleCreatedAt, ":", "-", -1))
errLog, err := os.Create(filepath.Join(bundleDir, "bundleGenerationError.log"))
if err != nil {
logrus.Errorf("Failed to create bundle generation log")
return "", err
}
defer errLog.Close()
metaFile := filepath.Join(bundleDir, "metadata.yaml")
encodeToYAMLFile(bundleMeta, metaFile, errLog)
yamlsDir := filepath.Join(bundleDir, "yamls")
var modules []interface{}
for _, moduleName := range c.sbm.BundleCollectors {
module := collectors.InitModuleCollector(moduleName, yamlsDir, c.sbm.Namespaces, c.sbm.discovery, c.matchesExcludeResources, encodeToYAMLFile, errLog)
modules = append(modules, module)
}
collectors.GetAllSupportBundleYAMLs(modules)
logsDir := filepath.Join(bundleDir, "logs")
c.generateSupportBundleLogs(logsDir, errLog)
return bundleName, nil
}
// matchesExcludeResources returns true if given resource group version mathces our ExcludeResources list.
func (c *Cluster) matchesExcludeResources(gv schema.GroupVersion, resource metav1.APIResource) bool {
for _, excludeResource := range c.sbm.ExcludeResources {
if gv.Group == excludeResource.Group && resource.Name == excludeResource.Resource {
return true
}
}
return false
}
func encodeToYAMLFile(obj interface{}, path string, errLog io.Writer) {
var err error
defer func() {
if err != nil {
fmt.Fprintf(errLog, "Support Bundle: failed to generate %v: %v\n", path, err)
}
}()
err = os.MkdirAll(filepath.Dir(path), os.FileMode(0755))
if err != nil {
return
}
f, err := os.Create(path)
if err != nil {
return
}
defer f.Close()
switch v := obj.(type) {
case runtime.Object:
serializer := k8sjson.NewSerializerWithOptions(k8sjson.DefaultMetaFactory, nil, nil, k8sjson.SerializerOptions{
// only a subset of yaml that matches JSON is generated
// https://github.com/kubernetes/apimachinery/blob/1af25b613b6482b465c4bf23501a9b02acdb3c0c/pkg/runtime/serializer/json/json.go#L86
Yaml: true,
Pretty: true,
Strict: true,
})
if err = serializer.Encode(v, f); err != nil {
return
}
default:
encoder := yaml.NewEncoder(f)
if err = encoder.Encode(obj); err != nil {
return
}
if err = encoder.Close(); err != nil {
return
}
}
}
type GetRuntimeObjectListFunc func() (runtime.Object, error)
func (c *Cluster) generateSupportBundleLogs(logsDir string, errLog io.Writer) {
namespaces := []string{"default", "kube-system", "cattle-system"}
namespaces = append(namespaces, c.sbm.Namespaces...)
for _, ns := range namespaces {
list, err := c.sbm.k8s.GetAllPodsList(ns)
if err != nil {
fmt.Fprintf(errLog, "Support bundle: cannot get pod list: %v\n", err)
return
}
podList, ok := list.(*corev1.PodList)
if !ok {
fmt.Fprintf(errLog, "BUG: Support bundle: didn't get pod list\n")
return
}
for _, pod := range podList.Items {
podName := pod.Name
podDir := filepath.Join(logsDir, ns, podName)
for _, container := range pod.Spec.Containers {
req := c.sbm.k8s.GetPodContainerLogRequest(ns, podName, container.Name)
getLogToFile(podDir, podName, container.Name, req, c.sbm.context, errLog, false)
restartCount, err := c.sbm.k8s.GetPodRestartCount(ns, podName, container.Name)
if err != nil {
fmt.Fprintf(errLog, "Cannot get pod `%s` info (error: %v), just continue", podName, err)
continue
}
if restartCount > 0 {
req := c.sbm.k8s.GetPodContainerPreviousLogRequest(ns, podName, container.Name)
getLogToFile(podDir, podName, container.Name, req, c.sbm.context, errLog, true)
}
}
}
}
}
func getLogToFile(podDir, podName, containerName string, req *rest.Request, sbmContext context.Context, errLog io.Writer, previousLog bool) {
logFileName := filepath.Join(podDir, containerName+".log")
if previousLog {
logFileName = filepath.Join(podDir, containerName+".log.1")
}
stream, err := req.Stream(sbmContext)
if err != nil {
fmt.Fprintf(errLog, "BUG: Support bundle: cannot get log for pod %v container %v: %v\n",
podName, containerName, err)
return
}
logrus.Debugf("Prepare to log to file: %s", logFileName)
streamLogToFile(stream, logFileName, errLog)
stream.Close()
}
func streamLogToFile(logStream io.ReadCloser, path string, errLog io.Writer) {
var err error
defer func() {
if err != nil {
fmt.Fprintf(errLog, "Support Bundle: failed to generate %v: %v\n", path, err)
}
}()
err = os.MkdirAll(filepath.Dir(path), os.FileMode(0755))
if err != nil {
return
}
f, err := os.Create(path)
if err != nil {
return
}
defer f.Close()
_, err = io.Copy(f, logStream)
if err != nil {
return
}
}