Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Immutable webhook for Cassandra User #505

Merged
merged 1 commit into from
Jul 31, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 71 additions & 0 deletions apis/clusterresources/v1beta1/cassandrauser_webhook.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
/*
Copyright 2022.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package v1beta1

import (
"k8s.io/apimachinery/pkg/runtime"
ctrl "sigs.k8s.io/controller-runtime"
logf "sigs.k8s.io/controller-runtime/pkg/log"
"sigs.k8s.io/controller-runtime/pkg/webhook"

"github.com/instaclustr/operator/pkg/models"
)

// log is for logging in this package.
var cassandrauserlog = logf.Log.WithName("cassandrauser-resource")

func (r *CassandraUser) SetupWebhookWithManager(mgr ctrl.Manager) error {
return ctrl.NewWebhookManagedBy(mgr).
For(r).
Complete()
}

// TODO(user): change verbs to "verbs=create;update;delete" if you want to enable deletion validation.
//+kubebuilder:webhook:path=/validate-clusterresources-instaclustr-com-v1beta1-cassandrauser,mutating=false,failurePolicy=fail,sideEffects=None,groups=clusterresources.instaclustr.com,resources=cassandrausers,verbs=create;update,versions=v1beta1,name=vcassandrauser.kb.io,admissionReviewVersions=v1

var _ webhook.Validator = &CassandraUser{}

// ValidateCreate implements webhook.Validator so a webhook will be registered for the type
func (u *CassandraUser) ValidateCreate() error {
cassandrauserlog.Info("validate create", "name", u.Name)

if u.Spec.SecretRef.Name == "" || u.Spec.SecretRef.Namespace == "" {
return models.ErrEmptySecretRef
}

return nil
}

// ValidateUpdate implements webhook.Validator so a webhook will be registered for the type
func (u *CassandraUser) ValidateUpdate(old runtime.Object) error {
cassandrauserlog.Info("validate update", "name", u.Name)

oldUser := old.(*CassandraUser)
if *u.Spec.SecretRef != *oldUser.Spec.SecretRef {
return models.ErrImmutableSecretRef
}

return nil
}

// ValidateDelete implements webhook.Validator so a webhook will be registered for the type
func (r *CassandraUser) ValidateDelete() error {
cassandrauserlog.Info("validate delete", "name", r.Name)

// TODO(user): fill in your validation logic upon object deletion.
return nil
}
8 changes: 4 additions & 4 deletions apis/clusterresources/v1beta1/opensearchuser_webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@ limitations under the License.
package v1beta1

import (
"fmt"

"k8s.io/apimachinery/pkg/runtime"
ctrl "sigs.k8s.io/controller-runtime"
logf "sigs.k8s.io/controller-runtime/pkg/log"
"sigs.k8s.io/controller-runtime/pkg/webhook"

"github.com/instaclustr/operator/pkg/models"
)

// log is for logging in this package.
Expand All @@ -43,7 +43,7 @@ func (u *OpenSearchUser) ValidateCreate() error {
opensearchuserlog.Info("validate create", "name", u.Name)

if u.Spec.SecretRef.Name == "" || u.Spec.SecretRef.Namespace == "" {
return fmt.Errorf("secretRef.name and secretRef.namespace should not be empty")
return models.ErrEmptySecretRef
}

return nil
Expand All @@ -55,7 +55,7 @@ func (u *OpenSearchUser) ValidateUpdate(old runtime.Object) error {

oldUser := old.(*OpenSearchUser)
if *u.Spec.SecretRef != *oldUser.Spec.SecretRef {
return fmt.Errorf("spec.secretRef field is immutable")
return models.ErrImmutableSecretRef
}

return nil
Expand Down
3 changes: 3 additions & 0 deletions apis/clusterresources/v1beta1/webhook_suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,9 @@ var _ = BeforeSuite(func() {
err = (&OpenSearchUser{}).SetupWebhookWithManager(mgr)
Expect(err).NotTo(HaveOccurred())

err = (&CassandraUser{}).SetupWebhookWithManager(mgr)
Expect(err).NotTo(HaveOccurred())

//+kubebuilder:scaffold:webhook

go func() {
Expand Down
20 changes: 20 additions & 0 deletions config/webhook/manifests.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,26 @@ webhooks:
resources:
- azurevnetpeerings
sideEffects: None
- admissionReviewVersions:
- v1
clientConfig:
service:
name: webhook-service
namespace: system
path: /validate-clusterresources-instaclustr-com-v1beta1-cassandrauser
failurePolicy: Fail
name: vcassandrauser.kb.io
rules:
- apiGroups:
- clusterresources.instaclustr.com
apiVersions:
- v1beta1
operations:
- CREATE
- UPDATE
resources:
- cassandrausers
sideEffects: None
- admissionReviewVersions:
- v1
clientConfig:
Expand Down
4 changes: 4 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -424,6 +424,10 @@ func main() {
setupLog.Error(err, "unable to create webhook", "webhook", "OpenSearchUser")
os.Exit(1)
}
if err = (&clusterresourcesv1beta1.CassandraUser{}).SetupWebhookWithManager(mgr); err != nil {
setupLog.Error(err, "unable to create webhook", "webhook", "CassandraUser")
os.Exit(1)
}
//+kubebuilder:scaffold:builder

if err := mgr.AddHealthzCheck("healthz", healthz.Ping); err != nil {
Expand Down
2 changes: 2 additions & 0 deletions pkg/models/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ var (
ErrImmutableAdvancedVisibility = errors.New("AdvancedVisibility array is immutable")
ErrImmutablePrivateLink = errors.New("PrivateLink array is immutable")
ErrImmutableNodesNumber = errors.New("nodes number is immutable")
ErrImmutableSecretRef = errors.New("secret reference is immutable")
ErrEmptySecretRef = errors.New("secretRef.name and secretRef.namespace should not be empty")
ErrMissingSecretKeys = errors.New("the secret is missing the correct keys for the user")
ErrUserStillExist = errors.New("the user is still attached to cluster")
)