Skip to content

Commit

Permalink
Addressing comments from Irina
Browse files Browse the repository at this point in the history
  • Loading branch information
edcdavid committed Sep 27, 2024
1 parent e5510ad commit 4655534
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 15 deletions.
19 changes: 10 additions & 9 deletions internal/configfsm/fsm.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ func InitFSM(state string) (fsm *stateless.StateMachine, err error) {
fsm.Configure(Missing).
OnEntry(func(_ context.Context, args ...any) error {
fsmHelper := args[0].(FsmHelper)
fmt.Println("Entering Missing")
fmt.Println("Entering " + Missing)
fsmHelper.ResetNonCompliantAt()
if fsmHelper.ArePoliciesMatched() {
return fsm.Fire(MissingToClusterNotReady, fsmHelper)
Expand All @@ -124,7 +124,7 @@ func InitFSM(state string) (fsm *stateless.StateMachine, err error) {
fsm.Configure(ClusterNotReady).
OnEntry(func(_ context.Context, args ...any) error {
fsmHelper := args[0].(FsmHelper)
fmt.Println("Entering ClusterNotReady")
fmt.Println("Entering " + ClusterNotReady)
if !fsmHelper.ArePoliciesMatched() {
return fsm.Fire(ClusterNotReadyToMissing, fsmHelper)
}
Expand All @@ -140,7 +140,7 @@ func InitFSM(state string) (fsm *stateless.StateMachine, err error) {
fsm.Configure(InProgress).
OnEntry(func(_ context.Context, args ...any) error {
fsmHelper := args[0].(FsmHelper)
fmt.Println("Entering InProgress")
fmt.Println("Entering " + InProgress)
if fsmHelper.IsNonCompliantAtZero() ||
fsmHelper.IsAllPoliciesCompliant() ||
(!fsmHelper.IsNonCompliantPolicyInEnforce() && !fsmHelper.IsAllPoliciesCompliant()) {
Expand All @@ -155,7 +155,7 @@ func InitFSM(state string) (fsm *stateless.StateMachine, err error) {
if !fsmHelper.IsClusterReady() {
return fsm.Fire(InProgressToClusterNotReady, fsmHelper)
}
if !fsmHelper.IsNonCompliantPolicyInEnforce() && !fsmHelper.IsAllPoliciesCompliant() {
if !fsmHelper.IsNonCompliantPolicyInEnforce() && !fsmHelper.IsAllPoliciesCompliant() { // checks OutOfDate condition
return fsm.Fire(InProgressToOutOfDate, fsmHelper)
}
if fsmHelper.IsAllPoliciesCompliant() {
Expand All @@ -171,11 +171,12 @@ func InitFSM(state string) (fsm *stateless.StateMachine, err error) {
fsm.Configure(OutOfDate).
OnEntry(func(_ context.Context, args ...any) error {
fsmHelper := args[0].(FsmHelper)
fmt.Println("Entering InProgress")
fmt.Println("Entering " + OutOfDate)
fsmHelper.ResetNonCompliantAt()
if !fsmHelper.ArePoliciesMatched() ||
!fsmHelper.IsClusterReady() ||
fsmHelper.IsNonCompliantPolicyInEnforce() {
fsmHelper.IsNonCompliantPolicyInEnforce() ||
fsmHelper.IsAllPoliciesCompliant() {
return fsm.Fire(OutOfDateToInProgress, fsmHelper)
}
return nil
Expand All @@ -185,7 +186,7 @@ func InitFSM(state string) (fsm *stateless.StateMachine, err error) {
fsm.Configure(Completed).
OnEntry(func(_ context.Context, args ...any) error {
fsmHelper := args[0].(FsmHelper)
fmt.Println("Entering Completed")
fmt.Println("Entering " + Completed)
fsmHelper.ResetNonCompliantAt()

if !fsmHelper.ArePoliciesMatched() ||
Expand All @@ -201,10 +202,10 @@ func InitFSM(state string) (fsm *stateless.StateMachine, err error) {
fsm.Configure(TimedOut).
OnEntry(func(_ context.Context, args ...any) error {
fsmHelper := args[0].(FsmHelper)
fmt.Println("Entering TimedOut")
fmt.Println("Entering " + TimedOut)

if !fsmHelper.ArePoliciesMatched() ||
(!fsmHelper.IsNonCompliantPolicyInEnforce() &&
(!fsmHelper.IsNonCompliantPolicyInEnforce() && // checks OutOfDate condition
!fsmHelper.IsAllPoliciesCompliant()) ||
fsmHelper.IsAllPoliciesCompliant() {
return fsm.Fire(TimedOutToInProgress, fsmHelper)
Expand Down
13 changes: 13 additions & 0 deletions internal/configfsm/fsm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -428,6 +428,19 @@ func TestFSMTimedoutOutOfDate(t *testing.T) {
state, err = aFSM.State(context.Background())
assert.Equal(t, nil, err, "err should be nil")
assert.Equal(t, OutOfDate, state, "State should be OutOfDate")

aTestHelper.ClusterReady = true
aTestHelper.NonCompliantPolicyInEnforce = false
aTestHelper.AllPoliciesCompliant = true

_, err = RunFSM(context.Background(), aFSM, &aTestHelper)
if err != nil {
fmt.Println(err)
}

state, err = aFSM.State(context.Background())
assert.Equal(t, nil, err, "err should be nil")
assert.Equal(t, Completed, state, "State should be Completed")
}

func TestDisplayGraph(t *testing.T) {
Expand Down
25 changes: 20 additions & 5 deletions internal/controllers/clusterrequest_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -953,7 +953,9 @@ var _ = Describe("ClusterRequestReconcile", func() {
managedCluster = &clusterv1.ManagedCluster{
ObjectMeta: metav1.ObjectMeta{Name: crName},
Status: clusterv1.ManagedClusterStatus{
Conditions: []metav1.Condition{{Type: clusterv1.ManagedClusterConditionAvailable, Status: metav1.ConditionFalse}},
Conditions: []metav1.Condition{{Type: clusterv1.ManagedClusterConditionAvailable, Status: metav1.ConditionFalse},
{Type: clusterv1.ManagedClusterConditionJoined, Status: metav1.ConditionFalse},
{Type: clusterv1.ManagedClusterConditionHubAccepted, Status: metav1.ConditionFalse}},
},
}
Expect(c.Create(ctx, managedCluster)).To(Succeed())
Expand Down Expand Up @@ -1025,9 +1027,16 @@ var _ = Describe("ClusterRequestReconcile", func() {
clusterInstance.Status.Conditions = append(clusterInstance.Status.Conditions, crProvisionedCond)
Expect(c.Status().Update(ctx, clusterInstance)).To(Succeed())
// Patch ManagedCluster to ready
readyCond := meta.FindStatusCondition(
availableCond := meta.FindStatusCondition(
managedCluster.Status.Conditions, clusterv1.ManagedClusterConditionAvailable)
readyCond.Status = metav1.ConditionTrue
joinedCond := meta.FindStatusCondition(
managedCluster.Status.Conditions, clusterv1.ManagedClusterConditionJoined)
acceptedCond := meta.FindStatusCondition(
managedCluster.Status.Conditions, clusterv1.ManagedClusterConditionHubAccepted)
availableCond.Status = metav1.ConditionTrue
joinedCond.Status = metav1.ConditionTrue
acceptedCond.Status = metav1.ConditionTrue

Expect(c.Status().Update(ctx, managedCluster)).To(Succeed())
// Patch enforce policy to Compliant
policy.Status.ComplianceState = "Compliant"
Expand Down Expand Up @@ -1121,9 +1130,15 @@ var _ = Describe("ClusterRequestReconcile", func() {
currentCI.Status.Conditions = append(clusterInstance.Status.Conditions, crProvisionedCond)
Expect(c.Status().Update(ctx, currentCI)).To(Succeed())
// Patch ManagedCluster to ready
readyCond := meta.FindStatusCondition(
availableCond := meta.FindStatusCondition(
managedCluster.Status.Conditions, clusterv1.ManagedClusterConditionAvailable)
readyCond.Status = metav1.ConditionTrue
joinedCond := meta.FindStatusCondition(
managedCluster.Status.Conditions, clusterv1.ManagedClusterConditionJoined)
acceptedCond := meta.FindStatusCondition(
managedCluster.Status.Conditions, clusterv1.ManagedClusterConditionHubAccepted)
availableCond.Status = metav1.ConditionTrue
joinedCond.Status = metav1.ConditionTrue
acceptedCond.Status = metav1.ConditionTrue
Expect(c.Status().Update(ctx, managedCluster)).To(Succeed())
// Patch enforce policy to Compliant
policy.Status.ComplianceState = "Compliant"
Expand Down
18 changes: 17 additions & 1 deletion internal/controllers/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -1034,6 +1034,8 @@ func ClusterIsReadyForPolicyConfig(
}

available := false
hubAccepted := false
joined := false

availableCondition := meta.FindStatusCondition(
managedCluster.Status.Conditions,
Expand All @@ -1042,7 +1044,21 @@ func ClusterIsReadyForPolicyConfig(
available = true
}

return available, nil
acceptedCondition := meta.FindStatusCondition(
managedCluster.Status.Conditions,
clusterv1.ManagedClusterConditionHubAccepted)
if acceptedCondition != nil && acceptedCondition.Status == metav1.ConditionTrue {
hubAccepted = true
}

joinedCondition := meta.FindStatusCondition(
managedCluster.Status.Conditions,
clusterv1.ManagedClusterConditionJoined)
if joinedCondition != nil && joinedCondition.Status == metav1.ConditionTrue {
joined = true
}

return available && hubAccepted && joined, nil
}

// TimeoutExceeded returns true if it's been more time than the timeout configuration.
Expand Down

0 comments on commit 4655534

Please sign in to comment.