From 66bea910c7bc8ee24f06bdca1d196cac688c2d13 Mon Sep 17 00:00:00 2001 From: Liang Deng <283304489@qq.com> Date: Sat, 15 Apr 2023 09:15:56 +0800 Subject: [PATCH] feat: Advanced Workload pre-download image support attach metadata in ImagePullJob (#1246) --- pkg/controller/cloneset/cloneset_predownload_image.go | 7 ++++++- pkg/controller/daemonset/daemonset_predownload_image.go | 7 ++++++- .../statefulset/statefulset_predownload_image.go | 7 ++++++- pkg/util/imagejob/utilfunction/imagejob_util.go | 6 +++++- 4 files changed, 23 insertions(+), 4 deletions(-) diff --git a/pkg/controller/cloneset/cloneset_predownload_image.go b/pkg/controller/cloneset/cloneset_predownload_image.go index f02fd4f019..a9bfaf0732 100644 --- a/pkg/controller/cloneset/cloneset_predownload_image.go +++ b/pkg/controller/cloneset/cloneset_predownload_image.go @@ -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) diff --git a/pkg/controller/daemonset/daemonset_predownload_image.go b/pkg/controller/daemonset/daemonset_predownload_image.go index 4e1117c1a4..a3466fa2d1 100644 --- a/pkg/controller/daemonset/daemonset_predownload_image.go +++ b/pkg/controller/daemonset/daemonset_predownload_image.go @@ -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) diff --git a/pkg/controller/statefulset/statefulset_predownload_image.go b/pkg/controller/statefulset/statefulset_predownload_image.go index 8f9c274cc6..bb5cd0ede0 100644 --- a/pkg/controller/statefulset/statefulset_predownload_image.go +++ b/pkg/controller/statefulset/statefulset_predownload_image.go @@ -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) diff --git a/pkg/util/imagejob/utilfunction/imagejob_util.go b/pkg/util/imagejob/utilfunction/imagejob_util.go index f99ba591b6..2e374d3a49 100644 --- a/pkg/util/imagejob/utilfunction/imagejob_util.go +++ b/pkg/util/imagejob/utilfunction/imagejob_util.go @@ -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 { @@ -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, + }, }, }