Skip to content

Commit

Permalink
🐛 handle the unknown status of the previously accepted cluster
Browse files Browse the repository at this point in the history
Signed-off-by: Yang Le <yangle@redhat.com>
  • Loading branch information
elgnay committed Oct 18, 2024
1 parent 6d68522 commit e516eff
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 49 deletions.
21 changes: 10 additions & 11 deletions pkg/registration/helpers/testing/testinghelpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
coordv1 "k8s.io/api/coordination/v1"
corev1 "k8s.io/api/core/v1"
rbacv1 "k8s.io/api/rbac/v1"
"k8s.io/apimachinery/pkg/api/meta"
"k8s.io/apimachinery/pkg/api/resource"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
kubeversion "k8s.io/client-go/pkg/version"
Expand Down Expand Up @@ -128,9 +129,17 @@ func NewManagedClusterWithStatus(capacity, allocatable clusterv1.ResourceList) *
return managedCluster
}

func NewDeniedManagedCluster() *clusterv1.ManagedCluster {
func NewDeniedManagedCluster(acceptedConditionStatus string) *clusterv1.ManagedCluster {
managedCluster := NewAcceptedManagedCluster()
managedCluster.Spec.HubAcceptsClient = false

meta.SetStatusCondition(&managedCluster.Status.Conditions, NewManagedClusterCondition(
clusterv1.ManagedClusterConditionHubAccepted,
acceptedConditionStatus,
"",
"",
nil,
))
return managedCluster
}

Expand Down Expand Up @@ -182,16 +191,6 @@ func NewAddOnLease(namespace, name string, renewTime time.Time) *coordv1.Lease {
}
}

func NewNamespace(name string, terminated bool) *corev1.Namespace {
namespace := &corev1.Namespace{}
namespace.Name = name
if terminated {
now := metav1.Now()
namespace.DeletionTimestamp = &now
}
return namespace
}

func NewManifestWork(namespace, name string, finalizers []string,
labels, annotations map[string]string, deletionTimestamp *metav1.Time) *workapiv1.ManifestWork {
work := &workapiv1.ManifestWork{
Expand Down
8 changes: 5 additions & 3 deletions pkg/registration/hub/lease/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,8 @@ func NewClusterLeaseController(
ToController("ManagedClusterLeaseController", recorder)
}

// sync checks the lease of each accepted cluster on hub to determine whether a managed cluster is available.
// sync checks the lease of each cluster on hub, which is accepted or previously accepted, to determine whether
// the managed cluster is available.
func (c *leaseController) sync(ctx context.Context, syncCtx factory.SyncContext) error {
clusterName := syncCtx.QueueKey()

Expand All @@ -84,8 +85,9 @@ func (c *leaseController) sync(ctx context.Context, syncCtx factory.SyncContext)
return err
}

if !meta.IsStatusConditionTrue(cluster.Status.Conditions, clusterv1.ManagedClusterConditionHubAccepted) {
// cluster is not accepted, skip it.
if cond := meta.FindStatusCondition(cluster.Status.Conditions,
clusterv1.ManagedClusterConditionHubAccepted); cond == nil {
// cluster was never accepted, skip it.
return nil
}

Expand Down
87 changes: 53 additions & 34 deletions pkg/registration/hub/lease/controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ import (
clusterscheme "open-cluster-management.io/api/client/cluster/clientset/versioned/scheme"
clusterinformers "open-cluster-management.io/api/client/cluster/informers/externalversions"
clusterv1 "open-cluster-management.io/api/cluster/v1"
v1 "open-cluster-management.io/api/cluster/v1"
"open-cluster-management.io/sdk-go/pkg/patcher"

"open-cluster-management.io/ocm/pkg/common/helpers"
Expand All @@ -28,6 +27,39 @@ import (
var now = time.Now()

func TestSync(t *testing.T) {
leaseStopUpdatingValidateActions := func(t *testing.T, hubKubeActions, clusterActions []clienttesting.Action) {
expected := metav1.Condition{
Type: clusterv1.ManagedClusterConditionAvailable,
Status: metav1.ConditionUnknown,
Reason: "ManagedClusterLeaseUpdateStopped",
Message: "Registration agent stopped updating its lease.",
}
testingcommon.AssertActions(t, clusterActions, "patch")
patch := clusterActions[0].(clienttesting.PatchAction).GetPatch()
managedCluster := &clusterv1.ManagedCluster{}
err := json.Unmarshal(patch, managedCluster)
if err != nil {
t.Fatal(err)
}
testingcommon.AssertCondition(t, managedCluster.Status.Conditions, expected)

if len(hubKubeActions) != 1 {
t.Errorf("Expected 1 event created in the sync loop, actual %d",
len(hubKubeActions))
}
actionEvent := hubKubeActions[0]
if actionEvent.GetResource().Resource != "events" {
t.Errorf("Expected event created, actual %s", actionEvent.GetResource())
}
if actionEvent.GetNamespace() != testinghelpers.TestManagedClusterName {
t.Errorf("Expected event created in namespace %s, actual %s",
testinghelpers.TestManagedClusterName, actionEvent.GetNamespace())
}
if actionEvent.GetVerb() != "create" {
t.Errorf("Expected event created, actual %s", actionEvent.GetVerb())
}
}

cases := []struct {
name string
clusters []runtime.Object
Expand All @@ -43,6 +75,15 @@ func TestSync(t *testing.T) {
testingcommon.AssertNoActions(t, clusterActions)
},
},
{
name: "sync previously accepted managed cluster",
clusters: []runtime.Object{testinghelpers.NewDeniedManagedCluster("False")},
clusterLeases: []runtime.Object{},
validateActions: func(t *testing.T, leaseActions, clusterActions []clienttesting.Action) {
testingcommon.AssertActions(t, leaseActions, "create")
testingcommon.AssertNoActions(t, clusterActions)
},
},
{
name: "there is no lease for a managed cluster",
clusters: []runtime.Object{testinghelpers.NewAcceptedManagedCluster()},
Expand All @@ -53,44 +94,22 @@ func TestSync(t *testing.T) {
},
},
{
name: "managed cluster stop update lease",
name: "accepted managed cluster stop update lease",
clusters: []runtime.Object{testinghelpers.NewAvailableManagedCluster()},
clusterLeases: []runtime.Object{
testinghelpers.NewManagedClusterLease("managed-cluster-lease", now.Add(-5*time.Minute)),
testinghelpers.NewManagedClusterLease(fmt.Sprintf("cluster-lease-%s", testinghelpers.TestManagedClusterName), now.Add(-5*time.Minute)),
},
validateActions: func(t *testing.T, hubKubeActions, clusterActions []clienttesting.Action) {
expected := metav1.Condition{
Type: clusterv1.ManagedClusterConditionAvailable,
Status: metav1.ConditionUnknown,
Reason: "ManagedClusterLeaseUpdateStopped",
Message: "Registration agent stopped updating its lease.",
}
testingcommon.AssertActions(t, clusterActions, "patch")
patch := clusterActions[0].(clienttesting.PatchAction).GetPatch()
managedCluster := &v1.ManagedCluster{}
err := json.Unmarshal(patch, managedCluster)
if err != nil {
t.Fatal(err)
}
testingcommon.AssertCondition(t, managedCluster.Status.Conditions, expected)

if len(hubKubeActions) != 1 {
t.Errorf("Expected 1 event created in the sync loop, actual %d",
len(hubKubeActions))
}
actionEvent := hubKubeActions[0]
if actionEvent.GetResource().Resource != "events" {
t.Errorf("Expected event created, actual %s", actionEvent.GetResource())
}
if actionEvent.GetNamespace() != testinghelpers.TestManagedClusterName {
t.Errorf("Expected event created in namespace %s, actual %s",
testinghelpers.TestManagedClusterName, actionEvent.GetNamespace())
}
if actionEvent.GetVerb() != "create" {
t.Errorf("Expected event created, actual %s", actionEvent.GetVerb())
}
validateActions: leaseStopUpdatingValidateActions,
},
{
name: "previously accepted managed cluster stop update lease",
clusters: []runtime.Object{testinghelpers.NewDeniedManagedCluster("False")},
clusterLeases: []runtime.Object{
testinghelpers.NewManagedClusterLease("managed-cluster-lease", now.Add(-5*time.Minute)),
testinghelpers.NewManagedClusterLease(fmt.Sprintf("cluster-lease-%s", testinghelpers.TestManagedClusterName), now.Add(-5*time.Minute)),
},
validateActions: leaseStopUpdatingValidateActions,
},
{
name: "managed cluster is available",
Expand All @@ -112,7 +131,7 @@ func TestSync(t *testing.T) {
}
testingcommon.AssertActions(t, clusterActions, "patch")
patch := clusterActions[0].(clienttesting.PatchAction).GetPatch()
managedCluster := &v1.ManagedCluster{}
managedCluster := &clusterv1.ManagedCluster{}
err := json.Unmarshal(patch, managedCluster)
if err != nil {
t.Fatal(err)
Expand Down
2 changes: 1 addition & 1 deletion pkg/registration/hub/managedcluster/controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ func TestSyncManagedCluster(t *testing.T) {
},
{
name: "deny an accepted spoke cluster",
startingObjects: []runtime.Object{testinghelpers.NewDeniedManagedCluster()},
startingObjects: []runtime.Object{testinghelpers.NewDeniedManagedCluster("True")},
validateActions: func(t *testing.T, actions []clienttesting.Action) {
expectedCondition := metav1.Condition{
Type: v1.ManagedClusterConditionHubAccepted,
Expand Down

0 comments on commit e516eff

Please sign in to comment.