Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Modify DeactivationService to set scheduled deactivation time for active UserSignups #988

Merged
merged 57 commits into from
May 15, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
57 commits
Select commit Hold shift + click to select a range
b744180
Allow registration service to get/list/watch UserTier resources
sbryzak Mar 11, 2024
73d39ca
regenerated from API
sbryzak Mar 12, 2024
cd8819a
regenerated
sbryzak Mar 15, 2024
7d35f22
updated deactivation controller
sbryzak Mar 15, 2024
0ece3f2
regenerated api
sbryzak Mar 15, 2024
fa5a1b8
Merge remote-tracking branch 'origin/master' into SANDBOX-554
sbryzak Mar 15, 2024
30ae9a9
Merge remote-tracking branch 'origin/master' into SANDBOX-554
sbryzak Mar 16, 2024
3d864f6
updated api
sbryzak Mar 16, 2024
27eab89
Merge remote-tracking branch 'origin/master' into SANDBOX-554
sbryzak Mar 20, 2024
76b3d25
Merge remote-tracking branch 'origin/master' into SANDBOX-554
sbryzak Mar 25, 2024
7376ab0
refactor
sbryzak Mar 26, 2024
8ac7c1c
restore missing code
sbryzak Mar 26, 2024
141eda3
revert change to deactivation controller, documented reasons why
sbryzak Mar 26, 2024
0018b07
added test
sbryzak Mar 27, 2024
96bd1d0
Merge branch 'master' into SANDBOX-554
mfrancisc Apr 4, 2024
fdc0529
Merge remote-tracking branch 'origin/master' into SANDBOX-554
sbryzak Apr 8, 2024
5fde800
moved deactivation schedule logic to deactivation controller
sbryzak Apr 8, 2024
bc400eb
Merge branch 'SANDBOX-554' of https://github.com/sbryzak/host-operato…
sbryzak Apr 8, 2024
ee76ba0
moved testing
sbryzak Apr 9, 2024
51582aa
updated
sbryzak Apr 11, 2024
cd5409c
added sufficient test coverage for all places where scheduled deactiv…
sbryzak Apr 11, 2024
d7f3ae4
Merge remote-tracking branch 'origin' into SANDBOX-554
sbryzak Apr 11, 2024
fa1967d
fix setting scheduled deactivation time to nil only when usersignup.S…
sbryzak Apr 12, 2024
7b5087b
disable gocyclo linter for reconcile function, breaking this function…
sbryzak Apr 12, 2024
91f822e
deactivation controller should reconcile after MUR is deleted in orde…
sbryzak Apr 14, 2024
50aeeb2
a smarter way to handle resetting the scheduled deactivation time to …
sbryzak Apr 15, 2024
645191e
fix test
sbryzak Apr 15, 2024
e5f0ed6
Merge remote-tracking branch 'origin/master' into SANDBOX-554
sbryzak Apr 17, 2024
5334e28
fixed broken test, improve coverage
sbryzak Apr 18, 2024
3496763
Merge remote-tracking branch 'origin/master' into SANDBOX-554
sbryzak Apr 18, 2024
9fcee7c
set scheduled deactivation time to nil when user in domain exclusion …
sbryzak Apr 20, 2024
74f9329
Merge remote-tracking branch 'origin/master' into SANDBOX-554
sbryzak Apr 21, 2024
bdf613b
Merge remote-tracking branch 'origin/master' into SANDBOX-554
sbryzak Apr 24, 2024
85b95f1
oops
sbryzak Apr 24, 2024
8333dea
set temporary deactivation time
sbryzak Apr 29, 2024
67ec7fd
Merge remote-tracking branch 'origin/master' into SANDBOX-554
sbryzak Apr 29, 2024
67082ca
improve coverage
sbryzak May 1, 2024
e729c5a
Merge remote-tracking branch 'origin/master' into SANDBOX-554
sbryzak May 1, 2024
193ac5b
even more coverage
sbryzak May 1, 2024
cdd1bcd
fixed lint
sbryzak May 1, 2024
cde7536
coverage
sbryzak May 1, 2024
c7df220
fixed linter
sbryzak May 2, 2024
087cdcb
fix potential infinite loop
sbryzak May 2, 2024
2652fa2
Merge remote-tracking branch 'origin/master' into SANDBOX-554
sbryzak May 2, 2024
e2dfbcb
fix
sbryzak May 5, 2024
5fcb467
removed commented code
sbryzak May 5, 2024
d9c3511
Merge remote-tracking branch 'origin/master' into SANDBOX-554
sbryzak May 9, 2024
5879cab
updated comments
sbryzak May 9, 2024
5a14763
removed predicate, set scheduled deactivation time to nil when in dea…
sbryzak May 10, 2024
a8bdb69
review comments
sbryzak May 10, 2024
10bbfb6
Merge remote-tracking branch 'origin/master' into SANDBOX-554
sbryzak May 12, 2024
473a24a
slightly improve logic
sbryzak May 14, 2024
3fcd207
Merge remote-tracking branch 'origin/master' into SANDBOX-554
sbryzak May 14, 2024
8486dc6
review comment
sbryzak May 14, 2024
9f420e9
Merge remote-tracking branch 'origin/master' into SANDBOX-554
sbryzak May 14, 2024
4f742c6
Merge remote-tracking branch 'origin/master' into SANDBOX-554
sbryzak May 14, 2024
1cbcfa9
coverage
sbryzak May 15, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 12 additions & 8 deletions controllers/deactivation/deactivation_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ func (r *Reconciler) SetupWithManager(mgr manager.Manager) error {
Named("deactivation").
For(&toolchainv1alpha1.MasterUserRecord{}, builder.WithPredicates(CreateAndUpdateOnlyPredicate{})).
Watches(&source.Kind{Type: &toolchainv1alpha1.UserSignup{}},
handler.EnqueueRequestsFromMapFunc(MapUserSignupToMasterUserRecord())).
handler.EnqueueRequestsFromMapFunc(MapUserSignupToMasterUserRecord()),
builder.WithPredicates(GenerationOrConditionsChangedPredicate{})).
Complete(r)
}

Expand Down Expand Up @@ -227,14 +228,17 @@ func (r *Reconciler) Reconcile(ctx context.Context, request ctrl.Request) (ctrl.
deactivatingCondition.Reason != toolchainv1alpha1.UserSignupDeactivatingNotificationCRCreatedReason {
// If the UserSignup has been marked as deactivating, however the deactivating notification hasn't been
// created yet, then set the deactivation timestamp to a temporary value, calculated as being the current
// timestamp plus the number of pre-deactivation days configured in the settings

// timestamp plus the number of pre-deactivation days configured in the settings, BUT only if it is currently nil
// OR the calculated value is more than 24 hours later than the current value
deactivationDueTime := time.Now().Add(time.Duration(deactivatingNotificationDays) * 24 * time.Hour)
ts := v1.NewTime(deactivationDueTime)
usersignup.Status.ScheduledDeactivationTimestamp = &ts
if err := r.Client.Status().Update(ctx, usersignup); err != nil {
logger.Error(err, "failed to update usersignup status")
return reconcile.Result{}, err
if usersignup.Status.ScheduledDeactivationTimestamp == nil ||
usersignup.Status.ScheduledDeactivationTimestamp.Time.Before(deactivationDueTime.Add(-24*time.Hour)) {
sbryzak marked this conversation as resolved.
Show resolved Hide resolved
ts := v1.NewTime(deactivationDueTime)
usersignup.Status.ScheduledDeactivationTimestamp = &ts
if err := r.Client.Status().Update(ctx, usersignup); err != nil {
logger.Error(err, "failed to update usersignup status")
return reconcile.Result{}, err
}
}

return reconcile.Result{}, nil
Expand Down
61 changes: 60 additions & 1 deletion controllers/deactivation/predicate.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
package deactivation

import "sigs.k8s.io/controller-runtime/pkg/event"
import (
toolchainv1alpha1 "github.com/codeready-toolchain/api/api/v1alpha1"
"sigs.k8s.io/controller-runtime/pkg/event"
"sigs.k8s.io/controller-runtime/pkg/predicate"
)

// CreateAndUpdateOnlyPredicate will filter out all events except Create and Update
type CreateAndUpdateOnlyPredicate struct {
Expand All @@ -25,3 +29,58 @@ func (CreateAndUpdateOnlyPredicate) Delete(_ event.DeleteEvent) bool {
func (CreateAndUpdateOnlyPredicate) Generic(_ event.GenericEvent) bool {
return false
}

type GenerationOrConditionsChangedPredicate struct {
predicate.GenerationChangedPredicate
}
alexeykazakov marked this conversation as resolved.
Show resolved Hide resolved

// Update implements default UpdateEvent filter for validating no generation change
sbryzak marked this conversation as resolved.
Show resolved Hide resolved
func (GenerationOrConditionsChangedPredicate) Update(e event.UpdateEvent) bool {
if e.ObjectOld == nil || e.ObjectNew == nil {
return false
}

if e.ObjectNew.GetGeneration() != e.ObjectOld.GetGeneration() {
return true
}

switch objNew := e.ObjectNew.(type) {
case *toolchainv1alpha1.UserSignup:
switch objOld := e.ObjectOld.(type) {
case *toolchainv1alpha1.UserSignup:
if !ConditionsMatch(objOld.Status.Conditions, objNew.Status.Conditions) {
return true
}
}
}

return false
}

func ConditionsMatch(first, second []toolchainv1alpha1.Condition) bool {
sbryzak marked this conversation as resolved.
Show resolved Hide resolved
if len(first) != len(second) {
return false
}
for _, c := range first {
if !ContainsCondition(second, c) {
return false
}
}
for _, c := range second {
if !ContainsCondition(first, c) {
return false
}
}
return true
}

// ContainsCondition returns true if the specified list of conditions contains the specified condition.
sbryzak marked this conversation as resolved.
Show resolved Hide resolved
// LastTransitionTime is ignored.
func ContainsCondition(conditions []toolchainv1alpha1.Condition, contains toolchainv1alpha1.Condition) bool {
for _, c := range conditions {
if c.Type == contains.Type {
return contains.Status == c.Status && contains.Reason == c.Reason && contains.Message == c.Message
}
}
return false
}
Loading