/
ch_pod_cluster.go
84 lines (74 loc) · 2.76 KB
/
ch_pod_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
/*
* Copyright (c) 2024 Yunshan Networks
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package tagrecorder
import (
"gorm.io/gorm/clause"
"github.com/deepflowio/deepflow/server/controller/common"
"github.com/deepflowio/deepflow/server/controller/db/mysql"
"github.com/deepflowio/deepflow/server/controller/recorder/pubsub/message"
)
type ChPodCluster struct {
SubscriberComponent[*message.PodClusterFieldsUpdate, message.PodClusterFieldsUpdate, mysql.PodCluster, mysql.ChPodCluster, IDKey]
resourceTypeToIconID map[IconKey]int
}
func NewChPodCluster(resourceTypeToIconID map[IconKey]int) *ChPodCluster {
mng := &ChPodCluster{
newSubscriberComponent[*message.PodClusterFieldsUpdate, message.PodClusterFieldsUpdate, mysql.PodCluster, mysql.ChPodCluster, IDKey](
common.RESOURCE_TYPE_POD_CLUSTER_EN, RESOURCE_TYPE_CH_POD,
),
resourceTypeToIconID,
}
mng.subscriberDG = mng
return mng
}
// sourceToTarget implements SubscriberDataGenerator
func (c *ChPodCluster) sourceToTarget(md *message.Metadata, source *mysql.PodCluster) (keys []IDKey, targets []mysql.ChPodCluster) {
iconID := c.resourceTypeToIconID[IconKey{
NodeType: RESOURCE_TYPE_POD_CLUSTER,
}]
sourceName := source.Name
if source.DeletedAt.Valid {
sourceName += " (deleted)"
}
keys = append(keys, IDKey{ID: source.ID})
targets = append(targets, mysql.ChPodCluster{
ID: source.ID,
Name: sourceName,
IconID: iconID,
TeamID: md.TeamID,
DomainID: md.DomainID,
})
return
}
// onResourceUpdated implements SubscriberDataGenerator
func (c *ChPodCluster) onResourceUpdated(sourceID int, fieldsUpdate *message.PodClusterFieldsUpdate, db *mysql.DB) {
updateInfo := make(map[string]interface{})
if fieldsUpdate.Name.IsDifferent() {
updateInfo["name"] = fieldsUpdate.Name.GetNew()
}
if len(updateInfo) > 0 {
var chItem mysql.ChPodCluster
db.Where("id = ?", sourceID).First(&chItem)
c.SubscriberComponent.dbOperator.update(chItem, updateInfo, IDKey{ID: sourceID}, db)
}
}
// softDeletedTargetsUpdated implements SubscriberDataGenerator
func (c *ChPodCluster) softDeletedTargetsUpdated(targets []mysql.ChPodCluster, db *mysql.DB) {
db.Clauses(clause.OnConflict{
Columns: []clause.Column{{Name: "id"}},
DoUpdates: clause.AssignmentColumns([]string{"name"}),
}).Create(&targets)
}