e0e362d700
SDK Tests / changes (push) Successful in 2m29s
Real E2E Tests / changes (push) Successful in 2m29s
Deploy Docs Pages / build (push) Has been cancelled
Deploy Docs Pages / deploy (push) Has been cancelled
Real E2E Tests / JavaScript E2E (docker bridge) (push) Has been cancelled
Real E2E Tests / Python E2E (docker bridge) (push) Has been cancelled
Real E2E Tests / Java E2E (docker bridge) (push) Has been cancelled
Real E2E Tests / C# E2E (docker bridge) (push) Has been cancelled
Real E2E Tests / Go E2E (docker bridge) (push) Has been cancelled
Real E2E Tests / Real E2E CI (push) Has been cancelled
SDK Tests / SDK CI (push) Has been cancelled
SDK Tests / CLI Tests (push) Has been cancelled
SDK Tests / Python SDK Quality (code-interpreter) (push) Has been cancelled
SDK Tests / Python SDK Quality (sandbox) (push) Has been cancelled
SDK Tests / Python SDK Tests (code-interpreter) (push) Has been cancelled
SDK Tests / JavaScript SDK Quality And Tests (code-interpreter) (push) Has been cancelled
SDK Tests / JavaScript SDK Quality And Tests (sandbox) (push) Has been cancelled
SDK Tests / Python SDK Tests (sandbox) (push) Has been cancelled
SDK Tests / CLI Quality (push) Has been cancelled
SDK Tests / Kotlin SDK Quality And Tests (sandbox) (push) Has been cancelled
SDK Tests / Kotlin SDK Quality And Tests (code-interpreter) (push) Has been cancelled
SDK Tests / C# SDK Quality And Tests (code-interpreter) (push) Has been cancelled
SDK Tests / C# SDK Quality And Tests (sandbox) (push) Has been cancelled
SDK Tests / Go SDK Quality And Tests (push) Has been cancelled
148 lines
3.9 KiB
Go
148 lines
3.9 KiB
Go
// Copyright 2025 Alibaba Group Holding Ltd.
|
|
//
|
|
// 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 expectations
|
|
|
|
import (
|
|
"sync"
|
|
"time"
|
|
|
|
"k8s.io/apimachinery/pkg/util/sets"
|
|
)
|
|
|
|
// ScaleAction is the action of scale, like create and delete.
|
|
type ScaleAction string
|
|
|
|
const (
|
|
// Create action
|
|
Create ScaleAction = "create"
|
|
// Delete action
|
|
Delete ScaleAction = "delete"
|
|
)
|
|
|
|
// ScaleExpectations is an interface that allows users to set and wait on expectations of pods scale.
|
|
type ScaleExpectations interface {
|
|
ExpectScale(controllerKey string, action ScaleAction, name string)
|
|
ObserveScale(controllerKey string, action ScaleAction, name string)
|
|
SatisfiedExpectations(controllerKey string) (bool, time.Duration, map[ScaleAction][]string)
|
|
DeleteExpectations(controllerKey string)
|
|
GetExpectations(controllerKey string) map[ScaleAction]sets.String
|
|
}
|
|
|
|
// NewScaleExpectations returns a common ScaleExpectations.
|
|
func NewScaleExpectations() ScaleExpectations {
|
|
return &realScaleExpectations{
|
|
controllerCache: make(map[string]*realControllerScaleExpectations),
|
|
}
|
|
}
|
|
|
|
type realScaleExpectations struct {
|
|
sync.Mutex
|
|
// key: parent key, workload namespace/name
|
|
controllerCache map[string]*realControllerScaleExpectations
|
|
}
|
|
|
|
type realControllerScaleExpectations struct {
|
|
// item: name for this object
|
|
objsCache map[ScaleAction]sets.String
|
|
firstUnsatisfiedTimestamp time.Time
|
|
}
|
|
|
|
func (r *realScaleExpectations) GetExpectations(controllerKey string) map[ScaleAction]sets.String {
|
|
r.Lock()
|
|
defer r.Unlock()
|
|
|
|
expectations := r.controllerCache[controllerKey]
|
|
if expectations == nil {
|
|
return nil
|
|
}
|
|
|
|
res := make(map[ScaleAction]sets.String, len(expectations.objsCache))
|
|
for k, v := range expectations.objsCache {
|
|
res[k] = sets.NewString(v.List()...)
|
|
}
|
|
|
|
return res
|
|
}
|
|
|
|
func (r *realScaleExpectations) ExpectScale(controllerKey string, action ScaleAction, name string) {
|
|
r.Lock()
|
|
defer r.Unlock()
|
|
|
|
expectations := r.controllerCache[controllerKey]
|
|
if expectations == nil {
|
|
expectations = &realControllerScaleExpectations{
|
|
objsCache: make(map[ScaleAction]sets.String),
|
|
}
|
|
r.controllerCache[controllerKey] = expectations
|
|
}
|
|
|
|
if s := expectations.objsCache[action]; s != nil {
|
|
s.Insert(name)
|
|
} else {
|
|
expectations.objsCache[action] = sets.NewString(name)
|
|
}
|
|
}
|
|
|
|
func (r *realScaleExpectations) ObserveScale(controllerKey string, action ScaleAction, name string) {
|
|
r.Lock()
|
|
defer r.Unlock()
|
|
|
|
expectations := r.controllerCache[controllerKey]
|
|
if expectations == nil {
|
|
return
|
|
}
|
|
|
|
s := expectations.objsCache[action]
|
|
if s == nil {
|
|
return
|
|
}
|
|
s.Delete(name)
|
|
|
|
for _, s := range expectations.objsCache {
|
|
if s.Len() > 0 {
|
|
return
|
|
}
|
|
}
|
|
delete(r.controllerCache, controllerKey)
|
|
}
|
|
|
|
func (r *realScaleExpectations) SatisfiedExpectations(controllerKey string) (bool, time.Duration, map[ScaleAction][]string) {
|
|
r.Lock()
|
|
defer r.Unlock()
|
|
|
|
expectations := r.controllerCache[controllerKey]
|
|
if expectations == nil {
|
|
return true, 0, nil
|
|
}
|
|
|
|
for a, s := range expectations.objsCache {
|
|
if s.Len() > 0 {
|
|
if expectations.firstUnsatisfiedTimestamp.IsZero() {
|
|
expectations.firstUnsatisfiedTimestamp = time.Now()
|
|
}
|
|
return false, time.Since(expectations.firstUnsatisfiedTimestamp), map[ScaleAction][]string{a: s.List()}
|
|
}
|
|
}
|
|
|
|
delete(r.controllerCache, controllerKey)
|
|
return true, 0, nil
|
|
}
|
|
|
|
func (r *realScaleExpectations) DeleteExpectations(controllerKey string) {
|
|
r.Lock()
|
|
defer r.Unlock()
|
|
delete(r.controllerCache, controllerKey)
|
|
}
|