Skip to content

Commit

Permalink
Fix nil error issue
Browse files Browse the repository at this point in the history
admission.Errored doesn't handle nil error case, it will raise a nil
pointer error when there is no ClusterSet found.
Fix it with a new error with error message.
Refactor unit test and add a new unit test for this case.

Signed-off-by: Lan Luo <[email protected]>
  • Loading branch information
luolanzone committed Aug 26, 2022
1 parent 68d274a commit 0e4948b
Show file tree
Hide file tree
Showing 2 changed files with 121 additions and 266 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package main
import (
"context"
"encoding/json"
"fmt"
"net/http"

admissionv1 "k8s.io/api/admission/v1"
Expand Down Expand Up @@ -85,8 +86,8 @@ func (v *memberClusterAnnounceValidator) Handle(ctx context.Context, req admissi
}

if len(clusterSetList.Items) == 0 {
klog.ErrorS(err, "No ClusterSet found", "Namespace", v.namespace)
return admission.Errored(http.StatusPreconditionFailed, err)
klog.ErrorS(nil, "No ClusterSet found", "Namespace", v.namespace)
return admission.Errored(http.StatusPreconditionFailed, fmt.Errorf("no ClusterSet found in Namespace %s", v.namespace))
}
clusterSet := clusterSetList.Items[0]
if clusterSet.Name != memberClusterAnnounce.ClusterSetID {
Expand Down
Loading

0 comments on commit 0e4948b

Please sign in to comment.