Skip to content

Commit

Permalink
Fix nil error issue (#4173)
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 few more test cases.

Signed-off-by: Lan Luo <luola@vmware.com>
  • Loading branch information
luolanzone authored Aug 30, 2022
1 parent 5507974 commit 33e1b53
Show file tree
Hide file tree
Showing 2 changed files with 147 additions and 264 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 33e1b53

Please sign in to comment.