Skip to content

Commit

Permalink
cmd: add options for NodeReclaimSpace operation on Kubernetes 1.24+
Browse files Browse the repository at this point in the history
Kubernetes versions < 1.24 should use the `-legacy` option, newer
versions require `-drivername` to build the StagingTargetPath where the
volume is mounted.

Signed-off-by: Niels de Vos <[email protected]>
  • Loading branch information
nixpanic authored and mergify[bot] committed Jun 15, 2022
1 parent eb0718f commit 267719c
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 4 deletions.
8 changes: 6 additions & 2 deletions cmd/csi-addons/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,25 @@ similar to:

```console
$ kubectl exec -c csi-addons csi-backend-nodeplugin -- csi-addons -h
-drivername string
name of the CSI driver
-endpoint string
CSI-Addons endpoint (default "unix:///tmp/csi-addons.sock")
-legacy
use legacy format for old Kubernetes versions
-operation string
csi-addons operation
-persistentvolume string
name of the PersistentVolume
-stagingpath string
staging path (default "/var/lib/kubelet/plugins/kubernetes.io/csi/pv/")
staging path (default "/var/lib/kubelet/plugins/kubernetes.io/csi/")

The following operations are supported:
- NodeReclaimSpace
- GetIdentity
- GetCapabilities
- Probe
- ControllerReclaimSpace
- NodeReclaimSpace
```

The above command assumes the running `csi-backend-nodeplugin` Pod has the
Expand Down
6 changes: 5 additions & 1 deletion cmd/csi-addons/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ import (

const (
endpoint = "unix:///tmp/csi-addons.sock"
stagingPath = "/var/lib/kubelet/plugins/kubernetes.io/csi/pv/"
stagingPath = "/var/lib/kubelet/plugins/kubernetes.io/csi/"
)

// command contains the parsed arguments that were passed while running the
Expand All @@ -40,6 +40,8 @@ type command struct {
stagingPath string
operation string
persistentVolume string
drivername string
legacy bool
}

// cmd is the single instance of the command struct, used inside main().
Expand All @@ -50,6 +52,8 @@ func init() {
flag.StringVar(&cmd.stagingPath, "stagingpath", stagingPath, "staging path")
flag.StringVar(&cmd.operation, "operation", "", "csi-addons operation")
flag.StringVar(&cmd.persistentVolume, "persistentvolume", "", "name of the PersistentVolume")
flag.StringVar(&cmd.drivername, "drivername", "", "name of the CSI driver")
flag.BoolVar(&cmd.legacy, "legacy", false, "use legacy format for old Kubernetes versions")

// output to show when --help is passed
flag.Usage = func() {
Expand Down
13 changes: 12 additions & 1 deletion cmd/csi-addons/reclaimspace.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package main

import (
"context"
"crypto/sha256"
"fmt"

"github.com/csi-addons/kubernetes-csi-addons/internal/proto"
Expand Down Expand Up @@ -83,7 +84,17 @@ func (nrs *NodeReclaimSpace) Init(c *command) error {
if c.stagingPath == "" {
return fmt.Errorf("stagingpath is not set")
}
nrs.stagingTargetPath = fmt.Sprintf("%s/%s/globalmount", c.stagingPath, c.persistentVolume)

if !c.legacy {
if c.drivername == "" {
return fmt.Errorf("drivername is not set")
}

unique := sha256.Sum256([]byte(c.persistentVolume))
nrs.stagingTargetPath = fmt.Sprintf("%s/%s/%s/globalmount", c.stagingPath, c.drivername, fmt.Sprintf("%x", unique))
} else {
nrs.stagingTargetPath = fmt.Sprintf("%s/pv/%s/globalmount", c.stagingPath, c.persistentVolume)
}

return nil
}
Expand Down

0 comments on commit 267719c

Please sign in to comment.