Skip to content

Commit

Permalink
cephfs: passing args to command
Browse files Browse the repository at this point in the history
Signed-off-by: Riya Singhal <[email protected]>
  • Loading branch information
riya-singhal31 committed Oct 6, 2023
1 parent fdf9a4d commit b54a6fe
Showing 1 changed file with 35 additions and 11 deletions.
46 changes: 35 additions & 11 deletions internal/csi-addons/networkfence/fencing.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ const (
blocklistTime = "157784760"
invalidCommandStr = "invalid command"
// we can always use mds rank 0, since all the clients have a session with rank-0.
mdsRank = "0"
mdsRank = 0
)

// NetworkFence contains the CIDR blocks to be blocked.
Expand Down Expand Up @@ -145,9 +145,15 @@ func (nf *NetworkFence) AddNetworkFence(ctx context.Context) error {
return nil
}

func listActiveClients(ctx context.Context) ([]activeClient, error) {
func (nf *NetworkFence) listActiveClients(ctx context.Context) ([]activeClient, error) {
arg := []string{
"--id", nf.cr.ID,
"--keyfile=" + nf.cr.KeyFile,
"-m", nf.Monitors,
}
// FIXME: replace the ceph command with go-ceph API in future
cmd := []string{"tell", fmt.Sprintf("mds.%s", mdsRank), "client", "ls"}
cmd := []string{"tell", fmt.Sprintf("mds.%d", mdsRank), "client", "ls"}
cmd = append(cmd, arg...)
stdout, stdErr, err := util.ExecCommandWithTimeout(ctx, 5*time.Minute, "ceph", cmd...)
if err != nil {
return nil, fmt.Errorf("failed to list active clients: %w, stderr: %q", err, stdErr)
Expand All @@ -161,9 +167,15 @@ func listActiveClients(ctx context.Context) ([]activeClient, error) {
return activeClients, nil
}

func evictCephFSClient(ctx context.Context, clientID int) error {
func (nf *NetworkFence) evictCephFSClient(ctx context.Context, clientID int) error {
arg := []string{
"--id", nf.cr.ID,
"--keyfile=" + nf.cr.KeyFile,
"-m", nf.Monitors,
}
// FIXME: replace the ceph command with go-ceph API in future
cmd := []string{"tell", fmt.Sprintf("mds.%s", mdsRank), "client", "evict", fmt.Sprintf("id=%d", clientID)}
cmd := []string{"tell", fmt.Sprintf("mds.%d", mdsRank), "client", "evict", fmt.Sprintf("id=%d", clientID)}
cmd = append(cmd, arg...)
_, stdErr, err := util.ExecCommandWithTimeout(ctx, 5*time.Minute, "ceph", cmd...)
if err != nil {
return fmt.Errorf("failed to evict client %d: %w, stderr: %q", clientID, err, stdErr)
Expand Down Expand Up @@ -241,7 +253,7 @@ func (ac *activeClient) fetchID() (int, error) {
func (nf *NetworkFence) AddClientEviction(ctx context.Context) error {
evictedIPs := make(map[string]bool)
// fetch active clients
activeClients, err := listActiveClients(ctx)
activeClients, err := nf.listActiveClients(ctx)
if err != nil {
return err
}
Expand All @@ -259,7 +271,7 @@ func (nf *NetworkFence) AddClientEviction(ctx context.Context) error {
return fmt.Errorf("error fetching client ID: %w", err)
}
// evict the client
err = evictCephFSClient(ctx, clientID)
err = nf.evictCephFSClient(ctx, clientID)
if err != nil {
return fmt.Errorf("error evicting client %d: %w", clientID, err)
}
Expand Down Expand Up @@ -294,9 +306,15 @@ func (nf *NetworkFence) AddClientEviction(ctx context.Context) error {
return nil
}

func getBlocklistEntries(ctx context.Context) ([]string, error) {
func (nf *NetworkFence) getBlocklistEntries(ctx context.Context) ([]string, error) {
arg := []string{
"--id", nf.cr.ID,
"--keyfile=" + nf.cr.KeyFile,
"-m", nf.Monitors,
}
// FIXME: replace the ceph command with go-ceph API in future
cmd := []string{"osd", "blocklist", "ls"}
cmd = append(cmd, arg...)
stdout, stdErr, err := util.ExecCommandWithTimeout(ctx, 5*time.Minute, "ceph", cmd...)
if err != nil {
return nil, fmt.Errorf("failed to list blocklisted clients: %w, stderr: %q", err, stdErr)
Expand All @@ -309,9 +327,15 @@ func getBlocklistEntries(ctx context.Context) ([]string, error) {
return entries, nil
}

func removeBlocklistEntry(ctx context.Context, entry string) error {
func (nf *NetworkFence) removeBlocklistEntry(ctx context.Context, entry string) error {
arg := []string{
"--id", nf.cr.ID,
"--keyfile=" + nf.cr.KeyFile,
"-m", nf.Monitors,
}
// FIXME: replace the ceph command with go-ceph API in future
cmd := []string{"osd", "blocklist", "rm", entry}
cmd = append(cmd, arg...)
_, stdErr, err := util.ExecCommandWithTimeout(ctx, 5*time.Minute, "ceph", cmd...)
if err != nil {
return fmt.Errorf("failed to un-blocklist entry %s: %w, stderr: %q", entry, err, stdErr)
Expand All @@ -325,7 +349,7 @@ func removeBlocklistEntry(ctx context.Context, entry string) error {
// using a network fence.
func (nf *NetworkFence) RemoveClientEviction(ctx context.Context) error {
// get the list of blocklist entries
blocklistEntries, err := getBlocklistEntries(ctx)
blocklistEntries, err := nf.getBlocklistEntries(ctx)
if err != nil {
return fmt.Errorf("failed to get blocklist entries: %w", err)
}
Expand All @@ -334,7 +358,7 @@ func (nf *NetworkFence) RemoveClientEviction(ctx context.Context) error {
for _, cidr := range nf.Cidr {
for _, entry := range blocklistEntries {
if strings.HasPrefix(entry, cidr) {
err := removeBlocklistEntry(ctx, entry)
err := nf.removeBlocklistEntry(ctx, entry)
if err != nil {
return fmt.Errorf("failed to remove blocklisted entry %s: %w", entry, err)
}
Expand Down

0 comments on commit b54a6fe

Please sign in to comment.