volcano.sh/volcano@v1.9.0/pkg/controllers/jobflow/jobflow_controller_handler.go (about)

     1  /*
     2  Copyright 2022 The Volcano 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 jobflow
    18  
    19  import (
    20  	"k8s.io/klog"
    21  
    22  	batch "volcano.sh/apis/pkg/apis/batch/v1alpha1"
    23  	jobflowv1alpha1 "volcano.sh/apis/pkg/apis/flow/v1alpha1"
    24  	"volcano.sh/apis/pkg/apis/helpers"
    25  	"volcano.sh/volcano/pkg/controllers/apis"
    26  )
    27  
    28  func (jf *jobflowcontroller) enqueue(req apis.FlowRequest) {
    29  	jf.queue.Add(req)
    30  }
    31  
    32  func (jf *jobflowcontroller) addJobFlow(obj interface{}) {
    33  	jobFlow, ok := obj.(*jobflowv1alpha1.JobFlow)
    34  	if !ok {
    35  		klog.Errorf("Failed to convert %v to jobFlow", obj)
    36  		return
    37  	}
    38  
    39  	// use struct instead of pointer
    40  	req := apis.FlowRequest{
    41  		Namespace:   jobFlow.Namespace,
    42  		JobFlowName: jobFlow.Name,
    43  
    44  		Action: jobflowv1alpha1.SyncJobFlowAction,
    45  		Event:  jobflowv1alpha1.OutOfSyncEvent,
    46  	}
    47  
    48  	jf.enqueueJobFlow(req)
    49  }
    50  
    51  func (jf *jobflowcontroller) updateJobFlow(oldObj, newObj interface{}) {
    52  	oldJobFlow, ok := oldObj.(*jobflowv1alpha1.JobFlow)
    53  	if !ok {
    54  		klog.Errorf("Failed to convert %v to vcjob", oldJobFlow)
    55  		return
    56  	}
    57  
    58  	newJobFlow, ok := newObj.(*jobflowv1alpha1.JobFlow)
    59  	if !ok {
    60  		klog.Errorf("Failed to convert %v to vcjob", newJobFlow)
    61  		return
    62  	}
    63  
    64  	if newJobFlow.ResourceVersion == oldJobFlow.ResourceVersion {
    65  		return
    66  	}
    67  
    68  	//Todo The update operation of JobFlow is reserved for possible future use. The current update operation on JobFlow will not affect the JobFlow process
    69  	if newJobFlow.Status.State.Phase != jobflowv1alpha1.Succeed || newJobFlow.Spec.JobRetainPolicy != jobflowv1alpha1.Delete {
    70  		return
    71  	}
    72  
    73  	req := apis.FlowRequest{
    74  		Namespace:   newJobFlow.Namespace,
    75  		JobFlowName: newJobFlow.Name,
    76  
    77  		Action: jobflowv1alpha1.SyncJobFlowAction,
    78  		Event:  jobflowv1alpha1.OutOfSyncEvent,
    79  	}
    80  
    81  	jf.enqueueJobFlow(req)
    82  }
    83  
    84  func (jf *jobflowcontroller) updateJob(oldObj, newObj interface{}) {
    85  	oldJob, ok := oldObj.(*batch.Job)
    86  	if !ok {
    87  		klog.Errorf("Failed to convert %v to vcjob", oldObj)
    88  		return
    89  	}
    90  
    91  	newJob, ok := newObj.(*batch.Job)
    92  	if !ok {
    93  		klog.Errorf("Failed to convert %v to vcjob", newObj)
    94  		return
    95  	}
    96  
    97  	// Filter out jobs that are not created from volcano jobflow
    98  	if !isControlledBy(newJob, helpers.JobFlowKind) {
    99  		return
   100  	}
   101  
   102  	if newJob.ResourceVersion == oldJob.ResourceVersion {
   103  		return
   104  	}
   105  
   106  	jobFlowName := getJobFlowNameByJob(newJob)
   107  	if jobFlowName == "" {
   108  		return
   109  	}
   110  
   111  	req := apis.FlowRequest{
   112  		Namespace:   newJob.Namespace,
   113  		JobFlowName: jobFlowName,
   114  		Action:      jobflowv1alpha1.SyncJobFlowAction,
   115  		Event:       jobflowv1alpha1.OutOfSyncEvent,
   116  	}
   117  
   118  	jf.enqueueJobFlow(req)
   119  }