sigs.k8s.io/kueue@v0.6.2/pkg/controller/admissionchecks/multikueue/jobset_adapter.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  package multikueue
    17  
    18  import (
    19  	"context"
    20  	"errors"
    21  	"fmt"
    22  
    23  	"k8s.io/apimachinery/pkg/runtime"
    24  	"k8s.io/apimachinery/pkg/types"
    25  	"k8s.io/klog/v2"
    26  	"sigs.k8s.io/controller-runtime/pkg/client"
    27  	jobset "sigs.k8s.io/jobset/api/jobset/v1alpha2"
    28  
    29  	kueuealpha "sigs.k8s.io/kueue/apis/kueue/v1alpha1"
    30  	"sigs.k8s.io/kueue/pkg/controller/constants"
    31  )
    32  
    33  type jobsetAdapter struct{}
    34  
    35  var _ jobAdapter = (*jobsetAdapter)(nil)
    36  
    37  func (b *jobsetAdapter) SyncJob(ctx context.Context, localClient client.Client, remoteClient client.Client, key types.NamespacedName, workloadName, origin string) error {
    38  	localJob := jobset.JobSet{}
    39  	err := localClient.Get(ctx, key, &localJob)
    40  	if err != nil {
    41  		return err
    42  	}
    43  
    44  	remoteJob := jobset.JobSet{}
    45  	err = remoteClient.Get(ctx, key, &remoteJob)
    46  	if client.IgnoreNotFound(err) != nil {
    47  		return err
    48  	}
    49  
    50  	// if the remote exists, just copy the status
    51  	if err == nil {
    52  		localJob.Status = remoteJob.Status
    53  		return localClient.Status().Update(ctx, &localJob)
    54  	}
    55  
    56  	remoteJob = jobset.JobSet{
    57  		ObjectMeta: cleanObjectMeta(&localJob.ObjectMeta),
    58  		Spec:       *localJob.Spec.DeepCopy(),
    59  	}
    60  
    61  	// add the prebuilt workload
    62  	if remoteJob.Labels == nil {
    63  		remoteJob.Labels = map[string]string{}
    64  	}
    65  	remoteJob.Labels[constants.PrebuiltWorkloadLabel] = workloadName
    66  	remoteJob.Labels[kueuealpha.MultiKueueOriginLabel] = origin
    67  
    68  	return remoteClient.Create(ctx, &remoteJob)
    69  }
    70  
    71  func (b *jobsetAdapter) DeleteRemoteObject(ctx context.Context, remoteClient client.Client, key types.NamespacedName) error {
    72  	job := jobset.JobSet{}
    73  	err := remoteClient.Get(ctx, key, &job)
    74  	if err != nil {
    75  		return client.IgnoreNotFound(err)
    76  	}
    77  	return client.IgnoreNotFound(remoteClient.Delete(ctx, &job))
    78  }
    79  
    80  func (b *jobsetAdapter) KeepAdmissionCheckPending() bool {
    81  	return false
    82  }
    83  
    84  var _ multiKueueWatcher = (*jobsetAdapter)(nil)
    85  
    86  func (*jobsetAdapter) GetEmptyList() client.ObjectList {
    87  	return &jobset.JobSetList{}
    88  }
    89  
    90  func (*jobsetAdapter) GetWorkloadKey(o runtime.Object) (types.NamespacedName, error) {
    91  	jobSet, isJobSet := o.(*jobset.JobSet)
    92  	if !isJobSet {
    93  		return types.NamespacedName{}, errors.New("not a jobset")
    94  	}
    95  
    96  	prebuiltWl, hasPrebuiltWorkload := jobSet.Labels[constants.PrebuiltWorkloadLabel]
    97  	if !hasPrebuiltWorkload {
    98  		return types.NamespacedName{}, fmt.Errorf("no prebuilt workload found for jobset: %s", klog.KObj(jobSet))
    99  	}
   100  
   101  	return types.NamespacedName{Name: prebuiltWl, Namespace: jobSet.Namespace}, nil
   102  }