Skip to content

Commit

Permalink
Merge pull request #4 from damsien/main
Browse files Browse the repository at this point in the history
🌱 Improve concurent object access
  • Loading branch information
damsien authored Dec 24, 2024
2 parents 9a00287 + f77c29e commit 274c2e5
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 18 deletions.
9 changes: 4 additions & 5 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,12 @@ module github.com/syngit-org/syngit-provider-github
go 1.22.0

require (
github.com/google/go-github v17.0.0+incompatible
github.com/onsi/ginkgo/v2 v2.19.0
github.com/onsi/gomega v1.33.1
github.com/syngit-org/syngit v0.2.0
github.com/syngit-org/syngit v0.2.1
golang.org/x/oauth2 v0.21.0
k8s.io/api v0.31.0
k8s.io/apimachinery v0.31.0
k8s.io/client-go v0.31.0
sigs.k8s.io/controller-runtime v0.19.1
Expand Down Expand Up @@ -37,8 +40,6 @@ require (
github.com/google/cel-go v0.20.1 // indirect
github.com/google/gnostic-models v0.6.8 // indirect
github.com/google/go-cmp v0.6.0 // indirect
github.com/google/go-github v17.0.0+incompatible // indirect
github.com/google/go-github/v67 v67.0.0 // indirect
github.com/google/go-querystring v1.1.0 // indirect
github.com/google/gofuzz v1.2.0 // indirect
github.com/google/pprof v0.0.0-20240525223248-4bfdf5a9a2af // indirect
Expand Down Expand Up @@ -73,7 +74,6 @@ require (
go.uber.org/zap v1.26.0 // indirect
golang.org/x/exp v0.0.0-20230515195305-f3d0a9c9a5cc // indirect
golang.org/x/net v0.26.0 // indirect
golang.org/x/oauth2 v0.21.0 // indirect
golang.org/x/sync v0.7.0 // indirect
golang.org/x/sys v0.21.0 // indirect
golang.org/x/term v0.21.0 // indirect
Expand All @@ -88,7 +88,6 @@ require (
gopkg.in/inf.v0 v0.9.1 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
k8s.io/api v0.31.0 // indirect
k8s.io/apiextensions-apiserver v0.31.0 // indirect
k8s.io/apiserver v0.31.0 // indirect
k8s.io/component-base v0.31.0 // indirect
Expand Down
6 changes: 2 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,6 @@ github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/google/go-github v17.0.0+incompatible h1:N0LgJ1j65A7kfXrZnUDaYCs/Sf4rEjNlfyDHW9dolSY=
github.com/google/go-github v17.0.0+incompatible/go.mod h1:zLgOLi98H3fifZn+44m+umXrS52loVEgC2AApnigrVQ=
github.com/google/go-github/v67 v67.0.0 h1:g11NDAmfaBaCO8qYdI9fsmbaRipHNWRIU/2YGvlh4rg=
github.com/google/go-github/v67 v67.0.0/go.mod h1:zH3K7BxjFndr9QSeFibx4lTKkYS3K9nDanoI1NjaOtY=
github.com/google/go-querystring v1.1.0 h1:AnCroh3fv4ZBgVIf1Iwtovgjaw/GiKJo8M8yD/fhyJ8=
github.com/google/go-querystring v1.1.0/go.mod h1:Kcdr2DB4koayq7X8pmAG4sNG59So17icRSOU623lUBU=
github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
Expand Down Expand Up @@ -135,8 +133,8 @@ github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO
github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
github.com/syngit-org/syngit v0.2.0 h1:5EBGq9E8j1q10JczGk9xWGiVmnT8qhZX+sVpKVm4/eA=
github.com/syngit-org/syngit v0.2.0/go.mod h1:Osb+uQGpzEGMEddxjHhD1NkDH0Lz7TlK2V8iHDqmczs=
github.com/syngit-org/syngit v0.2.1 h1:WDnF9qasE/CYkOVZWjnLRm6WMIytYZUm70eoqZW3zCo=
github.com/syngit-org/syngit v0.2.1/go.mod h1:y4pGzsw07EylMFasASfHSE0CNn2s3GtNtB+OpVIHOtU=
github.com/x448/float16 v0.8.4 h1:qLwI1I70+NjRFUR3zs1JPUCgaCXSh3SW62uAKT1mSBM=
github.com/x448/float16 v0.8.4/go.mod h1:14CWIYCyZA/cWjXOioeEpHeN/83MdbZDRQHoFcYsOfg=
github.com/yuin/goldmark v1.1.27/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
Expand Down
66 changes: 57 additions & 9 deletions internal/controller/remoteuser_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package controller
import (
"context"
"fmt"
"maps"
"slices"

"github.com/google/go-github/github"
Expand All @@ -32,6 +33,7 @@ import (
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/event"
"sigs.k8s.io/controller-runtime/pkg/handler"
"sigs.k8s.io/controller-runtime/pkg/log"
"sigs.k8s.io/controller-runtime/pkg/predicate"
Expand Down Expand Up @@ -75,7 +77,6 @@ func (r *RemoteUserReconciler) Reconcile(ctx context.Context, req ctrl.Request)
var secret corev1.Secret
namespacedNameSecret := types.NamespacedName{Namespace: req.Namespace, Name: remoteUser.Spec.SecretRef.Name}
if err := r.Get(ctx, namespacedNameSecret, &secret); err != nil {
fmt.Println(err)
remoteUserChecker.secret = corev1.Secret{}
} else {
remoteUserChecker.secret = secret
Expand All @@ -84,11 +85,28 @@ func (r *RemoteUserReconciler) Reconcile(ctx context.Context, req ctrl.Request)
remoteUserChecker.testConnection()

remoteUser.Status.Conditions = remoteUserChecker.remoteUser.Status.Conditions
_ = r.Status().Update(ctx, &remoteUser)
_ = r.updateStatus(ctx, req, remoteUserChecker.remoteUser.Status, 2)

return ctrl.Result{}, nil
}

func (r *RemoteUserReconciler) updateStatus(ctx context.Context, req ctrl.Request, status syngit.RemoteUserStatus, retryNumber int) error {
var remoteUser syngit.RemoteUser
if err := r.Get(ctx, req.NamespacedName, &remoteUser); err != nil {
return err
}

remoteUser.Status.ConnexionStatus = status.ConnexionStatus
remoteUser.Status.Conditions = status.Conditions
if err := r.Status().Update(ctx, &remoteUser); err != nil {
if retryNumber > 0 {
return r.updateStatus(ctx, req, status, retryNumber-1)
}
return err
}
return nil
}

func (ruc *RemoteUserChecker) testConnection() {
conditions := ruc.remoteUser.Status.DeepCopy().Conditions

Expand All @@ -112,6 +130,8 @@ func (ruc *RemoteUserChecker) testConnection() {
Message: err.Error(),
LastTransitionTime: metav1.Now(),
}
ruc.remoteUser.Status.ConnexionStatus.Status = ""
ruc.remoteUser.Status.ConnexionStatus.Details = err.Error()
ruc.remoteUser.Status.Conditions = typeBasedConditionUpdater(conditions, condition)
} else {
condition := metav1.Condition{
Expand All @@ -121,6 +141,8 @@ func (ruc *RemoteUserChecker) testConnection() {
Message: fmt.Sprintf("Authentication was successful with the user %s", user.GetLogin()),
LastTransitionTime: metav1.Now(),
}
ruc.remoteUser.Status.ConnexionStatus.Details = ""
ruc.remoteUser.Status.ConnexionStatus.Status = syngit.GitConnected
ruc.remoteUser.Status.Conditions = typeBasedConditionUpdater(conditions, condition)
}
}
Expand Down Expand Up @@ -151,7 +173,7 @@ func typeBasedConditionRemover(conditions []metav1.Condition, typeKind string) [
func (r *RemoteUserReconciler) findObjectsForSecret(ctx context.Context, secret client.Object) []reconcile.Request {
attachedRemoteUsers := &syngit.RemoteUserList{}
listOps := &client.ListOptions{
FieldSelector: fields.OneTermEqualSelector(secretRefField, secret.GetName()),
FieldSelector: fields.OneTermEqualSelector(syngit.SecretRefField, secret.GetName()),
Namespace: secret.GetNamespace(),
}
err := r.List(ctx, attachedRemoteUsers, listOps)
Expand All @@ -171,20 +193,46 @@ func (r *RemoteUserReconciler) findObjectsForSecret(ctx context.Context, secret
return requests
}

const (
secretRefField = ".spec.secretRef.name"
)

// SetupWithManager sets up the controller with the Manager.
func (r *RemoteUserReconciler) SetupWithManager(mgr ctrl.Manager) error {
if err := mgr.GetFieldIndexer().IndexField(context.Background(), &syngit.RemoteUser{}, syngit.SecretRefField, func(rawObj client.Object) []string {
// Extract the Secret name from the RemoteUser Spec, if one is provided
remoteUser := rawObj.(*syngit.RemoteUser)
if remoteUser.Spec.SecretRef.Name == "" {
return nil
}
return []string{remoteUser.Spec.SecretRef.Name}
}); err != nil {
return err
}

p := predicate.Funcs{
UpdateFunc: func(e event.UpdateEvent) bool {
oldObject, _ := e.ObjectOld.(*syngit.RemoteUser)
newObject, _ := e.ObjectNew.(*syngit.RemoteUser)

if newObject != nil {
if !maps.Equal(oldObject.DeepCopy().Labels, newObject.DeepCopy().Labels) {
return true
}
if !maps.Equal(oldObject.DeepCopy().Annotations, newObject.DeepCopy().Annotations) {
return true
}
if oldObject.DeepCopy().Spec != newObject.Spec {
return true
}
}
return false
},
}

return ctrl.NewControllerManagedBy(mgr).
For(&syngit.RemoteUser{}).
For(&syngit.RemoteUser{}, builder.WithPredicates(p)).
Named("remoteuser").
Watches(
&corev1.Secret{},
handler.EnqueueRequestsFromMapFunc(r.findObjectsForSecret),
builder.WithPredicates(predicate.ResourceVersionChangedPredicate{}),
).
WithEventFilter(predicate.GenerationChangedPredicate{}).
Complete(r)
}

0 comments on commit 274c2e5

Please sign in to comment.