@@ -20,7 +20,6 @@ import (
2020 "context"
2121 _ "crypto/sha256"
2222 "fmt"
23- "net/http"
2423 "strings"
2524 "time"
2625
@@ -29,6 +28,7 @@ import (
2928 "k8s.io/apimachinery/pkg/selection"
3029
3130 "github.com/docker/distribution/reference"
31+ "gitlab.enix.io/products/docker-cache-registry/api/v1alpha1"
3232 dcrenixiov1alpha1 "gitlab.enix.io/products/docker-cache-registry/api/v1alpha1"
3333 "gitlab.enix.io/products/docker-cache-registry/internal/registry"
3434 corev1 "k8s.io/api/core/v1"
@@ -100,10 +100,25 @@ func (r *PodReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.R
100100 expiresAt := metav1 .NewTime (time .Now ().Add (r .ExpiryDelay ))
101101 log .Info ("cachedimage not is use anymore, setting an expiry date" , "cachedImage" , klog .KObj (& cachedImage ), "expiresAt" , expiresAt )
102102 cachedImage .Spec .ExpiresAt = & expiresAt
103+
104+ err := r .Patch (ctx , & cachedImage , client .Merge )
105+ if err != nil && ! apierrors .IsNotFound (err ) {
106+ return ctrl.Result {}, err
107+ }
103108 }
104109
105- err := r .Patch (ctx , & cachedImage , client .Apply , client .FieldOwner ("pod-controller" ), client .ForceOwnership )
110+ var ci dcrenixiov1alpha1.CachedImage
111+ err := r .Get (ctx , client .ObjectKeyFromObject (& cachedImage ), & ci )
106112 if err != nil && ! apierrors .IsNotFound (err ) {
113+ if apierrors .IsNotFound (err ) {
114+ continue
115+ } else {
116+ return ctrl.Result {}, err
117+ }
118+ }
119+
120+ err = r .updatePodCount (ctx , & ci )
121+ if err != nil {
107122 return ctrl.Result {}, err
108123 }
109124 }
@@ -132,12 +147,13 @@ func (r *PodReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.R
132147 return ctrl.Result {}, err
133148 }
134149 } else {
150+ patch := client .MergeFrom (ci .DeepCopy ())
135151 ci .Spec = cachedImage .Spec
136- err = r . Update ( ctx , & ci )
137- if err != nil {
138- if statusErr , ok := err .( * apierrors. StatusError ); ok && statusErr . Status (). Code == http . StatusConflict {
139- return ctrl. Result { Requeue : true }, nil
140- }
152+
153+ if err = r . Patch ( ctx , & ci , patch ); err != nil {
154+ return ctrl. Result {}, err
155+ }
156+ if err = r . updatePodCount ( ctx , & ci ); err != nil {
141157 return ctrl.Result {}, err
142158 }
143159 }
@@ -204,6 +220,34 @@ func (r *PodReconciler) podsWithDeletingCachedImages(obj client.Object) []ctrl.R
204220 return make ([]ctrl.Request , 0 )
205221}
206222
223+ // updatePodCount update CachedImage UsedBy status
224+ func (r * PodReconciler ) updatePodCount (ctx context.Context , cachedImage * dcrenixiov1alpha1.CachedImage ) error {
225+ var podsList corev1.PodList
226+ if err := r .List (ctx , & podsList , client.MatchingFields {cachedImageOwnerKey : cachedImage .Name }); err != nil && ! apierrors .IsNotFound (err ) {
227+ return err
228+ }
229+
230+ pods := []v1alpha1.PodReference {}
231+ for _ , pod := range podsList .Items {
232+ if ! pod .DeletionTimestamp .IsZero () {
233+ continue
234+ }
235+ pods = append (pods , v1alpha1.PodReference {NamespacedName : pod .Namespace + "/" + pod .Name })
236+ }
237+
238+ patch := client .MergeFrom (cachedImage .DeepCopy ())
239+ cachedImage .Status .UsedBy = v1alpha1.UsedBy {
240+ Pods : pods ,
241+ Count : len (pods ),
242+ }
243+
244+ if err := r .Status ().Patch (context .Background (), cachedImage , patch ); err != nil {
245+ return client .IgnoreNotFound (err )
246+ }
247+
248+ return nil
249+ }
250+
207251func desiredCachedImages (ctx context.Context , pod * corev1.Pod ) []dcrenixiov1alpha1.CachedImage {
208252 pullSecretNames := []string {}
209253
0 commit comments