sigs.k8s.io/kueue@v0.6.2/pkg/visibility/api/rest/pending_workloads_cq.go (about) 1 // Copyright 2023 The Kubernetes Authors. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package rest 16 17 import ( 18 "context" 19 "fmt" 20 21 "github.com/go-logr/logr" 22 "k8s.io/apimachinery/pkg/api/errors" 23 "k8s.io/apimachinery/pkg/runtime" 24 "k8s.io/apiserver/pkg/registry/rest" 25 ctrl "sigs.k8s.io/controller-runtime" 26 27 "sigs.k8s.io/kueue/apis/visibility/v1alpha1" 28 "sigs.k8s.io/kueue/pkg/constants" 29 "sigs.k8s.io/kueue/pkg/queue" 30 31 _ "k8s.io/metrics/pkg/apis/metrics/install" 32 ) 33 34 type pendingWorkloadsInCqREST struct { 35 queueMgr *queue.Manager 36 log logr.Logger 37 } 38 39 var _ rest.Storage = &pendingWorkloadsInCqREST{} 40 var _ rest.GetterWithOptions = &pendingWorkloadsInCqREST{} 41 var _ rest.Scoper = &pendingWorkloadsInCqREST{} 42 43 func NewPendingWorkloadsInCqREST(kueueMgr *queue.Manager) *pendingWorkloadsInCqREST { 44 return &pendingWorkloadsInCqREST{ 45 queueMgr: kueueMgr, 46 log: ctrl.Log.WithName("pending-workload-in-cq"), 47 } 48 } 49 50 // New implements rest.Storage interface 51 func (m *pendingWorkloadsInCqREST) New() runtime.Object { 52 return &v1alpha1.PendingWorkloadsSummary{} 53 } 54 55 // Destroy implements rest.Storage interface 56 func (m *pendingWorkloadsInCqREST) Destroy() {} 57 58 // Get implements rest.GetterWithOptions interface 59 // It fetches information about pending workloads and returns according to query params 60 func (m *pendingWorkloadsInCqREST) Get(ctx context.Context, name string, opts runtime.Object) (runtime.Object, error) { 61 pendingWorkloadOpts, ok := opts.(*v1alpha1.PendingWorkloadOptions) 62 if !ok { 63 return nil, fmt.Errorf("invalid options object: %#v", opts) 64 } 65 limit := pendingWorkloadOpts.Limit 66 offset := pendingWorkloadOpts.Offset 67 68 wls := make([]v1alpha1.PendingWorkload, 0, limit) 69 pendingWorkloadsInfo := m.queueMgr.PendingWorkloadsInfo(name) 70 if pendingWorkloadsInfo == nil { 71 return nil, errors.NewNotFound(v1alpha1.Resource("clusterqueue"), name) 72 } 73 74 localQueuePositions := make(map[string]int32, 0) 75 76 for index := 0; index < int(offset+limit) && index < len(pendingWorkloadsInfo); index++ { 77 // Update positions in LocalQueue 78 wlInfo := pendingWorkloadsInfo[index] 79 queueName := wlInfo.Obj.Spec.QueueName 80 positionInLocalQueue := localQueuePositions[queueName] 81 localQueuePositions[queueName]++ 82 83 if index >= int(offset) { 84 // Add a workload to results 85 wls = append(wls, *newPendingWorkload(wlInfo, positionInLocalQueue, index)) 86 } 87 } 88 return &v1alpha1.PendingWorkloadsSummary{Items: wls}, nil 89 } 90 91 // NewGetOptions creates a new options object 92 func (m *pendingWorkloadsInCqREST) NewGetOptions() (runtime.Object, bool, string) { 93 // If no query parameters were passed the generated defaults function are not executed so it's necessary to set default values here as well 94 return &v1alpha1.PendingWorkloadOptions{ 95 Limit: constants.DefaultPendingWorkloadsLimit, 96 }, false, "" 97 } 98 99 // NamespaceScoped implements rest.Scoper interface 100 func (m *pendingWorkloadsInCqREST) NamespaceScoped() bool { 101 return false 102 }