-
Notifications
You must be signed in to change notification settings - Fork 67
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add webhook for schemareplication to prevent updates
on rabbitmqClusterReference
- Loading branch information
Showing
9 changed files
with
149 additions
and
14 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,44 @@ | ||
package v1alpha2 | ||
|
||
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 (s *SchemaReplication) SetupWebhookWithManager(mgr ctrl.Manager) error { | ||
return ctrl.NewWebhookManagedBy(mgr). | ||
For(s). | ||
Complete() | ||
} | ||
|
||
// +kubebuilder:webhook:verbs=create;update,path=/validate-rabbitmq-com-v1alpha2-schemareplication,mutating=false,failurePolicy=fail,groups=rabbitmq.com,resources=schemareplications,versions=v1alpha2,name=vschemareplication.kb.io,sideEffects=none,admissionReviewVersions=v1 | ||
|
||
var _ webhook.Validator = &SchemaReplication{} | ||
|
||
// no validation on create | ||
func (s *SchemaReplication) ValidateCreate() error { | ||
return nil | ||
} | ||
|
||
// ValidateUpdate implements webhook.Validator so a webhook will be registered for the type | ||
func (s *SchemaReplication) ValidateUpdate(old runtime.Object) error { | ||
oldReplication, ok := old.(*SchemaReplication) | ||
if !ok { | ||
return apierrors.NewBadRequest(fmt.Sprintf("expected a schema replication type but got a %T", old)) | ||
} | ||
|
||
if s.Spec.RabbitmqClusterReference != oldReplication.Spec.RabbitmqClusterReference { | ||
return apierrors.NewForbidden(s.GroupResource(), s.Name, | ||
field.Forbidden(field.NewPath("spec", "rabbitmqClusterReference"), "update on rabbitmqClusterReference is forbidden")) | ||
} | ||
return nil | ||
} | ||
|
||
// no validation on delete | ||
func (s *SchemaReplication) 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,41 @@ | ||
package v1alpha2 | ||
|
||
import ( | ||
. "github.com/onsi/ginkgo" | ||
. "github.com/onsi/gomega" | ||
corev1 "k8s.io/api/core/v1" | ||
apierrors "k8s.io/apimachinery/pkg/api/errors" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
) | ||
|
||
var _ = Describe("schema-replication webhook", func() { | ||
var replication = SchemaReplication{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Name: "test-replication", | ||
}, | ||
Spec: SchemaReplicationSpec{ | ||
UpstreamSecret: &corev1.LocalObjectReference{ | ||
Name: "a-secret", | ||
}, | ||
RabbitmqClusterReference: RabbitmqClusterReference{ | ||
Name: "a-cluster", | ||
}, | ||
}, | ||
} | ||
|
||
It("does not allow updates on RabbitmqClusterReference", func() { | ||
updated := replication.DeepCopy() | ||
updated.Spec.RabbitmqClusterReference = RabbitmqClusterReference{ | ||
Name: "different-cluster", | ||
} | ||
Expect(apierrors.IsForbidden(updated.ValidateUpdate(&replication))).To(BeTrue()) | ||
}) | ||
|
||
It("allows updates on spec.upstreamSecret", func() { | ||
updated := replication.DeepCopy() | ||
updated.Spec.UpstreamSecret = &corev1.LocalObjectReference{ | ||
Name: "a-different-secret", | ||
} | ||
Expect(updated.ValidateUpdate(&replication)).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