volcano.sh/volcano@v1.9.0/pkg/controllers/job/job_controller_plugins.go (about)

     1  /*
     2  Copyright 2019 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 job
    18  
    19  import (
    20  	"fmt"
    21  
    22  	v1 "k8s.io/api/core/v1"
    23  	"k8s.io/klog/v2"
    24  
    25  	batch "volcano.sh/apis/pkg/apis/batch/v1alpha1"
    26  	"volcano.sh/volcano/pkg/controllers/job/plugins"
    27  	pluginsinterface "volcano.sh/volcano/pkg/controllers/job/plugins/interface"
    28  )
    29  
    30  func (cc *jobcontroller) pluginOnPodCreate(job *batch.Job, pod *v1.Pod) error {
    31  	client := pluginsinterface.PluginClientset{KubeClients: cc.kubeClient}
    32  	for name, args := range job.Spec.Plugins {
    33  		pb, found := plugins.GetPluginBuilder(name)
    34  		if !found {
    35  			err := fmt.Errorf("failed to get plugin %s", name)
    36  			klog.Error(err)
    37  			return err
    38  		}
    39  		klog.Infof("Starting to execute plugin at <pluginOnPodCreate>: %s on job: <%s/%s>", name, job.Namespace, job.Name)
    40  		if err := pb(client, args).OnPodCreate(pod, job); err != nil {
    41  			klog.Errorf("Failed to process on pod create plugin %s, err %v.", name, err)
    42  			return err
    43  		}
    44  	}
    45  	return nil
    46  }
    47  
    48  func (cc *jobcontroller) pluginOnJobAdd(job *batch.Job) error {
    49  	client := pluginsinterface.PluginClientset{KubeClients: cc.kubeClient}
    50  	if job.Status.ControlledResources == nil {
    51  		job.Status.ControlledResources = make(map[string]string)
    52  	}
    53  	for name, args := range job.Spec.Plugins {
    54  		pb, found := plugins.GetPluginBuilder(name)
    55  		if !found {
    56  			err := fmt.Errorf("failed to get plugin %s", name)
    57  			klog.Error(err)
    58  			return err
    59  		}
    60  		klog.Infof("Starting to execute plugin at <pluginOnJobAdd>: %s on job: <%s/%s>", name, job.Namespace, job.Name)
    61  		if err := pb(client, args).OnJobAdd(job); err != nil {
    62  			klog.Errorf("Failed to process on job add plugin %s, err %v.", name, err)
    63  			return err
    64  		}
    65  	}
    66  
    67  	return nil
    68  }
    69  
    70  func (cc *jobcontroller) pluginOnJobDelete(job *batch.Job) error {
    71  	if job.Status.ControlledResources == nil {
    72  		job.Status.ControlledResources = make(map[string]string)
    73  	}
    74  	client := pluginsinterface.PluginClientset{KubeClients: cc.kubeClient}
    75  	for name, args := range job.Spec.Plugins {
    76  		pb, found := plugins.GetPluginBuilder(name)
    77  		if !found {
    78  			err := fmt.Errorf("failed to get plugin %s", name)
    79  			klog.Error(err)
    80  			return err
    81  		}
    82  		klog.Infof("Starting to execute plugin at <pluginOnJobDelete>: %s on job: <%s/%s>", name, job.Namespace, job.Name)
    83  		if err := pb(client, args).OnJobDelete(job); err != nil {
    84  			klog.Errorf("failed to process on job delete plugin %s, err %v.", name, err)
    85  			return err
    86  		}
    87  	}
    88  
    89  	return nil
    90  }
    91  
    92  func (cc *jobcontroller) pluginOnJobUpdate(job *batch.Job) error {
    93  	client := pluginsinterface.PluginClientset{KubeClients: cc.kubeClient}
    94  	if job.Status.ControlledResources == nil {
    95  		job.Status.ControlledResources = make(map[string]string)
    96  	}
    97  	for name, args := range job.Spec.Plugins {
    98  		pb, found := plugins.GetPluginBuilder(name)
    99  		if !found {
   100  			err := fmt.Errorf("failed to get plugin %s", name)
   101  			klog.Error(err)
   102  			return err
   103  		}
   104  		klog.Infof("Starting to execute plugin at <pluginOnJobUpdate>: %s on job: <%s/%s>", name, job.Namespace, job.Name)
   105  		if err := pb(client, args).OnJobUpdate(job); err != nil {
   106  			klog.Errorf("Failed to process on job update plugin %s, err %v.", name, err)
   107  			return err
   108  		}
   109  	}
   110  
   111  	return nil
   112  }