Copyright(C) 2026. Huawei Technologies Co.,Ltd. All rights reserved.
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 workload
import (
"context"
"fmt"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"
"infer-operator/pkg/api/v1"
"infer-operator/pkg/common"
)
type WorkLoadHandlerFactory struct {
workloadHandlerMap map[string]WorkLoadHandler
}
func NewWorkLoadHandlerFactory() *WorkLoadHandlerFactory {
return &WorkLoadHandlerFactory{
workloadHandlerMap: make(map[string]WorkLoadHandler),
}
}
func (factory *WorkLoadHandlerFactory) Register(gvk schema.GroupVersionKind, handler WorkLoadHandler) error {
if _, ok := factory.workloadHandlerMap[gvk.String()]; ok {
return fmt.Errorf("duplicate workload handler for GVK %s", gvk)
}
factory.workloadHandlerMap[gvk.String()] = handler
return nil
}
func (factory *WorkLoadHandlerFactory) GetWorkLoadHandler(gvk schema.GroupVersionKind) (WorkLoadHandler, error) {
handler, ok := factory.workloadHandlerMap[gvk.String()]
if !ok {
return nil, fmt.Errorf("can not find workload handler for GVK %s", gvk)
}
return handler, nil
}
type WorkLoadHandler interface {
CheckOrCreateWorkLoad(ctx context.Context, instanceSet *v1.InstanceSet, indexer common.InstanceIndexer) error
DeleteExtraWorkLoad(ctx context.Context, indexer common.InstanceIndexer, indexLimit int) error
GetWorkLoadReadyReplicas(ctx context.Context, indexer common.InstanceIndexer) (int, error)
Validate(spec runtime.RawExtension) error
GetReplicas(spec runtime.RawExtension) (int32, error)
ListWorkLoad(ctx context.Context, selectLabels map[string]string, namespace string, filters ...WorkLoadFilter) ([]WorkLoadInterface, error)
DeleteWorkLoad(ctx context.Context, selectLabels map[string]string, namespace string, filters ...WorkLoadFilter) error
UpdateWorkLoad(ctx context.Context, selectLabels map[string]string, namespace string, updater WorkloadUpdater, filters ...WorkLoadFilter) error
}
type WorkLoadInterface interface {
GetWorkLoadObjMeta() metav1.ObjectMeta
SetWorkLoadObjMeta(metav1.ObjectMeta)
GetWorkLoadReplicas() int32
IsWorkLoadReady() bool
}
type WorkLoadFilter func(workLoad WorkLoadInterface) bool
type WorkloadUpdater func(workLoad WorkLoadInterface)