Skip to content

Commit

Permalink
feat(controllers): retain policy
Browse files Browse the repository at this point in the history
  • Loading branch information
paullaffitte committed Oct 20, 2023
1 parent c6a978a commit f8ad007
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 25 deletions.
9 changes: 6 additions & 3 deletions api/v1alpha1/cachedimage_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,11 @@ var RepositoryLabelName = "kuik.enix.io/repository"
type CachedImageSpec struct {
SourceImage string `json:"sourceImage"`
// +optional
ExpiresAt *metav1.Time `json:"expiresAt,omitempty"`
PullSecretNames []string `json:"pullSecretNames,omitempty"`
PullSecretsNamespace string `json:"pullSecretsNamespace,omitempty"`
ExpiresAt *metav1.Time `json:"expiresAt,omitempty"`
// +optional
Retain bool `json:"retain,omitempty"`
PullSecretNames []string `json:"pullSecretNames,omitempty"`
PullSecretsNamespace string `json:"pullSecretsNamespace,omitempty"`
}

type PodReference struct {
Expand All @@ -36,6 +38,7 @@ type CachedImageStatus struct {
//+kubebuilder:subresource:status
//+kubebuilder:resource:scope=Cluster,shortName=ci
//+kubebuilder:printcolumn:name="Cached",type="boolean",JSONPath=".status.isCached"
//+kubebuilder:printcolumn:name="Retain",type="boolean",JSONPath=".spec.retain"
//+kubebuilder:printcolumn:name="Expires at",type="string",JSONPath=".spec.expiresAt"
//+kubebuilder:printcolumn:name="Pods count",type="integer",JSONPath=".status.usedBy.count"
//+kubebuilder:printcolumn:name="Age",type="date",JSONPath=".metadata.creationTimestamp"
Expand Down
5 changes: 5 additions & 0 deletions config/crd/bases/kuik.enix.io_cachedimages.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ spec:
- jsonPath: .status.isCached
name: Cached
type: boolean
- jsonPath: .spec.retain
name: Retain
type: boolean
- jsonPath: .spec.expiresAt
name: Expires at
type: string
Expand Down Expand Up @@ -59,6 +62,8 @@ spec:
type: array
pullSecretsNamespace:
type: string
retain:
type: boolean
sourceImage:
type: string
required:
Expand Down
51 changes: 31 additions & 20 deletions controllers/cachedimage_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,8 +100,35 @@ func (r *CachedImageReconciler) Reconcile(ctx context.Context, req ctrl.Request)

log = log.WithValues("sourceImage", cachedImage.Spec.SourceImage)

// Delete expired CachedImage and schedule deletion for expiring ones
// Update CachedImage UsedBy status
if requeue, err := r.updatePodCount(ctx, &cachedImage); requeue {
return ctrl.Result{Requeue: true}, nil
} else if err != nil {
return ctrl.Result{}, err
}

// Set an expiration date for unused CachedImage
expiresAt := cachedImage.Spec.ExpiresAt
if len(cachedImage.Status.UsedBy.Pods) == 0 && !cachedImage.Spec.Retain {
expiresAt := metav1.NewTime(time.Now().Add(r.ExpiryDelay))
log.Info("cachedimage is no longer used, setting an expiry date", "cachedImage", klog.KObj(&cachedImage), "expiresAt", expiresAt)
cachedImage.Spec.ExpiresAt = &expiresAt

err := r.Patch(ctx, &cachedImage, client.Merge)
if err != nil && !apierrors.IsNotFound(err) {
return ctrl.Result{}, err
}
} else {
log.Info("cachedimage is used or retained", "cachedImage", klog.KObj(&cachedImage), "expiresAt", expiresAt, "retain", cachedImage.Spec.Retain)
patch := client.MergeFrom(cachedImage.DeepCopy())
cachedImage.Spec.ExpiresAt = nil
err := r.Patch(ctx, &cachedImage, patch)
if err != nil && !apierrors.IsNotFound(err) {
return ctrl.Result{}, err
}
}

// Delete expired CachedImage and schedule deletion for expiring ones
if !expiresAt.IsZero() {
if time.Now().After(expiresAt.Time) {
log.Info("cachedimage expired, deleting it", "now", time.Now(), "expiresAt", expiresAt)
Expand Down Expand Up @@ -156,25 +183,6 @@ func (r *CachedImageReconciler) Reconcile(ctx context.Context, req ctrl.Request)
return ctrl.Result{}, err
}

// Update CachedImage UsedBy status
if requeue, err := r.updatePodCount(ctx, &cachedImage); requeue {
return ctrl.Result{Requeue: true}, nil
} else if err != nil {
return ctrl.Result{}, err
}

// Set an expiration date for unused CachedImage
if len(cachedImage.Status.UsedBy.Pods) == 0 {
expiresAt := metav1.NewTime(time.Now().Add(r.ExpiryDelay))
log.Info("cachedimage not is use anymore, setting an expiry date", "cachedImage", klog.KObj(&cachedImage), "expiresAt", expiresAt)
cachedImage.Spec.ExpiresAt = &expiresAt

err := r.Patch(ctx, &cachedImage, client.Merge)
if err != nil && !apierrors.IsNotFound(err) {
return ctrl.Result{}, err
}
}

log.Info("reconciled cachedimage")
return ctrl.Result{}, nil
}
Expand Down Expand Up @@ -211,6 +219,9 @@ func (r *CachedImageReconciler) SetupWithManager(mgr ctrl.Manager, maxConcurrent
&source.Kind{Type: &corev1.Pod{}},
handler.EnqueueRequestsFromMapFunc(r.cachedImagesRequestFromPod),
builder.WithPredicates(predicate.Funcs{
// GenericFunc: func(e event.GenericEvent) bool {
// return true
// },
DeleteFunc: func(e event.DeleteEvent) bool {
pod := e.Object.(*corev1.Pod)
var currentPod corev1.Pod
Expand Down
7 changes: 5 additions & 2 deletions controllers/pod_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ const LabelImageRewrittenName = "kuik.enix.io/images-rewritten"
// PodReconciler reconciles a Pod object
type PodReconciler struct {
client.Client
Scheme *runtime.Scheme
Scheme *runtime.Scheme
}

//+kubebuilder:rbac:groups=core,resources=pods,verbs=get;list;watch;create;update;patch;delete
Expand Down Expand Up @@ -89,7 +89,10 @@ func (r *PodReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.R
}
} else {
patch := client.MergeFrom(ci.DeepCopy())
ci.Spec = cachedImage.Spec

ci.Spec.PullSecretNames = cachedImage.Spec.PullSecretNames
ci.Spec.PullSecretsNamespace = cachedImage.Spec.PullSecretsNamespace
ci.Spec.SourceImage = cachedImage.Spec.SourceImage

if err = r.Patch(ctx, &ci, patch); err != nil {
return ctrl.Result{}, err
Expand Down
5 changes: 5 additions & 0 deletions helm/kube-image-keeper/templates/cachedimage-crd.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ spec:
- jsonPath: .status.isCached
name: Cached
type: boolean
- jsonPath: .spec.retain
name: Retain
type: boolean
- jsonPath: .spec.expiresAt
name: Expires at
type: string
Expand Down Expand Up @@ -56,6 +59,8 @@ spec:
type: array
pullSecretsNamespace:
type: string
retain:
type: boolean
sourceImage:
type: string
required:
Expand Down

0 comments on commit f8ad007

Please sign in to comment.