Skip to content

Commit

Permalink
Merge pull request #151 from 0xff-dev/main
Browse files Browse the repository at this point in the history
feat: add some events to the repository controller
  • Loading branch information
bjwswang authored Jul 22, 2023
2 parents 350b0e2 + 2914a7a commit c3219a4
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 4 deletions.
26 changes: 25 additions & 1 deletion controllers/repository_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,14 @@ import (
"context"
"fmt"
"reflect"
"strings"

"github.com/go-logr/logr"
v1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/client-go/tools/record"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/builder"
"sigs.k8s.io/controller-runtime/pkg/client"
Expand All @@ -43,7 +45,8 @@ import (
// RepositoryReconciler reconciles a Repository object
type RepositoryReconciler struct {
client.Client
Scheme *runtime.Scheme
Scheme *runtime.Scheme
Recorder record.EventRecorder

C map[string]repository.IWatcher
}
Expand Down Expand Up @@ -164,6 +167,17 @@ func (r *RepositoryReconciler) OnRepositryUpdate(u event.UpdateEvent) bool {
oldRepo := u.ObjectOld.(*corev1alpha1.Repository)
newRepo := u.ObjectNew.(*corev1alpha1.Repository)

buf := strings.Builder{}
if oldRepo.Spec.URL != newRepo.Spec.URL {
buf.WriteString(fmt.Sprintf(" 'url' changes from %s to %s.", oldRepo.Spec.URL, newRepo.Spec.URL))
}
if oldRepo.Spec.AuthSecret != newRepo.Spec.AuthSecret {
buf.WriteString(fmt.Sprintf(" 'authSecret' changes from %s to %s.", oldRepo.Spec.AuthSecret, newRepo.Spec.AuthSecret))
}
if str := buf.String(); len(str) > 0 {
r.Recorder.Event(newRepo, v1.EventTypeNormal, "Update", str)
}

return oldRepo.Spec.URL != newRepo.Spec.URL ||
oldRepo.Spec.AuthSecret != newRepo.Spec.AuthSecret ||
!corev1alpha1.IsPullStrategySame(oldRepo.Spec.PullStategy, newRepo.Spec.PullStategy) ||
Expand All @@ -178,7 +192,17 @@ func (r *RepositoryReconciler) OnRepositryUpdate(u event.UpdateEvent) bool {
func (r *RepositoryReconciler) SetupWithManager(mgr ctrl.Manager) error {
return ctrl.NewControllerManagedBy(mgr).
For(&corev1alpha1.Repository{}, builder.WithPredicates(predicate.Funcs{
CreateFunc: func(ce event.CreateEvent) bool {
obj := ce.Object.(*v1alpha1.Repository)
r.Recorder.Eventf(obj, v1.EventTypeNormal, "Created", "add new repository %s", obj.GetName())
return true
},
UpdateFunc: r.OnRepositryUpdate,
DeleteFunc: func(de event.DeleteEvent) bool {
obj := de.Object.(*v1alpha1.Repository)
r.Recorder.Eventf(obj, v1.EventTypeNormal, "Created", "delete repository %s", obj.GetName())
return true
},
})).
Complete(r)
}
7 changes: 4 additions & 3 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,9 +91,10 @@ func main() {
ctx := ctrl.SetupSignalHandler()

if err = (&controllers.RepositoryReconciler{
Client: mgr.GetClient(),
Scheme: mgr.GetScheme(),
C: make(map[string]repository.IWatcher),
Client: mgr.GetClient(),
Scheme: mgr.GetScheme(),
C: make(map[string]repository.IWatcher),
Recorder: mgr.GetEventRecorderFor("repository-reconcile"),
}).SetupWithManager(mgr); err != nil {
setupLog.Error(err, "unable to create controller", "controller", "Repository")
os.Exit(1)
Expand Down

0 comments on commit c3219a4

Please sign in to comment.