-
Notifications
You must be signed in to change notification settings - Fork 28
/
process_node_zips.go
292 lines (250 loc) · 6.92 KB
/
process_node_zips.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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
package objects
import (
"archive/zip"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"strings"
"time"
"github.com/sirupsen/logrus"
v1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
bundlekit "github.com/rancher/support-bundle-kit/pkg/simulator/apis/supportbundlekit.io/v1"
"github.com/rancher/support-bundle-kit/pkg/simulator/crd"
)
const (
DefaultNodeDir = "nodes"
DefaultPodNamespace = "support-bundle-node-info"
)
var NodeInfoNS = v1.Namespace{
TypeMeta: metav1.TypeMeta{
Kind: "Namespace",
APIVersion: "v1",
},
ObjectMeta: metav1.ObjectMeta{
Name: DefaultPodNamespace,
},
}
var NodeInfoSA = v1.ServiceAccount{
TypeMeta: metav1.TypeMeta{
Kind: "ServiceAccount",
APIVersion: "v1",
},
ObjectMeta: metav1.ObjectMeta{
Name: "default",
Namespace: DefaultPodNamespace,
},
Secrets: []v1.ObjectReference{
{
Name: "default",
Kind: "Secret",
APIVersion: "v1",
Namespace: DefaultPodNamespace,
},
},
}
var NodeInfoSASecret = v1.Secret{
TypeMeta: metav1.TypeMeta{
Kind: "Secret",
APIVersion: "v1",
},
ObjectMeta: metav1.ObjectMeta{
Name: "default",
Namespace: DefaultPodNamespace,
},
StringData: make(map[string]string),
}
// CreateNodeZipObjects will create associated objects in the cluster
func (o *ObjectManager) CreateNodeZipObjects() error {
noStatusObjs, withStatusObjs, err := o.ProcessNodeZipObjects()
if err != nil {
return err
}
err = o.ApplyObjects(noStatusObjs, false, nil)
if err != nil {
return err
}
return o.ApplyObjects(withStatusObjs, true, nil)
}
// ProcessNodeZipObjects will read the contents of the zip file and generate associated runtime.Objects
func (o *ObjectManager) ProcessNodeZipObjects() (noStatusObjs []runtime.Object, withStatusObjs []runtime.Object, err error) {
// unzip files
// drop them into the yamls folder
// clean up later
bundleAbsPath, err := filepath.Abs(o.path)
if err != nil {
return noStatusObjs, withStatusObjs, fmt.Errorf("error evaulating absolute path to node dirs: %v", err)
}
crdObjects, err := crd.Objects(false)
if err != nil {
return noStatusObjs, withStatusObjs, fmt.Errorf("error generating CRD objects: %v", err)
}
nodeZipList, err := generateNodeZipList(bundleAbsPath)
if err != nil {
return noStatusObjs, withStatusObjs, err
}
podList, nodeConfig, err := generateObjects(nodeZipList)
if err != nil {
return noStatusObjs, withStatusObjs, err
}
noStatusObjs = []runtime.Object{
&NodeInfoNS, &NodeInfoSASecret, &NodeInfoSA,
}
noStatusObjs = append(noStatusObjs, crdObjects...)
for _, v := range podList {
withStatusObjs = append(withStatusObjs, v)
}
for _, v := range nodeConfig {
noStatusObjs = append(noStatusObjs, v)
}
return noStatusObjs, withStatusObjs, nil
}
func generateNodeZipList(bundleAbsPath string) ([]string, error) {
var nodeZipList []string
err := filepath.Walk(filepath.Join(bundleAbsPath, DefaultNodeDir), func(path string, info os.FileInfo, err error) error {
if err != nil {
fmt.Printf("prevent panic by handling failure accessing a path %q: %v\n", path, err)
return err
}
if !info.IsDir() && strings.Contains(path, ".zip") {
absPath, err := filepath.Abs(path)
if err != nil {
return err
}
nodeZipList = append(nodeZipList, absPath)
}
return nil
})
if err != nil {
return nodeZipList, fmt.Errorf("error during dir walk: %v", err)
}
return nodeZipList, nil
}
func generateObjects(nodeZipList []string) ([]*v1.Pod, []*bundlekit.NodeConfig, error) {
var podList []*v1.Pod
var configList []*bundlekit.NodeConfig
for _, zipFile := range nodeZipList {
pod, nodeconfig, err := walkZipFiles(zipFile)
if err != nil {
return nil, nil, err
}
// ignore node.zip files where no containers were found
if pod != nil {
podList = append(podList, pod)
}
configList = append(configList, nodeconfig)
}
return podList, configList, nil
}
func walkZipFiles(zipFile string) (*v1.Pod, *bundlekit.NodeConfig, error) {
nodeName := strings.Split(zipFile, ".zip")
pod := &v1.Pod{
TypeMeta: metav1.TypeMeta{
Kind: "Pod",
APIVersion: "v1",
},
ObjectMeta: metav1.ObjectMeta{
Name: filepath.Base(nodeName[0]),
Namespace: DefaultPodNamespace,
},
}
nConfig := &bundlekit.NodeConfig{
TypeMeta: metav1.TypeMeta{
Kind: "NodeConfig",
APIVersion: "supportbundlekit.io/v1",
},
ObjectMeta: metav1.ObjectMeta{
Name: filepath.Base(nodeName[0]),
Namespace: DefaultPodNamespace,
},
}
r, err := zip.OpenReader(zipFile)
if err != nil {
return pod, nConfig, fmt.Errorf("error opening zip file %v:", err)
}
defer r.Close()
var nodeConfigSpec []bundlekit.NodeConfigSpec
var containers []v1.Container
// iterate files into pod and nodeConfig object
for _, f := range r.File {
// config files
if strings.Contains(f.Name, "configs") && !f.FileInfo().IsDir() {
contentBytes, err := ReadContent(f)
if err != nil {
return pod, nConfig, err
}
ncSpec := bundlekit.NodeConfigSpec{
FileName: f.Name,
Content: string(contentBytes),
}
nodeConfigSpec = append(nodeConfigSpec, ncSpec)
}
// generate pod object, skip parent directory
if strings.Contains(f.Name, "logs") && !f.FileInfo().IsDir() {
containerName := strings.Split(f.Name, ".log")
c := v1.Container{
Name: filepath.Base(containerName[0]),
Image: "noimage",
}
containers = append(containers, c)
}
}
nConfig.Spec = nodeConfigSpec
if len(containers) == 0 {
logrus.Warnf("No pod being created for node %s as zip file has no log files associated with this node", filepath.Base(nodeName[0]))
return nil, nConfig, nil
}
pod.Spec.Containers = containers
pod.Spec.NodeName = filepath.Base(nodeName[0])
pod.Status = *generatePodStatus(pod)
return pod, nConfig, nil
}
func ReadContent(f *zip.File) ([]byte, error) {
zipReader, err := f.Open()
if err != nil {
return nil, fmt.Errorf("error reading file %s: %v", f.Name, err)
}
defer zipReader.Close()
contentBytes, err := ioutil.ReadAll(zipReader)
return contentBytes, err
}
func generatePodStatus(pod *v1.Pod) *v1.PodStatus {
podStatus := pod.Status.DeepCopy()
podStatus.Phase = v1.PodRunning
podStatus.StartTime = &metav1.Time{Time: time.Now()}
podStatus.Conditions = []v1.PodCondition{
v1.PodCondition{
Type: v1.PodReady,
Status: v1.ConditionTrue,
},
v1.PodCondition{
Type: v1.PodScheduled,
Status: v1.ConditionTrue,
},
v1.PodCondition{
Type: v1.PodInitialized,
Status: v1.ConditionTrue,
},
v1.PodCondition{
Type: v1.ContainersReady,
Status: v1.ConditionTrue,
},
}
var cStats []v1.ContainerStatus
for _, c := range pod.Spec.Containers {
cStats = append(cStats, v1.ContainerStatus{
Name: c.Name,
Ready: true,
RestartCount: 0,
State: v1.ContainerState{
Running: &v1.ContainerStateRunning{
StartedAt: metav1.Time{Time: time.Now()},
},
},
})
}
podStatus.ContainerStatuses = cStats
return podStatus
}