-
Notifications
You must be signed in to change notification settings - Fork 775
/
Copy pathimagejob_util.go
95 lines (82 loc) · 3.16 KB
/
imagejob_util.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
/*
Copyright 2021 The Kruise Authors.
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 utilfunction
import (
"context"
"strconv"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/apimachinery/pkg/util/intstr"
"k8s.io/klog/v2"
utilpointer "k8s.io/utils/pointer"
"sigs.k8s.io/controller-runtime/pkg/client"
appsv1alpha1 "github.com/openkruise/kruise/apis/apps/v1alpha1"
)
func CreateJobForWorkload(c client.Client, owner metav1.Object, gvk schema.GroupVersionKind, name, image string, labels map[string]string, annotations map[string]string, podSelector metav1.LabelSelector, pullSecrets []string) error {
var pullTimeoutSeconds int32 = 300
if str, ok := owner.GetAnnotations()[appsv1alpha1.ImagePreDownloadTimeoutSecondsKey]; ok {
if i, err := strconv.ParseInt(str, 10, 32); err == nil {
pullTimeoutSeconds = int32(i)
}
}
parallelism := intstr.FromInt(1)
if str, ok := owner.GetAnnotations()[appsv1alpha1.ImagePreDownloadParallelismKey]; ok {
if i, err := strconv.Atoi(str); err == nil {
parallelism = intstr.FromInt(i)
}
}
job := &appsv1alpha1.ImagePullJob{
ObjectMeta: metav1.ObjectMeta{
Namespace: owner.GetNamespace(),
Name: name,
OwnerReferences: []metav1.OwnerReference{*metav1.NewControllerRef(owner, gvk)},
Labels: labels,
},
Spec: appsv1alpha1.ImagePullJobSpec{
Image: image,
ImagePullJobTemplate: appsv1alpha1.ImagePullJobTemplate{
PullSecrets: pullSecrets,
PodSelector: &appsv1alpha1.ImagePullJobPodSelector{LabelSelector: podSelector},
Parallelism: ¶llelism,
PullPolicy: &appsv1alpha1.PullPolicy{BackoffLimit: utilpointer.Int32Ptr(1), TimeoutSeconds: &pullTimeoutSeconds},
CompletionPolicy: appsv1alpha1.CompletionPolicy{
Type: appsv1alpha1.Always,
TTLSecondsAfterFinished: utilpointer.Int32Ptr(600),
},
SandboxConfig: &appsv1alpha1.SandboxConfig{
Annotations: annotations,
Labels: labels,
},
},
},
}
return c.Create(context.TODO(), job)
}
func DeleteJobsForWorkload(c client.Client, ownerObj metav1.Object) error {
jobList := &appsv1alpha1.ImagePullJobList{}
if err := c.List(context.TODO(), jobList, client.InNamespace(ownerObj.GetNamespace())); err != nil {
return err
}
for i := range jobList.Items {
job := &jobList.Items[i]
owner := metav1.GetControllerOf(job)
if owner == nil || owner.UID != ownerObj.GetUID() {
continue
}
klog.InfoS("Deleting ImagePullJob for workload", "jobName", job.Name, "ownerKind", owner.Kind, "ownerNamespace", ownerObj.GetNamespace(), "ownerName", ownerObj.GetName())
if err := c.Delete(context.TODO(), job); err != nil {
return err
}
}
return nil
}