Skip to content

Commit

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

type PodReference struct {
Expand All @@ -38,7 +36,6 @@ 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: 0 additions & 5 deletions config/crd/bases/kuik.enix.io_cachedimages.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,6 @@ 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 @@ -62,8 +59,6 @@ spec:
type: array
pullSecretsNamespace:
type: string
retain:
type: boolean
sourceImage:
type: string
required:
Expand Down
51 changes: 20 additions & 31 deletions controllers/cachedimage_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,35 +102,8 @@ func (r *CachedImageReconciler) Reconcile(ctx context.Context, req ctrl.Request)

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

// 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
expiresAt := cachedImage.Spec.ExpiresAt
if !expiresAt.IsZero() {
if time.Now().After(expiresAt.Time) {
log.Info("cachedimage expired, deleting it", "now", time.Now(), "expiresAt", expiresAt)
Expand Down Expand Up @@ -185,6 +158,25 @@ 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 @@ -221,9 +213,6 @@ 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: 2 additions & 5 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,10 +89,7 @@ func (r *PodReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.R
}
} else {
patch := client.MergeFrom(ci.DeepCopy())

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

if err = r.Patch(ctx, &ci, patch); err != nil {
return ctrl.Result{}, err
Expand Down
5 changes: 0 additions & 5 deletions helm/kube-image-keeper/templates/cachedimage-crd.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,6 @@ 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,8 +56,6 @@ spec:
type: array
pullSecretsNamespace:
type: string
retain:
type: boolean
sourceImage:
type: string
required:
Expand Down

0 comments on commit 5c47152

Please sign in to comment.