-
Notifications
You must be signed in to change notification settings - Fork 0
/
clusterinventorysvc.go
103 lines (93 loc) · 3.52 KB
/
clusterinventorysvc.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
package services
import (
"database/sql"
"fmt"
"strings"
_ "github.com/go-sql-driver/mysql"
"github.com/tcotav/k8siinv/types"
)
type ClusterInventoryService struct {
db *sql.DB
}
func NewClusterInventoryService(db *sql.DB) *ClusterInventoryService {
return &ClusterInventoryService{db: db}
}
func (c *ClusterInventoryService) saveCluster(clusterName string, generatedAt string) (int64, error) {
// couple parts -- want to save the cluster image metadata
stmt, err := c.db.Prepare("INSERT INTO clusters(name, firstSeen, lastSeen) VALUES(?,?,?) ON DUPLICATE KEY UPDATE id=LAST_INSERT_ID(id), lastSeen=?")
if err != nil {
return -1, fmt.Errorf("prepare insert cluster fail %v", err.Error())
}
defer stmt.Close()
res, err := stmt.Exec(clusterName, generatedAt, generatedAt, generatedAt)
if err != nil {
return -1, fmt.Errorf("exec insert cluster fail %v", err.Error())
}
lastId, err := res.LastInsertId()
if err != nil {
return -1, fmt.Errorf("lastinsertid - insert cluster fail %v", err.Error())
}
return lastId, nil
}
func (c *ClusterInventoryService) saveImage(name string, version string, generatedAt string) (int64, error) {
// couple parts -- want to save the cluster image metadata
stmt, err := c.db.Prepare("INSERT INTO images(name, version, firstSeen, lastSeen) VALUES(?,?,?,?) ON DUPLICATE KEY UPDATE id=LAST_INSERT_ID(id), lastSeen=?")
if err != nil {
return -1, fmt.Errorf("prepare - insert image fail %v", err.Error())
}
defer stmt.Close()
res, err := stmt.Exec(name, version, generatedAt, generatedAt, generatedAt)
if err != nil {
return -1, fmt.Errorf("exec - insert image fail %v", err.Error())
}
lastId, err := res.LastInsertId()
if err != nil {
return -1, fmt.Errorf("lastinsertid - insert image fail %v", err.Error())
}
return lastId, nil
}
func (c *ClusterInventoryService) saveImageList(clusterId int64, generatedAt string, imgList []types.PodImageState) error {
for _, pimg := range imgList {
// for every image in the list, we do this
for _, i := range pimg.Images {
// check our image table and update as applicable
imgParts := strings.Split(i, ":")
version := ""
if len(imgParts) == 2 {
version = imgParts[1]
}
imgId, err := c.saveImage(imgParts[0], version, generatedAt)
// update cluster image which details more of how and where our image is used
stmt, err := c.db.Prepare("INSERT INTO clusterinventory(imgid, clusterid, podname, namespace, starttime) VALUES(?,?,?,?,?)")
if err != nil {
return fmt.Errorf("prepare clusterinv fail %v", err.Error())
}
defer stmt.Close()
_, err = stmt.Exec(imgId, clusterId, pimg.Name, pimg.Namespace, pimg.StartTime)
if err != nil {
return fmt.Errorf("exec - prepare clusterinv fail %v", err.Error())
}
}
}
return nil
}
func (c *ClusterInventoryService) SaveClusterInventory(inv *types.ClusterInventory) error {
// first get the clusterID or insert the cluster
clusterId, err := c.saveCluster(inv.ClusterName, inv.GeneratedAt)
if err != nil {
return err
}
// then update the activity table showing this job just ran
stmt, err := c.db.Prepare("INSERT INTO activity(clusterid, version, activityDate) VALUES(?, ?, ?)")
if err != nil {
return fmt.Errorf("prepare - insert activity fail %v", err.Error())
}
defer stmt.Close()
_, err = stmt.Exec(clusterId, inv.Version, inv.GeneratedAt)
if err != nil {
return fmt.Errorf("exec - insert activity fail %v", err.Error())
}
// then update the clusterinv + images tables with our list of images
err = c.saveImageList(clusterId, inv.GeneratedAt, inv.ImageState)
return err
}