Skip to content

Commit

Permalink
feat: add license agent component (#88)
Browse files Browse the repository at this point in the history
Signed-off-by: iutx <[email protected]>
  • Loading branch information
iutx authored Oct 11, 2023
1 parent 5a8d77d commit f4fbbcd
Show file tree
Hide file tree
Showing 4 changed files with 103 additions and 41 deletions.
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
2.3
2.4
129 changes: 94 additions & 35 deletions pkg/cluster/diff/dicediff.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,11 @@ import (

"github.com/google/go-cmp/cmp"

"github.com/erda-project/dice-operator/pkg/spec"
"github.com/erda-project/dice-operator/pkg/utils"
"github.com/erda-project/erda/pkg/parser/diceyml"
"github.com/erda-project/erda/pkg/strutil"

"github.com/erda-project/dice-operator/pkg/spec"
"github.com/erda-project/dice-operator/pkg/utils"
)

const (
Expand All @@ -46,9 +47,9 @@ const (
)

type SpecDiff struct {
enableAutoScaleDiff bool
currentEnableAutoScale bool
targetEnableAutoScale bool
enableAutoScaleDiff bool
currentEnableAutoScale bool
targetEnableAutoScale bool
// spec.Dice
diceGlobalEnvDiff bool
currentDiceGlobalEnv map[string]string
Expand Down Expand Up @@ -103,6 +104,15 @@ type SpecDiff struct {
currentUCServices map[string]*diceyml.Service
targetUCServices map[string]*diceyml.Service

// spec.license
licenseGlobalEnvDiff bool
currentLicenseGlobalEnv map[string]string
targetLicenseGlobalEnv map[string]string

licenseServiceDiff bool
currentLicenseServices map[string]*diceyml.Service
targetLicenseServices map[string]*diceyml.Service

// spec.spotAnalyzer
spotAnalyzerGlobalEnvDiff bool
currentSpotAnalyzerGlobalEnv map[string]string
Expand Down Expand Up @@ -229,7 +239,7 @@ type Actions struct {
UpdatedDaemonSet map[string]*diceyml.Service
DeletedDaemonSet map[string]*diceyml.Service

UpdatedServicesPA map[string]*diceyml.Service
UpdatedServicesPA map[string]*diceyml.Service
EnableAutoScaleDiff bool
}

Expand Down Expand Up @@ -287,6 +297,8 @@ func NewSpecDiff(current, target *spec.DiceCluster) *SpecDiff {
targetDiceUIGlobalEnv: make(map[string]string),
currentUCGlobalEnv: make(map[string]string),
targetUCGlobalEnv: make(map[string]string),
currentLicenseGlobalEnv: make(map[string]string),
targetLicenseGlobalEnv: make(map[string]string),
currentSpotAnalyzerGlobalEnv: make(map[string]string),
targetSpotAnalyzerGlobalEnv: make(map[string]string),
currentSpotCollectorGlobalEnv: make(map[string]string),
Expand Down Expand Up @@ -326,6 +338,8 @@ func NewSpecDiff(current, target *spec.DiceCluster) *SpecDiff {
targetDiceUIServices: make(map[string]*diceyml.Service),
currentUCServices: make(map[string]*diceyml.Service),
targetUCServices: make(map[string]*diceyml.Service),
currentLicenseServices: make(map[string]*diceyml.Service),
targetLicenseServices: make(map[string]*diceyml.Service),
currentSpotAnalyzerServices: make(map[string]*diceyml.Service),
targetSpotAnalyzerServices: make(map[string]*diceyml.Service),
currentSpotCollectorServices: make(map[string]*diceyml.Service),
Expand Down Expand Up @@ -361,7 +375,7 @@ func NewSpecDiff(current, target *spec.DiceCluster) *SpecDiff {
}
return &r
}
if current.Spec.EnableAutoScale != target.Spec.EnableAutoScale {
if current.Spec.EnableAutoScale != target.Spec.EnableAutoScale {
r.enableAutoScaleDiff = true
}
diffDice(*diceyml.CopyObj(&current.Spec.Dice), *diceyml.CopyObj(&target.Spec.Dice), &r)
Expand All @@ -370,6 +384,7 @@ func NewSpecDiff(current, target *spec.DiceCluster) *SpecDiff {
diffPandora(*diceyml.CopyObj(&current.Spec.Pandora), *diceyml.CopyObj(&target.Spec.Pandora), &r)
diffDiceUI(*diceyml.CopyObj(&current.Spec.DiceUI), *diceyml.CopyObj(&target.Spec.DiceUI), &r)
diffUC(*diceyml.CopyObj(&current.Spec.UC), *diceyml.CopyObj(&target.Spec.UC), &r)
diffLicense(*diceyml.CopyObj(&current.Spec.License), *diceyml.CopyObj(&target.Spec.License), &r)
diffSpotAnalyzer(*diceyml.CopyObj(&current.Spec.SpotAnalyzer), *diceyml.CopyObj(&target.Spec.SpotAnalyzer), &r)
diffSpotCollector(*diceyml.CopyObj(&current.Spec.SpotCollector), *diceyml.CopyObj(&target.Spec.SpotCollector), &r)
diffSpotDashboard(*diceyml.CopyObj(&current.Spec.SpotDashboard), *diceyml.CopyObj(&target.Spec.SpotDashboard), &r)
Expand Down Expand Up @@ -435,6 +450,8 @@ func (d *SpecDiff) filterEdgeClusterServices() {
f(d.targetDiceUIServices)
f(d.currentUCServices)
f(d.targetUCServices)
f(d.currentLicenseServices)
f(d.targetLicenseServices)
f(d.currentSpotAnalyzerServices)
f(d.targetSpotAnalyzerServices)
f(d.currentSpotCollectorServices)
Expand Down Expand Up @@ -466,17 +483,17 @@ func (d *SpecDiff) filterEdgeClusterServices() {

func (d *SpecDiff) GetActions() *Actions {
r := &Actions{
AddedServices: make(map[string]*diceyml.Service),
UpdatedServices: make(map[string]*diceyml.Service),
DeletedServices: make(map[string]*diceyml.Service),
AddedDaemonSet: make(map[string]*diceyml.Service),
UpdatedDaemonSet: make(map[string]*diceyml.Service),
DeletedDaemonSet: make(map[string]*diceyml.Service),
UpdatedServicesPA: make(map[string]*diceyml.Service),
EnableAutoScaleDiff: d.enableAutoScaleDiff,
AddedServices: make(map[string]*diceyml.Service),
UpdatedServices: make(map[string]*diceyml.Service),
DeletedServices: make(map[string]*diceyml.Service),
AddedDaemonSet: make(map[string]*diceyml.Service),
UpdatedDaemonSet: make(map[string]*diceyml.Service),
DeletedDaemonSet: make(map[string]*diceyml.Service),
UpdatedServicesPA: make(map[string]*diceyml.Service),
EnableAutoScaleDiff: d.enableAutoScaleDiff,
}

differentServices := make(map[string]*diceyml.Service)
differentServices := make(map[string]*diceyml.Service)

// spec.dice
missingInSet1, missingInSet2, shared := diffServiceset(d.currentDiceServices, d.targetDiceServices)
Expand All @@ -487,7 +504,7 @@ func (d *SpecDiff) GetActions() *Actions {
differentServices = getDifferentServices(d.currentDiceServices, d.targetDiceServices, shared)
}
*/
if d.enableAutoScaleDiff{
if d.enableAutoScaleDiff {
mergemap(r.UpdatedServicesPA, shared)
}
differentServices = getDifferentServices(d.currentDiceServices, d.targetDiceServices, shared)
Expand All @@ -504,7 +521,7 @@ func (d *SpecDiff) GetActions() *Actions {
}
// spec.addonplatform
missingInSet1, missingInSet2, shared = diffServiceset(d.currentAddonPlatformServices, d.targetAddonPlatformServices)
if d.enableAutoScaleDiff{
if d.enableAutoScaleDiff {
mergemap(r.UpdatedServicesPA, shared)
}
differentServices = getDifferentServices(d.currentAddonPlatformServices, d.targetAddonPlatformServices, shared)
Expand All @@ -521,7 +538,7 @@ func (d *SpecDiff) GetActions() *Actions {
}
// spec.gittar
missingInSet1, missingInSet2, shared = diffServiceset(d.currentGittarServices, d.targetGittarServices)
if d.enableAutoScaleDiff{
if d.enableAutoScaleDiff {
mergemap(r.UpdatedServicesPA, shared)
}
differentServices = getDifferentServices(d.currentGittarServices, d.targetGittarServices, shared)
Expand All @@ -538,7 +555,7 @@ func (d *SpecDiff) GetActions() *Actions {
}
// spec.pandora
missingInSet1, missingInSet2, shared = diffServiceset(d.currentPandoraServices, d.targetPandoraServices)
if d.enableAutoScaleDiff{
if d.enableAutoScaleDiff {
mergemap(r.UpdatedServicesPA, shared)
}
differentServices = getDifferentServices(d.currentPandoraServices, d.targetPandoraServices, shared)
Expand All @@ -555,7 +572,7 @@ func (d *SpecDiff) GetActions() *Actions {
}
// spec.diceui
missingInSet1, missingInSet2, shared = diffServiceset(d.currentDiceUIServices, d.targetDiceUIServices)
if d.enableAutoScaleDiff{
if d.enableAutoScaleDiff {
mergemap(r.UpdatedServicesPA, shared)
}
differentServices = getDifferentServices(d.currentDiceUIServices, d.targetDiceUIServices, shared)
Expand All @@ -572,7 +589,7 @@ func (d *SpecDiff) GetActions() *Actions {
}
// spec.uc
missingInSet1, missingInSet2, shared = diffServiceset(d.currentUCServices, d.targetUCServices)
if d.enableAutoScaleDiff{
if d.enableAutoScaleDiff {
mergemap(r.UpdatedServicesPA, shared)
}
differentServices = getDifferentServices(d.currentUCServices, d.targetUCServices, shared)
Expand All @@ -588,9 +605,27 @@ func (d *SpecDiff) GetActions() *Actions {
mergemap(r.UpdatedServices, differentServices)
}

// spec.license
missingInSet1, missingInSet2, shared = diffServiceset(d.currentLicenseServices, d.targetLicenseServices)
if d.enableAutoScaleDiff {
mergemap(r.UpdatedServicesPA, shared)
}
differentServices = getDifferentServices(d.currentLicenseServices, d.targetLicenseServices, shared)
expandGlobalEnv(d.targetLicenseGlobalEnv, missingInSet1)
expandGlobalEnv(d.targetLicenseGlobalEnv, missingInSet2)
expandGlobalEnv(d.targetLicenseGlobalEnv, shared)

mergemap(r.AddedServices, missingInSet1)
mergemap(r.DeletedServices, missingInSet2)
if d.licenseGlobalEnvDiff {
mergemap(r.UpdatedServices, shared)
} else {
mergemap(r.UpdatedServices, differentServices)
}

// spec.spotAnalyzer
missingInSet1, missingInSet2, shared = diffServiceset(d.currentSpotAnalyzerServices, d.targetSpotAnalyzerServices)
if d.enableAutoScaleDiff{
if d.enableAutoScaleDiff {
mergemap(r.UpdatedServicesPA, shared)
}
differentServices = getDifferentServices(d.currentSpotAnalyzerServices, d.targetSpotAnalyzerServices, shared)
Expand All @@ -608,7 +643,7 @@ func (d *SpecDiff) GetActions() *Actions {

// spec.spotCollector
missingInSet1, missingInSet2, shared = diffServiceset(d.currentSpotCollectorServices, d.targetSpotCollectorServices)
if d.enableAutoScaleDiff{
if d.enableAutoScaleDiff {
mergemap(r.UpdatedServicesPA, shared)
}
differentServices = getDifferentServices(d.currentSpotCollectorServices, d.targetSpotCollectorServices, shared)
Expand All @@ -626,7 +661,7 @@ func (d *SpecDiff) GetActions() *Actions {

// spec.spotDashboard
missingInSet1, missingInSet2, shared = diffServiceset(d.currentSpotDashboardServices, d.targetSpotDashboardServices)
if d.enableAutoScaleDiff{
if d.enableAutoScaleDiff {
mergemap(r.UpdatedServicesPA, shared)
}
differentServices = getDifferentServices(d.currentSpotDashboardServices, d.targetSpotDashboardServices, shared)
Expand All @@ -644,7 +679,7 @@ func (d *SpecDiff) GetActions() *Actions {

// spec.fluentBit
missingInSet1, missingInSet2, shared = diffServiceset(d.currentFluentBitServices, d.targetFluentBitServices)
if d.enableAutoScaleDiff{
if d.enableAutoScaleDiff {
mergemap(r.UpdatedServicesPA, shared)
}
differentServices = getDifferentServices(d.currentFluentBitServices, d.targetFluentBitServices, shared)
Expand All @@ -662,7 +697,7 @@ func (d *SpecDiff) GetActions() *Actions {

// spec.spotFilebeat (Daemonset)
missingInSet1, missingInSet2, shared = diffServiceset(d.currentSpotFilebeatServices, d.targetSpotFilebeatServices)
if d.enableAutoScaleDiff{
if d.enableAutoScaleDiff {
mergemap(r.UpdatedServicesPA, shared)
}
differentServices = getDifferentServices(d.currentSpotFilebeatServices, d.targetSpotFilebeatServices, shared)
Expand All @@ -680,7 +715,7 @@ func (d *SpecDiff) GetActions() *Actions {

// spec.spotStatus
missingInSet1, missingInSet2, shared = diffServiceset(d.currentSpotStatusServices, d.targetSpotStatusServices)
if d.enableAutoScaleDiff{
if d.enableAutoScaleDiff {
mergemap(r.UpdatedServicesPA, shared)
}
differentServices = getDifferentServices(d.currentSpotStatusServices, d.targetSpotStatusServices, shared)
Expand All @@ -698,7 +733,7 @@ func (d *SpecDiff) GetActions() *Actions {

// spec.spotTelegraf (telegraf: daemonset, telegraf-platform: deployment)
missingInSet1, missingInSet2, shared = diffServiceset(d.currentSpotTelegrafServices, d.targetSpotTelegrafServices)
if d.enableAutoScaleDiff{
if d.enableAutoScaleDiff {
mergemap(r.UpdatedServicesPA, shared)
}
differentServices = getDifferentServices(d.currentSpotTelegrafServices, d.targetSpotTelegrafServices, shared)
Expand Down Expand Up @@ -733,7 +768,7 @@ func (d *SpecDiff) GetActions() *Actions {
}
// spec.tmc
missingInSet1, missingInSet2, shared = diffServiceset(d.currentTmcServices, d.targetTmcServices)
if d.enableAutoScaleDiff{
if d.enableAutoScaleDiff {
mergemap(r.UpdatedServicesPA, shared)
}
differentServices = getDifferentServices(d.currentTmcServices, d.targetTmcServices, shared)
Expand All @@ -751,7 +786,7 @@ func (d *SpecDiff) GetActions() *Actions {

// spec.hepa
missingInSet1, missingInSet2, shared = diffServiceset(d.currentHepaServices, d.targetHepaServices)
if d.enableAutoScaleDiff{
if d.enableAutoScaleDiff {
mergemap(r.UpdatedServicesPA, shared)
}
differentServices = getDifferentServices(d.currentHepaServices, d.targetHepaServices, shared)
Expand All @@ -768,7 +803,7 @@ func (d *SpecDiff) GetActions() *Actions {
}
// spec.spotMonitor
missingInSet1, missingInSet2, shared = diffServiceset(d.currentSpotMonitorServices, d.targetSpotMonitorServices)
if d.enableAutoScaleDiff{
if d.enableAutoScaleDiff {
mergemap(r.UpdatedServicesPA, shared)
}
differentServices = getDifferentServices(d.currentSpotMonitorServices, d.targetSpotMonitorServices, shared)
Expand All @@ -785,7 +820,7 @@ func (d *SpecDiff) GetActions() *Actions {
}
// spec.fdp
missingInSet1, missingInSet2, shared = diffServiceset(d.currentFdpServices, d.targetFdpServices)
if d.enableAutoScaleDiff{
if d.enableAutoScaleDiff {
mergemap(r.UpdatedServicesPA, shared)
}
differentServices = getDifferentServices(d.currentFdpServices, d.targetFdpServices, shared)
Expand All @@ -803,7 +838,7 @@ func (d *SpecDiff) GetActions() *Actions {

// spec.fdpUI
missingInSet1, missingInSet2, shared = diffServiceset(d.currentFdpUIServices, d.targetFdpUIServices)
if d.enableAutoScaleDiff{
if d.enableAutoScaleDiff {
mergemap(r.UpdatedServicesPA, shared)
}
differentServices = getDifferentServices(d.currentFdpUIServices, d.targetFdpUIServices, shared)
Expand All @@ -820,7 +855,7 @@ func (d *SpecDiff) GetActions() *Actions {
}
// spec.meshController
missingInSet1, missingInSet2, shared = diffServiceset(d.currentMeshControllerServices, d.targetMeshControllerServices)
if d.enableAutoScaleDiff{
if d.enableAutoScaleDiff {
mergemap(r.UpdatedServicesPA, shared)
}
differentServices = getDifferentServices(d.currentMeshControllerServices, d.targetMeshControllerServices, shared)
Expand Down Expand Up @@ -867,6 +902,7 @@ func diffFromBlank(target *spec.DiceCluster, specdiff *SpecDiff) {
specdiff.pandoraGlobalEnvDiff = true
specdiff.diceUIGlobalEnvDiff = true
specdiff.ucGlobalEnvDiff = true
specdiff.licenseGlobalEnvDiff = true
specdiff.spotAnalyzerGlobalEnvDiff = true
specdiff.spotCollectorGlobalEnvDiff = true
specdiff.spotDashboardGlobalEnvDiff = true
Expand All @@ -887,6 +923,7 @@ func diffFromBlank(target *spec.DiceCluster, specdiff *SpecDiff) {
pandora := diceyml.CopyObj(&target.Spec.Pandora)
diceui := diceyml.CopyObj(&target.Spec.DiceUI)
uc := diceyml.CopyObj(&target.Spec.UC)
license := diceyml.CopyObj(&target.Spec.License)
spotAnalyzer := diceyml.CopyObj(&target.Spec.SpotAnalyzer)
spotCollector := diceyml.CopyObj(&target.Spec.SpotCollector)
spotDashboard := diceyml.CopyObj(&target.Spec.SpotDashboard)
Expand All @@ -908,6 +945,7 @@ func diffFromBlank(target *spec.DiceCluster, specdiff *SpecDiff) {
specdiff.targetPandoraGlobalEnv = pandora.Envs
specdiff.targetDiceUIGlobalEnv = diceui.Envs
specdiff.targetUCGlobalEnv = uc.Envs
specdiff.targetLicenseGlobalEnv = license.Envs
specdiff.targetSpotAnalyzerGlobalEnv = spotAnalyzer.Envs
specdiff.targetSpotCollectorGlobalEnv = spotCollector.Envs
specdiff.targetSpotDashboardGlobalEnv = spotDashboard.Envs
Expand All @@ -928,6 +966,7 @@ func diffFromBlank(target *spec.DiceCluster, specdiff *SpecDiff) {
specdiff.pandoraServiceDiff = true
specdiff.diceUIServiceDiff = true
specdiff.ucServiceDiff = true
specdiff.licenseServiceDiff = true
specdiff.spotAnalyzerServiceDiff = true
specdiff.spotCollectorServiceDiff = true
specdiff.spotDashboardServiceDiff = true
Expand All @@ -948,6 +987,7 @@ func diffFromBlank(target *spec.DiceCluster, specdiff *SpecDiff) {
specdiff.targetPandoraServices = pandora.Services
specdiff.targetDiceUIServices = diceui.Services
specdiff.targetUCServices = uc.Services
specdiff.targetLicenseServices = license.Services
specdiff.targetSpotAnalyzerServices = spotAnalyzer.Services
specdiff.targetSpotCollectorServices = spotCollector.Services
specdiff.targetSpotDashboardServices = spotDashboard.Services
Expand Down Expand Up @@ -995,6 +1035,11 @@ func diffUC(current, target diceyml.Object, specdiff *SpecDiff) {
diffUCServices(current.Services, target.Services, specdiff)
}

func diffLicense(current, target diceyml.Object, specdiff *SpecDiff) {
diffLicenseGlobalEnv(current.Envs, target.Envs, specdiff)
diffLicenseServices(current.Services, target.Services, specdiff)
}

func diffSpotAnalyzer(current, target diceyml.Object, specdiff *SpecDiff) {
diffSpotAnalyzerGlobalEnv(current.Envs, target.Envs, specdiff)
diffSpotAnalyzerServices(current.Services, target.Services, specdiff)
Expand Down Expand Up @@ -1085,6 +1130,13 @@ func diffUCGlobalEnv(current, target map[string]string, specdiff *SpecDiff) {
&specdiff.currentUCGlobalEnv, &specdiff.targetUCGlobalEnv,
&specdiff.ucGlobalEnvDiff)
}

func diffLicenseGlobalEnv(current, target map[string]string, specdiff *SpecDiff) {
auxDiffGlobalEnv(current, target,
&specdiff.currentLicenseGlobalEnv, &specdiff.targetLicenseGlobalEnv,
&specdiff.licenseGlobalEnvDiff)
}

func diffSpotAnalyzerGlobalEnv(current, target map[string]string, specdiff *SpecDiff) {
auxDiffGlobalEnv(current, target,
&specdiff.currentSpotAnalyzerGlobalEnv, &specdiff.targetSpotAnalyzerGlobalEnv,
Expand Down Expand Up @@ -1203,6 +1255,13 @@ func diffUCServices(current, target map[string]*diceyml.Service, specdiff *SpecD
&specdiff.currentUCServices, &specdiff.targetUCServices,
specdiff.enableAutoScaleDiff, &specdiff.ucServiceDiff)
}

func diffLicenseServices(current, target map[string]*diceyml.Service, specdiff *SpecDiff) {
auxDiffServices(current, target,
&specdiff.currentLicenseServices, &specdiff.targetLicenseServices,
specdiff.enableAutoScaleDiff, &specdiff.licenseServiceDiff)
}

func diffSpotAnalyzerServices(current, target map[string]*diceyml.Service, specdiff *SpecDiff) {
auxDiffServices(current, target,
&specdiff.currentSpotAnalyzerServices, &specdiff.targetSpotAnalyzerServices,
Expand Down
Loading

0 comments on commit f4fbbcd

Please sign in to comment.