Skip to content

Commit

Permalink
fix error of when cluster status condition update
Browse files Browse the repository at this point in the history
Signed-off-by: changzhen <changzhen5@huawei.com>
  • Loading branch information
XiShanYongYe-Chang committed Aug 19, 2024
1 parent 5082426 commit 729f640
Show file tree
Hide file tree
Showing 2 changed files with 96 additions and 2 deletions.
78 changes: 76 additions & 2 deletions pkg/controllers/status/cluster_status_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,15 @@ func (c *ClusterStatusController) Reconcile(ctx context.Context, req controllerr
return controllerruntime.Result{Requeue: true}, nil
}

<<<<<<< HEAD

Check failure on line 156 in pkg/controllers/status/cluster_status_controller.go

View workflow job for this annotation

GitHub Actions / init (v1.27.3)

syntax error: unexpected <<, expected }

Check failure on line 156 in pkg/controllers/status/cluster_status_controller.go

View workflow job for this annotation

GitHub Actions / init (v1.29.0)

syntax error: unexpected <<, expected }

Check failure on line 156 in pkg/controllers/status/cluster_status_controller.go

View workflow job for this annotation

GitHub Actions / init (v1.28.0)

syntax error: unexpected <<, expected }

Check failure on line 156 in pkg/controllers/status/cluster_status_controller.go

View workflow job for this annotation

GitHub Actions / lint

syntax error: unexpected <<, expected }

Check failure on line 156 in pkg/controllers/status/cluster_status_controller.go

View workflow job for this annotation

GitHub Actions / compile

syntax error: unexpected <<, expected }
return c.syncClusterStatus(cluster)
=======
err := c.syncClusterStatus(ctx, cluster)
if err != nil {
return controllerruntime.Result{}, err
}
return controllerruntime.Result{RequeueAfter: c.ClusterStatusUpdateFrequency.Duration}, nil
>>>>>>> fix error of when cluster status condition update
}

// SetupWithManager creates a controller and register to controller manager.
Expand All @@ -169,7 +177,11 @@ func (c *ClusterStatusController) SetupWithManager(mgr controllerruntime.Manager
}).Complete(c)
}

<<<<<<< HEAD

Check failure on line 180 in pkg/controllers/status/cluster_status_controller.go

View workflow job for this annotation

GitHub Actions / init (v1.27.3)

syntax error: non-declaration statement outside function body

Check failure on line 180 in pkg/controllers/status/cluster_status_controller.go

View workflow job for this annotation

GitHub Actions / init (v1.29.0)

syntax error: non-declaration statement outside function body

Check failure on line 180 in pkg/controllers/status/cluster_status_controller.go

View workflow job for this annotation

GitHub Actions / init (v1.28.0)

syntax error: non-declaration statement outside function body

Check failure on line 180 in pkg/controllers/status/cluster_status_controller.go

View workflow job for this annotation

GitHub Actions / lint

syntax error: non-declaration statement outside function body

Check failure on line 180 in pkg/controllers/status/cluster_status_controller.go

View workflow job for this annotation

GitHub Actions / compile

syntax error: non-declaration statement outside function body
func (c *ClusterStatusController) syncClusterStatus(cluster *clusterv1alpha1.Cluster) (controllerruntime.Result, error) {
=======

Check failure on line 182 in pkg/controllers/status/cluster_status_controller.go

View workflow job for this annotation

GitHub Actions / init (v1.27.3)

syntax error: unexpected ==, expected }

Check failure on line 182 in pkg/controllers/status/cluster_status_controller.go

View workflow job for this annotation

GitHub Actions / init (v1.29.0)

syntax error: unexpected ==, expected }

Check failure on line 182 in pkg/controllers/status/cluster_status_controller.go

View workflow job for this annotation

GitHub Actions / init (v1.28.0)

syntax error: unexpected ==, expected }

Check failure on line 182 in pkg/controllers/status/cluster_status_controller.go

View workflow job for this annotation

GitHub Actions / lint

syntax error: unexpected ==, expected }

Check failure on line 182 in pkg/controllers/status/cluster_status_controller.go

View workflow job for this annotation

GitHub Actions / compile

syntax error: unexpected ==, expected }
func (c *ClusterStatusController) syncClusterStatus(ctx context.Context, cluster *clusterv1alpha1.Cluster) error {
>>>>>>> fix error of when cluster status condition update

Check failure on line 184 in pkg/controllers/status/cluster_status_controller.go

View workflow job for this annotation

GitHub Actions / init (v1.27.3)

syntax error: unexpected >>, expected }

Check failure on line 184 in pkg/controllers/status/cluster_status_controller.go

View workflow job for this annotation

GitHub Actions / init (v1.29.0)

syntax error: unexpected >>, expected }

Check failure on line 184 in pkg/controllers/status/cluster_status_controller.go

View workflow job for this annotation

GitHub Actions / init (v1.28.0)

syntax error: unexpected >>, expected }

Check failure on line 184 in pkg/controllers/status/cluster_status_controller.go

View workflow job for this annotation

GitHub Actions / lint

syntax error: unexpected >>, expected }

Check failure on line 184 in pkg/controllers/status/cluster_status_controller.go

View workflow job for this annotation

GitHub Actions / compile

syntax error: unexpected >>, expected }
start := time.Now()
defer func() {

Check failure on line 186 in pkg/controllers/status/cluster_status_controller.go

View workflow job for this annotation

GitHub Actions / init (v1.27.3)

method has no receiver

Check failure on line 186 in pkg/controllers/status/cluster_status_controller.go

View workflow job for this annotation

GitHub Actions / init (v1.27.3)

syntax error: unexpected {, expected name

Check failure on line 186 in pkg/controllers/status/cluster_status_controller.go

View workflow job for this annotation

GitHub Actions / init (v1.29.0)

method has no receiver

Check failure on line 186 in pkg/controllers/status/cluster_status_controller.go

View workflow job for this annotation

GitHub Actions / init (v1.29.0)

syntax error: unexpected {, expected name

Check failure on line 186 in pkg/controllers/status/cluster_status_controller.go

View workflow job for this annotation

GitHub Actions / init (v1.28.0)

method has no receiver

Check failure on line 186 in pkg/controllers/status/cluster_status_controller.go

View workflow job for this annotation

GitHub Actions / init (v1.28.0)

syntax error: unexpected {, expected name

Check failure on line 186 in pkg/controllers/status/cluster_status_controller.go

View workflow job for this annotation

GitHub Actions / lint

method has no receiver

Check failure on line 186 in pkg/controllers/status/cluster_status_controller.go

View workflow job for this annotation

GitHub Actions / lint

syntax error: unexpected {, expected name

Check failure on line 186 in pkg/controllers/status/cluster_status_controller.go

View workflow job for this annotation

GitHub Actions / compile

method has no receiver

Check failure on line 186 in pkg/controllers/status/cluster_status_controller.go

View workflow job for this annotation

GitHub Actions / compile

syntax error: unexpected {, expected name
metrics.RecordClusterStatus(cluster)
Expand All @@ -182,7 +194,11 @@ func (c *ClusterStatusController) syncClusterStatus(cluster *clusterv1alpha1.Clu
clusterClient, err := c.ClusterClientSetFunc(cluster.Name, c.Client, c.ClusterClientOption)
if err != nil {
klog.Errorf("Failed to create a ClusterClient for the given member cluster: %v, err is : %v", cluster.Name, err)
<<<<<<< HEAD
return c.setStatusCollectionFailedCondition(cluster, currentClusterStatus, fmt.Sprintf("failed to create a ClusterClient: %v", err))
=======
return setStatusCollectionFailedCondition(ctx, c.Client, cluster, fmt.Sprintf("failed to create a ClusterClient: %v", err))
>>>>>>> fix error of when cluster status condition update
}

online, healthy := getClusterHealthStatus(clusterClient)
Expand All @@ -193,8 +209,12 @@ func (c *ClusterStatusController) syncClusterStatus(cluster *clusterv1alpha1.Clu
if !online && readyCondition.Status != metav1.ConditionTrue {
klog.V(2).Infof("Cluster(%s) still offline after %s, ensuring offline is set.",
cluster.Name, c.ClusterFailureThreshold.Duration)
<<<<<<< HEAD
meta.SetStatusCondition(&currentClusterStatus.Conditions, *readyCondition)
return c.updateStatusIfNeeded(cluster, currentClusterStatus)
=======
return updateStatusCondition(ctx, c.Client, cluster, *readyCondition)
>>>>>>> fix error of when cluster status condition update
}

// skip collecting cluster status if not ready
Expand All @@ -211,15 +231,23 @@ func (c *ClusterStatusController) syncClusterStatus(cluster *clusterv1alpha1.Clu
// can be safely removed from current controller.
c.initializeGenericInformerManagerForCluster(clusterClient)

err := c.setCurrentClusterStatus(clusterClient, cluster, &currentClusterStatus)
err = c.setCurrentClusterStatus(clusterClient, cluster, &currentClusterStatus)
if err != nil {
<<<<<<< HEAD
return controllerruntime.Result{Requeue: true}, err
}
}

meta.SetStatusCondition(&currentClusterStatus.Conditions, *readyCondition)

return c.updateStatusIfNeeded(cluster, currentClusterStatus)
=======
return err
}
}

return c.updateStatusIfNeeded(ctx, cluster, currentClusterStatus, *readyCondition)
>>>>>>> fix error of when cluster status condition update
}

func (c *ClusterStatusController) setCurrentClusterStatus(clusterClient *util.ClusterClient, cluster *clusterv1alpha1.Cluster, currentClusterStatus *clusterv1alpha1.ClusterStatus) error {
Expand Down Expand Up @@ -266,6 +294,7 @@ func (c *ClusterStatusController) setCurrentClusterStatus(clusterClient *util.Cl
return nil
}

<<<<<<< HEAD

Check failure on line 297 in pkg/controllers/status/cluster_status_controller.go

View workflow job for this annotation

GitHub Actions / init (v1.27.3)

syntax error: non-declaration statement outside function body

Check failure on line 297 in pkg/controllers/status/cluster_status_controller.go

View workflow job for this annotation

GitHub Actions / init (v1.29.0)

syntax error: non-declaration statement outside function body

Check failure on line 297 in pkg/controllers/status/cluster_status_controller.go

View workflow job for this annotation

GitHub Actions / init (v1.28.0)

syntax error: non-declaration statement outside function body

Check failure on line 297 in pkg/controllers/status/cluster_status_controller.go

View workflow job for this annotation

GitHub Actions / lint

syntax error: non-declaration statement outside function body

Check failure on line 297 in pkg/controllers/status/cluster_status_controller.go

View workflow job for this annotation

GitHub Actions / compile

syntax error: non-declaration statement outside function body
func (c *ClusterStatusController) setStatusCollectionFailedCondition(cluster *clusterv1alpha1.Cluster, currentClusterStatus clusterv1alpha1.ClusterStatus, message string) (controllerruntime.Result, error) {
readyCondition := util.NewCondition(clusterv1alpha1.ClusterConditionReady, statusCollectionFailed, message, metav1.ConditionFalse)
meta.SetStatusCondition(&currentClusterStatus.Conditions, readyCondition)
Expand All @@ -284,6 +313,29 @@ func (c *ClusterStatusController) updateStatusIfNeeded(cluster *clusterv1alpha1.
cluster.Status.ResourceSummary = currentClusterStatus.ResourceSummary
updateErr := c.Status().Update(context.TODO(), cluster)
if updateErr == nil {
=======

Check failure on line 316 in pkg/controllers/status/cluster_status_controller.go

View workflow job for this annotation

GitHub Actions / init (v1.27.3)

syntax error: unexpected ==, expected }

Check failure on line 316 in pkg/controllers/status/cluster_status_controller.go

View workflow job for this annotation

GitHub Actions / init (v1.29.0)

syntax error: unexpected ==, expected }

Check failure on line 316 in pkg/controllers/status/cluster_status_controller.go

View workflow job for this annotation

GitHub Actions / init (v1.28.0)

syntax error: unexpected ==, expected }

Check failure on line 316 in pkg/controllers/status/cluster_status_controller.go

View workflow job for this annotation

GitHub Actions / lint

syntax error: unexpected ==, expected }

Check failure on line 316 in pkg/controllers/status/cluster_status_controller.go

View workflow job for this annotation

GitHub Actions / compile

syntax error: unexpected ==, expected }
func setStatusCollectionFailedCondition(ctx context.Context, c client.Client, cluster *clusterv1alpha1.Cluster, message string) error {
readyCondition := util.NewCondition(clusterv1alpha1.ClusterConditionReady, statusCollectionFailed, message, metav1.ConditionFalse)
return updateStatusCondition(ctx, c, cluster, readyCondition)
}

Check failure on line 320 in pkg/controllers/status/cluster_status_controller.go

View workflow job for this annotation

GitHub Actions / init (v1.27.3)

syntax error: unexpected newline in argument list; possibly missing comma or )

Check failure on line 320 in pkg/controllers/status/cluster_status_controller.go

View workflow job for this annotation

GitHub Actions / init (v1.29.0)

syntax error: unexpected newline in argument list; possibly missing comma or )

Check failure on line 320 in pkg/controllers/status/cluster_status_controller.go

View workflow job for this annotation

GitHub Actions / init (v1.28.0)

syntax error: unexpected newline in argument list; possibly missing comma or )

Check failure on line 320 in pkg/controllers/status/cluster_status_controller.go

View workflow job for this annotation

GitHub Actions / compile

syntax error: unexpected newline in argument list; possibly missing comma or )

// updateStatusIfNeeded calls updateStatus only if the status of the member cluster is not the same as the old status
func (c *ClusterStatusController) updateStatusIfNeeded(ctx context.Context, cluster *clusterv1alpha1.Cluster, currentClusterStatus clusterv1alpha1.ClusterStatus, conditions ...metav1.Condition) error {
for _, condition := range conditions {
meta.SetStatusCondition(&currentClusterStatus.Conditions, condition)
}
if !equality.Semantic.DeepEqual(cluster.Status, currentClusterStatus) {
klog.V(4).Infof("Start to update cluster status: %s", cluster.Name)
err := retry.RetryOnConflict(retry.DefaultRetry, func() (err error) {
_, err = helper.UpdateStatus(ctx, c.Client, cluster, func() error {
cluster.Status.KubernetesVersion = currentClusterStatus.KubernetesVersion
cluster.Status.APIEnablements = currentClusterStatus.APIEnablements
cluster.Status.NodeSummary = currentClusterStatus.NodeSummary
cluster.Status.ResourceSummary = currentClusterStatus.ResourceSummary
for _, condition := range conditions {
meta.SetStatusCondition(&cluster.Status.Conditions, condition)
}
>>>>>>> fix error of when cluster status condition update
return nil
}

Expand All @@ -297,11 +349,33 @@ func (c *ClusterStatusController) updateStatusIfNeeded(cluster *clusterv1alpha1.
})
if err != nil {
klog.Errorf("Failed to update health status of the member cluster: %v, err is : %v", cluster.Name, err)
<<<<<<< HEAD
return controllerruntime.Result{Requeue: true}, err
=======
return err
>>>>>>> fix error of when cluster status condition update
}
}

return controllerruntime.Result{RequeueAfter: c.ClusterStatusUpdateFrequency.Duration}, nil
return nil
}

func updateStatusCondition(ctx context.Context, c client.Client, cluster *clusterv1alpha1.Cluster, conditions ...metav1.Condition) error {
klog.V(4).Infof("Start to update cluster(%s) status condition", cluster.Name)
err := retry.RetryOnConflict(retry.DefaultRetry, func() (err error) {
_, err = helper.UpdateStatus(ctx, c, cluster, func() error {
for _, condition := range conditions {
meta.SetStatusCondition(&cluster.Status.Conditions, condition)
}
return nil
})
return err
})
if err != nil {
klog.Errorf("Failed to update status condition of the member cluster: %v, err is : %v", cluster.Name, err)
return err
}
return nil
}

func (c *ClusterStatusController) initializeGenericInformerManagerForCluster(clusterClient *util.ClusterClient) {
Expand Down
20 changes: 20 additions & 0 deletions pkg/controllers/status/cluster_status_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -228,9 +228,13 @@ func TestClusterStatusController_syncClusterStatus(t *testing.T) {
if err := c.Client.Create(context.Background(), cluster); err != nil {
t.Fatalf("Failed to create cluster: %v", err)
}
<<<<<<< HEAD
res, err := c.syncClusterStatus(cluster)
expect := controllerruntime.Result{}
assert.Equal(t, expect, res)
=======
err := c.syncClusterStatus(context.Background(), cluster)
>>>>>>> fix error of when cluster status condition update
assert.Empty(t, err)
})
t.Run("online is false, readyCondition.Status isn't true", func(t *testing.T) {
Expand Down Expand Up @@ -275,9 +279,13 @@ func TestClusterStatusController_syncClusterStatus(t *testing.T) {
t.Fatalf("Failed to create cluster: %v", err)
}

<<<<<<< HEAD
res, err := c.syncClusterStatus(cluster)
expect := controllerruntime.Result{}
assert.Equal(t, expect, res)
=======
err := c.syncClusterStatus(context.Background(), cluster)
>>>>>>> fix error of when cluster status condition update
assert.Empty(t, err)
})

Expand Down Expand Up @@ -322,9 +330,13 @@ func TestClusterStatusController_syncClusterStatus(t *testing.T) {
if err := c.Client.Create(context.Background(), cluster); err != nil {
t.Fatalf("Failed to create cluster: %v", err)
}
<<<<<<< HEAD
res, err := c.syncClusterStatus(cluster)
expect := controllerruntime.Result{}
assert.Equal(t, expect, res)
=======
err := c.syncClusterStatus(context.Background(), cluster)
>>>>>>> fix error of when cluster status condition update
assert.Empty(t, err)
})
}
Expand Down Expand Up @@ -913,8 +925,12 @@ func TestClusterStatusController_updateStatusIfNeeded(t *testing.T) {
ClusterClientSetFunc: util.NewClusterClientSet,
}

<<<<<<< HEAD
actual, err := c.updateStatusIfNeeded(cluster, currentClusterStatus)
assert.Equal(t, controllerruntime.Result{}, actual)
=======
err := c.updateStatusIfNeeded(context.Background(), cluster, currentClusterStatus)
>>>>>>> fix error of when cluster status condition update
assert.Empty(t, err, "updateStatusIfNeeded returns error")
})

Expand Down Expand Up @@ -978,9 +994,13 @@ func TestClusterStatusController_updateStatusIfNeeded(t *testing.T) {
ClusterClientSetFunc: util.NewClusterClientSet,
}

<<<<<<< HEAD
actual, err := c.updateStatusIfNeeded(cluster, currentClusterStatus)
expect := controllerruntime.Result{Requeue: true}
assert.Equal(t, expect, actual)
=======
err := c.updateStatusIfNeeded(context.Background(), cluster, currentClusterStatus)
>>>>>>> fix error of when cluster status condition update
assert.NotEmpty(t, err, "updateStatusIfNeeded doesn't return error")
})
}
Expand Down

0 comments on commit 729f640

Please sign in to comment.