...

Source file src/k8s.io/kubernetes/pkg/controller/cronjob/injection.go

Documentation: k8s.io/kubernetes/pkg/controller/cronjob

     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 cronjob
    18  
    19  import (
    20  	"context"
    21  	"sync"
    22  
    23  	batchv1 "k8s.io/api/batch/v1"
    24  	"k8s.io/apimachinery/pkg/api/errors"
    25  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    26  	"k8s.io/apimachinery/pkg/runtime/schema"
    27  	clientset "k8s.io/client-go/kubernetes"
    28  	"k8s.io/client-go/tools/record"
    29  )
    30  
    31  // cjControlInterface is an interface that knows how to update CronJob status
    32  // created as an interface to allow testing.
    33  type cjControlInterface interface {
    34  	UpdateStatus(ctx context.Context, cj *batchv1.CronJob) (*batchv1.CronJob, error)
    35  	// GetCronJob retrieves a CronJob.
    36  	GetCronJob(ctx context.Context, namespace, name string) (*batchv1.CronJob, error)
    37  }
    38  
    39  // realCJControl is the default implementation of cjControlInterface.
    40  type realCJControl struct {
    41  	KubeClient clientset.Interface
    42  }
    43  
    44  func (c *realCJControl) GetCronJob(ctx context.Context, namespace, name string) (*batchv1.CronJob, error) {
    45  	return c.KubeClient.BatchV1().CronJobs(namespace).Get(ctx, name, metav1.GetOptions{})
    46  }
    47  
    48  var _ cjControlInterface = &realCJControl{}
    49  
    50  func (c *realCJControl) UpdateStatus(ctx context.Context, cj *batchv1.CronJob) (*batchv1.CronJob, error) {
    51  	return c.KubeClient.BatchV1().CronJobs(cj.Namespace).UpdateStatus(ctx, cj, metav1.UpdateOptions{})
    52  }
    53  
    54  // fakeCJControl is the default implementation of cjControlInterface.
    55  type fakeCJControl struct {
    56  	CronJob *batchv1.CronJob
    57  	Updates []batchv1.CronJob
    58  }
    59  
    60  func (c *fakeCJControl) GetCronJob(ctx context.Context, namespace, name string) (*batchv1.CronJob, error) {
    61  	if name == c.CronJob.Name && namespace == c.CronJob.Namespace {
    62  		return c.CronJob, nil
    63  	}
    64  	return nil, errors.NewNotFound(schema.GroupResource{
    65  		Group:    "v1beta1",
    66  		Resource: "cronjobs",
    67  	}, name)
    68  }
    69  
    70  var _ cjControlInterface = &fakeCJControl{}
    71  
    72  func (c *fakeCJControl) UpdateStatus(ctx context.Context, cj *batchv1.CronJob) (*batchv1.CronJob, error) {
    73  	c.Updates = append(c.Updates, *cj)
    74  	return cj, nil
    75  }
    76  
    77  // ------------------------------------------------------------------ //
    78  
    79  // jobControlInterface is an interface that knows how to add or delete jobs
    80  // created as an interface to allow testing.
    81  type jobControlInterface interface {
    82  	// GetJob retrieves a Job.
    83  	GetJob(namespace, name string) (*batchv1.Job, error)
    84  	// CreateJob creates new Jobs according to the spec.
    85  	CreateJob(namespace string, job *batchv1.Job) (*batchv1.Job, error)
    86  	// DeleteJob deletes the Job identified by name.
    87  	// TODO: delete by UID?
    88  	DeleteJob(namespace string, name string) error
    89  }
    90  
    91  // realJobControl is the default implementation of jobControlInterface.
    92  type realJobControl struct {
    93  	KubeClient clientset.Interface
    94  	Recorder   record.EventRecorder
    95  }
    96  
    97  var _ jobControlInterface = &realJobControl{}
    98  
    99  func (r realJobControl) GetJob(namespace, name string) (*batchv1.Job, error) {
   100  	return r.KubeClient.BatchV1().Jobs(namespace).Get(context.TODO(), name, metav1.GetOptions{})
   101  }
   102  
   103  func (r realJobControl) CreateJob(namespace string, job *batchv1.Job) (*batchv1.Job, error) {
   104  	return r.KubeClient.BatchV1().Jobs(namespace).Create(context.TODO(), job, metav1.CreateOptions{})
   105  }
   106  
   107  func (r realJobControl) DeleteJob(namespace string, name string) error {
   108  	background := metav1.DeletePropagationBackground
   109  	return r.KubeClient.BatchV1().Jobs(namespace).Delete(context.TODO(), name, metav1.DeleteOptions{PropagationPolicy: &background})
   110  }
   111  
   112  type fakeJobControl struct {
   113  	sync.Mutex
   114  	Job           *batchv1.Job
   115  	Jobs          []batchv1.Job
   116  	DeleteJobName []string
   117  	Err           error
   118  	CreateErr     error
   119  	UpdateJobName []string
   120  	PatchJobName  []string
   121  	Patches       [][]byte
   122  }
   123  
   124  var _ jobControlInterface = &fakeJobControl{}
   125  
   126  func (f *fakeJobControl) CreateJob(namespace string, job *batchv1.Job) (*batchv1.Job, error) {
   127  	f.Lock()
   128  	defer f.Unlock()
   129  	if f.CreateErr != nil {
   130  		return nil, f.CreateErr
   131  	}
   132  	f.Jobs = append(f.Jobs, *job)
   133  	job.UID = "test-uid"
   134  	return job, nil
   135  }
   136  
   137  func (f *fakeJobControl) GetJob(namespace, name string) (*batchv1.Job, error) {
   138  	f.Lock()
   139  	defer f.Unlock()
   140  	if f.Err != nil {
   141  		return nil, f.Err
   142  	}
   143  	return f.Job, nil
   144  }
   145  
   146  func (f *fakeJobControl) DeleteJob(namespace string, name string) error {
   147  	f.Lock()
   148  	defer f.Unlock()
   149  	if f.Err != nil {
   150  		return f.Err
   151  	}
   152  	f.DeleteJobName = append(f.DeleteJobName, name)
   153  	return nil
   154  }
   155  

View as plain text