Skip to content

Commit bf40e83

Browse files
authored
Merge pull request #792 from luomingmeng/dev/fix-merge-indicator-spec
fix(spd): fix spd mergeIndicatorSpec
2 parents 8575865 + 322a9a6 commit bf40e83

File tree

4 files changed

+52
-24
lines changed

4 files changed

+52
-24
lines changed

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ vet: ## Run go vet against code.
126126

127127
.PHONY: test
128128
test: ## Run go test against code.
129-
go test -v -json -coverprofile=coverage.txt -parallel=16 -p=16 -covermode=atomic -race -coverpkg=./... \
129+
go test -v -coverprofile=coverage.txt -parallel=16 -p=16 -covermode=atomic -race -coverpkg=./... \
130130
`go list ./pkg/... | grep -E -v "pkg/scheduler|pkg/controller/resource-recommend|pkg/util/resource-recommend"`
131131

132132
.PHONY: license

pkg/controller/spd/spd_indicator.go

Lines changed: 46 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ limitations under the License.
1717
package spd
1818

1919
import (
20+
"strings"
2021
"time"
2122

2223
apiequality "k8s.io/apimachinery/pkg/api/equality"
@@ -192,39 +193,54 @@ func (sc *SPDController) mergeIndicatorSpec(spd *apiworkload.ServiceProfileDescr
192193
util.InsertSPDExtendedIndicatorSpec(&spd.Spec, &indicator)
193194
}
194195

195-
for i := 0; i < len(spd.Spec.BusinessIndicator); i++ {
196-
if _, ok := sc.indicatorsSpecBusiness[spd.Spec.BusinessIndicator[i].Name]; !ok {
197-
klog.Infof("skip spec business %v for spd %v", spd.Spec.BusinessIndicator[i].Name, spd.Name)
198-
spd.Spec.BusinessIndicator = append(spd.Spec.BusinessIndicator[:i], spd.Spec.BusinessIndicator[i+1:]...)
196+
// process BusinessIndicator
197+
businessNew := spd.Spec.BusinessIndicator[:0]
198+
for _, indicator := range spd.Spec.BusinessIndicator {
199+
if shouldKeep(indicator.Name, sc.indicatorsSpecBusiness) {
200+
businessNew = append(businessNew, indicator)
201+
} else {
202+
klog.Infof("skip spec business %v for spd %v", indicator.Name, spd.Name)
199203
}
200204
}
201-
202-
for i := 0; i < len(spd.Spec.SystemIndicator); i++ {
203-
if _, ok := sc.indicatorsSpecSystem[spd.Spec.SystemIndicator[i].Name]; !ok {
204-
klog.Infof("skip spec system %v for spd %v", spd.Spec.SystemIndicator[i].Name, spd.Name)
205-
spd.Spec.SystemIndicator = append(spd.Spec.SystemIndicator[:i], spd.Spec.SystemIndicator[i+1:]...)
205+
spd.Spec.BusinessIndicator = businessNew
206+
207+
// process SystemIndicator
208+
systemNew := spd.Spec.SystemIndicator[:0]
209+
for _, indicator := range spd.Spec.SystemIndicator {
210+
if shouldKeep(indicator.Name, sc.indicatorsSpecSystem) {
211+
systemNew = append(systemNew, indicator)
212+
} else {
213+
klog.Infof("skip spec system %v for spd %v", indicator.Name, spd.Name)
206214
}
207215
}
208-
209-
for i := 0; i < len(spd.Spec.ExtendedIndicator); i++ {
210-
if _, ok := sc.indicatorsSpecExtended[spd.Spec.ExtendedIndicator[i].Name]; !ok {
211-
klog.Infof("skip spec extended %v for spd %v", spd.Spec.ExtendedIndicator[i].Name, spd.Name)
212-
spd.Spec.ExtendedIndicator = append(spd.Spec.ExtendedIndicator[:i], spd.Spec.ExtendedIndicator[i+1:]...)
216+
spd.Spec.SystemIndicator = systemNew
217+
218+
// process ExtendedIndicator
219+
extendedNew := spd.Spec.ExtendedIndicator[:0]
220+
for _, indicator := range spd.Spec.ExtendedIndicator {
221+
if shouldKeep(indicator.Name, sc.indicatorsSpecExtended) {
222+
extendedNew = append(extendedNew, indicator)
223+
} else {
224+
klog.Infof("skip spec extended %v for spd %v", indicator.Name, spd.Name)
213225
}
214226
}
227+
spd.Spec.ExtendedIndicator = extendedNew
215228
}
216229

217230
func (sc *SPDController) mergeIndicatorStatus(spd *apiworkload.ServiceProfileDescriptor, expected apiworkload.ServiceProfileDescriptorStatus) {
218231
for _, indicator := range expected.BusinessStatus {
219232
util.InsertSPDBusinessIndicatorStatus(&spd.Status, &indicator)
220233
}
221234

222-
for i := 0; i < len(spd.Status.BusinessStatus); i++ {
223-
if _, ok := sc.indicatorsStatusBusiness[spd.Status.BusinessStatus[i].Name]; !ok {
224-
klog.Infof("skip status business %v for spd %v", spd.Status.BusinessStatus[i].Name, spd.Name)
225-
spd.Status.BusinessStatus = append(spd.Status.BusinessStatus[:i], spd.Status.BusinessStatus[i+1:]...)
235+
BusinessStatusNew := spd.Status.BusinessStatus[:0]
236+
for _, indicator := range spd.Status.BusinessStatus {
237+
if shouldKeep(indicator.Name, sc.indicatorsStatusBusiness) {
238+
BusinessStatusNew = append(BusinessStatusNew, indicator)
239+
} else {
240+
klog.Infof("skip status business %v for spd %v", indicator.Name, spd.Name)
226241
}
227242
}
243+
spd.Status.BusinessStatus = BusinessStatusNew
228244

229245
for _, aggMetrics := range expected.AggMetrics {
230246
util.InsertSPDAggMetricsStatus(&spd.Status, &aggMetrics)
@@ -242,3 +258,15 @@ func (sc *SPDController) mergeIndicatorStatus(spd *apiworkload.ServiceProfileDes
242258
}
243259
}
244260
}
261+
262+
func shouldKeep[K ~string](name K, validMap map[K]interface{}) bool {
263+
if _, ok := validMap[name]; ok {
264+
return true
265+
}
266+
for key := range validMap {
267+
if strings.HasPrefix(string(name), string(key)) {
268+
return true
269+
}
270+
}
271+
return false
272+
}

pkg/controller/spd/spd_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -969,7 +969,7 @@ func TestIndicatorUpdater(t *testing.T) {
969969
Current: &current,
970970
},
971971
{
972-
Name: "system-2",
972+
Name: "business-2",
973973
Current: &current,
974974
},
975975
},
@@ -1067,7 +1067,7 @@ func TestIndicatorUpdater(t *testing.T) {
10671067
Status: apiworkload.ServiceProfileDescriptorStatus{
10681068
BusinessStatus: []apiworkload.ServiceBusinessIndicatorStatus{
10691069
{
1070-
Name: "system-2",
1070+
Name: "business-2",
10711071
Current: &value,
10721072
},
10731073
},
@@ -1246,11 +1246,11 @@ func TestIndicatorUpdater(t *testing.T) {
12461246
sc.indicatorManager.UpdateBusinessIndicatorStatus(
12471247
nn, []apiworkload.ServiceBusinessIndicatorStatus{
12481248
{
1249-
Name: "system-1",
1249+
Name: "business-1",
12501250
Current: &value,
12511251
},
12521252
{
1253-
Name: "system-2",
1253+
Name: "business-2",
12541254
Current: &value,
12551255
},
12561256
},

pkg/util/spd.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -313,7 +313,7 @@ func InsertSPDBusinessIndicatorStatus(status *apiworkload.ServiceProfileDescript
313313
status.BusinessStatus = append(status.BusinessStatus, *serviceBusinessIndicatorStatus)
314314
}
315315

316-
// InsertSPDAggMetrics inserts aggMetrics into spd status.
316+
// InsertSPDAggMetricsStatus inserts aggMetrics into spd status.
317317
func InsertSPDAggMetricsStatus(status *apiworkload.ServiceProfileDescriptorStatus,
318318
serviceAggPodMetrics *apiworkload.AggPodMetrics,
319319
) {

0 commit comments

Comments
 (0)