-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Validate CR/node name from the node names in the cluster
We want to validate that CR has been created correctly. Also UT functionality
- Loading branch information
Showing
5 changed files
with
109 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package utils | ||
|
||
import ( | ||
"fmt" | ||
"net/http" | ||
|
||
apiErrors "k8s.io/apimachinery/pkg/api/errors" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
) | ||
|
||
// buildApiError returns new error with updated status | ||
func buildApiError(err error, msg string) error { | ||
var buildError error | ||
switch { | ||
case apiErrors.IsNotFound(err): | ||
// Handle "Not Found" error | ||
buildError = &apiErrors.StatusError{ErrStatus: metav1.Status{ | ||
Status: metav1.StatusFailure, | ||
Code: http.StatusNotFound, | ||
Message: msg, | ||
Reason: metav1.StatusReasonNotFound, | ||
}} | ||
case apiErrors.IsForbidden(err): | ||
// Handle "Forbidden" error | ||
buildError = &apiErrors.StatusError{ErrStatus: metav1.Status{ | ||
Status: metav1.StatusFailure, | ||
Code: http.StatusForbidden, | ||
Message: msg, | ||
Reason: metav1.StatusReasonForbidden, | ||
}} | ||
default: | ||
// Handle other error types | ||
buildError = fmt.Errorf("error %w does not match the following http error codes: %d, %d", err, http.StatusNotFound, http.StatusForbidden) | ||
} | ||
return buildError | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package utils | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
|
||
corev1 "k8s.io/api/core/v1" | ||
"k8s.io/apimachinery/pkg/types" | ||
"sigs.k8s.io/controller-runtime/pkg/client" | ||
) | ||
|
||
// CheckNodeName returns an error if nodeName doesn't match any node name int the cluster, otherwise a nil | ||
func CheckNodeName(r client.Reader, nodeName string) error { | ||
node := &corev1.Node{} | ||
objNodeName := types.NamespacedName{Name: nodeName} | ||
err := r.Get(context.Background(), objNodeName, node) | ||
if err != nil { | ||
// no match between CR name and the cluster nodes | ||
msg := fmt.Sprintf("node %s is invalid", nodeName) | ||
err = buildApiError(err, msg) | ||
return fmt.Errorf("CR's name doesn't match any node name in the cluster - %w", err) | ||
} | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters