Skip to content

Commit

Permalink
feat(controllers): add a finalizer on Repositories
Browse files Browse the repository at this point in the history
  • Loading branch information
plaffitt committed Feb 8, 2024
1 parent 3a0cc36 commit 23ce61d
Show file tree
Hide file tree
Showing 6 changed files with 349 additions and 27 deletions.
9 changes: 9 additions & 0 deletions api/v1alpha1/repository_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,20 @@ type RepositorySpec struct {

// RepositoryStatus defines the observed state of Repository
type RepositoryStatus struct {
Phase string `json:"phase,omitempty"`
//+listType=map
//+listMapKey=type
//+patchStrategy=merge
//+patchMergeKey=type
//+optional
Conditions []metav1.Condition `json:"conditions,omitempty" patchStrategy:"merge" patchMergeKey:"type" protobuf:"bytes,1,rep,name=conditions"`
}

//+kubebuilder:object:root=true
//+kubebuilder:subresource:status
//+kubebuilder:resource:scope=Cluster,shortName=repo
//+kubebuilder:printcolumn:name="Status",type="string",JSONPath=".status.phase"
//+kubebuilder:printcolumn:name="Age",type="date",JSONPath=".metadata.creationTimestamp"

// Repository is the Schema for the repositories API
type Repository struct {
Expand Down
83 changes: 82 additions & 1 deletion config/crd/bases/kuik.enix.io_repositories.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,14 @@ spec:
singular: repository
scope: Cluster
versions:
- name: v1alpha1
- additionalPrinterColumns:
- jsonPath: .status.phase
name: Status
type: string
- jsonPath: .metadata.creationTimestamp
name: Age
type: date
name: v1alpha1
schema:
openAPIV3Schema:
description: Repository is the Schema for the repositories API
Expand Down Expand Up @@ -50,6 +57,80 @@ spec:
type: object
status:
description: RepositoryStatus defines the observed state of Repository
properties:
conditions:
items:
description: "Condition contains details for one aspect of the current
state of this API Resource. --- This struct is intended for direct
use as an array at the field path .status.conditions. For example,
\n type FooStatus struct{ // Represents the observations of a
foo's current state. // Known .status.conditions.type are: \"Available\",
\"Progressing\", and \"Degraded\" // +patchMergeKey=type // +patchStrategy=merge
// +listType=map // +listMapKey=type Conditions []metav1.Condition
`json:\"conditions,omitempty\" patchStrategy:\"merge\" patchMergeKey:\"type\"
protobuf:\"bytes,1,rep,name=conditions\"` \n // other fields }"
properties:
lastTransitionTime:
description: lastTransitionTime is the last time the condition
transitioned from one status to another. This should be when
the underlying condition changed. If that is not known, then
using the time when the API field changed is acceptable.
format: date-time
type: string
message:
description: message is a human readable message indicating
details about the transition. This may be an empty string.
maxLength: 32768
type: string
observedGeneration:
description: observedGeneration represents the .metadata.generation
that the condition was set based upon. For instance, if .metadata.generation
is currently 12, but the .status.conditions[x].observedGeneration
is 9, the condition is out of date with respect to the current
state of the instance.
format: int64
minimum: 0
type: integer
reason:
description: reason contains a programmatic identifier indicating
the reason for the condition's last transition. Producers
of specific condition types may define expected values and
meanings for this field, and whether the values are considered
a guaranteed API. The value should be a CamelCase string.
This field may not be empty.
maxLength: 1024
minLength: 1
pattern: ^[A-Za-z]([A-Za-z0-9_,:]*[A-Za-z0-9_])?$
type: string
status:
description: status of the condition, one of True, False, Unknown.
enum:
- "True"
- "False"
- Unknown
type: string
type:
description: type of condition in CamelCase or in foo.example.com/CamelCase.
--- Many .condition.type values are consistent across resources
like Available, but because arbitrary conditions can be useful
(see .node.status.conditions), the ability to deconflict is
important. The regex it matches is (dns1123SubdomainFmt/)?(qualifiedNameFmt)
maxLength: 316
pattern: ^([a-z0-9]([-a-z0-9]*[a-z0-9])?(\.[a-z0-9]([-a-z0-9]*[a-z0-9])?)*/)?(([A-Za-z0-9][-A-Za-z0-9_.]*)?[A-Za-z0-9])$
type: string
required:
- lastTransitionTime
- message
- reason
- status
- type
type: object
type: array
x-kubernetes-list-map-keys:
- type
x-kubernetes-list-type: map
phase:
type: string
type: object
type: object
served: true
Expand Down
55 changes: 35 additions & 20 deletions controllers/cachedimage_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,10 @@ import (
"github.com/enix/kube-image-keeper/internal/registry"
)

// https://book.kubebuilder.io/reference/using-finalizers.html
const cachedImageFinalizerName = "cachedimage.kuik.enix.io/finalizer"
const (
cachedImageFinalizerName = "cachedimage.kuik.enix.io/finalizer"
repositoryOwnerKey = ".metadata.repositoryOwner"
)

// CachedImageReconciler reconciles a CachedImage object
type CachedImageReconciler struct {
Expand Down Expand Up @@ -113,6 +115,37 @@ func (r *CachedImageReconciler) Reconcile(ctx context.Context, req ctrl.Request)
return ctrl.Result{}, nil
}

// Create or patch related repository
named, err := cachedImage.Repository()
if err != nil {
return ctrl.Result{}, err
}

repositoryName := named.Name()
repository := kuikv1alpha1.Repository{ObjectMeta: metav1.ObjectMeta{Name: registry.SanitizeName(repositoryName)}}
operation, err := controllerutil.CreateOrPatch(ctx, r.Client, &repository, func() error {
repository.Spec.Name = repositoryName
return nil
})

if err != nil {
return ctrl.Result{}, err
}

log.Info("repository updated", "repository", klog.KObj(&repository), "operation", operation)

// Set owner reference
owner := &kuikv1alpha1.Repository{}
if err := r.Get(context.Background(), client.ObjectKeyFromObject(&repository), owner); err != nil {
return ctrl.Result{}, err
}
if err := controllerutil.SetOwnerReference(owner, &cachedImage, r.Scheme); err != nil {
return ctrl.Result{}, err
}
if err := r.Update(ctx, &cachedImage); err != nil {
return ctrl.Result{}, err
}

// Remove image from registry when CachedImage is being deleted, finalizer is removed after it
if !cachedImage.ObjectMeta.DeletionTimestamp.IsZero() {
if controllerutil.ContainsFinalizer(&cachedImage, cachedImageFinalizerName) {
Expand Down Expand Up @@ -196,24 +229,6 @@ func (r *CachedImageReconciler) Reconcile(ctx context.Context, req ctrl.Request)
}
}

named, err := cachedImage.Repository()
if err != nil {
return ctrl.Result{}, err
}

repositoryName := named.Name()
repository := kuikv1alpha1.Repository{ObjectMeta: metav1.ObjectMeta{Name: registry.SanitizeName(repositoryName)}}
operation, err := controllerutil.CreateOrPatch(ctx, r.Client, &repository, func() error {
repository.Spec.Name = repositoryName
return nil
})

if err != nil {
return ctrl.Result{}, err
}

log.Info("repository reconcilied", "repository", klog.KObj(&repository), "operation", operation)

// Adding image to registry
log.Info("caching image")
isCached, err := registry.ImageIsCached(cachedImage.Spec.SourceImage)
Expand Down
5 changes: 2 additions & 3 deletions controllers/pod_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ func (r *PodReconciler) podsWithDeletingCachedImages(obj client.Object) []ctrl.R
cachedImage := obj.(*kuikv1alpha1.CachedImage)
var currentCachedImage kuikv1alpha1.CachedImage
// wait for the CachedImage to be really deleted
if err := r.Get(context.Background(), types.NamespacedName{Name: cachedImage.Name}, &currentCachedImage); err == nil || !apierrors.IsNotFound(err) {
if err := r.Get(context.Background(), client.ObjectKeyFromObject(cachedImage), &currentCachedImage); err == nil || !apierrors.IsNotFound(err) {
return make([]ctrl.Request, 0)
}

Expand All @@ -179,8 +179,7 @@ func (r *PodReconciler) podsWithDeletingCachedImages(obj client.Object) []ctrl.R
if cachedImage.Spec.SourceImage == value {
log.Info("image in use", "pod", pod.Namespace+"/"+pod.Name)
res := make([]ctrl.Request, 1)
res[0].Name = pod.Name
res[0].Namespace = pod.Namespace
res[0].NamespacedName = client.ObjectKeyFromObject(&pod)
return res
}
}
Expand Down
141 changes: 139 additions & 2 deletions controllers/repository_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,29 @@ package controllers
import (
"context"

apierrors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/api/meta"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/types"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/builder"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
"sigs.k8s.io/controller-runtime/pkg/event"
"sigs.k8s.io/controller-runtime/pkg/handler"
"sigs.k8s.io/controller-runtime/pkg/log"
"sigs.k8s.io/controller-runtime/pkg/predicate"
"sigs.k8s.io/controller-runtime/pkg/source"

kuikv1alpha1 "github.com/enix/kube-image-keeper/api/v1alpha1"
)

const (
repositoryFinalizerName = "repository.kuik.enix.io/finalizer"
typeReadyRepository = "Ready"
)

// RepositoryReconciler reconciles a Repository object
type RepositoryReconciler struct {
client.Client
Expand All @@ -31,16 +46,138 @@ type RepositoryReconciler struct {
// For more details, check Reconcile and its Result here:
// - https://pkg.go.dev/sigs.k8s.io/[email protected]/pkg/reconcile
func (r *RepositoryReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) {
_ = log.FromContext(ctx)
log := log.FromContext(ctx)

var repository kuikv1alpha1.Repository
if err := r.Get(ctx, req.NamespacedName, &repository); err != nil {
return ctrl.Result{}, client.IgnoreNotFound(err)
}

log.Info("reconciling repository")

if !repository.ObjectMeta.DeletionTimestamp.IsZero() {
r.UpdateStatus(ctx, &repository, []metav1.Condition{{
Type: typeReadyRepository,
Status: metav1.ConditionFalse,
Reason: "AskedForDeletion",
Message: "Repository has been asked to be deleted",
}})

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 {
return ctrl.Result{}, nil
}

// TODO(user): your logic here
log.Info("removing finalizer")
controllerutil.RemoveFinalizer(&repository, repositoryFinalizerName)
if err := r.Update(ctx, &repository); err != nil {
return ctrl.Result{}, err
}
}

return ctrl.Result{}, nil
}

err := r.UpdateStatus(ctx, &repository, []metav1.Condition{{
Type: typeReadyRepository,
Status: metav1.ConditionTrue,
Reason: "Created",
Message: "Repository is ready",
}})
if err != nil {
return ctrl.Result{}, err
}

// Add finalizer to keep the Repository during image removal from registry on deletion
if !controllerutil.ContainsFinalizer(&repository, repositoryFinalizerName) {
log.Info("adding finalizer")
controllerutil.AddFinalizer(&repository, repositoryFinalizerName)
if err := r.Update(ctx, &repository); err != nil {
return ctrl.Result{}, err
}
}

return ctrl.Result{}, nil
}

func (r *RepositoryReconciler) UpdateStatus(ctx context.Context, repository *kuikv1alpha1.Repository, conditions []metav1.Condition) error {
log := log.FromContext(ctx)

for _, condition := range conditions {
meta.SetStatusCondition(&repository.Status.Conditions, condition)
}

conditionReady := meta.FindStatusCondition(repository.Status.Conditions, typeReadyRepository)
if conditionReady.Status == metav1.ConditionTrue {
repository.Status.Phase = "Ready"
} else if conditionReady.Status == metav1.ConditionFalse {
repository.Status.Phase = "Terminating"
} else {
repository.Status.Phase = ""
}

if err := r.Status().Update(ctx, repository); err != nil {
log.Error(err, "Failed to update Repository status")
return err
}

return nil
}

// SetupWithManager sets up the controller with the Manager.
func (r *RepositoryReconciler) SetupWithManager(mgr ctrl.Manager) error {
p := predicate.Funcs{
DeleteFunc: func(e event.DeleteEvent) bool {
return true
},
}

// Create an index to list CachedImage by Repository
if err := mgr.GetFieldIndexer().IndexField(context.Background(), &kuikv1alpha1.CachedImage{}, repositoryOwnerKey, func(rawObj client.Object) []string {
cachedImage := rawObj.(*kuikv1alpha1.CachedImage)

owners := cachedImage.GetOwnerReferences()
for _, owner := range owners {
if owner.APIVersion != kuikv1alpha1.GroupVersion.String() || owner.Kind != "Repository" {
return nil
}

return []string{owner.Name}
}

return []string{}
}); err != nil {
return err
}

return ctrl.NewControllerManagedBy(mgr).
For(&kuikv1alpha1.Repository{}).
Watches(
&source.Kind{Type: &kuikv1alpha1.CachedImage{}},
handler.EnqueueRequestsFromMapFunc(r.repositoryWithDeletingCachedImages),
builder.WithPredicates(p),
).
Complete(r)
}

func (r *RepositoryReconciler) repositoryWithDeletingCachedImages(obj client.Object) []ctrl.Request {
cachedImage := obj.(*kuikv1alpha1.CachedImage)
var currentCachedImage kuikv1alpha1.CachedImage
// wait for the CachedImage to be really deleted
if err := r.Get(context.Background(), client.ObjectKeyFromObject(cachedImage), &currentCachedImage); err == nil || !apierrors.IsNotFound(err) {
return nil
}

repositoryName, ok := cachedImage.Labels[kuikv1alpha1.RepositoryLabelName]
if !ok {
return nil
}

return []ctrl.Request{{NamespacedName: types.NamespacedName{Name: repositoryName}}}
}
Loading

0 comments on commit 23ce61d

Please sign in to comment.