...

Source file src/k8s.io/client-go/listers/batch/v1/job_expansion.go

Documentation: k8s.io/client-go/listers/batch/v1

     1  /*
     2  Copyright 2016 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 v1
    18  
    19  import (
    20  	"fmt"
    21  
    22  	batch "k8s.io/api/batch/v1"
    23  	"k8s.io/api/core/v1"
    24  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    25  	"k8s.io/apimachinery/pkg/labels"
    26  )
    27  
    28  // JobListerExpansion allows custom methods to be added to
    29  // JobLister.
    30  type JobListerExpansion interface {
    31  	// GetPodJobs returns a list of Jobs that potentially
    32  	// match a Pod. Only the one specified in the Pod's ControllerRef
    33  	// will actually manage it.
    34  	// Returns an error only if no matching Jobs are found.
    35  	GetPodJobs(pod *v1.Pod) (jobs []batch.Job, err error)
    36  }
    37  
    38  // GetPodJobs returns a list of Jobs that potentially
    39  // match a Pod. Only the one specified in the Pod's ControllerRef
    40  // will actually manage it.
    41  // Returns an error only if no matching Jobs are found.
    42  func (l *jobLister) GetPodJobs(pod *v1.Pod) (jobs []batch.Job, err error) {
    43  	if len(pod.Labels) == 0 {
    44  		err = fmt.Errorf("no jobs found for pod %v because it has no labels", pod.Name)
    45  		return
    46  	}
    47  
    48  	var list []*batch.Job
    49  	list, err = l.Jobs(pod.Namespace).List(labels.Everything())
    50  	if err != nil {
    51  		return
    52  	}
    53  	for _, job := range list {
    54  		selector, err := metav1.LabelSelectorAsSelector(job.Spec.Selector)
    55  		if err != nil {
    56  			// This object has an invalid selector, it does not match the pod
    57  			continue
    58  		}
    59  		if !selector.Matches(labels.Set(pod.Labels)) {
    60  			continue
    61  		}
    62  		jobs = append(jobs, *job)
    63  	}
    64  	if len(jobs) == 0 {
    65  		err = fmt.Errorf("could not find jobs for pod %s in namespace %s with labels: %v", pod.Name, pod.Namespace, pod.Labels)
    66  	}
    67  	return
    68  }
    69  
    70  // JobNamespaceListerExpansion allows custom methods to be added to
    71  // JobNamespaceLister.
    72  type JobNamespaceListerExpansion interface{}
    73  

View as plain text