-
Notifications
You must be signed in to change notification settings - Fork 63
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add validation webhook for users.rabbitmq.com
- Loading branch information
Showing
9 changed files
with
125 additions
and
2 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package v1alpha1 | ||
|
||
import ( | ||
"fmt" | ||
apierrors "k8s.io/apimachinery/pkg/api/errors" | ||
"k8s.io/apimachinery/pkg/runtime" | ||
"k8s.io/apimachinery/pkg/util/validation/field" | ||
ctrl "sigs.k8s.io/controller-runtime" | ||
"sigs.k8s.io/controller-runtime/pkg/webhook" | ||
) | ||
|
||
func (u *User) SetupWebhookWithManager(mgr ctrl.Manager) error { | ||
return ctrl.NewWebhookManagedBy(mgr). | ||
For(u). | ||
Complete() | ||
} | ||
|
||
// +kubebuilder:webhook:verbs=create;update,path=/validate-rabbitmq-com-v1alpha1-user,mutating=false,failurePolicy=fail,groups=rabbitmq.com,resources=users,versions=v1alpha1,name=vuser.kb.io,sideEffects=none,admissionReviewVersions=v1 | ||
|
||
var _ webhook.Validator = &User{} | ||
|
||
// no validation on create | ||
func (u *User) ValidateCreate() error { | ||
return nil | ||
} | ||
|
||
// returns error type 'forbidden' for updates on rabbitmqClusterReference | ||
// user.spec.tags can be updated | ||
func (u *User) ValidateUpdate(old runtime.Object) error { | ||
oldUser, ok := old.(*User) | ||
if !ok { | ||
return apierrors.NewBadRequest(fmt.Sprintf("expected a user but got a %T", old)) | ||
} | ||
|
||
if u.Spec.RabbitmqClusterReference != oldUser.Spec.RabbitmqClusterReference { | ||
return apierrors.NewForbidden(u.GroupResource(), u.Name, | ||
field.Forbidden(field.NewPath("spec", "rabbitmqClusterReference"), "update on rabbitmqClusterReference is forbidden")) | ||
} | ||
return nil | ||
} | ||
|
||
// no validation on delete | ||
func (u *User) ValidateDelete() error { | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package v1alpha1 | ||
|
||
import ( | ||
. "github.com/onsi/ginkgo" | ||
. "github.com/onsi/gomega" | ||
apierrors "k8s.io/apimachinery/pkg/api/errors" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
) | ||
|
||
var _ = Describe("user webhook", func() { | ||
var user = User{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Name: "test", | ||
}, | ||
Spec: UserSpec{ | ||
Tags: []UserTag{"policymaker"}, | ||
RabbitmqClusterReference: RabbitmqClusterReference{ | ||
Name: "a-cluster", | ||
Namespace: "default", | ||
}, | ||
}, | ||
} | ||
|
||
It("does not allow updates on RabbitmqClusterReference", func() { | ||
new := user.DeepCopy() | ||
new.Spec.RabbitmqClusterReference = RabbitmqClusterReference{ | ||
Name: "new-cluster", | ||
Namespace: "default", | ||
} | ||
Expect(apierrors.IsForbidden(new.ValidateUpdate(&user))).To(BeTrue()) | ||
}) | ||
|
||
It("allows update on tags", func() { | ||
new := user.DeepCopy() | ||
new.Spec.Tags = []UserTag{"monitoring"} | ||
Expect(new.ValidateUpdate(&user)).To(Succeed()) | ||
}) | ||
}) |
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
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