volcano.sh/volcano@v1.9.0/pkg/controllers/job/plugins/env/env.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 env
    18  
    19  import (
    20  	v1 "k8s.io/api/core/v1"
    21  
    22  	batch "volcano.sh/apis/pkg/apis/batch/v1alpha1"
    23  	jobhelpers "volcano.sh/volcano/pkg/controllers/job/helpers"
    24  	pluginsinterface "volcano.sh/volcano/pkg/controllers/job/plugins/interface"
    25  )
    26  
    27  type envPlugin struct {
    28  	// Arguments given for the plugin
    29  	pluginArguments []string
    30  
    31  	Clientset pluginsinterface.PluginClientset
    32  }
    33  
    34  // New creates env plugin.
    35  func New(client pluginsinterface.PluginClientset, arguments []string) pluginsinterface.PluginInterface {
    36  	envPlugin := envPlugin{pluginArguments: arguments, Clientset: client}
    37  
    38  	return &envPlugin
    39  }
    40  
    41  func (ep *envPlugin) Name() string {
    42  	return "env"
    43  }
    44  
    45  func (ep *envPlugin) OnPodCreate(pod *v1.Pod, job *batch.Job) error {
    46  	index := jobhelpers.GetPodIndexUnderTask(pod)
    47  
    48  	// add VK_TASK_INDEX and VC_TASK_INDEX env to each container
    49  	for i := range pod.Spec.Containers {
    50  		pod.Spec.Containers[i].Env = append(pod.Spec.Containers[i].Env, v1.EnvVar{Name: TaskVkIndex, Value: index})
    51  		pod.Spec.Containers[i].Env = append(pod.Spec.Containers[i].Env, v1.EnvVar{Name: TaskIndex, Value: index})
    52  	}
    53  
    54  	// add VK_TASK_INDEX and VC_TASK_INDEX env to each init container
    55  	for i := range pod.Spec.InitContainers {
    56  		pod.Spec.InitContainers[i].Env = append(pod.Spec.InitContainers[i].Env, v1.EnvVar{Name: TaskVkIndex, Value: index})
    57  		pod.Spec.InitContainers[i].Env = append(pod.Spec.InitContainers[i].Env, v1.EnvVar{Name: TaskIndex, Value: index})
    58  	}
    59  
    60  	return nil
    61  }
    62  
    63  func (ep *envPlugin) OnJobAdd(job *batch.Job) error {
    64  	if job.Status.ControlledResources["plugin-"+ep.Name()] == ep.Name() {
    65  		return nil
    66  	}
    67  
    68  	job.Status.ControlledResources["plugin-"+ep.Name()] = ep.Name()
    69  
    70  	return nil
    71  }
    72  
    73  func (ep *envPlugin) OnJobDelete(job *batch.Job) error {
    74  	if job.Status.ControlledResources["plugin-"+ep.Name()] != ep.Name() {
    75  		return nil
    76  	}
    77  	delete(job.Status.ControlledResources, "plugin-"+ep.Name())
    78  	return nil
    79  }
    80  
    81  func (ep *envPlugin) OnJobUpdate(job *batch.Job) error {
    82  	return nil
    83  }