Skip to content

Commit

Permalink
refactor: refactor AsResult method in shared.go
Browse files Browse the repository at this point in the history
Signed-off-by: Phoeniix Zhao <[email protected]>
  • Loading branch information
Jiawei Zhao authored and Phoenix500526 committed Jan 10, 2024
1 parent dc5cf46 commit 900f7cd
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 17 deletions.
4 changes: 4 additions & 0 deletions api/v1alpha1/xlinecluster_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ import (
"k8s.io/apimachinery/pkg/types"
)

// NOTE: json tags are required. Any new fields you add must have json tags for the fields to be serialized.

// XlineCluster is the Schema for the xlineclusters API
// +kubebuilder:object:root=true
// +kubebuilder:subresource:status
Expand Down Expand Up @@ -83,12 +85,14 @@ const (
StageComplete XlineClusterOprStage = "complete"
)

// XlineClusterRecStatus represents XlineCluster reconcile status
type XlineClusterRecStatus struct {
Stage XlineClusterOprStage `json:"stage,omitempty"`
StageStatus OprStageStatus `json:"stageStatus,omitempty"`
LastMessage string `json:"lastMessage,omitempty"`
}

// XlineClusterSyncStatus represents XlineCluster sync status
type XlineClusterSyncStatus struct {
Image string `json:"image,omitempty"`
StatefulSetRef NamespacedName `json:"statefulSetRef,omitempty"`
Expand Down
19 changes: 2 additions & 17 deletions internal/controller/sharded.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,14 @@ func (r *StCtrlErrSet) AsResult() (ctrl.Result, error) {
if r.Update != nil {
errMap["update-status"] = r.Update
}
mergedErr := MergeErrorsWithTag(errMap)
if mergedErr == nil {
if len(errMap) == 0 {
if updateConflict {
return ctrl.Result{Requeue: true}, nil
} else {
return ctrl.Result{}, nil
}
} else {
return ctrl.Result{Requeue: true}, mergedErr
return ctrl.Result{Requeue: true}, &MultiTaggedError{Errors: errMap}
}
}

Expand All @@ -56,17 +55,3 @@ func (e *MultiTaggedError) Error() string {
}
return strings.Join(errStrs, "; ")
}

// MergeErrorsWithTag merges multiple errors into one with tags.
func MergeErrorsWithTag(errors map[string]error) *MultiTaggedError {
errMap := make(map[string]error)
for tag, err := range errors {
if err != nil {
errMap[tag] = err
}
}
if len(errMap) == 0 {
return nil
}
return &MultiTaggedError{Errors: errMap}
}
42 changes: 42 additions & 0 deletions internal/controller/sharded_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package controller

import (
"errors"
"net/http"
"testing"

"github.com/stretchr/testify/assert"
update_errors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
ctrl "sigs.k8s.io/controller-runtime"
)

func TestStCtrlErrSet_AsResult(t *testing.T) {
// Test case 1: No errors
errSet := StCtrlErrSet{}
result, err := errSet.AsResult()
assert.NoError(t, err)
assert.Equal(t, ctrl.Result{}, result)

// Test case 2: Update conflict error
statusErr := update_errors.StatusError{ErrStatus: metav1.Status{
Code: http.StatusConflict,
Reason: metav1.StatusReasonConflict,
}}
errSet = StCtrlErrSet{
Update: error(&statusErr),
}
result, err = errSet.AsResult()
assert.NoError(t, err)
assert.Equal(t, ctrl.Result{Requeue: true}, result)

// Test case 3: Other errors
errSet = StCtrlErrSet{
Rec: errors.New("reconciliation error"),
Sync: errors.New("sync error"),
Update: errors.New("update status error"),
}
result, err = errSet.AsResult()
assert.Error(t, err)
assert.Equal(t, ctrl.Result{Requeue: true}, result)
}

0 comments on commit 900f7cd

Please sign in to comment.