From f4fbbcddd063fae3d7d0511fe62b7298d3e5f55e Mon Sep 17 00:00:00 2001 From: Asher Liu Date: Wed, 11 Oct 2023 15:20:05 +0800 Subject: [PATCH] feat: add license agent component (#88) Signed-off-by: iutx --- VERSION | 2 +- pkg/cluster/diff/dicediff.go | 129 +++++++++++++++++++++++++---------- pkg/envs/envs.go | 4 +- pkg/spec/spec.go | 9 +-- 4 files changed, 103 insertions(+), 41 deletions(-) diff --git a/VERSION b/VERSION index c0943d3..7208c21 100644 --- a/VERSION +++ b/VERSION @@ -1 +1 @@ -2.3 \ No newline at end of file +2.4 \ No newline at end of file diff --git a/pkg/cluster/diff/dicediff.go b/pkg/cluster/diff/dicediff.go index b4d568e..ebba57a 100644 --- a/pkg/cluster/diff/dicediff.go +++ b/pkg/cluster/diff/dicediff.go @@ -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 ( @@ -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 @@ -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 @@ -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 } @@ -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), @@ -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), @@ -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(¤t.Spec.Dice), *diceyml.CopyObj(&target.Spec.Dice), &r) @@ -370,6 +384,7 @@ func NewSpecDiff(current, target *spec.DiceCluster) *SpecDiff { diffPandora(*diceyml.CopyObj(¤t.Spec.Pandora), *diceyml.CopyObj(&target.Spec.Pandora), &r) diffDiceUI(*diceyml.CopyObj(¤t.Spec.DiceUI), *diceyml.CopyObj(&target.Spec.DiceUI), &r) diffUC(*diceyml.CopyObj(¤t.Spec.UC), *diceyml.CopyObj(&target.Spec.UC), &r) + diffLicense(*diceyml.CopyObj(¤t.Spec.License), *diceyml.CopyObj(&target.Spec.License), &r) diffSpotAnalyzer(*diceyml.CopyObj(¤t.Spec.SpotAnalyzer), *diceyml.CopyObj(&target.Spec.SpotAnalyzer), &r) diffSpotCollector(*diceyml.CopyObj(¤t.Spec.SpotCollector), *diceyml.CopyObj(&target.Spec.SpotCollector), &r) diffSpotDashboard(*diceyml.CopyObj(¤t.Spec.SpotDashboard), *diceyml.CopyObj(&target.Spec.SpotDashboard), &r) @@ -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) @@ -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) @@ -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) @@ -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) @@ -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) @@ -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) @@ -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) @@ -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) @@ -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) @@ -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) @@ -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) @@ -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) @@ -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) @@ -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) @@ -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) @@ -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) @@ -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) @@ -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) @@ -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) @@ -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) @@ -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) @@ -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 @@ -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) @@ -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 @@ -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 @@ -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 @@ -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) @@ -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, @@ -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, diff --git a/pkg/envs/envs.go b/pkg/envs/envs.go index 599a2ba..261818c 100644 --- a/pkg/envs/envs.go +++ b/pkg/envs/envs.go @@ -21,9 +21,10 @@ import ( "github.com/sirupsen/logrus" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" - "github.com/erda-project/dice-operator/pkg/spec" "github.com/erda-project/erda/pkg/parser/diceyml" "github.com/erda-project/erda/pkg/strutil" + + "github.com/erda-project/dice-operator/pkg/spec" ) const ( @@ -44,6 +45,7 @@ func GetAllServices(cluster *spec.DiceCluster) []diceyml.Services { cluster.Spec.Pandora.Services, cluster.Spec.DiceUI.Services, cluster.Spec.UC.Services, + cluster.Spec.License.Services, cluster.Spec.SpotAnalyzer.Services, cluster.Spec.SpotCollector.Services, cluster.Spec.SpotDashboard.Services, diff --git a/pkg/spec/spec.go b/pkg/spec/spec.go index 04f9cf4..ed018a6 100644 --- a/pkg/spec/spec.go +++ b/pkg/spec/spec.go @@ -47,14 +47,14 @@ const ( ClusterSizeProd ClusterSize = "prod" ) -const( +const ( PANameHPA = "HPA" PANameVPA = "VPA" ) -type PATarget struct{ - PAKind string - TargetControllerKind string +type PATarget struct { + PAKind string + TargetControllerKind string } type DiceClusterList struct { @@ -99,6 +99,7 @@ type ClusterSpec struct { Pandora diceyml.Object `json:"pandora"` DiceUI diceyml.Object `json:"diceUI"` UC diceyml.Object `json:"uc"` + License diceyml.Object `json:"license"` SpotAnalyzer diceyml.Object `json:"spotAnalyzer"` SpotCollector diceyml.Object `json:"spotCollector"` SpotDashboard diceyml.Object `json:"spotDashboard"`