/
cronjob.go
122 lines (103 loc) · 2.96 KB
/
cronjob.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
/*
Copyright 2022 Red Hat
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package cronjob
import (
"context"
"fmt"
"time"
batchv1 "k8s.io/api/batch/v1"
k8s_errors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/types"
"github.com/openstack-k8s-operators/lib-common/modules/common/helper"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
)
// NewCronJob returns an initialized CronJob.
func NewCronJob(
cronjob *batchv1.CronJob,
timeout time.Duration,
) *CronJob {
return &CronJob{
cronjob: cronjob,
timeout: timeout,
}
}
// CreateOrPatch - creates or patches a cronjob, reconciles after Xs if object won't exist.
func (cj *CronJob) CreateOrPatch(
ctx context.Context,
h *helper.Helper,
) (ctrl.Result, error) {
op, err := controllerutil.CreateOrPatch(ctx, h.GetClient(), cj.cronjob, func() error {
err := controllerutil.SetControllerReference(h.GetBeforeObject(), cj.cronjob, h.GetScheme())
if err != nil {
return err
}
return nil
})
if err != nil {
if k8s_errors.IsNotFound(err) {
h.GetLogger().Info(fmt.Sprintf("CronJob %s not found, reconcile in %s", cj.cronjob.Name, cj.timeout))
return ctrl.Result{RequeueAfter: cj.timeout}, nil
}
return ctrl.Result{}, err
}
if op != controllerutil.OperationResultNone {
h.GetLogger().Info(fmt.Sprintf("CronJob %s - %s", cj.cronjob.Name, op))
}
return ctrl.Result{}, nil
}
// Delete - delete a cronjob.
func (cj *CronJob) Delete(
ctx context.Context,
h *helper.Helper,
) error {
err := h.GetClient().Delete(ctx, cj.cronjob)
if err != nil && !k8s_errors.IsNotFound(err) {
return fmt.Errorf("Error deleting cronjob %s: %w", cj.cronjob.Name, err)
}
return nil
}
// GetCronJob - get the cronjob object.
func (cj *CronJob) GetCronJob() batchv1.CronJob {
return *cj.cronjob
}
// GetCronJobWithName func
func GetCronJobWithName(
ctx context.Context,
h *helper.Helper,
name string,
namespace string,
) (*batchv1.CronJob, error) {
cronjob := &batchv1.CronJob{}
err := h.GetClient().Get(ctx, types.NamespacedName{Name: name, Namespace: namespace}, cronjob)
if err != nil {
return cronjob, err
}
return cronjob, nil
}
// GetCronJobByName - returns a *CronJob object with specified name and namespace
func GetCronJobByName(
ctx context.Context,
h *helper.Helper,
name string,
namespace string,
) (*CronJob, error) {
cronjob, err := GetCronJobWithName(ctx, h, name, namespace)
cj := &CronJob{
cronjob: cronjob,
}
if err != nil {
return cj, err
}
return cj, nil
}