-
Notifications
You must be signed in to change notification settings - Fork 1
/
k3s.go
102 lines (90 loc) · 2.22 KB
/
k3s.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
package publish
import (
"bufio"
"context"
"fmt"
"os"
"path/filepath"
"regexp"
"github.com/dbason/opni-supportagent/pkg/input"
"github.com/dbason/opni-supportagent/pkg/util"
)
func ShipK3SControlPlane(
ctx context.Context,
endpoint string,
clusterName string,
nodeName string,
username string,
password string,
) error {
var (
dateFile *os.File
timezone string
year string
err error
)
// Extract timezone and year from the date output
if _, err = os.Stat("systeminfo/date"); err == nil {
dateFile, err = os.Open("systeminfo/date")
if err != nil {
return err
}
defer dateFile.Close()
}
scanner := bufio.NewScanner(dateFile)
scanner.Scan()
dateline := scanner.Text()
re := regexp.MustCompile(`^[A-Z][a-z]{2} [A-Z][a-z]{2} \d{1,2} \d{2}:\d{2}:\d{2} ([A-Z]{3}) (\d{4})`)
matches := re.FindStringSubmatch(dateline)
if len(matches) != 0 {
timezone = matches[1]
year = matches[2]
}
if _, err := os.Stat("journald/k3s"); err != nil {
return err
}
opensearch, err := input.NewOpensearchInput(ctx, endpoint, username, password, input.OpensearchConfig{
ClusterID: clusterName,
Paths: []string{"journald/k3s"},
Component: "k3s",
NodeName: nodeName,
})
if err != nil {
return err
}
journaldParser := input.NewDateZoneParser(timezone, year, input.JournaldRegex, input.JournaldLayout)
_, _, err = opensearch.Publish(journaldParser, input.LogTypeControlplane)
if err != nil {
return err
}
files, err := filepath.Glob("k3s/podlogs/cattle-system-rancher-*")
if err != nil {
util.Log.Errorf("unable to list rancher files: %s", err)
return nil
}
parser := &input.MultipleParser{
Dateformats: []input.Dateformat{
{
DateRegex: input.RancherRegex,
Layout: input.RancherLayout,
},
{
DateRegex: input.KlogRegex,
Layout: input.KlogLayout,
DateSuffix: fmt.Sprintf(" %s %s", timezone, year),
},
},
}
rancher, err := input.NewOpensearchInput(ctx, endpoint, username, password, input.OpensearchConfig{
ClusterID: clusterName,
NodeName: nodeName,
Component: "",
Paths: files,
})
if err != nil {
return err
}
util.Log.Info("publishing rancher server logs")
_, _, err = rancher.Publish(parser, input.LogTypeRancher)
return err
}