Skip to content

Commit

Permalink
feat: Advanced Workload pre-download image support attach metadata in…
Browse files Browse the repository at this point in the history
… ImagePullJob (openkruise#1246)
  • Loading branch information
YTGhost authored Apr 15, 2023
1 parent 26dd7fb commit 66bea91
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 4 deletions.
7 changes: 6 additions & 1 deletion pkg/controller/cloneset/cloneset_predownload_image.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,13 +108,18 @@ func (r *ReconcileCloneSet) createImagePullJobsForInPlaceUpdate(cs *appsv1alpha1
}
labelMap[history.ControllerRevisionHashLabel] = updateRevision.Labels[history.ControllerRevisionHashLabel]

annotationMap := make(map[string]string)
for k, v := range cs.Spec.Template.Annotations {
annotationMap[k] = v
}

containerImages := diffImagesBetweenRevisions(currentRevision, updateRevision)
klog.V(3).Infof("CloneSet %s/%s begin to create ImagePullJobs for revision %s -> %s: %v",
cs.Namespace, cs.Name, currentRevision.Name, updateRevision.Name, containerImages)
for name, image := range containerImages {
// job name is revision name + container name, it can not be more than 255 characters
jobName := fmt.Sprintf("%s-%s", updateRevision.Name, name)
err := imagejobutilfunc.CreateJobForWorkload(r.Client, cs, clonesetutils.ControllerKind, jobName, image, labelMap, *selector, pullSecrets)
err := imagejobutilfunc.CreateJobForWorkload(r.Client, cs, clonesetutils.ControllerKind, jobName, image, labelMap, annotationMap, *selector, pullSecrets)
if err != nil {
if !errors.IsAlreadyExists(err) {
klog.Errorf("CloneSet %s/%s failed to create ImagePullJob %s: %v", cs.Namespace, cs.Name, jobName, err)
Expand Down
7 changes: 6 additions & 1 deletion pkg/controller/daemonset/daemonset_predownload_image.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,13 +79,18 @@ func (dsc *ReconcileDaemonSet) createImagePullJobsForInPlaceUpdate(ds *appsv1alp
}
labelMap[history.ControllerRevisionHashLabel] = updateRevision.Labels[history.ControllerRevisionHashLabel]

annotationMap := make(map[string]string)
for k, v := range ds.Spec.Template.Annotations {
annotationMap[k] = v
}

containerImages := diffImagesBetweenRevisions(oldRevisions, updateRevision)
klog.V(3).Infof("DaemonSet %s/%s begin to create ImagePullJobs for revision %s: %v",
ds.Namespace, ds.Name, updateRevision.Name, containerImages)
for name, image := range containerImages {
// job name is revision name + container name, it can not be more than 255 characters
jobName := fmt.Sprintf("%s-%s", updateRevision.Name, name)
err := imagejobutilfunc.CreateJobForWorkload(dsc.Client, ds, controllerKind, jobName, image, labelMap, *selector, pullSecrets)
err := imagejobutilfunc.CreateJobForWorkload(dsc.Client, ds, controllerKind, jobName, image, labelMap, annotationMap, *selector, pullSecrets)
if err != nil {
if !errors.IsAlreadyExists(err) {
klog.Errorf("DaemonSet %s/%s failed to create ImagePullJob %s: %v", ds.Namespace, ds.Name, jobName, err)
Expand Down
7 changes: 6 additions & 1 deletion pkg/controller/statefulset/statefulset_predownload_image.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,13 +110,18 @@ func (dss *defaultStatefulSetControl) createImagePullJobsForInPlaceUpdate(sts *a
}
labelMap[history.ControllerRevisionHashLabel] = updateRevision.Labels[history.ControllerRevisionHashLabel]

annotationMap := make(map[string]string)
for k, v := range sts.Spec.Template.Annotations {
annotationMap[k] = v
}

containerImages := diffImagesBetweenRevisions(currentRevision, updateRevision)
klog.V(3).Infof("Statefulset %s/%s begin to create ImagePullJobs for revision %s -> %s: %v",
sts.Namespace, sts.Name, currentRevision.Name, updateRevision.Name, containerImages)
for name, image := range containerImages {
// job name is revision name + container name, it can not be more than 255 characters
jobName := fmt.Sprintf("%s-%s", updateRevision.Name, name)
err := imagejobutilfunc.CreateJobForWorkload(sigsruntimeClient, sts, controllerKind, jobName, image, labelMap, *selector, pullSecrets)
err := imagejobutilfunc.CreateJobForWorkload(sigsruntimeClient, sts, controllerKind, jobName, image, labelMap, annotationMap, *selector, pullSecrets)
if err != nil {
if !errors.IsAlreadyExists(err) {
klog.Errorf("Statefulset %s/%s failed to create ImagePullJob %s: %v", sts.Namespace, sts.Name, jobName, err)
Expand Down
6 changes: 5 additions & 1 deletion pkg/util/imagejob/utilfunction/imagejob_util.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ import (
"sigs.k8s.io/controller-runtime/pkg/client"
)

func CreateJobForWorkload(c client.Client, owner metav1.Object, gvk schema.GroupVersionKind, name, image string, labels map[string]string, podSelector metav1.LabelSelector, pullSecrets []string) error {
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.Atoi(str); err == nil {
Expand Down Expand Up @@ -61,6 +61,10 @@ func CreateJobForWorkload(c client.Client, owner metav1.Object, gvk schema.Group
Type: appsv1alpha1.Always,
TTLSecondsAfterFinished: utilpointer.Int32Ptr(600),
},
SandboxConfig: &appsv1alpha1.SandboxConfig{
Annotations: annotations,
Labels: labels,
},
},
}

Expand Down

0 comments on commit 66bea91

Please sign in to comment.