Skip to content

Commit

Permalink
feat(controllers): add Repository.status.images
Browse files Browse the repository at this point in the history
  • Loading branch information
plaffitt committed Feb 8, 2024
1 parent 23ce61d commit 39633c8
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 5 deletions.
4 changes: 3 additions & 1 deletion api/v1alpha1/repository_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
5 changes: 5 additions & 0 deletions config/crd/bases/kuik.enix.io_repositories.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -129,6 +132,8 @@ spec:
x-kubernetes-list-map-keys:
- type
x-kubernetes-list-type: map
images:
type: integer
phase:
type: string
type: object
Expand Down
24 changes: 20 additions & 4 deletions controllers/repository_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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 {
Expand Down Expand Up @@ -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)
}

Expand All @@ -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
Expand Down
5 changes: 5 additions & 0 deletions helm/kube-image-keeper/templates/repository-crd.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -126,6 +129,8 @@ spec:
x-kubernetes-list-map-keys:
- type
x-kubernetes-list-type: map
images:
type: integer
phase:
type: string
type: object
Expand Down

0 comments on commit 39633c8

Please sign in to comment.