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

     1  /*
     2  Copyright 2024 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 multikueue
    18  
    19  import (
    20  	"context"
    21  	"fmt"
    22  	"strings"
    23  
    24  	"sigs.k8s.io/controller-runtime/pkg/client"
    25  
    26  	kueuealpha "sigs.k8s.io/kueue/apis/kueue/v1alpha1"
    27  	kueue "sigs.k8s.io/kueue/apis/kueue/v1beta1"
    28  	"sigs.k8s.io/kueue/pkg/util/admissioncheck"
    29  )
    30  
    31  const (
    32  	UsingKubeConfigs             = "spec.kubeconfigs"
    33  	UsingMultiKueueClusters      = "spec.multiKueueClusters"
    34  	AdmissionCheckUsingConfigKey = "spec.multiKueueConfig"
    35  )
    36  
    37  var (
    38  	configGVK = kueuealpha.GroupVersion.WithKind("MultiKueueConfig")
    39  )
    40  
    41  func getIndexUsingKubeConfigs(configNamespace string) func(obj client.Object) []string {
    42  	return func(obj client.Object) []string {
    43  		cluster, isCluster := obj.(*kueuealpha.MultiKueueCluster)
    44  		if !isCluster {
    45  			return nil
    46  		}
    47  		return []string{strings.Join([]string{configNamespace, cluster.Spec.KubeConfig.Location}, "/")}
    48  	}
    49  }
    50  
    51  func indexUsingMultiKueueClusters(obj client.Object) []string {
    52  	config, isConfig := obj.(*kueuealpha.MultiKueueConfig)
    53  	if !isConfig {
    54  		return nil
    55  	}
    56  	return config.Spec.Clusters
    57  }
    58  
    59  func SetupIndexer(ctx context.Context, indexer client.FieldIndexer, configNamespace string) error {
    60  	if err := indexer.IndexField(ctx, &kueuealpha.MultiKueueCluster{}, UsingKubeConfigs, getIndexUsingKubeConfigs(configNamespace)); err != nil {
    61  		return fmt.Errorf("setting index on clusters using kubeconfig: %w", err)
    62  	}
    63  	if err := indexer.IndexField(ctx, &kueuealpha.MultiKueueConfig{}, UsingMultiKueueClusters, indexUsingMultiKueueClusters); err != nil {
    64  		return fmt.Errorf("setting index on configs using clusters: %w", err)
    65  	}
    66  	if err := indexer.IndexField(ctx, &kueue.AdmissionCheck{}, AdmissionCheckUsingConfigKey, admissioncheck.IndexerByConfigFunction(ControllerName, configGVK)); err != nil {
    67  		return fmt.Errorf("setting index on admission checks config: %w", err)
    68  	}
    69  	return nil
    70  }