diff --git a/api/v1alpha1/repository_types.go b/api/v1alpha1/repository_types.go index d4ad5265..276b99dc 100644 --- a/api/v1alpha1/repository_types.go +++ b/api/v1alpha1/repository_types.go @@ -13,7 +13,8 @@ type RepositorySpec struct { // RepositoryStatus defines the observed state of Repository type RepositoryStatus struct { - Phase string `json:"phase,omitempty"` + Images int `json:"images,omitempty"` + Phase string `json:"phase,omitempty"` //+listType=map //+listMapKey=type //+patchStrategy=merge @@ -26,6 +27,7 @@ type RepositoryStatus struct { //+kubebuilder:subresource:status //+kubebuilder:resource:scope=Cluster,shortName=repo //+kubebuilder:printcolumn:name="Status",type="string",JSONPath=".status.phase" +//+kubebuilder:printcolumn:name="Images",type="string",JSONPath=".status.images" //+kubebuilder:printcolumn:name="Age",type="date",JSONPath=".metadata.creationTimestamp" // Repository is the Schema for the repositories API diff --git a/config/crd/bases/kuik.enix.io_repositories.yaml b/config/crd/bases/kuik.enix.io_repositories.yaml index 53adb727..545e1bc8 100644 --- a/config/crd/bases/kuik.enix.io_repositories.yaml +++ b/config/crd/bases/kuik.enix.io_repositories.yaml @@ -21,6 +21,9 @@ spec: - jsonPath: .status.phase name: Status type: string + - jsonPath: .status.images + name: Images + type: string - jsonPath: .metadata.creationTimestamp name: Age type: date @@ -129,6 +132,8 @@ spec: x-kubernetes-list-map-keys: - type x-kubernetes-list-type: map + images: + type: integer phase: type: string type: object diff --git a/controllers/repository_controller.go b/controllers/repository_controller.go index a76a230c..2af43cec 100644 --- a/controllers/repository_controller.go +++ b/controllers/repository_controller.go @@ -55,6 +55,12 @@ func (r *RepositoryReconciler) Reconcile(ctx context.Context, req ctrl.Request) log.Info("reconciling repository") + var cachedImageList kuikv1alpha1.CachedImageList + if err := r.List(ctx, &cachedImageList, client.MatchingFields{repositoryOwnerKey: repository.Name}); err != nil && !apierrors.IsNotFound(err) { + return ctrl.Result{}, err + } + repository.Status.Images = len(cachedImageList.Items) + if !repository.ObjectMeta.DeletionTimestamp.IsZero() { r.UpdateStatus(ctx, &repository, []metav1.Condition{{ Type: typeReadyRepository, @@ -64,10 +70,6 @@ func (r *RepositoryReconciler) Reconcile(ctx context.Context, req ctrl.Request) }}) if controllerutil.ContainsFinalizer(&repository, repositoryFinalizerName) { - var cachedImageList kuikv1alpha1.CachedImageList - if err := r.List(ctx, &cachedImageList, client.MatchingFields{repositoryOwnerKey: repository.Name}); err != nil && !apierrors.IsNotFound(err) { - return ctrl.Result{}, err - } log.Info("repository is deleting", "cachedImages", len(cachedImageList.Items)) if len(cachedImageList.Items) > 0 { @@ -163,6 +165,15 @@ func (r *RepositoryReconciler) SetupWithManager(mgr ctrl.Manager) error { handler.EnqueueRequestsFromMapFunc(r.repositoryWithDeletingCachedImages), builder.WithPredicates(p), ). + Watches( + &source.Kind{Type: &kuikv1alpha1.CachedImage{}}, + handler.EnqueueRequestsFromMapFunc(requestRepositoryFromCachedImage), + builder.WithPredicates(predicate.Funcs{ + CreateFunc: func(e event.CreateEvent) bool { + return true + }, + }), + ). Complete(r) } @@ -174,6 +185,11 @@ func (r *RepositoryReconciler) repositoryWithDeletingCachedImages(obj client.Obj return nil } + return requestRepositoryFromCachedImage(cachedImage) +} + +func requestRepositoryFromCachedImage(obj client.Object) []ctrl.Request { + cachedImage := obj.(*kuikv1alpha1.CachedImage) repositoryName, ok := cachedImage.Labels[kuikv1alpha1.RepositoryLabelName] if !ok { return nil diff --git a/helm/kube-image-keeper/templates/repository-crd.yaml b/helm/kube-image-keeper/templates/repository-crd.yaml index d7caeb94..11a3ee1c 100644 --- a/helm/kube-image-keeper/templates/repository-crd.yaml +++ b/helm/kube-image-keeper/templates/repository-crd.yaml @@ -18,6 +18,9 @@ spec: - jsonPath: .status.phase name: Status type: string + - jsonPath: .status.images + name: Images + type: string - jsonPath: .metadata.creationTimestamp name: Age type: date @@ -126,6 +129,8 @@ spec: x-kubernetes-list-map-keys: - type x-kubernetes-list-type: map + images: + type: integer phase: type: string type: object