sigs.k8s.io/kueue@v0.6.2/test/integration/controller/jobs/pod/suite_test.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 pod
    18  
    19  import (
    20  	"context"
    21  	"path/filepath"
    22  	"testing"
    23  
    24  	"github.com/onsi/ginkgo/v2"
    25  	"github.com/onsi/gomega"
    26  	"k8s.io/client-go/rest"
    27  	"sigs.k8s.io/controller-runtime/pkg/client"
    28  	"sigs.k8s.io/controller-runtime/pkg/manager"
    29  
    30  	config "sigs.k8s.io/kueue/apis/config/v1beta1"
    31  	"sigs.k8s.io/kueue/pkg/cache"
    32  	"sigs.k8s.io/kueue/pkg/constants"
    33  	"sigs.k8s.io/kueue/pkg/controller/core"
    34  	"sigs.k8s.io/kueue/pkg/controller/core/indexer"
    35  	"sigs.k8s.io/kueue/pkg/controller/jobframework"
    36  	"sigs.k8s.io/kueue/pkg/controller/jobs/job"
    37  	"sigs.k8s.io/kueue/pkg/controller/jobs/pod"
    38  	"sigs.k8s.io/kueue/pkg/queue"
    39  	"sigs.k8s.io/kueue/pkg/scheduler"
    40  	"sigs.k8s.io/kueue/pkg/util/kubeversion"
    41  	"sigs.k8s.io/kueue/pkg/webhooks"
    42  	"sigs.k8s.io/kueue/test/integration/framework"
    43  	// +kubebuilder:scaffold:imports
    44  )
    45  
    46  var (
    47  	cfg                  *rest.Config
    48  	k8sClient            client.Client
    49  	serverVersionFetcher *kubeversion.ServerVersionFetcher
    50  	ctx                  context.Context
    51  	fwk                  *framework.Framework
    52  	crdPath              = filepath.Join("..", "..", "..", "..", "..", "config", "components", "crd", "bases")
    53  	webhookPath          = filepath.Join("..", "..", "..", "..", "..", "config", "components", "webhook")
    54  )
    55  
    56  func TestAPIs(t *testing.T) {
    57  	gomega.RegisterFailHandler(ginkgo.Fail)
    58  
    59  	ginkgo.RunSpecs(t,
    60  		"Pod Controller Suite",
    61  	)
    62  }
    63  
    64  func managerSetup(opts ...jobframework.Option) framework.ManagerSetup {
    65  	return func(mgr manager.Manager, ctx context.Context) {
    66  		err := indexer.Setup(ctx, mgr.GetFieldIndexer())
    67  		gomega.Expect(err).NotTo(gomega.HaveOccurred())
    68  
    69  		err = pod.SetupIndexes(ctx, mgr.GetFieldIndexer())
    70  		gomega.Expect(err).NotTo(gomega.HaveOccurred())
    71  
    72  		podReconciler := pod.NewReconciler(
    73  			mgr.GetClient(),
    74  			mgr.GetEventRecorderFor(constants.JobControllerName),
    75  			opts...)
    76  		err = podReconciler.SetupWithManager(mgr)
    77  		gomega.Expect(err).NotTo(gomega.HaveOccurred())
    78  
    79  		// Job reconciler is enabled for "pod parent managed by queue" tests
    80  		err = job.SetupIndexes(ctx, mgr.GetFieldIndexer())
    81  		gomega.Expect(err).NotTo(gomega.HaveOccurred())
    82  
    83  		jobReconciler := job.NewReconciler(
    84  			mgr.GetClient(),
    85  			mgr.GetEventRecorderFor(constants.JobControllerName),
    86  			opts...)
    87  		err = jobReconciler.SetupWithManager(mgr)
    88  		gomega.Expect(err).NotTo(gomega.HaveOccurred())
    89  
    90  		cCache := cache.New(mgr.GetClient())
    91  		queues := queue.NewManager(mgr.GetClient(), cCache)
    92  
    93  		configuration := &config.Configuration{}
    94  		mgr.GetScheme().Default(configuration)
    95  
    96  		failedCtrl, err := core.SetupControllers(mgr, queues, cCache, configuration)
    97  		gomega.Expect(err).ToNot(gomega.HaveOccurred(), "controller", failedCtrl)
    98  
    99  		err = pod.SetupWebhook(mgr, opts...)
   100  		gomega.Expect(err).NotTo(gomega.HaveOccurred())
   101  		failedWebhook, err := webhooks.Setup(mgr)
   102  		gomega.Expect(err).ToNot(gomega.HaveOccurred(), "webhook", failedWebhook)
   103  	}
   104  }
   105  
   106  func managerAndSchedulerSetup(opts ...jobframework.Option) framework.ManagerSetup {
   107  	return func(mgr manager.Manager, ctx context.Context) {
   108  		err := indexer.Setup(ctx, mgr.GetFieldIndexer())
   109  		gomega.Expect(err).NotTo(gomega.HaveOccurred())
   110  
   111  		err = pod.SetupIndexes(ctx, mgr.GetFieldIndexer())
   112  		gomega.Expect(err).NotTo(gomega.HaveOccurred())
   113  
   114  		reconciler := pod.NewReconciler(
   115  			mgr.GetClient(),
   116  			mgr.GetEventRecorderFor(constants.JobControllerName),
   117  			opts...)
   118  		err = reconciler.SetupWithManager(mgr)
   119  		gomega.Expect(err).NotTo(gomega.HaveOccurred())
   120  
   121  		cCache := cache.New(mgr.GetClient())
   122  		queues := queue.NewManager(mgr.GetClient(), cCache)
   123  
   124  		configuration := &config.Configuration{}
   125  		mgr.GetScheme().Default(configuration)
   126  
   127  		failedCtrl, err := core.SetupControllers(mgr, queues, cCache, configuration)
   128  		gomega.Expect(err).ToNot(gomega.HaveOccurred(), "controller", failedCtrl)
   129  
   130  		err = pod.SetupWebhook(mgr, opts...)
   131  		gomega.Expect(err).NotTo(gomega.HaveOccurred())
   132  
   133  		failedWebhook, err := webhooks.Setup(mgr)
   134  		gomega.Expect(err).ToNot(gomega.HaveOccurred(), "webhook", failedWebhook)
   135  
   136  		sched := scheduler.New(queues, cCache, mgr.GetClient(), mgr.GetEventRecorderFor(constants.AdmissionName))
   137  		err = sched.Start(ctx)
   138  		gomega.Expect(err).NotTo(gomega.HaveOccurred())
   139  	}
   140  }