mirror of
https://github.com/ceph/ceph-csi.git
synced 2025-06-14 10:53:34 +00:00
cleanup: address golangci 'funcorder' linter problems
The new 'funcorder' linter expects all public functions to be placed before private functions of a struct. Many private functions needed moving further down into their files. Some files had many issues reported. To reduce the churn in those files, they have been annotated with a `//nolint:funcorder` comment. Signed-off-by: Niels de Vos <ndevos@ibm.com>
This commit is contained in:
committed by
mergify[bot]
parent
0907f39d95
commit
0a22e3a186
@ -89,30 +89,123 @@ func NewNetworkFence(
|
||||
return nwFence, nil
|
||||
}
|
||||
|
||||
// addCephBlocklist adds an IP to ceph osd blocklist.
|
||||
func (nf *NetworkFence) addCephBlocklist(ctx context.Context, ip string, useRange bool) error {
|
||||
arg := []string{
|
||||
"--id", nf.cr.ID,
|
||||
"--keyfile=" + nf.cr.KeyFile,
|
||||
"-m", nf.Monitors,
|
||||
}
|
||||
// TODO: add blocklist till infinity.
|
||||
// Currently, ceph does not provide the functionality to blocklist IPs
|
||||
// for infinite time. As a workaround, add a blocklist for 5 YEARS to
|
||||
// represent infinity from ceph-csi side.
|
||||
// At any point in this time, the IPs can be unblocked by an UnfenceClusterReq.
|
||||
// This needs to be updated once ceph provides functionality for the same.
|
||||
cmd := []string{"osd", "blocklist"}
|
||||
if useRange {
|
||||
cmd = append(cmd, "range")
|
||||
}
|
||||
cmd = append(cmd, "add", ip, blocklistTime)
|
||||
cmd = append(cmd, arg...)
|
||||
_, stdErr, err := util.ExecCommand(ctx, "ceph", cmd...)
|
||||
// AddClientEviction blocks access for all the IPs in the CIDR block
|
||||
// using client eviction, it also blocks the entire CIDR.
|
||||
func (nf *NetworkFence) AddClientEviction(ctx context.Context) error {
|
||||
evictedIPs := make(map[string]bool)
|
||||
// fetch active clients
|
||||
activeClients, err := nf.listActiveClients(ctx)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to blocklist IP %q: %w stderr: %q", ip, err, stdErr)
|
||||
return err
|
||||
}
|
||||
// iterate through CIDR blocks and check if any active client matches
|
||||
for _, cidr := range nf.Cidr {
|
||||
for _, client := range activeClients {
|
||||
var clientIP string
|
||||
clientIP, err = client.fetchIP()
|
||||
if err != nil {
|
||||
return fmt.Errorf("error fetching client IP: %w", err)
|
||||
}
|
||||
// check if the clientIP is in the CIDR block
|
||||
if isIPInCIDR(ctx, clientIP, cidr) {
|
||||
var clientID int
|
||||
clientID, err = client.fetchID()
|
||||
if err != nil {
|
||||
return fmt.Errorf("error fetching client ID: %w", err)
|
||||
}
|
||||
// evict the client
|
||||
err = nf.evictCephFSClient(ctx, clientID)
|
||||
if err != nil {
|
||||
return fmt.Errorf("error evicting client %d: %w", clientID, err)
|
||||
}
|
||||
log.DebugLog(ctx, "client %d has been evicted\n", clientID)
|
||||
// add the CIDR to the list of blocklisted IPs
|
||||
evictedIPs[clientIP] = true
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// add the range based blocklist for CIDR
|
||||
err = nf.AddNetworkFence(ctx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// RemoveNetworkFence unblocks access for all the IPs in the IP range mentioned via the CIDR block
|
||||
// using a network fence.
|
||||
// Unfencing one of the protocols(CephFS or RBD) suggests the node is expected to be recovered, so
|
||||
// both CephFS and RBD are expected to work again too.
|
||||
// example:
|
||||
// Create RBD NetworkFence CR for one IP 10.10.10.10
|
||||
// Created CephFS NetworkFence CR for IP range but above IP comes in the Range
|
||||
// Delete the CephFS Network Fence CR to unblocklist the IP
|
||||
// So now the IP (10.10.10.10) is (un)blocklisted and can be used by both protocols.
|
||||
func (nf *NetworkFence) RemoveNetworkFence(ctx context.Context) error {
|
||||
hasBlocklistRangeSupport := true
|
||||
// for each CIDR block, convert it into a range of IPs so as to undo blocklisting operation.
|
||||
for _, cidr := range nf.Cidr {
|
||||
// try range blocklist cmd, if invalid fallback to
|
||||
// iterating through IP range.
|
||||
if hasBlocklistRangeSupport {
|
||||
err := nf.removeCephBlocklist(ctx, cidr, "", true)
|
||||
if err == nil {
|
||||
continue
|
||||
}
|
||||
if !strings.Contains(err.Error(), invalidCommandStr) {
|
||||
return fmt.Errorf("failed to remove blocklist range %q: %w", cidr, err)
|
||||
}
|
||||
hasBlocklistRangeSupport = false
|
||||
}
|
||||
// fetch the list of IPs from a CIDR block
|
||||
hosts, err := getIPRange(cidr)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to convert CIDR block %s to corresponding IP range", cidr)
|
||||
}
|
||||
// remove ceph blocklist for each IP in the range mentioned by the CIDR
|
||||
for _, host := range hosts {
|
||||
// 0 is used as nonce here to tell ceph
|
||||
// to remove the blocklist entry matching: <host>:0/0
|
||||
// it is same as telling ceph to remove just the IP
|
||||
// without specifying any port or nonce with it.
|
||||
err := nf.removeCephBlocklist(ctx, host, "0", false)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (nf *NetworkFence) RemoveClientEviction(ctx context.Context) error {
|
||||
// Remove the CIDR block first
|
||||
err := nf.RemoveNetworkFence(ctx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// Get the ceph blocklist
|
||||
blocklist, err := nf.getCephBlocklist(ctx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// For each CIDR block, remove the IPs in the blocklist
|
||||
// that fall under the CIDR with nonce
|
||||
for _, cidr := range nf.Cidr {
|
||||
hosts := nf.parseBlocklistForCIDR(ctx, blocklist, cidr)
|
||||
log.DebugLog(ctx, "parsed blocklist for CIDR %s: %+v", cidr, hosts)
|
||||
|
||||
for _, host := range hosts {
|
||||
err := nf.removeCephBlocklist(ctx, host.IP, host.Nonce, false)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
}
|
||||
log.DebugLog(ctx, "blocklisted IP %q successfully", ip)
|
||||
|
||||
return nil
|
||||
}
|
||||
@ -153,6 +246,34 @@ func (nf *NetworkFence) AddNetworkFence(ctx context.Context) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// addCephBlocklist adds an IP to ceph osd blocklist.
|
||||
func (nf *NetworkFence) addCephBlocklist(ctx context.Context, ip string, useRange bool) error {
|
||||
arg := []string{
|
||||
"--id", nf.cr.ID,
|
||||
"--keyfile=" + nf.cr.KeyFile,
|
||||
"-m", nf.Monitors,
|
||||
}
|
||||
// TODO: add blocklist till infinity.
|
||||
// Currently, ceph does not provide the functionality to blocklist IPs
|
||||
// for infinite time. As a workaround, add a blocklist for 5 YEARS to
|
||||
// represent infinity from ceph-csi side.
|
||||
// At any point in this time, the IPs can be unblocked by an UnfenceClusterReq.
|
||||
// This needs to be updated once ceph provides functionality for the same.
|
||||
cmd := []string{"osd", "blocklist"}
|
||||
if useRange {
|
||||
cmd = append(cmd, "range")
|
||||
}
|
||||
cmd = append(cmd, "add", ip, blocklistTime)
|
||||
cmd = append(cmd, arg...)
|
||||
_, stdErr, err := util.ExecCommand(ctx, "ceph", cmd...)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to blocklist IP %q: %w stderr: %q", ip, err, stdErr)
|
||||
}
|
||||
log.DebugLog(ctx, "blocklisted IP %q successfully", ip)
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (nf *NetworkFence) listActiveClients(ctx context.Context) ([]activeClient, error) {
|
||||
arg := []string{
|
||||
"--id", nf.cr.ID,
|
||||
@ -238,51 +359,6 @@ func (ac *activeClient) fetchID() (int, error) {
|
||||
return 0, fmt.Errorf("failed to extract client ID, incorrect format: %s", clientInfo)
|
||||
}
|
||||
|
||||
// AddClientEviction blocks access for all the IPs in the CIDR block
|
||||
// using client eviction, it also blocks the entire CIDR.
|
||||
func (nf *NetworkFence) AddClientEviction(ctx context.Context) error {
|
||||
evictedIPs := make(map[string]bool)
|
||||
// fetch active clients
|
||||
activeClients, err := nf.listActiveClients(ctx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
// iterate through CIDR blocks and check if any active client matches
|
||||
for _, cidr := range nf.Cidr {
|
||||
for _, client := range activeClients {
|
||||
var clientIP string
|
||||
clientIP, err = client.fetchIP()
|
||||
if err != nil {
|
||||
return fmt.Errorf("error fetching client IP: %w", err)
|
||||
}
|
||||
// check if the clientIP is in the CIDR block
|
||||
if isIPInCIDR(ctx, clientIP, cidr) {
|
||||
var clientID int
|
||||
clientID, err = client.fetchID()
|
||||
if err != nil {
|
||||
return fmt.Errorf("error fetching client ID: %w", err)
|
||||
}
|
||||
// evict the client
|
||||
err = nf.evictCephFSClient(ctx, clientID)
|
||||
if err != nil {
|
||||
return fmt.Errorf("error evicting client %d: %w", clientID, err)
|
||||
}
|
||||
log.DebugLog(ctx, "client %d has been evicted\n", clientID)
|
||||
// add the CIDR to the list of blocklisted IPs
|
||||
evictedIPs[clientIP] = true
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// add the range based blocklist for CIDR
|
||||
err = nf.AddNetworkFence(ctx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// getIPRange returns a list of IPs from the IP range
|
||||
// corresponding to a CIDR block.
|
||||
func getIPRange(cidr string) ([]string, error) {
|
||||
@ -357,82 +433,6 @@ func (nf *NetworkFence) removeCephBlocklist(ctx context.Context, ip, nonce strin
|
||||
return nil
|
||||
}
|
||||
|
||||
// RemoveNetworkFence unblocks access for all the IPs in the IP range mentioned via the CIDR block
|
||||
// using a network fence.
|
||||
// Unfencing one of the protocols(CephFS or RBD) suggests the node is expected to be recovered, so
|
||||
// both CephFS and RBD are expected to work again too.
|
||||
// example:
|
||||
// Create RBD NetworkFence CR for one IP 10.10.10.10
|
||||
// Created CephFS NetworkFence CR for IP range but above IP comes in the Range
|
||||
// Delete the CephFS Network Fence CR to unblocklist the IP
|
||||
// So now the IP (10.10.10.10) is (un)blocklisted and can be used by both protocols.
|
||||
func (nf *NetworkFence) RemoveNetworkFence(ctx context.Context) error {
|
||||
hasBlocklistRangeSupport := true
|
||||
// for each CIDR block, convert it into a range of IPs so as to undo blocklisting operation.
|
||||
for _, cidr := range nf.Cidr {
|
||||
// try range blocklist cmd, if invalid fallback to
|
||||
// iterating through IP range.
|
||||
if hasBlocklistRangeSupport {
|
||||
err := nf.removeCephBlocklist(ctx, cidr, "", true)
|
||||
if err == nil {
|
||||
continue
|
||||
}
|
||||
if !strings.Contains(err.Error(), invalidCommandStr) {
|
||||
return fmt.Errorf("failed to remove blocklist range %q: %w", cidr, err)
|
||||
}
|
||||
hasBlocklistRangeSupport = false
|
||||
}
|
||||
// fetch the list of IPs from a CIDR block
|
||||
hosts, err := getIPRange(cidr)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to convert CIDR block %s to corresponding IP range", cidr)
|
||||
}
|
||||
// remove ceph blocklist for each IP in the range mentioned by the CIDR
|
||||
for _, host := range hosts {
|
||||
// 0 is used as nonce here to tell ceph
|
||||
// to remove the blocklist entry matching: <host>:0/0
|
||||
// it is same as telling ceph to remove just the IP
|
||||
// without specifying any port or nonce with it.
|
||||
err := nf.removeCephBlocklist(ctx, host, "0", false)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (nf *NetworkFence) RemoveClientEviction(ctx context.Context) error {
|
||||
// Remove the CIDR block first
|
||||
err := nf.RemoveNetworkFence(ctx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// Get the ceph blocklist
|
||||
blocklist, err := nf.getCephBlocklist(ctx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// For each CIDR block, remove the IPs in the blocklist
|
||||
// that fall under the CIDR with nonce
|
||||
for _, cidr := range nf.Cidr {
|
||||
hosts := nf.parseBlocklistForCIDR(ctx, blocklist, cidr)
|
||||
log.DebugLog(ctx, "parsed blocklist for CIDR %s: %+v", cidr, hosts)
|
||||
|
||||
for _, host := range hosts {
|
||||
err := nf.removeCephBlocklist(ctx, host.IP, host.Nonce, false)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// getCephBlocklist fetches the ceph blocklist and returns it as a string.
|
||||
func (nf *NetworkFence) getCephBlocklist(ctx context.Context) (string, error) {
|
||||
arg := []string{
|
||||
|
Reference in New Issue
Block a user