Skip to content

Commit

Permalink
simplify syncNodeImages func (#868)
Browse files Browse the repository at this point in the history
Signed-off-by: hantmac <hantmac@outlook.com>
  • Loading branch information
hantmac authored Dec 28, 2021
1 parent 06e56fb commit 797935c
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 39 deletions.
35 changes: 5 additions & 30 deletions pkg/controller/imagepulljob/imagepulljob_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -249,34 +249,9 @@ func (r *ReconcileImagePullJob) syncNodeImages(job *appsv1alpha1.ImagePullJob, n
parallelism = len(notSyncedNodeImages)
}

ownerRef := &v1.ObjectReference{
APIVersion: controllerKind.GroupVersion().String(),
Kind: controllerKind.Kind,
Name: job.Name,
Namespace: job.Namespace,
UID: job.UID,
}
var secrets []appsv1alpha1.ReferenceObject
for _, secret := range job.Spec.PullSecrets {
secrets = append(secrets,
appsv1alpha1.ReferenceObject{
Namespace: job.Namespace,
Name: secret,
})
}

pullPolicy := appsv1alpha1.ImageTagPullPolicy{}
if job.Spec.PullPolicy != nil {
pullPolicy.BackoffLimit = job.Spec.PullPolicy.BackoffLimit
pullPolicy.TimeoutSeconds = job.Spec.PullPolicy.TimeoutSeconds
}
if job.Spec.CompletionPolicy.Type == appsv1alpha1.Never {
pullPolicy.TTLSecondsAfterFinished = getTTLSecondsForNever()
pullPolicy.ActiveDeadlineSeconds = getActiveDeadlineSecondsForNever()
} else {
pullPolicy.TTLSecondsAfterFinished = getTTLSecondsForAlways(job)
pullPolicy.ActiveDeadlineSeconds = job.Spec.CompletionPolicy.ActiveDeadlineSeconds
}
ownerRef := getOwnerRef(job)
secrets := getSecrets(job)
pullPolicy := getImagePullPolicy(job)

now := metav1.NewTime(r.clock.Now())
imageName, imageTag, _ := daemonutil.NormalizeImageRefToNameTag(job.Spec.Image)
Expand Down Expand Up @@ -304,7 +279,7 @@ func (r *ReconcileImagePullJob) syncNodeImages(job *appsv1alpha1.ImagePullJob, n
if tagSpec.Tag != imageTag {
continue
}
if containsObjectRef(tagSpec.OwnerReferences, *ownerRef) {
if util.ContainsObjectRef(tagSpec.OwnerReferences, *ownerRef) {
skip = true
return nil
}
Expand All @@ -330,7 +305,7 @@ func (r *ReconcileImagePullJob) syncNodeImages(job *appsv1alpha1.ImagePullJob, n
imageSpec.Tags = append(imageSpec.Tags, appsv1alpha1.ImageTagSpec{
Tag: imageTag,
Version: foundVersion + 1,
PullPolicy: &pullPolicy,
PullPolicy: pullPolicy,
OwnerReferences: []v1.ObjectReference{*ownerRef},
CreatedAt: &now,
})
Expand Down
47 changes: 38 additions & 9 deletions pkg/controller/imagepulljob/imagepulljob_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,44 @@ func getTTLSecondsForAlways(job *appsv1alpha1.ImagePullJob) *int32 {
return &ret
}

func getOwnerRef(job *appsv1alpha1.ImagePullJob) *v1.ObjectReference {
return &v1.ObjectReference{
APIVersion: controllerKind.GroupVersion().String(),
Kind: controllerKind.Kind,
Name: job.Name,
Namespace: job.Namespace,
UID: job.UID,
}
}

func getSecrets(job *appsv1alpha1.ImagePullJob) []appsv1alpha1.ReferenceObject {
var secrets []appsv1alpha1.ReferenceObject
for _, secret := range job.Spec.PullSecrets {
secrets = append(secrets,
appsv1alpha1.ReferenceObject{
Namespace: job.Namespace,
Name: secret,
})
}
return secrets
}

func getImagePullPolicy(job *appsv1alpha1.ImagePullJob) *appsv1alpha1.ImageTagPullPolicy {
pullPolicy := &appsv1alpha1.ImageTagPullPolicy{}
if job.Spec.PullPolicy != nil {
pullPolicy.BackoffLimit = job.Spec.PullPolicy.BackoffLimit
pullPolicy.TimeoutSeconds = job.Spec.PullPolicy.TimeoutSeconds
}
if job.Spec.CompletionPolicy.Type == appsv1alpha1.Never {
pullPolicy.TTLSecondsAfterFinished = getTTLSecondsForNever()
pullPolicy.ActiveDeadlineSeconds = getActiveDeadlineSecondsForNever()
} else {
pullPolicy.TTLSecondsAfterFinished = getTTLSecondsForAlways(job)
pullPolicy.ActiveDeadlineSeconds = job.Spec.CompletionPolicy.ActiveDeadlineSeconds
}
return pullPolicy
}

func getTTLSecondsForNever() *int32 {
// 24h +- 10min
var ret = defaultTTLSecondsForNever + rand.Int31n(1200) - 600
Expand All @@ -71,15 +109,6 @@ func containsObject(slice []appsv1alpha1.ReferenceObject, obj appsv1alpha1.Refer
return false
}

func containsObjectRef(slice []v1.ObjectReference, obj v1.ObjectReference) bool {
for _, o := range slice {
if o.UID == obj.UID {
return true
}
}
return false
}

func formatStatusMessage(status *appsv1alpha1.ImagePullJobStatus) (ret string) {
if status.CompletionTime != nil {
return "job has completed"
Expand Down
9 changes: 9 additions & 0 deletions pkg/util/pods.go
Original file line number Diff line number Diff line change
Expand Up @@ -272,3 +272,12 @@ func InjectReadinessGateToPod(pod *v1.Pod, conditionType v1.PodConditionType) {
}
pod.Spec.ReadinessGates = append(pod.Spec.ReadinessGates, v1.PodReadinessGate{ConditionType: conditionType})
}

func ContainsObjectRef(slice []v1.ObjectReference, obj v1.ObjectReference) bool {
for _, o := range slice {
if o.UID == obj.UID {
return true
}
}
return false
}

0 comments on commit 797935c

Please sign in to comment.