Skip to content

Commit b1c0d86

Browse files
committed
fix(controllers): fix pod count
closes #35
1 parent aa3c7ca commit b1c0d86

File tree

2 files changed

+51
-32
lines changed

2 files changed

+51
-32
lines changed

controllers/cachedimage_controller.go

Lines changed: 0 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@ import (
3232
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
3333
"sigs.k8s.io/controller-runtime/pkg/log"
3434

35-
"gitlab.enix.io/products/docker-cache-registry/api/v1alpha1"
3635
dcrenixiov1alpha1 "gitlab.enix.io/products/docker-cache-registry/api/v1alpha1"
3736
"gitlab.enix.io/products/docker-cache-registry/internal/registry"
3837
)
@@ -102,30 +101,6 @@ func (r *CachedImageReconciler) Reconcile(ctx context.Context, req ctrl.Request)
102101
}
103102
}
104103

105-
// Update CachedImage UsedBy status
106-
var podsList corev1.PodList
107-
if err := r.List(ctx, &podsList, client.MatchingFields{cachedImageOwnerKey: cachedImage.Name}); err != nil {
108-
return ctrl.Result{}, err
109-
}
110-
pods := []v1alpha1.PodReference{}
111-
for _, pod := range podsList.Items {
112-
if !pod.DeletionTimestamp.IsZero() {
113-
continue
114-
}
115-
pods = append(pods, v1alpha1.PodReference{NamespacedName: pod.Namespace + "/" + pod.Name})
116-
}
117-
cachedImage.Status.UsedBy = v1alpha1.UsedBy{
118-
Pods: pods,
119-
Count: len(pods),
120-
}
121-
err := r.Status().Update(context.Background(), &cachedImage)
122-
if err != nil {
123-
if statusErr, ok := err.(*errors.StatusError); ok && statusErr.Status().Code == http.StatusConflict {
124-
return ctrl.Result{Requeue: true}, nil
125-
}
126-
return ctrl.Result{}, err
127-
}
128-
129104
log = log.WithValues("sourceImage", cachedImage.Spec.SourceImage)
130105

131106
// Delete expired CachedImage and schedule deletion for expiring ones

controllers/pod_controller.go

Lines changed: 51 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -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+
207251
func desiredCachedImages(ctx context.Context, pod *corev1.Pod) []dcrenixiov1alpha1.CachedImage {
208252
pullSecretNames := []string{}
209253

0 commit comments

Comments
 (0)