-
Notifications
You must be signed in to change notification settings - Fork 2.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
core: cephblockpoolRadosNamespace cleanup
Clean up pool images and snapshots in the radosnamespace Signed-off-by: sp98 <sapillai@redhat.com>
- Loading branch information
Showing
9 changed files
with
236 additions
and
13 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
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,56 @@ | ||
/* | ||
Copyright 2024 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 cleanup | ||
|
||
import ( | ||
"github.com/pkg/errors" | ||
"github.com/rook/rook/pkg/clusterd" | ||
"github.com/rook/rook/pkg/daemon/ceph/client" | ||
cephclient "github.com/rook/rook/pkg/daemon/ceph/client" | ||
) | ||
|
||
func RadosNamespaceCleanup(context *clusterd.Context, clusterInfo *client.ClusterInfo, poolName, radosNamespace string) error { | ||
logger.Infof("starting clean up of CephBlockPoolRadosNamespace %q resources in blockpool %q", radosNamespace, poolName) | ||
|
||
images, err := cephclient.ListImagesInRadosNamespace(context, clusterInfo, poolName, radosNamespace) | ||
if err != nil { | ||
return errors.Wrapf(err, "failed to list images in blockpool %q in rados namespace %q", poolName, radosNamespace) | ||
} | ||
|
||
var retErr error | ||
for _, image := range images { | ||
err := cephclient.PurgeImageSnapshots(context, clusterInfo, poolName, image.Name, radosNamespace) | ||
if err != nil { | ||
retErr = errors.Wrapf(err, "failed to purges snapshots of the image %q in pool %q.", image.Name, poolName) | ||
logger.Error(retErr) | ||
} | ||
|
||
err = cephclient.DeleteImageInRadosNamespace(context, clusterInfo, image.Name, poolName, radosNamespace) | ||
if err != nil { | ||
retErr = errors.Wrapf(err, "failed to delete image %q in pool %q.", image.Name, poolName) | ||
logger.Error(retErr) | ||
} | ||
} | ||
|
||
if retErr != nil { | ||
logger.Errorf("failed to clean up CephBlockPoolRadosNamespace %q resources in blockpool %q", radosNamespace, poolName) | ||
return retErr | ||
} | ||
|
||
logger.Infof("successfully cleaned up CephBlockPoolRadosNamespace %q resources in blockpool %q", radosNamespace, poolName) | ||
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,80 @@ | ||
/* | ||
Copyright 2024 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 cleanup | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/pkg/errors" | ||
"github.com/rook/rook/pkg/clusterd" | ||
cephclient "github.com/rook/rook/pkg/daemon/ceph/client" | ||
exectest "github.com/rook/rook/pkg/util/exec/test" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
const ( | ||
mockImageLSResponse = `[{"image":"csi-vol-136268e8-5386-4453-a6bd-9dca381d187d","id":"16e35cfa56a7","size":1073741824,"format":2}]` | ||
) | ||
|
||
func TestRadosNamespace(t *testing.T) { | ||
clusterInfo := cephclient.AdminTestClusterInfo("mycluster") | ||
poolName := "test-pool" | ||
radosNamespace := "test-namespace" | ||
|
||
t.Run("no images in rados namespace", func(t *testing.T) { | ||
executor := &exectest.MockExecutor{} | ||
executor.MockExecuteCommandWithOutput = func(command string, args ...string) (string, error) { | ||
logger.Infof("Command: %s %v", command, args) | ||
if args[0] == "ls" && args[1] == "-l" { | ||
assert.Equal(t, poolName, args[2]) | ||
return "", nil | ||
} | ||
return "", errors.New("unknown command") | ||
} | ||
context := &clusterd.Context{Executor: executor} | ||
err := RadosNamespaceCleanup(context, clusterInfo, poolName, radosNamespace) | ||
assert.NoError(t, err) | ||
}) | ||
|
||
t.Run("images available in rados namespace", func(t *testing.T) { | ||
executor := &exectest.MockExecutor{} | ||
executor.MockExecuteCommandWithOutput = func(command string, args ...string) (string, error) { | ||
logger.Infof("Command: %s %v", command, args) | ||
// list all subvolumes in subvolumegroup | ||
if args[0] == "ls" && args[1] == "-l" { | ||
assert.Equal(t, poolName, args[2]) | ||
return mockImageLSResponse, nil | ||
} | ||
if args[0] == "snap" && args[1] == "purge" { | ||
assert.Equal(t, "test-pool/csi-vol-136268e8-5386-4453-a6bd-9dca381d187d", args[2]) | ||
assert.Equal(t, "--namespace", args[3]) | ||
assert.Equal(t, radosNamespace, args[4]) | ||
return "", nil | ||
} | ||
if args[0] == "rm" { | ||
assert.Equal(t, "test-pool/csi-vol-136268e8-5386-4453-a6bd-9dca381d187d", args[1]) | ||
assert.Equal(t, "--namespace", args[2]) | ||
assert.Equal(t, radosNamespace, args[3]) | ||
return "", nil | ||
} | ||
return "", errors.New("unknown command") | ||
} | ||
context := &clusterd.Context{Executor: executor} | ||
err := RadosNamespaceCleanup(context, clusterInfo, poolName, radosNamespace) | ||
assert.NoError(t, err) | ||
}) | ||
} |
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