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

core: check for duplicate ceph fs pool names #14653

Merged
merged 1 commit into from
Sep 9, 2024
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
23 changes: 23 additions & 0 deletions pkg/operator/ceph/file/filesystem.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,14 @@ func validateFilesystem(context *clusterd.Context, clusterInfo *cephclient.Clust
if len(f.Spec.DataPools) == 0 {
return nil
}

// Ensure duplicate pool names are not present in the spec.
if len(f.Spec.DataPools) > 1 {
if hasDuplicatePoolNames(f.Spec.DataPools) {
return errors.New("duplicate pool names in the data pool spec")
}
}

if err := cephpool.ValidatePoolSpec(context, clusterInfo, clusterSpec, &f.Spec.MetadataPool); err != nil {
return errors.Wrap(err, "invalid metadata pool")
}
Expand All @@ -157,6 +165,21 @@ func validateFilesystem(context *clusterd.Context, clusterInfo *cephclient.Clust
return nil
}

func hasDuplicatePoolNames(poolSpecList []cephv1.NamedPoolSpec) bool {
poolNames := make(map[string]struct{})
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

poolNames := make(map[string]int)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not storing anything as value here. So struct{} should be good. Its 0-sized so no memory is required to store struct{}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

int will have a default value of 0

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ohkay, make sense:)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

map[Type]struct{} is a low-overhead way of approximating a set data structure in Golang.

for _, poolSpec := range poolSpecList {
if poolSpec.Name != "" {
if _, has := poolNames[poolSpec.Name]; has {
logger.Errorf("duplicate pool name %q in the data pool spec", poolSpec.Name)
return true
}
poolNames[poolSpec.Name] = struct{}{}
}
}

return false
}

// newFS creates a new instance of the file (MDS) service
func newFS(name, namespace string) *Filesystem {
return &Filesystem{
Expand Down
20 changes: 20 additions & 0 deletions pkg/operator/ceph/file/filesystem_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,26 @@ func TestValidateSpec(t *testing.T) {
assert.Nil(t, validateFilesystem(context, clusterInfo, clusterSpec, fs))
}

func TestHasDuplicatePoolNames(t *testing.T) {
// PoolSpec with no duplicates
fs := &cephv1.CephFilesystem{
Spec: cephv1.FilesystemSpec{
DataPools: []cephv1.NamedPoolSpec{
{Name: "pool1"},
{Name: "pool2"},
},
},
}

result := hasDuplicatePoolNames(fs.Spec.DataPools)
assert.False(t, result)

// add duplicate pool name in the spec.
fs.Spec.DataPools = append(fs.Spec.DataPools, cephv1.NamedPoolSpec{Name: "pool1"})
result = hasDuplicatePoolNames(fs.Spec.DataPools)
assert.True(t, result)
}

func TestGenerateDataPoolNames(t *testing.T) {
fs := &Filesystem{Name: "fake", Namespace: "fake"}
fsSpec := cephv1.FilesystemSpec{
Expand Down
Loading