Skip to content

Commit

Permalink
refactor: Only allow one instance of ShipwrightBuild
Browse files Browse the repository at this point in the history
- Ignore and delete the second instance
  • Loading branch information
k37y committed Oct 17, 2023
1 parent 6c71665 commit e1e5443
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 18 deletions.
1 change: 1 addition & 0 deletions config/rbac/role.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,7 @@ rules:
resources:
- shipwrightbuilds
verbs:
- delete
- get
- list
- patch
Expand Down
57 changes: 40 additions & 17 deletions controllers/shipwrightbuild_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import (
"context"
"fmt"

"sort"
errors2 "github.com/pkg/errors"
"github.com/go-logr/logr"
"github.com/manifestival/manifestival"
tektonoperatorv1alpha1client "github.com/tektoncd/operator/pkg/client/clientset/versioned/typed/operator/v1alpha1"
Expand Down Expand Up @@ -93,6 +95,12 @@ func (r *ShipwrightBuildReconciler) Reconcile(ctx context.Context, req ctrl.Requ
return Requeue()
}

bList := &v1alpha1.ShipwrightBuildList{}
err = r.Client.List(context.TODO(), bList, &client.ListOptions{})
if err != nil {
return ctrl.Result{}, errors2.Wrap(err, "failed listing all Shipwright instances")
}

// retrieving the ShipwrightBuild instance requested for reconcile
b := &v1alpha1.ShipwrightBuild{}
if err := r.Get(ctx, req.NamespacedName, b); err != nil {
Expand All @@ -103,6 +111,21 @@ func (r *ShipwrightBuildReconciler) Reconcile(ctx context.Context, req ctrl.Requ
logger.Error(err, "retrieving ShipwrightBuild object from cache")
return RequeueOnError(err)
}
if len(bList.Items) > 0 {
if len(bList.Items) > 1 {
sort.Slice(bList.Items, func(i, j int) bool {
return bList.Items[j].CreationTimestamp.After(bList.Items[i].CreationTimestamp.Time)
})
}
if bList.Items[0].Name != req.Name {
logger.Info("Ignoring ShipwrightBuild.operator.shipwright.io because one already exists and does not match existing name")
err = r.Client.Delete(context.TODO(), b, &client.DeleteOptions{})
if err != nil {
logger.Error(err, "failed to remove ShipwrightBuild.operator.shipwright.io instance")
}
return ctrl.Result{}, nil
}
}
init := b.Status.Conditions == nil
if init {
b.Status.Conditions = make([]metav1.Condition, 0)
Expand Down Expand Up @@ -161,8 +184,8 @@ func (r *ShipwrightBuildReconciler) Reconcile(ctx context.Context, req ctrl.Requ
// transforming object to target the namespace informed on the CRD (.spec.namespace)
images := common.ToLowerCaseKeys(common.ImagesFromEnv(common.ShipwrightImagePrefix))
manifest, err := r.Manifest.
Filter(manifestival.Not(manifestival.ByKind("Namespace"))).
Transform(manifestival.InjectNamespace(targetNamespace), common.DeploymentImages(images))
Filter(manifestival.Not(manifestival.ByKind("Namespace"))).
Transform(manifestival.InjectNamespace(targetNamespace), common.DeploymentImages(images))
if err != nil {
logger.Error(err, "transforming manifests, injecting namespace")
return RequeueWithError(err)
Expand Down Expand Up @@ -239,19 +262,19 @@ func (r *ShipwrightBuildReconciler) SetupWithManager(mgr ctrl.Manager) error {
return err
}
return ctrl.NewControllerManagedBy(mgr).
For(&v1alpha1.ShipwrightBuild{}, builder.WithPredicates(predicate.Funcs{
CreateFunc: func(ce event.CreateEvent) bool {
// all new objects must be subject to reconciliation
return true
},
DeleteFunc: func(e event.DeleteEvent) bool {
// objects that haven't been confirmed deleted must be subject to reconciliation
return !e.DeleteStateUnknown
},
UpdateFunc: func(e event.UpdateEvent) bool {
// objects that have updated generation must be subject to reconciliation
return e.ObjectOld.GetGeneration() != e.ObjectNew.GetGeneration()
},
})).
Complete(r)
For(&v1alpha1.ShipwrightBuild{}, builder.WithPredicates(predicate.Funcs{
CreateFunc: func(ce event.CreateEvent) bool {
// all new objects must be subject to reconciliation
return true
},
DeleteFunc: func(e event.DeleteEvent) bool {
// objects that haven't been confirmed deleted must be subject to reconciliation
return !e.DeleteStateUnknown
},
UpdateFunc: func(e event.UpdateEvent) bool {
// objects that have updated generation must be subject to reconciliation
return e.ObjectOld.GetGeneration() != e.ObjectNew.GetGeneration()
},
})).
Complete(r)
}
2 changes: 1 addition & 1 deletion controllers/shipwrightbuild_rbac.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ package controllers
// +kubebuilder:rbac:groups=rbac.authorization.k8s.io,resources=roles,resourceNames=shipwright-build-controller,verbs=update;patch;delete
// +kubebuilder:rbac:groups=rbac.authorization.k8s.io,resources=rolebindings,verbs=get;list;watch;create
// +kubebuilder:rbac:groups=rbac.authorization.k8s.io,resources=rolebindings,resourceNames=shipwright-build-controller,verbs=update;patch;delete
// +kubebuilder:rbac:groups=operator.shipwright.io,resources=shipwrightbuilds,verbs=get;list;watch;update;patch
// +kubebuilder:rbac:groups=operator.shipwright.io,resources=shipwrightbuilds,verbs=get;list;watch;update;patch;delete
// +kubebuilder:rbac:groups=operator.shipwright.io,resources=shipwrightbuilds/finalizers,verbs=update
// +kubebuilder:rbac:groups=operator.shipwright.io,resources=shipwrightbuilds/status,verbs=get;update;patch
// +kubebuilder:rbac:groups=operator.tekton.dev,resources=tektonconfigs,verbs=get;list;create
Expand Down

0 comments on commit e1e5443

Please sign in to comment.