Skip to content

Commit

Permalink
Merge pull request stolostron#343 from qiujian16/fix-info-bug
Browse files Browse the repository at this point in the history
Fix nodelist bug
  • Loading branch information
openshift-merge-robot authored May 8, 2021
2 parents 3614814 + a5d6beb commit 0e326b8
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 28 deletions.
43 changes: 15 additions & 28 deletions pkg/klusterlet/clusterinfo/clusterinfo_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ func (r *ClusterInfoReconciler) Reconcile(ctx context.Context, req ctrl.Request)
}

// Update cluster info status here.
newStatus := clusterv1beta1.ClusterInfoStatus{}
newStatus := clusterInfo.DeepCopy().Status
var errs []error

// Config Agent endpoint
Expand Down Expand Up @@ -125,38 +125,25 @@ func (r *ClusterInfoReconciler) Reconcile(ctx context.Context, req ctrl.Request)
Message: "Managed cluster info is synced",
}
if len(errs) > 0 {
newSyncedCondition.Status = metav1.ConditionFalse
newSyncedCondition.Reason = clusterv1beta1.ReasonManagedClusterInfoSyncedFailed
applyErrors := errors.NewAggregate(errs)
newSyncedCondition.Message = applyErrors.Error()
}

needUpdate := false
oldStatus := clusterInfo.Status.DeepCopy()
oldSyncedCondition := meta.FindStatusCondition(oldStatus.Conditions, clusterv1beta1.ManagedClusterInfoSynced)
if oldSyncedCondition != nil {
oldSyncedCondition.LastTransitionTime = metav1.Time{}
if !equality.Semantic.DeepEqual(newSyncedCondition, *oldSyncedCondition) {
needUpdate = true
newSyncedCondition = metav1.Condition{
Type: clusterv1beta1.ManagedClusterInfoSynced,
Status: metav1.ConditionFalse,
Reason: clusterv1beta1.ReasonManagedClusterInfoSyncedFailed,
Message: errors.NewAggregate(errs).Error(),
}
} else {
needUpdate = true
}

oldStatus.Conditions = []metav1.Condition{}
if !equality.Semantic.DeepEqual(newStatus, *oldStatus) {
needUpdate = true
meta.SetStatusCondition(&newStatus.Conditions, newSyncedCondition)

if equality.Semantic.DeepEqual(newStatus, clusterInfo.Status) {
return ctrl.Result{}, nil
}

if needUpdate {
newStatus.Conditions = clusterInfo.Status.Conditions
meta.SetStatusCondition(&newStatus.Conditions, newSyncedCondition)
clusterInfo.Status = newStatus
err = r.Client.Status().Update(ctx, clusterInfo)
if err != nil {
log.Error(err, "Failed to update status")
return ctrl.Result{}, err
}
clusterInfo.Status = newStatus
err = r.Client.Status().Update(ctx, clusterInfo)
if err != nil {
log.Error(err, "Failed to update status")
return ctrl.Result{}, err
}

r.RefreshAgentServer(clusterInfo)
Expand Down
11 changes: 11 additions & 0 deletions test/e2e/clusterinfos_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,17 @@ var _ = ginkgo.Describe("Testing ManagedClusterInfo", func() {
}, eventuallyTimeout, eventuallyInterval).Should(gomega.BeTrue())
})

ginkgo.It("should have node list reported successfully in cluster", func() {
gomega.Eventually(func() error {
managedClusterInfo, err := util.GetResource(dynamicClient, clusterInfoGVR, managedClusterName, managedClusterName)
if err != nil {
return err
}
// check the ManagedClusterInfo status
return util.CheckNodeList(managedClusterInfo)
}, eventuallyTimeout, eventuallyInterval).ShouldNot(gomega.HaveOccurred())
})

ginkgo.It("should have a valid condition", func() {
gomega.Eventually(func() bool {
managedClusterInfo, err := util.GetResource(dynamicClient, clusterInfoGVR, managedClusterName, managedClusterName)
Expand Down
13 changes: 13 additions & 0 deletions test/e2e/util/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -520,6 +520,19 @@ func GetConditionTypeFromStatus(obj *unstructured.Unstructured, typeName string)
return false
}

func CheckNodeList(obj *unstructured.Unstructured) error {
nodeList, found, err := unstructured.NestedSlice(obj.Object, "status", "nodeList")
if err != nil || !found {
return fmt.Errorf("failed to get nodeList. found:%v, err:%v", found, err)
}

if len(nodeList) == 0 {
return fmt.Errorf("expect items in node list")
}

return nil
}

func CheckDistributionInfo(obj *unstructured.Unstructured) error {
distributionInfo, found, err := unstructured.NestedMap(obj.Object, "status", "distributionInfo")
if err != nil || !found {
Expand Down

0 comments on commit 0e326b8

Please sign in to comment.