diff --git a/pkg/apis/ceph.rook.io/v1/subvolumegroup.go b/pkg/apis/ceph.rook.io/v1/subvolumegroup.go new file mode 100644 index 000000000..9fc528927 --- /dev/null +++ b/pkg/apis/ceph.rook.io/v1/subvolumegroup.go @@ -0,0 +1,42 @@ +/* +Copyright 2022 The Rook Authors. All rights reserved. + +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 v1 + +import ( + "github.com/pkg/errors" + "k8s.io/apimachinery/pkg/runtime" + "sigs.k8s.io/controller-runtime/pkg/webhook" +) + +var _ webhook.Validator = &CephFilesystemSubVolumeGroup{} + +func (c *CephFilesystemSubVolumeGroup) ValidateCreate() error { + return nil +} + +func (c *CephFilesystemSubVolumeGroup) ValidateUpdate(old runtime.Object) error { + logger.Infof("validate update %s/%s CephFilesystemSubVolumeGroup", c.Namespace, c.Name) + sg := old.(*CephFilesystemSubVolumeGroup) + if sg.Spec.FilesystemName != c.Spec.FilesystemName { + return errors.New("invalid update: filesystem name cannot be changed") + } + return nil +} + +func (c *CephFilesystemSubVolumeGroup) ValidateDelete() error { + return nil +} diff --git a/pkg/apis/ceph.rook.io/v1/subvolumegroup_test.go b/pkg/apis/ceph.rook.io/v1/subvolumegroup_test.go new file mode 100644 index 000000000..a01366038 --- /dev/null +++ b/pkg/apis/ceph.rook.io/v1/subvolumegroup_test.go @@ -0,0 +1,43 @@ +/* +Copyright 2022 The Rook Authors. All rights reserved. + +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 v1 + +import ( + "testing" + + "github.com/stretchr/testify/assert" + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" +) + +func TestCephFilesystemSubVolumeGroupValidateUpdate(t *testing.T) { + rn := &CephFilesystemSubVolumeGroup{ + ObjectMeta: metav1.ObjectMeta{ + Name: "subvol-name", + }, + Spec: CephFilesystemSubVolumeGroupSpec{ + FilesystemName: "filesystem-name", + }, + } + ur := rn.DeepCopy() + // validate with same filesystem name + err := ur.ValidateUpdate(rn) + assert.NoError(t, err) + // validate with different filesystem name + ur.Spec.FilesystemName = "new-filesystem-name" + err = ur.ValidateUpdate(rn) + assert.Error(t, err) +} diff --git a/pkg/operator/ceph/cr_manager.go b/pkg/operator/ceph/cr_manager.go index d1197de31..715a8daa4 100644 --- a/pkg/operator/ceph/cr_manager.go +++ b/pkg/operator/ceph/cr_manager.go @@ -75,6 +75,7 @@ var ( &cephv1.CephBlockPool{}, &cephv1.CephObjectStore{}, &cephv1.CephBlockPoolRadosNamespace{}, + &cephv1.CephFilesystemSubVolumeGroup{}, } ) diff --git a/pkg/operator/ceph/webhook-config.go b/pkg/operator/ceph/webhook-config.go index a0714ba1b..f42a6c7c4 100644 --- a/pkg/operator/ceph/webhook-config.go +++ b/pkg/operator/ceph/webhook-config.go @@ -33,13 +33,14 @@ import ( ) const ( - issuerName = "selfsigned-issuer" - certificateName = "rook-admission-controller-cert" - webhookConfigName = "rook-ceph-webhook" - serviceCephClusterPath = "/validate-ceph-rook-io-v1-cephcluster" - serviceCephBlockPoolPath = "/validate-ceph-rook-io-v1-cephblockpool" - serviceCephObjectStorePath = "/validate-ceph-rook-io-v1-cephobjectstore" - serviceCephBlockPoolRadosNamespacePath = "/validate-ceph-rook-io-v1-cephblockpoolradosnamespace" + issuerName = "selfsigned-issuer" + certificateName = "rook-admission-controller-cert" + webhookConfigName = "rook-ceph-webhook" + serviceCephClusterPath = "/validate-ceph-rook-io-v1-cephcluster" + serviceCephBlockPoolPath = "/validate-ceph-rook-io-v1-cephblockpool" + serviceCephObjectStorePath = "/validate-ceph-rook-io-v1-cephobjectstore" + serviceCephBlockPoolRadosNamespacePath = "/validate-ceph-rook-io-v1-cephblockpoolradosnamespace" + serviceCephFileSystemSubvolumeGroupPath = "/validate-ceph-rook-io-v1-cephfilesystemsubvolumegroup" ) func fetchorCreateIssuer(ctx context.Context, certMgrClient *cs.CertmanagerV1Client) (*api.Issuer, error) { @@ -162,6 +163,7 @@ func addValidatingWebhookConfig(ctx context.Context, clusterdContext *clusterd.C fmt.Sprintf("cephblockpool-wh-%s-%s.rook.io", admissionControllerAppName, namespace), fmt.Sprintf("cephobjectstore-wh-%s-%s.rook.io", admissionControllerAppName, namespace), fmt.Sprintf("cephblockpoolradosnamespace-wh-%s-%s.rook.io", admissionControllerAppName, namespace), + fmt.Sprintf("cephfilesystemsubvolumegroup-wh-%s-%s.rook.io", admissionControllerAppName, namespace), } resources := []string{ @@ -169,6 +171,7 @@ func addValidatingWebhookConfig(ctx context.Context, clusterdContext *clusterd.C "cephblockpools", "cephobjectstores", "cephblockpoolradosnamespaces", + "cephfilesystemsubvolumegroups", } resourcesServicePath := []string{ @@ -176,6 +179,7 @@ func addValidatingWebhookConfig(ctx context.Context, clusterdContext *clusterd.C serviceCephBlockPoolPath, serviceCephObjectStorePath, serviceCephBlockPoolRadosNamespacePath, + serviceCephFileSystemSubvolumeGroupPath, } logger.Infof("Creating webhook %s/%s.", namespace, webhookConfigName)