Skip to content

Commit 39633c8

Browse files
committed
feat(controllers): add Repository.status.images
1 parent 23ce61d commit 39633c8

File tree

4 files changed

+33
-5
lines changed

4 files changed

+33
-5
lines changed

api/v1alpha1/repository_types.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@ type RepositorySpec struct {
1313

1414
// RepositoryStatus defines the observed state of Repository
1515
type RepositoryStatus struct {
16-
Phase string `json:"phase,omitempty"`
16+
Images int `json:"images,omitempty"`
17+
Phase string `json:"phase,omitempty"`
1718
//+listType=map
1819
//+listMapKey=type
1920
//+patchStrategy=merge
@@ -26,6 +27,7 @@ type RepositoryStatus struct {
2627
//+kubebuilder:subresource:status
2728
//+kubebuilder:resource:scope=Cluster,shortName=repo
2829
//+kubebuilder:printcolumn:name="Status",type="string",JSONPath=".status.phase"
30+
//+kubebuilder:printcolumn:name="Images",type="string",JSONPath=".status.images"
2931
//+kubebuilder:printcolumn:name="Age",type="date",JSONPath=".metadata.creationTimestamp"
3032

3133
// Repository is the Schema for the repositories API

config/crd/bases/kuik.enix.io_repositories.yaml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@ spec:
2121
- jsonPath: .status.phase
2222
name: Status
2323
type: string
24+
- jsonPath: .status.images
25+
name: Images
26+
type: string
2427
- jsonPath: .metadata.creationTimestamp
2528
name: Age
2629
type: date
@@ -129,6 +132,8 @@ spec:
129132
x-kubernetes-list-map-keys:
130133
- type
131134
x-kubernetes-list-type: map
135+
images:
136+
type: integer
132137
phase:
133138
type: string
134139
type: object

controllers/repository_controller.go

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,12 @@ func (r *RepositoryReconciler) Reconcile(ctx context.Context, req ctrl.Request)
5555

5656
log.Info("reconciling repository")
5757

58+
var cachedImageList kuikv1alpha1.CachedImageList
59+
if err := r.List(ctx, &cachedImageList, client.MatchingFields{repositoryOwnerKey: repository.Name}); err != nil && !apierrors.IsNotFound(err) {
60+
return ctrl.Result{}, err
61+
}
62+
repository.Status.Images = len(cachedImageList.Items)
63+
5864
if !repository.ObjectMeta.DeletionTimestamp.IsZero() {
5965
r.UpdateStatus(ctx, &repository, []metav1.Condition{{
6066
Type: typeReadyRepository,
@@ -64,10 +70,6 @@ func (r *RepositoryReconciler) Reconcile(ctx context.Context, req ctrl.Request)
6470
}})
6571

6672
if controllerutil.ContainsFinalizer(&repository, repositoryFinalizerName) {
67-
var cachedImageList kuikv1alpha1.CachedImageList
68-
if err := r.List(ctx, &cachedImageList, client.MatchingFields{repositoryOwnerKey: repository.Name}); err != nil && !apierrors.IsNotFound(err) {
69-
return ctrl.Result{}, err
70-
}
7173

7274
log.Info("repository is deleting", "cachedImages", len(cachedImageList.Items))
7375
if len(cachedImageList.Items) > 0 {
@@ -163,6 +165,15 @@ func (r *RepositoryReconciler) SetupWithManager(mgr ctrl.Manager) error {
163165
handler.EnqueueRequestsFromMapFunc(r.repositoryWithDeletingCachedImages),
164166
builder.WithPredicates(p),
165167
).
168+
Watches(
169+
&source.Kind{Type: &kuikv1alpha1.CachedImage{}},
170+
handler.EnqueueRequestsFromMapFunc(requestRepositoryFromCachedImage),
171+
builder.WithPredicates(predicate.Funcs{
172+
CreateFunc: func(e event.CreateEvent) bool {
173+
return true
174+
},
175+
}),
176+
).
166177
Complete(r)
167178
}
168179

@@ -174,6 +185,11 @@ func (r *RepositoryReconciler) repositoryWithDeletingCachedImages(obj client.Obj
174185
return nil
175186
}
176187

188+
return requestRepositoryFromCachedImage(cachedImage)
189+
}
190+
191+
func requestRepositoryFromCachedImage(obj client.Object) []ctrl.Request {
192+
cachedImage := obj.(*kuikv1alpha1.CachedImage)
177193
repositoryName, ok := cachedImage.Labels[kuikv1alpha1.RepositoryLabelName]
178194
if !ok {
179195
return nil

helm/kube-image-keeper/templates/repository-crd.yaml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@ spec:
1818
- jsonPath: .status.phase
1919
name: Status
2020
type: string
21+
- jsonPath: .status.images
22+
name: Images
23+
type: string
2124
- jsonPath: .metadata.creationTimestamp
2225
name: Age
2326
type: date
@@ -126,6 +129,8 @@ spec:
126129
x-kubernetes-list-map-keys:
127130
- type
128131
x-kubernetes-list-type: map
132+
images:
133+
type: integer
129134
phase:
130135
type: string
131136
type: object

0 commit comments

Comments
 (0)