sigs.k8s.io/kueue@v0.6.2/pkg/controller/admissionchecks/provisioning/indexer.go (about)

     1  /*
     2  Copyright 2023 The Kubernetes Authors.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package provisioning
    18  
    19  import (
    20  	"context"
    21  	"fmt"
    22  
    23  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    24  	autoscaling "k8s.io/autoscaler/cluster-autoscaler/apis/provisioningrequest/autoscaling.x-k8s.io/v1beta1"
    25  	"sigs.k8s.io/controller-runtime/pkg/client"
    26  	"sigs.k8s.io/controller-runtime/pkg/client/apiutil"
    27  	"sigs.k8s.io/controller-runtime/pkg/manager"
    28  
    29  	kueue "sigs.k8s.io/kueue/apis/kueue/v1beta1"
    30  	"sigs.k8s.io/kueue/pkg/util/admissioncheck"
    31  	"sigs.k8s.io/kueue/pkg/util/slices"
    32  )
    33  
    34  const (
    35  	RequestsOwnedByWorkloadKey     = "metadata.ownedByWorkload"
    36  	WorkloadsWithAdmissionCheckKey = "status.admissionChecks"
    37  	AdmissionCheckUsingConfigKey   = "spec.provisioningRequestConfig"
    38  )
    39  
    40  var (
    41  	configGVK = kueue.GroupVersion.WithKind(ConfigKind)
    42  )
    43  
    44  func indexRequestsOwner(obj client.Object) []string {
    45  	refs := obj.GetOwnerReferences()
    46  	if len(refs) == 0 {
    47  		return nil
    48  	}
    49  	return slices.Map(refs, func(r *metav1.OwnerReference) string { return r.Name })
    50  }
    51  
    52  func indexWorkloadsChecks(obj client.Object) []string {
    53  	wl, isWl := obj.(*kueue.Workload)
    54  	if !isWl || len(wl.Status.AdmissionChecks) == 0 {
    55  		return nil
    56  	}
    57  	return slices.Map(wl.Status.AdmissionChecks, func(c *kueue.AdmissionCheckState) string { return c.Name })
    58  }
    59  
    60  func SetupIndexer(ctx context.Context, indexer client.FieldIndexer) error {
    61  	if err := indexer.IndexField(ctx, &autoscaling.ProvisioningRequest{}, RequestsOwnedByWorkloadKey, indexRequestsOwner); err != nil {
    62  		return fmt.Errorf("setting index on provisionRequest owner: %w", err)
    63  	}
    64  	if err := indexer.IndexField(ctx, &kueue.Workload{}, WorkloadsWithAdmissionCheckKey, indexWorkloadsChecks); err != nil {
    65  		return fmt.Errorf("setting index on workloads checks: %w", err)
    66  	}
    67  
    68  	if err := indexer.IndexField(ctx, &kueue.AdmissionCheck{}, AdmissionCheckUsingConfigKey, admissioncheck.IndexerByConfigFunction(ControllerName, configGVK)); err != nil {
    69  		return fmt.Errorf("setting index on admission checks config: %w", err)
    70  	}
    71  	return nil
    72  }
    73  
    74  func ServerSupportsProvisioningRequest(mgr manager.Manager) bool {
    75  	gvk, err := apiutil.GVKForObject(&autoscaling.ProvisioningRequest{}, mgr.GetScheme())
    76  	if err != nil {
    77  		return false
    78  	}
    79  	if _, err = mgr.GetRESTMapper().RESTMapping(gvk.GroupKind(), gvk.Version); err != nil {
    80  		return false
    81  	}
    82  	return true
    83  }